Skip to content

Commit dfaaa08

Browse files
committed
fix: DocumentRefPathの連結を後で行うようにした
1 parent dca4b3a commit dfaaa08

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pkg/inserter/js_inserter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package inserter
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"os"
87
"strconv"
98
"strings"
@@ -57,18 +56,20 @@ func (j *JSInserter) Execute(ctx context.Context, cn, path string) error {
5756
return xerrors.Errorf("failed to unmarshal json: %w", err)
5857
}
5958

60-
err = j.CreateItem(ctx, cn, jms, make([]int, 0))
59+
docPath := make([]string, 0)
60+
err = j.CreateItem(ctx, append(docPath, cn), jms, make([]int, 0))
6161
if err != nil {
6262
return xerrors.Errorf("failed to create item: %w", err)
6363
}
6464

6565
return nil
6666
}
6767

68-
func (j *JSInserter) CreateItem(ctx context.Context, cn string, items []JsonModelItem, collectionIndexes []int) error {
68+
func (j *JSInserter) CreateItem(ctx context.Context, path []string, items []JsonModelItem, collectionIndexes []int) error {
6969
for idx, parentItem := range items {
7070
nowIndexes := append(collectionIndexes, idx)
71-
err := j.ci.CreateItem(ctx, cn, parentItem.Ref, parentItem.Payload)
71+
docPath := strings.Join(path, "/")
72+
err := j.ci.CreateItem(ctx, docPath, parentItem.Ref, parentItem.Payload)
7273
if err != nil {
7374
errorIndexes := make([]string, 0)
7475
for _, v := range nowIndexes {
@@ -80,7 +81,7 @@ func (j *JSInserter) CreateItem(ctx context.Context, cn string, items []JsonMode
8081
continue
8182
}
8283
for collectionName, subItems := range parentItem.SubCollections {
83-
err := j.CreateItem(ctx, fmt.Sprintf("%s/%s/%s", cn, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
84+
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
8485
if err != nil {
8586
return xerrors.Errorf("failed to create item in array: %w", err)
8687
}

pkg/inserter/json_inserter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package inserter
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"os"
87
"strconv"
98
"strings"
@@ -33,18 +32,20 @@ func (j *JSONInserter) Execute(ctx context.Context, cn, path string) error {
3332
return xerrors.Errorf("failed to unmarshal json: %w", err)
3433
}
3534

36-
err = j.CreateItem(ctx, cn, jm.Items, make([]int, 0))
35+
docPath := make([]string, 0)
36+
err = j.CreateItem(ctx, append(docPath, cn), jm.Items, make([]int, 0))
3737
if err != nil {
3838
return xerrors.Errorf("failed to create item: %w", err)
3939
}
4040

4141
return nil
4242
}
4343

44-
func (j *JSONInserter) CreateItem(ctx context.Context, cn string, items []JsonModelItem, collectionIndexes []int) error {
44+
func (j *JSONInserter) CreateItem(ctx context.Context, path []string, items []JsonModelItem, collectionIndexes []int) error {
4545
for idx, parentItem := range items {
4646
nowIndexes := append(collectionIndexes, idx)
47-
err := j.ci.CreateItem(ctx, cn, parentItem.Ref, parentItem.Payload)
47+
docPath := strings.Join(path, "/")
48+
err := j.ci.CreateItem(ctx, docPath, parentItem.Ref, parentItem.Payload)
4849
if err != nil {
4950
errorIndexes := make([]string, 0)
5051
for _, v := range nowIndexes {
@@ -56,7 +57,7 @@ func (j *JSONInserter) CreateItem(ctx context.Context, cn string, items []JsonMo
5657
continue
5758
}
5859
for collectionName, subItems := range parentItem.SubCollections {
59-
err := j.CreateItem(ctx, fmt.Sprintf("%s/%s/%s", cn, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
60+
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
6061
if err != nil {
6162
return xerrors.Errorf("failed to create item in array: %w", err)
6263
}

0 commit comments

Comments
 (0)