Skip to content

Commit

Permalink
add support for JSON type
Browse files Browse the repository at this point in the history
  • Loading branch information
double-di committed May 15, 2024
1 parent a33cef5 commit 1046e61
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 @@ -520,7 +520,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 @@ -669,6 +669,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 @@ -760,6 +762,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 @@ -1027,7 +1033,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 1046e61

Please sign in to comment.