Skip to content

Commit

Permalink
Merge pull request #86 from tyamagu2/v2-json
Browse files Browse the repository at this point in the history
Add JSON column support to v2
  • Loading branch information
kazegusuri authored Dec 20, 2021
2 parents dc6b340 + f892406 commit c69509a
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
PROJECT: yo-test
INSTANCE: yo-test
DATABASE: yo-test
- image: gcr.io/cloud-spanner-emulator/emulator:1.2.0
- image: gcr.io/cloud-spanner-emulator/emulator:1.4.0
working_directory: /go/src/github.com/cloudspannerecosystem/yo
steps:
- checkout
Expand Down
26 changes: 15 additions & 11 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ module go.mercari.io/yo/v2
go 1.16

require (
cloud.google.com/go v0.81.0
cloud.google.com/go/spanner v1.18.0
cloud.google.com/go v0.99.0
cloud.google.com/go/spanner v1.28.0
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cncf/xds/go v0.0.0-20211216145620-d92e9ce0af51 // indirect
github.com/envoyproxy/go-control-plane v0.10.1 // indirect
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.5
github.com/google/go-cmp v0.5.6
github.com/jinzhu/inflection v1.0.0
github.com/kenshaw/snaker v0.1.0
github.com/spf13/cobra v1.1.1
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
golang.org/x/tools v0.1.1
google.golang.org/api v0.46.0
google.golang.org/genproto v0.0.0-20210510173355-fb37daa5cd7a
google.golang.org/grpc v1.37.1
google.golang.org/protobuf v1.26.0
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/tools v0.1.5
google.golang.org/api v0.63.0
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa
google.golang.org/grpc v1.43.0
google.golang.org/protobuf v1.27.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.4.0
)
160 changes: 131 additions & 29 deletions v2/go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions v2/loader/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func parseSpannerType(dt string, nullable bool) (int, string, string) {
typ = "spanner.NullNumeric"
}

case "JSON":
nilVal = `spanner.NullJSON{Valid: true}`
typ = "spanner.NullJSON"
if nullable {
nilVal = `spanner.NullJSON{}`
}

default:
if strings.HasPrefix(dt, "ARRAY<") {
eleDataType := strings.TrimSuffix(strings.TrimPrefix(dt, "ARRAY<"), ">")
Expand Down
34 changes: 34 additions & 0 deletions v2/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ func TestDefaultFullType(t *testing.T) {
date := civil.DateOf(now)
tomorrow := now.AddDate(0, 0, 1)
nextdate := civil.DateOf(tomorrow)
json := spanner.NullJSON{
Valid: true,
Value: `{"a": "b"}`,
}
jsonNull := spanner.NullJSON{}

table := map[string]struct {
ft *default_models.FullType
Expand Down Expand Up @@ -337,6 +342,8 @@ func TestDefaultFullType(t *testing.T) {
Date: date,
Valid: true,
},
FTJSON: json,
FTJSONNull: json,
FTArrayStringNull: []string{"xxx1", "yyy1"},
FTArrayString: []string{"xxx1", "yyy1"},
FTArrayBoolNull: []bool{true, false},
Expand All @@ -351,6 +358,8 @@ func TestDefaultFullType(t *testing.T) {
FTArrayFloat: []float64{0.111, 0.222},
FTArrayDateNull: []civil.Date{date, nextdate},
FTArrayDate: []civil.Date{date, nextdate},
FTArrayJSONNull: []spanner.NullJSON{json, jsonNull},
FTArrayJSON: []spanner.NullJSON{json, jsonNull},
},
},
"case2": {
Expand All @@ -372,6 +381,8 @@ func TestDefaultFullType(t *testing.T) {
FTDateNull: spanner.NullDate{},
FTArrayStringNull: []string{"xxx2", "yyy2"},
FTArrayString: []string{"xxx2", "yyy2"},
FTJSON: json,
FTJSONNull: jsonNull,
FTArrayBoolNull: nil,
FTArrayBool: []bool{true, false},
FTArrayBytesNull: nil,
Expand All @@ -384,6 +395,8 @@ func TestDefaultFullType(t *testing.T) {
FTArrayFloat: []float64{0.111, 0.222},
FTArrayDateNull: nil,
FTArrayDate: []civil.Date{date, nextdate},
FTArrayJSONNull: nil,
FTArrayJSON: []spanner.NullJSON{json, jsonNull},
},
},
"case3": {
Expand All @@ -403,6 +416,8 @@ func TestDefaultFullType(t *testing.T) {
FTFloatNull: spanner.NullFloat64{},
FTDate: date,
FTDateNull: spanner.NullDate{},
FTJSON: json,
FTJSONNull: jsonNull,
FTArrayStringNull: []string{},
FTArrayString: []string{},
FTArrayBoolNull: []bool{},
Expand All @@ -417,6 +432,8 @@ func TestDefaultFullType(t *testing.T) {
FTArrayFloat: []float64{},
FTArrayDateNull: []civil.Date{},
FTArrayDate: []civil.Date{},
FTArrayJSONNull: []spanner.NullJSON{},
FTArrayJSON: []spanner.NullJSON{},
},
},
}
Expand Down Expand Up @@ -628,6 +645,11 @@ func TestLegacyDefaultFullType(t *testing.T) {
date := civil.DateOf(now)
tomorrow := now.AddDate(0, 0, 1)
nextdate := civil.DateOf(tomorrow)
json := spanner.NullJSON{
Valid: true,
Value: `{"a": "b"}`,
}
jsonNull := spanner.NullJSON{}

table := map[string]struct {
ft *legacy_models.FullType
Expand Down Expand Up @@ -667,6 +689,8 @@ func TestLegacyDefaultFullType(t *testing.T) {
Date: date,
Valid: true,
},
FTJSON: json,
FTJSONNull: json,
FTArrayStringNull: []string{"xxx1", "yyy1"},
FTArrayString: []string{"xxx1", "yyy1"},
FTArrayBoolNull: []bool{true, false},
Expand All @@ -681,6 +705,8 @@ func TestLegacyDefaultFullType(t *testing.T) {
FTArrayFloat: []float64{0.111, 0.222},
FTArrayDateNull: []civil.Date{date, nextdate},
FTArrayDate: []civil.Date{date, nextdate},
FTArrayJSONNull: []spanner.NullJSON{json, jsonNull},
FTArrayJSON: []spanner.NullJSON{json, jsonNull},
},
},
"case2": {
Expand All @@ -700,6 +726,8 @@ func TestLegacyDefaultFullType(t *testing.T) {
FTFloatNull: spanner.NullFloat64{},
FTDate: date,
FTDateNull: spanner.NullDate{},
FTJSON: json,
FTJSONNull: jsonNull,
FTArrayStringNull: []string{"xxx2", "yyy2"},
FTArrayString: []string{"xxx2", "yyy2"},
FTArrayBoolNull: nil,
Expand All @@ -714,6 +742,8 @@ func TestLegacyDefaultFullType(t *testing.T) {
FTArrayFloat: []float64{0.111, 0.222},
FTArrayDateNull: nil,
FTArrayDate: []civil.Date{date, nextdate},
FTArrayJSONNull: nil,
FTArrayJSON: []spanner.NullJSON{json, jsonNull},
},
},
"case3": {
Expand All @@ -733,6 +763,8 @@ func TestLegacyDefaultFullType(t *testing.T) {
FTFloatNull: spanner.NullFloat64{},
FTDate: date,
FTDateNull: spanner.NullDate{},
FTJSON: json,
FTJSONNull: jsonNull,
FTArrayStringNull: []string{},
FTArrayString: []string{},
FTArrayBoolNull: []bool{},
Expand All @@ -747,6 +779,8 @@ func TestLegacyDefaultFullType(t *testing.T) {
FTArrayFloat: []float64{},
FTArrayDateNull: []civil.Date{},
FTArrayDate: []civil.Date{},
FTArrayJSONNull: []spanner.NullJSON{},
FTArrayJSON: []spanner.NullJSON{},
},
},
}
Expand Down
4 changes: 4 additions & 0 deletions v2/test/testdata/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ CREATE TABLE FullTypes (
FTFloatNull FLOAT64,
FTDate DATE NOT NULL,
FTDateNull DATE,
FTJson JSON NOT NULL,
FTJsonNull JSON,
FTArrayStringNull ARRAY<STRING(32)>,
FTArrayString ARRAY<STRING(32)> NOT NULL,
FTArrayBoolNull ARRAY<BOOL>,
Expand All @@ -83,6 +85,8 @@ CREATE TABLE FullTypes (
FTArrayFloat ARRAY<FLOAT64> NOT NULL,
FTArrayDateNull ARRAY<DATE>,
FTArrayDate ARRAY<DATE> NOT NULL,
FTArrayJsonNull ARRAY<JSON>,
FTArrayJson ARRAY<JSON> NOT NULL,
) PRIMARY KEY(PKey);

CREATE UNIQUE INDEX FullTypesByFTString ON FullTypes(FTString);
Expand Down
38 changes: 33 additions & 5 deletions v2/test/testmodels/default/full_type.yo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions v2/test/testmodels/dump_types/full_type.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* FTFloatNull FLOAT64 spanner.NullFloat64
* FTDate DATE civil.Date
* FTDateNull DATE spanner.NullDate
* FTJSON JSON spanner.NullJSON
* FTJSONNull JSON spanner.NullJSON
* FTArrayStringNull ARRAY<STRING(32)> []string
* FTArrayString ARRAY<STRING(32)> []string
* FTArrayBoolNull ARRAY<BOOL> []bool
Expand All @@ -29,6 +31,8 @@
* FTArrayFloat ARRAY<FLOAT64> []float64
* FTArrayDateNull ARRAY<DATE> []civil.Date
* FTArrayDate ARRAY<DATE> []civil.Date
* FTArrayJSONNull ARRAY<JSON> []spanner.NullJSON
* FTArrayJSON ARRAY<JSON> []spanner.NullJSON

# Primary Key

Expand Down
Loading

0 comments on commit c69509a

Please sign in to comment.