Skip to content

Commit 56f9bd7

Browse files
authored
Merge pull request #19 from gcp-kit/feat/settable-ID
任意ID対応
2 parents 0bf2c8f + 6846d5a commit 56f9bd7

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

pkg/inserter/common_inserter.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ func NewCommonInserter(client *firestore.Client) *CommonInserter {
2626
}
2727

2828
// CreateItem - item を Firestore に作る
29-
func (c *CommonInserter) CreateItem(ctx context.Context, cn, refID string, item map[string]interface{}) error {
29+
func (c *CommonInserter) CreateItem(ctx context.Context, cn, ID, refID string, item map[string]interface{}) error {
3030
item = c.tryParseDate(item)
3131
item = c.setRefs(item)
3232

33-
d := c.client.Collection(cn).NewDoc()
33+
var d *firestore.DocumentRef
34+
if ID == "" {
35+
d = c.client.Collection(cn).NewDoc()
36+
} else {
37+
d = c.client.Collection(cn).Doc(ID)
38+
}
3439
_, err := d.Create(ctx, item)
3540
if err != nil {
3641
return xerrors.Errorf("failed to create item: %w", err)

pkg/inserter/js_inserter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (j *JSInserter) CreateItem(ctx context.Context, path []string, items []Docu
7575
for idx, parentItem := range items {
7676
nowIndexes := append(collectionIndexes, idx)
7777
docPath := strings.Join(path, "/")
78-
err := j.ci.CreateItem(ctx, docPath, parentItem.Ref, parentItem.Payload)
78+
err := j.ci.CreateItem(ctx, docPath, parentItem.ID, parentItem.Ref, parentItem.Payload)
7979
if err != nil {
8080
errorIndexes := make([]string, 0)
8181
for _, v := range nowIndexes {

pkg/inserter/json_inserter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (j *JSONInserter) CreateItem(ctx context.Context, path []string, items []Do
5151
for idx, parentItem := range items {
5252
nowIndexes := append(collectionIndexes, idx)
5353
docPath := strings.Join(path, "/")
54-
err := j.ci.CreateItem(ctx, docPath, parentItem.Ref, parentItem.Payload)
54+
err := j.ci.CreateItem(ctx, docPath, parentItem.ID, parentItem.Ref, parentItem.Payload)
5555
if err != nil {
5656
errorIndexes := make([]string, 0)
5757
for _, v := range nowIndexes {

pkg/inserter/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Collection struct {
1111

1212
// Document - Modelが持つアイテム
1313
type Document struct {
14+
ID string `json:"id" yaml:"id"`
1415
Ref string `json:"ref" yaml:"ref"`
1516
Payload map[string]interface{} `json:"payload" yaml:"payload"`
1617
SubCollections map[CollectionName][]Document `json:"sub_collections"`

pkg/inserter/yaml_inserter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (y *YAMLInserter) Execute(ctx context.Context, cn, path string) error {
3434
}
3535

3636
for idx, item := range ym.Items {
37-
err := y.ci.CreateItem(ctx, cn, item.Ref, item.Payload)
37+
err := y.ci.CreateItem(ctx, cn, item.ID, item.Ref, item.Payload)
3838
if err != nil {
3939
return xerrors.Errorf("failed to create item in array (index=%d): %w", idx, err)
4040
}

samples/Car/dummies.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1.0",
3+
"items": [
4+
{
5+
"id": "car-id-1",
6+
"ref": "dummy-car-1",
7+
"payload": {
8+
"name": "Car1"
9+
}
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)