Skip to content

Commit 0a355b4

Browse files
authored
Merge pull request #11 from /issues/10/main
YAMLを読み込めるようにした
2 parents 6f81271 + 268780d commit 0a355b4

File tree

10 files changed

+110
-29
lines changed

10 files changed

+110
-29
lines changed

cmd/fti/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
"github.com/gcp-kit/fti/pkg/config"
1616
"github.com/gcp-kit/fti/pkg/files"
1717
"github.com/gcp-kit/fti/pkg/inserter"
18+
_ "github.com/goccy/go-yaml"
1819
"github.com/heetch/confita"
1920
"github.com/heetch/confita/backend/env"
2021
"github.com/heetch/confita/backend/file"
2122
"github.com/heetch/confita/backend/flags"
2223
"golang.org/x/xerrors"
23-
_ "gopkg.in/yaml.v2"
2424
)
2525

2626
var (

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.17
55
require (
66
cloud.google.com/go/firestore v1.6.1
77
github.com/BurntSushi/toml v0.4.1
8+
github.com/goccy/go-yaml v1.9.5
89
github.com/heetch/confita v0.10.0
910
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
10-
gopkg.in/yaml.v2 v2.4.0
1111
rogchap.com/v8go v0.6.0
1212
)
1313

@@ -19,19 +19,23 @@ require (
1919
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect
2020
github.com/envoyproxy/go-control-plane v0.10.0 // indirect
2121
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
22+
github.com/fatih/color v1.13.0 // indirect
2223
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
2324
github.com/golang/protobuf v1.5.2 // indirect
2425
github.com/google/go-cmp v0.5.6 // indirect
2526
github.com/googleapis/gax-go/v2 v2.1.1 // indirect
27+
github.com/mattn/go-colorable v0.1.12 // indirect
28+
github.com/mattn/go-isatty v0.0.14 // indirect
2629
github.com/pkg/errors v0.9.1 // indirect
2730
go.opencensus.io v0.23.0 // indirect
2831
golang.org/x/net v0.0.0-20211108170745-6635138e15ea // indirect
2932
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
30-
golang.org/x/sys v0.0.0-20211108224332-cbcd623f202e // indirect
33+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
3134
golang.org/x/text v0.3.7 // indirect
3235
google.golang.org/api v0.60.0 // indirect
3336
google.golang.org/appengine v1.6.7 // indirect
3437
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 // indirect
3538
google.golang.org/grpc v1.42.0 // indirect
3639
google.golang.org/protobuf v1.27.1 // indirect
40+
gopkg.in/yaml.v2 v2.4.0 // indirect
3741
)

go.sum

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
111111
github.com/envoyproxy/protoc-gen-validate v0.6.2 h1:JiO+kJTpmYGjEodY7O1Zk8oZcNz1+f30UtwtXoFUPzE=
112112
github.com/envoyproxy/protoc-gen-validate v0.6.2/go.mod h1:2t7qjJNvHPx8IjnBOzl9E9/baC+qXE/TeeyBRzgJDws=
113113
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
114+
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
115+
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
116+
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
114117
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
115118
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
116119
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -119,8 +122,17 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2
119122
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
120123
github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc=
121124
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
125+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
126+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
127+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
128+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
129+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
130+
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
131+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
122132
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
123133
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
134+
github.com/goccy/go-yaml v1.9.5 h1:Eh/+3uk9kLxG4koCX6lRMAPS1OaMSAi+FJcya0INdB0=
135+
github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
124136
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
125137
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
126138
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -257,9 +269,18 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
257269
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
258270
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
259271
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
272+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
273+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
260274
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
261275
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
276+
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
277+
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
278+
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
279+
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
262280
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
281+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
282+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
283+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
263284
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
264285
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
265286
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -347,6 +368,7 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
347368
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
348369
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
349370
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
371+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
350372
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
351373
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
352374
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -477,6 +499,7 @@ golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7w
477499
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
478500
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
479501
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
502+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
480503
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
481504
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
482505
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -510,10 +533,11 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc
510533
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
511534
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
512535
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
536+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
513537
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
514538
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
515-
golang.org/x/sys v0.0.0-20211108224332-cbcd623f202e h1:9nbuBbpiqktwdlzHKUohsD5+y2a0QvX98gIWK2ARkqc=
516-
golang.org/x/sys v0.0.0-20211108224332-cbcd623f202e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
539+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
540+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
517541
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
518542
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
519543
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

pkg/inserter/js_inserter.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (j *JSInserter) Execute(ctx context.Context, cn, path string) error {
5454
return xerrors.Errorf("failed to marshal json of returned value: %w", err)
5555
}
5656

57-
jms := make([]JSONModelItem, 0)
57+
jms := make([]Document, 0)
5858
err = json.Unmarshal(jb, &jms)
5959
if err != nil {
6060
return xerrors.Errorf("failed to unmarshal json: %w", err)
@@ -69,9 +69,9 @@ func (j *JSInserter) Execute(ctx context.Context, cn, path string) error {
6969
return nil
7070
}
7171

72-
// CreateItem - ItemのCreate
73-
// nolint: dupl
74-
func (j *JSInserter) CreateItem(ctx context.Context, path []string, items []JSONModelItem, collectionIndexes []int) error {
72+
// CreateItem - Firestore にアイテムを生成する
73+
// nolint:dupl
74+
func (j *JSInserter) CreateItem(ctx context.Context, path []string, items []Document, collectionIndexes []int) error {
7575
for idx, parentItem := range items {
7676
nowIndexes := append(collectionIndexes, idx)
7777
docPath := strings.Join(path, "/")
@@ -87,7 +87,7 @@ func (j *JSInserter) CreateItem(ctx context.Context, path []string, items []JSON
8787
continue
8888
}
8989
for collectionName, subItems := range parentItem.SubCollections {
90-
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
90+
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], string(collectionName)), subItems, nowIndexes)
9191
if err != nil {
9292
return xerrors.Errorf("failed to create item in array: %w", err)
9393
}

pkg/inserter/json_inserter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (j *JSONInserter) Execute(ctx context.Context, cn, path string) error {
3030
return xerrors.Errorf("failed to read json file: %+v", err)
3131
}
3232

33-
jm := new(JSONModel)
33+
jm := new(Collection)
3434
err = json.Unmarshal(jb, jm)
3535
if err != nil {
3636
return xerrors.Errorf("failed to unmarshal json: %w", err)
@@ -45,9 +45,9 @@ func (j *JSONInserter) Execute(ctx context.Context, cn, path string) error {
4545
return nil
4646
}
4747

48-
// CreateItem - ItemのCreate
48+
// CreateItem - Firestore にアイテムを生成する
4949
// nolint: dupl
50-
func (j *JSONInserter) CreateItem(ctx context.Context, path []string, items []JSONModelItem, collectionIndexes []int) error {
50+
func (j *JSONInserter) CreateItem(ctx context.Context, path []string, items []Document, collectionIndexes []int) error {
5151
for idx, parentItem := range items {
5252
nowIndexes := append(collectionIndexes, idx)
5353
docPath := strings.Join(path, "/")
@@ -63,7 +63,7 @@ func (j *JSONInserter) CreateItem(ctx context.Context, path []string, items []JS
6363
continue
6464
}
6565
for collectionName, subItems := range parentItem.SubCollections {
66-
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], collectionName), subItems, nowIndexes)
66+
err := j.CreateItem(ctx, append(path, j.ci.refIDs[parentItem.Ref], string(collectionName)), subItems, nowIndexes)
6767
if err != nil {
6868
return xerrors.Errorf("failed to create item in array: %w", err)
6969
}

pkg/inserter/json_model.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

pkg/inserter/model.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package inserter
2+
3+
// CollectionName - Name of collection
4+
type CollectionName string
5+
6+
// Collection - JSONやYAMLのモデル
7+
type Collection struct {
8+
Version string `json:"version" yaml:"version"`
9+
Items []Document `json:"items" yaml:"version"`
10+
}
11+
12+
// Document - Modelが持つアイテム
13+
type Document struct {
14+
Ref string `json:"ref" yaml:"ref"`
15+
Payload map[string]interface{} `json:"payload" yaml:"payload"`
16+
SubCollections map[CollectionName][]Document `json:"sub_collections"`
17+
}

pkg/inserter/yaml_inserter.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package inserter
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"github.com/goccy/go-yaml"
8+
"golang.org/x/xerrors"
9+
)
10+
11+
// YAMLInserter - InserterのYAML実装
12+
type YAMLInserter struct {
13+
ci *CommonInserter
14+
}
15+
16+
// NewYAMLInserter - YAMLInserter constructor
17+
func NewYAMLInserter(ci *CommonInserter) *YAMLInserter {
18+
return &YAMLInserter{
19+
ci: ci,
20+
}
21+
}
22+
23+
// Execute - YAMLを読み込んでアイテムを生成する
24+
func (y *YAMLInserter) Execute(ctx context.Context, cn, path string) error {
25+
yb, err := os.ReadFile(path)
26+
if err != nil {
27+
return xerrors.Errorf("failed to read yaml file: %+v", err)
28+
}
29+
30+
ym := new(Collection)
31+
err = yaml.Unmarshal(yb, ym)
32+
if err != nil {
33+
return xerrors.Errorf("failed to unmarshal yaml: %w", err)
34+
}
35+
36+
for idx, item := range ym.Items {
37+
err := y.ci.CreateItem(ctx, cn, item.Ref, item.Payload)
38+
if err != nil {
39+
return xerrors.Errorf("failed to create item in array (index=%d): %w", idx, err)
40+
}
41+
}
42+
43+
return nil
44+
}

samples/Food/dummies.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '1.0'
2+
items:
3+
- ref: dummy-1
4+
payload:
5+
name: Orange
6+
origin: Japan

samples/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
targets:
22
- ./samples/Account
3+
- ./samples/Food
34
- ./samples/Option
45
- ./samples/Survey
56
firestore_project_on_emulator: sample

0 commit comments

Comments
 (0)