@@ -2,12 +2,10 @@ package inserter
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"log"
7
6
"os"
8
7
"path/filepath"
9
8
"strings"
10
- "time"
11
9
12
10
"cloud.google.com/go/firestore"
13
11
"github.com/go-generalize/fti/pkg/config"
@@ -16,14 +14,12 @@ import (
16
14
)
17
15
18
16
type Inserter struct {
19
- client * firestore.Client
20
- refIDs map [string ]string
17
+ jsonInserter * JSONInserter
21
18
}
22
19
23
20
func NewInserter (client * firestore.Client ) * Inserter {
24
21
return & Inserter {
25
- client : client ,
26
- refIDs : map [string ]string {},
22
+ jsonInserter : NewJSONInserter (client ),
27
23
}
28
24
}
29
25
@@ -54,7 +50,7 @@ func (i *Inserter) executeFile(ctx context.Context) func(path string, info os.Fi
54
50
var err error
55
51
switch {
56
52
case strings .HasSuffix (path , ".json" ):
57
- err = i .executeJSON (ctx , cn , path )
53
+ err = i .jsonInserter . executeJSON (ctx , cn , path )
58
54
if err != nil {
59
55
log .Printf ("failed to insert json file: %s\n %+v" , path , err )
60
56
}
@@ -65,97 +61,3 @@ func (i *Inserter) executeFile(ctx context.Context) func(path string, info os.Fi
65
61
return nil
66
62
}
67
63
}
68
-
69
- func (i * Inserter ) executeJSON (ctx context.Context , cn , path string ) error {
70
- jb , err := os .ReadFile (path )
71
- if err != nil {
72
- return xerrors .Errorf ("failed to read json file: %+v" , err )
73
- }
74
-
75
- jm := new (JsonModel )
76
- err = json .Unmarshal (jb , jm )
77
- if err != nil {
78
- return xerrors .Errorf ("failed to unmarshal json: %w" , err )
79
- }
80
-
81
- if payload , ok := jm .Payload .([]interface {}); ok {
82
- for idx , p := range payload {
83
- mp , ok := p .(map [string ]interface {})
84
- if ! ok {
85
- continue
86
- }
87
- err := i .createItem (ctx , cn , jm .Ref , mp )
88
- if err != nil {
89
- return xerrors .Errorf ("failed to create item in array (index=%d): %w" , idx , err )
90
- }
91
- }
92
- } else if payload , ok := jm .Payload .(map [string ]interface {}); ok {
93
- err := i .createItem (ctx , cn , jm .Ref , payload )
94
- if err != nil {
95
- return xerrors .Errorf ("failed to create item: %w" , err )
96
- }
97
- } else {
98
- // print log or error?
99
- }
100
-
101
- return nil
102
- }
103
-
104
- func (i * Inserter ) createItem (ctx context.Context , cn , refID string , item map [string ]interface {}) error {
105
- item = i .tryParseDate (item )
106
- item = i .setRefs (item )
107
-
108
- d := i .client .Collection (cn ).NewDoc ()
109
- _ , err := d .Create (ctx , item )
110
- if err != nil {
111
- return xerrors .Errorf ("failed to create item: %w" , err )
112
- }
113
-
114
- if refID != "" {
115
- if _ , ok := i .refIDs [refID ]; ok {
116
- return xerrors .Errorf ("already ref id: %s" , refID )
117
- }
118
- i .refIDs [refID ] = d .ID
119
- }
120
-
121
- return nil
122
- }
123
-
124
- func (i * Inserter ) tryParseDate (item map [string ]interface {}) map [string ]interface {} {
125
- for k , v := range item {
126
- switch vt := v .(type ) {
127
- case string :
128
- pt , err := time .Parse (time .RFC3339 , vt )
129
- if err != nil {
130
- // print log?
131
- continue
132
- }
133
- item [k ] = pt
134
-
135
- case map [string ]interface {}:
136
- item [k ] = i .tryParseDate (vt )
137
- }
138
- }
139
-
140
- return item
141
- }
142
-
143
- func (i * Inserter ) setRefs (item map [string ]interface {}) map [string ]interface {} {
144
- for k , v := range item {
145
- switch vt := v .(type ) {
146
- case string :
147
- if strings .HasPrefix (vt , "$" ) {
148
- refID := strings .TrimPrefix (vt , "$" )
149
- rv , ok := i .refIDs [refID ]
150
- if ! ok {
151
- log .Printf ("%s was not found" , refID )
152
- continue
153
- }
154
- item [k ] = rv
155
- }
156
- }
157
- }
158
-
159
- return item
160
-
161
- }
0 commit comments