Skip to content

Commit

Permalink
Merge pull request #102 from gcpug/support-json-type-code
Browse files Browse the repository at this point in the history
add support for JSON type
  • Loading branch information
sinmetal authored May 16, 2024
2 parents 5f7a593 + 1046e61 commit 211cd94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions server/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func (it *rows) next() ([]interface{}, bool) {
values[i] = reflect.New(reflect.TypeOf(sql.NullInt64{}))
case TCFloat64:
values[i] = reflect.New(reflect.TypeOf(sql.NullFloat64{}))
case TCTimestamp, TCDate, TCString:
case TCTimestamp, TCDate, TCString, TCJson:
values[i] = reflect.New(reflect.TypeOf(sql.NullString{}))
case TCBytes:
values[i] = reflect.New(reflect.TypeOf(&[]byte{}))
Expand Down Expand Up @@ -676,6 +676,8 @@ func makeSpannerTypeFromValueType(typ ValueType) *spannerpb.Type {
code = spannerpb.TypeCode_ARRAY
case TCStruct:
code = spannerpb.TypeCode_STRUCT
case TCJson:
code = spannerpb.TypeCode_JSON
}

st := &spannerpb.Type{Code: code}
Expand Down Expand Up @@ -767,6 +769,10 @@ func makeValueTypeFromSpannerType(typ *spannerpb.Type) (ValueType, error) {
FieldTypes: types,
},
}, nil
case spannerpb.TypeCode_JSON:
return ValueType{
Code: TCJson,
}, nil
}

return ValueType{}, fmt.Errorf("unknown code for spanner.Type: %v", typ.Code)
Expand Down Expand Up @@ -1034,7 +1040,7 @@ func makeDataFromSpannerValue(key string, v *structpb.Value, typ ValueType) (int
return s, nil
}

case TCString:
case TCString, TCJson:
switch vv := v.Kind.(type) {
case *structpb.Value_StringValue:
return vv.StringValue, nil
Expand Down
19 changes: 19 additions & 0 deletions server/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ func TestDatabaseEncDec(t *testing.T) {
},
expected: makeTestArray(TCBytes, []byte("xyz"), []byte("xxx")),
},
"JSON": {
value: makeStringValue(`{"a": 1, "b": 2}`),
typ: ValueType{
Code: TCJson,
},
expected: `{"a": 1, "b": 2}`,
},
}

uuid := uuidpkg.New().String()
Expand Down Expand Up @@ -723,6 +730,18 @@ func TestMakeValueFromSpannerValue(t *testing.T) {
},
},
},
"JSON": {
value: makeStringValue(`{"a": 1, "b": 2}`),
typ: &spannerpb.Type{
Code: spannerpb.TypeCode_JSON,
},
expected: Value{
Data: `{"a": 1, "b": 2}`,
Type: ValueType{
Code: TCJson,
},
},
},
}

for name, tc := range table {
Expand Down

0 comments on commit 211cd94

Please sign in to comment.