@@ -3,7 +3,6 @@ package inserter
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "fmt"
7
6
"os"
8
7
"strconv"
9
8
"strings"
@@ -33,18 +32,20 @@ func (j *JSONInserter) Execute(ctx context.Context, cn, path string) error {
33
32
return xerrors .Errorf ("failed to unmarshal json: %w" , err )
34
33
}
35
34
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 ))
37
37
if err != nil {
38
38
return xerrors .Errorf ("failed to create item: %w" , err )
39
39
}
40
40
41
41
return nil
42
42
}
43
43
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 {
45
45
for idx , parentItem := range items {
46
46
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 )
48
49
if err != nil {
49
50
errorIndexes := make ([]string , 0 )
50
51
for _ , v := range nowIndexes {
@@ -56,7 +57,7 @@ func (j *JSONInserter) CreateItem(ctx context.Context, cn string, items []JsonMo
56
57
continue
57
58
}
58
59
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 )
60
61
if err != nil {
61
62
return xerrors .Errorf ("failed to create item in array: %w" , err )
62
63
}
0 commit comments