@@ -3,28 +3,22 @@ package inserter
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "log"
7
6
"os"
8
- "strings"
9
- "time"
10
7
11
- "cloud.google.com/go/firestore"
12
8
"golang.org/x/xerrors"
13
9
)
14
10
15
11
type JSONInserter struct {
16
- client * firestore.Client
17
- refIDs map [string ]string
12
+ ci * CommonInserter
18
13
}
19
14
20
- func NewJSONInserter (client * firestore. Client ) * JSONInserter {
15
+ func NewJSONInserter (ci * CommonInserter ) * JSONInserter {
21
16
return & JSONInserter {
22
- client : client ,
23
- refIDs : map [string ]string {},
17
+ ci : ci ,
24
18
}
25
19
}
26
20
27
- func (j * JSONInserter ) executeJSON (ctx context.Context , cn , path string ) error {
21
+ func (j * JSONInserter ) Execute (ctx context.Context , cn , path string ) error {
28
22
jb , err := os .ReadFile (path )
29
23
if err != nil {
30
24
return xerrors .Errorf ("failed to read json file: %+v" , err )
@@ -36,83 +30,12 @@ func (j *JSONInserter) executeJSON(ctx context.Context, cn, path string) error {
36
30
return xerrors .Errorf ("failed to unmarshal json: %w" , err )
37
31
}
38
32
39
- if payload , ok := jm .Payload .([]interface {}); ok {
40
- for idx , p := range payload {
41
- mp , ok := p .(map [string ]interface {})
42
- if ! ok {
43
- continue
44
- }
45
- err := j .createItem (ctx , cn , jm .Ref , mp )
46
- if err != nil {
47
- return xerrors .Errorf ("failed to create item in array (index=%d): %w" , idx , err )
48
- }
49
- }
50
- } else if payload , ok := jm .Payload .(map [string ]interface {}); ok {
51
- err := j .createItem (ctx , cn , jm .Ref , payload )
33
+ for idx , item := range jm .Items {
34
+ err := j .ci .CreateItem (ctx , cn , item .Ref , item .Payload )
52
35
if err != nil {
53
- return xerrors .Errorf ("failed to create item: %w" , err )
54
- }
55
- } else {
56
- // print log or error?
57
- }
58
-
59
- return nil
60
- }
61
-
62
- func (j * JSONInserter ) createItem (ctx context.Context , cn , refID string , item map [string ]interface {}) error {
63
- item = j .tryParseDate (item )
64
- item = j .setRefs (item )
65
-
66
- d := j .client .Collection (cn ).NewDoc ()
67
- _ , err := d .Create (ctx , item )
68
- if err != nil {
69
- return xerrors .Errorf ("failed to create item: %w" , err )
70
- }
71
-
72
- if refID != "" {
73
- if _ , ok := j .refIDs [refID ]; ok {
74
- return xerrors .Errorf ("already ref id: %s" , refID )
36
+ return xerrors .Errorf ("failed to create item in array (index=%d): %w" , idx , err )
75
37
}
76
- j .refIDs [refID ] = d .ID
77
38
}
78
39
79
40
return nil
80
41
}
81
-
82
- func (j * JSONInserter ) tryParseDate (item map [string ]interface {}) map [string ]interface {} {
83
- for k , v := range item {
84
- switch vt := v .(type ) {
85
- case string :
86
- pt , err := time .Parse (time .RFC3339 , vt )
87
- if err != nil {
88
- // print log?
89
- continue
90
- }
91
- item [k ] = pt
92
-
93
- case map [string ]interface {}:
94
- item [k ] = j .tryParseDate (vt )
95
- }
96
- }
97
-
98
- return item
99
- }
100
-
101
- func (j * JSONInserter ) setRefs (item map [string ]interface {}) map [string ]interface {} {
102
- for k , v := range item {
103
- switch vt := v .(type ) {
104
- case string :
105
- if strings .HasPrefix (vt , "$" ) {
106
- refID := strings .TrimPrefix (vt , "$" )
107
- rv , ok := j .refIDs [refID ]
108
- if ! ok {
109
- log .Printf ("%s was not found" , refID )
110
- continue
111
- }
112
- item [k ] = rv
113
- }
114
- }
115
- }
116
-
117
- return item
118
- }
0 commit comments