@@ -3,13 +3,14 @@ package integrations
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "errors"
6
+ _ "errors"
7
7
"fmt"
8
- "strings"
8
+ _ "strings"
9
9
"sync"
10
10
"time"
11
11
12
12
firebase "firebase.google.com/go"
13
+ _ "google.golang.org/api/iterator"
13
14
"google.golang.org/api/option"
14
15
15
16
"github.com/SkySingh04/fractal/interfaces"
@@ -30,8 +31,7 @@ type FirebaseDestination struct {
30
31
}
31
32
32
33
func (f FirebaseSource ) FetchData (req interfaces.Request ) (interface {}, error ) {
33
- logger .Infof ("Connecting to Firebase Source: Collection=%s, Document=%s, using Service Account=%s" ,
34
- req .Collection , req .Document , req .CredentialFileAddr )
34
+ logger .Infof ("Connecting to Firebase Source: Collection=%s, using Service Account=%s" , req .Collection , req .CredentialFileAddr )
35
35
36
36
opt := option .WithCredentialsFile (req .CredentialFileAddr )
37
37
app , err := firebase .NewApp (context .Background (), nil , opt )
@@ -45,41 +45,30 @@ func (f FirebaseSource) FetchData(req interfaces.Request) (interface{}, error) {
45
45
}
46
46
defer client .Close ()
47
47
48
- dataChan := make (chan map [string ]interface {}, 1 )
49
- errChan := make (chan error , 1 )
50
- var wg sync.WaitGroup
48
+ docs , err := client .Collection (req .Collection ).Documents (context .Background ()).GetAll ()
49
+ if err != nil {
50
+ return nil , fmt .Errorf ("failed to fetch documents: %w" , err )
51
+ }
51
52
52
- wg .Add (1 )
53
- go func () {
54
- defer wg .Done ()
55
- dsnap , err := client .Collection (req .Collection ).Doc (req .Document ).Get (context .Background ())
56
- if err != nil {
57
- errChan <- fmt .Errorf ("failed to fetch document from Firestore: %w" , err )
58
- return
59
- }
53
+ logger .Infof ("Fetched documents from Firebase: %d documents" , len (docs ))
54
+ for i , doc := range docs {
55
+ logger .Infof ("Document %d ID: %s, Data: %v" , i , doc .Ref .ID , doc .Data ())
56
+ }
60
57
61
- if ! dsnap . Exists () {
62
- errChan <- fmt . Errorf ( "document not found: Collection=%s, Document=%s" , req . Collection , req . Document )
63
- return
64
- }
58
+ var allData [] map [ string ] interface {}
59
+ for _ , doc := range docs {
60
+ data := doc . Data ()
61
+ logger . Infof ( "Fetched data from Firebase: %v" , data )
65
62
66
- dataChan <- dsnap .Data ()
67
- }()
63
+ data ["_id" ] = doc .Ref .ID
68
64
69
- wg .Wait ()
70
- close (dataChan )
71
- close (errChan )
65
+ validatedData := data
66
+ transformedData := validatedData
72
67
73
- select {
74
- case data := <- dataChan :
75
- validatedData , err := validateFirebaseData (data )
76
- if err != nil {
77
- return nil , err
78
- }
79
- return transformFirebaseData (validatedData ), nil
80
- case err := <- errChan :
81
- return nil , err
68
+ allData = append (allData , transformedData )
82
69
}
70
+
71
+ return allData , nil
83
72
}
84
73
85
74
func (f FirebaseDestination ) SendData (data interface {}, req interfaces.Request ) error {
@@ -127,6 +116,8 @@ func (f FirebaseDestination) SendData(data interface{}, req interfaces.Request)
127
116
}
128
117
129
118
func convertToMap (data interface {}, result * map [string ]interface {}) error {
119
+ logger .Infof ("Firebase data to map: %v" , data )
120
+
130
121
temp , err := json .Marshal (data )
131
122
if err != nil {
132
123
return fmt .Errorf ("failed to marshal data to JSON: %w" , err )
@@ -140,18 +131,18 @@ func convertToMap(data interface{}, result *map[string]interface{}) error {
140
131
141
132
func validateFirebaseData (data map [string ]interface {}) (map [string ]interface {}, error ) {
142
133
logger .Infof ("Validating Firebase data: %v" , data )
143
- message , ok := data [ "data" ].( string )
144
- if ! ok || strings .TrimSpace (message ) == "" {
145
- return nil , errors .New ("invalid or missing 'data' field" )
146
- }
134
+ // // message, ok := data;
135
+ // if !ok || strings.TrimSpace(message) == "" {
136
+ // return nil, errors.New("invalid or missing 'data' field")
137
+ // }
147
138
return data , nil
148
139
}
149
140
150
141
func transformFirebaseData (data map [string ]interface {}) map [string ]interface {} {
151
142
logger .Infof ("Transforming Firebase data: %v" , data )
152
- if message , ok := data ["data" ].(string ); ok {
153
- data ["data" ] = strings .ToUpper (message )
154
- }
143
+ // if message, ok := data["data"].(string); ok {
144
+ // data["data"] = strings.ToUpper(message)
145
+ // }
155
146
data ["processed" ] = time .Now ().Format (time .RFC3339 )
156
147
return data
157
148
}
0 commit comments