|
5 | 5 | "context"
|
6 | 6 | "encoding/json"
|
7 | 7 | "os"
|
| 8 | + "strconv" |
| 9 | + "strings" |
8 | 10 |
|
9 | 11 | "golang.org/x/xerrors"
|
10 | 12 | )
|
@@ -34,10 +36,35 @@ func (j *JSONInserter) Execute(ctx context.Context, cn, path string) error {
|
34 | 36 | return xerrors.Errorf("failed to unmarshal json: %w", err)
|
35 | 37 | }
|
36 | 38 |
|
37 |
| - for idx, item := range jm.Items { |
38 |
| - err := j.ci.CreateItem(ctx, cn, item.Ref, item.Payload) |
| 39 | + docPath := make([]string, 0) |
| 40 | + err = j.CreateItem(ctx, append(docPath, cn), jm.Items, make([]int, 0)) |
| 41 | + if err != nil { |
| 42 | + return xerrors.Errorf("failed to create item: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func (j *JSONInserter) CreateItem(ctx context.Context, path []string, items []JSONModelItem, collectionIndexes []int) error { |
| 49 | + for idx, parentItem := range items { |
| 50 | + nowIndexes := append(collectionIndexes, idx) |
| 51 | + docPath := strings.Join(path, "/") |
| 52 | + err := j.ci.CreateItem(ctx, docPath, parentItem.Ref, parentItem.Payload) |
39 | 53 | if err != nil {
|
40 |
| - return xerrors.Errorf("failed to create item in array (index=%d): %w", idx, err) |
| 54 | + errorIndexes := make([]string, 0) |
| 55 | + for _, v := range nowIndexes { |
| 56 | + errorIndexes = append(errorIndexes, strconv.Itoa(v)) |
| 57 | + } |
| 58 | + return xerrors.Errorf("failed to create item in array (index=%s): %w", strings.Join(errorIndexes, "/"), err) |
| 59 | + } |
| 60 | + if parentItem.SubCollections == nil || len(parentItem.SubCollections) == 0 { |
| 61 | + continue |
| 62 | + } |
| 63 | + for collectionName, subItems := range parentItem.SubCollections { |
| 64 | + err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes) |
| 65 | + if err != nil { |
| 66 | + return xerrors.Errorf("failed to create item in array: %w", err) |
| 67 | + } |
41 | 68 | }
|
42 | 69 | }
|
43 | 70 |
|
|
0 commit comments