|
| 1 | +package inserter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "cloud.google.com/go/firestore" |
| 13 | + "github.com/go-generalize/fti/pkg/config" |
| 14 | + "github.com/go-generalize/fti/pkg/files" |
| 15 | + "golang.org/x/xerrors" |
| 16 | +) |
| 17 | + |
| 18 | +type Inserter struct { |
| 19 | + client *firestore.Client |
| 20 | + refIDs map[string]string |
| 21 | +} |
| 22 | + |
| 23 | +func NewInserter(client *firestore.Client) *Inserter { |
| 24 | + return &Inserter{ |
| 25 | + client: client, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (i *Inserter) Execute(ctx context.Context, cfg *config.Config) error { |
| 30 | + targetDir := cfg.Targets |
| 31 | + for _, t := range targetDir { |
| 32 | + if !files.Exists(t) { |
| 33 | + return xerrors.Errorf("cannot find target directory: %s", targetDir) |
| 34 | + } |
| 35 | + |
| 36 | + err := filepath.Walk(t, i.executeFile(ctx)) |
| 37 | + if err != nil { |
| 38 | + return xerrors.Errorf("failed to execute file: %w", err) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func (i *Inserter) executeFile(ctx context.Context) func(path string, info os.FileInfo, _ error) error { |
| 46 | + return func(path string, info os.FileInfo, _ error) error { |
| 47 | + if info.IsDir() { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + cn := filepath.Base(filepath.Dir(path)) |
| 52 | + |
| 53 | + var err error |
| 54 | + switch { |
| 55 | + case strings.HasSuffix(path, ".json"): |
| 56 | + err = i.executeJSON(ctx, cn, path) |
| 57 | + if err != nil { |
| 58 | + log.Printf("failed to insert json file: %s\n%+v", path, err) |
| 59 | + } |
| 60 | + case strings.HasSuffix(path, ".js"): |
| 61 | + // TODO: implements here for js api |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (i *Inserter) executeJSON(ctx context.Context, cn, path string) error { |
| 69 | + jb, err := os.ReadFile(path) |
| 70 | + if err != nil { |
| 71 | + return xerrors.Errorf("failed to read json file: %+v", err) |
| 72 | + } |
| 73 | + |
| 74 | + jm := new(JsonModel) |
| 75 | + err = json.Unmarshal(jb, jm) |
| 76 | + if err != nil { |
| 77 | + return xerrors.Errorf("failed to unmarshal json: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + if payload, ok := jm.Payload.([]interface{}); ok { |
| 81 | + for idx, p := range payload { |
| 82 | + mp, ok := p.(map[string]interface{}) |
| 83 | + if !ok { |
| 84 | + continue |
| 85 | + } |
| 86 | + err := i.createItem(ctx, cn, mp) |
| 87 | + if err != nil { |
| 88 | + return xerrors.Errorf("failed to create item in array (index=%d): %w", idx, err) |
| 89 | + } |
| 90 | + } |
| 91 | + } else if payload, ok := jm.Payload.(map[string]interface{}); ok { |
| 92 | + err := i.createItem(ctx, cn, payload) |
| 93 | + if err != nil { |
| 94 | + return xerrors.Errorf("failed to create item: %w", err) |
| 95 | + } |
| 96 | + } else { |
| 97 | + // print log or error? |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (i *Inserter) createItem(ctx context.Context, cn string, item map[string]interface{}) error { |
| 104 | + item = i.tryParseDate(item) |
| 105 | + |
| 106 | + log.Printf("collection: %s, item: %+v", cn, item) |
| 107 | + _, err := i.client.Collection(cn).NewDoc().Create(ctx, item) |
| 108 | + if err != nil { |
| 109 | + return xerrors.Errorf("failed to create item: %w", err) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (i *Inserter) tryParseDate(item map[string]interface{}) map[string]interface{} { |
| 115 | + for k, v := range item { |
| 116 | + switch vt := v.(type) { |
| 117 | + case string: |
| 118 | + pt, err := time.Parse(time.RFC3339, vt) |
| 119 | + if err != nil { |
| 120 | + // print log? |
| 121 | + continue |
| 122 | + } |
| 123 | + item[k] = pt |
| 124 | + |
| 125 | + case map[string]interface{}: |
| 126 | + item[k] = i.tryParseDate(vt) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return item |
| 131 | +} |
0 commit comments