Skip to content

Commit 5c74658

Browse files
authored
Merge pull request #6 from gcp-kit/feat/sub_collection
Feat/sub collection
2 parents 0910eb4 + edffd9b commit 5c74658

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

pkg/inserter/js_inserter.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"encoding/json"
77
"os"
8+
"strconv"
9+
"strings"
810

911
"golang.org/x/xerrors"
1012
v8 "rogchap.com/v8go"
@@ -58,10 +60,35 @@ func (j *JSInserter) Execute(ctx context.Context, cn, path string) error {
5860
return xerrors.Errorf("failed to unmarshal json: %w", err)
5961
}
6062

61-
for idx, item := range jms {
62-
err = j.ci.CreateItem(ctx, cn, item.Ref, item.Payload)
63+
docPath := make([]string, 0)
64+
err = j.CreateItem(ctx, append(docPath, cn), jms, make([]int, 0))
65+
if err != nil {
66+
return xerrors.Errorf("failed to create item: %w", err)
67+
}
68+
69+
return nil
70+
}
71+
72+
func (j *JSInserter) CreateItem(ctx context.Context, path []string, items []JSONModelItem, collectionIndexes []int) error {
73+
for idx, parentItem := range items {
74+
nowIndexes := append(collectionIndexes, idx)
75+
docPath := strings.Join(path, "/")
76+
err := j.ci.CreateItem(ctx, docPath, parentItem.Ref, parentItem.Payload)
6377
if err != nil {
64-
return xerrors.Errorf("failed to create item (index: %d): %w", idx, err)
78+
errorIndexes := make([]string, 0)
79+
for _, v := range nowIndexes {
80+
errorIndexes = append(errorIndexes, strconv.Itoa(v))
81+
}
82+
return xerrors.Errorf("failed to create item in array (index=%s): %w", strings.Join(errorIndexes, "/"), err)
83+
}
84+
if parentItem.SubCollections == nil || len(parentItem.SubCollections) == 0 {
85+
continue
86+
}
87+
for collectionName, subItems := range parentItem.SubCollections {
88+
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
89+
if err != nil {
90+
return xerrors.Errorf("failed to create item in array: %w", err)
91+
}
6592
}
6693
}
6794

pkg/inserter/json_inserter.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"encoding/json"
77
"os"
8+
"strconv"
9+
"strings"
810

911
"golang.org/x/xerrors"
1012
)
@@ -34,10 +36,35 @@ func (j *JSONInserter) Execute(ctx context.Context, cn, path string) error {
3436
return xerrors.Errorf("failed to unmarshal json: %w", err)
3537
}
3638

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)
3953
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+
}
4168
}
4269
}
4370

pkg/inserter/json_model.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type JSONModel struct {
99

1010
// JSONModelItem - JsonModel のアイテム
1111
type JSONModelItem struct {
12-
Ref string `json:"ref"`
13-
Payload map[string]interface{} `json:"payload"`
12+
Ref string `json:"ref"`
13+
Payload map[string]interface{} `json:"payload"`
14+
SubCollections map[string][]JSONModelItem `json:"sub_collections"`
1415
}

0 commit comments

Comments
 (0)