Skip to content

Commit

Permalink
Fix comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
hengfengli committed May 15, 2021
1 parent 626e669 commit 1547ebd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
8 changes: 4 additions & 4 deletions spanner/integration_test.go
Expand Up @@ -1611,8 +1611,8 @@ func TestIntegration_BasicTypes(t *testing.T) {
}
msg := Message{"Alice", "Hello", 1294706395881547000}
jsonStr := `{"Name":"Alice","Body":"Hello","Time":1294706395881547000}`
var unmarshaledJSONstruct interface{}
json.Unmarshal([]byte(jsonStr), &unmarshaledJSONstruct)
var unmarshalledJSONstruct interface{}
json.Unmarshal([]byte(jsonStr), &unmarshalledJSONstruct)

tests := []struct {
col string
Expand Down Expand Up @@ -1725,9 +1725,9 @@ func TestIntegration_BasicTypes(t *testing.T) {
want interface{}
}{
{col: "JSON", val: msg, want: msg},
{col: "JSON", val: msg, want: NullJSON{unmarshaledJSONstruct, true}},
{col: "JSON", val: msg, want: NullJSON{unmarshalledJSONstruct, true}},
{col: "JSON", val: NullJSON{msg, true}, want: msg},
{col: "JSON", val: NullJSON{msg, true}, want: NullJSON{unmarshaledJSONstruct, true}},
{col: "JSON", val: NullJSON{msg, true}, want: NullJSON{unmarshalledJSONstruct, true}},
{col: "JSON", val: NullJSON{msg, false}},
{col: "JSON", val: nil, want: NullJSON{}},
{col: "JSONArray", val: []NullJSON(nil)},
Expand Down
12 changes: 4 additions & 8 deletions spanner/value.go
Expand Up @@ -460,7 +460,7 @@ func (n *NullNumeric) UnmarshalJSON(payload []byte) error {
// NullJSON represents a Cloud Spanner JSON that may be NULL.
type NullJSON struct {
Value interface{} // Val contains the value when it is non-NULL, and nil when NULL.
Valid bool // Valid is true if Numeric is not NULL.
Valid bool // Valid is true if Json is not NULL.
}

// IsNull implements NullableValue.IsNull for NullJSON.
Expand All @@ -475,7 +475,7 @@ func (n NullJSON) String() string {
}
b, err := json.Marshal(n.Value)
if err != nil {
return nullString
return fmt.Sprintf("error: %v", err)
}
return fmt.Sprintf("%v", string(b))
}
Expand Down Expand Up @@ -1420,7 +1420,7 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}) error {
if err != nil {
return fmt.Errorf("failed to read json string: %s", err)
}
// Check if it can be unmarshaled to the given type.
// Check if it can be unmarshalled to the given type.
err = json.Unmarshal([]byte(x), ptr)
if err != nil {
return fmt.Errorf("failed to unmarshal the json string: %s", err)
Expand Down Expand Up @@ -2924,27 +2924,23 @@ func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
return encodeValue(nv)
}

fmt.Println("coming here - 1")
// Check if the value is a variant of a base type.
decodableType := getDecodableSpannerType(v, false)
fmt.Printf("%T %v\n", v, decodableType)
if decodableType != spannerTypeUnknown && decodableType != spannerTypeInvalid {
converted, err := convertCustomTypeValue(decodableType, v)
if err != nil {
return nil, nil, err
}
return encodeValue(converted)
}
fmt.Println("coming here - 2")

// Check if it can be marshaled to a json string.
// Check if it can be marshalled to a json string.
b, err := json.Marshal(v)
if err == nil {
pb.Kind = stringKind(string(b))
pt = jsonType()
return pb, pt, nil
}
fmt.Println("coming here - 3")

if !isStructOrArrayOfStructValue(v) {
return nil, nil, errEncoderUnsupportedType(v)
Expand Down
12 changes: 6 additions & 6 deletions spanner/value_test.go
Expand Up @@ -1270,8 +1270,8 @@ func TestDecodeValue(t *testing.T) {
}
msg := Message{"Alice", "Hello", 1294706395881547000}
jsonStr := `{"Name":"Alice","Body":"Hello","Time":1294706395881547000}`
var unmarshaledJSONstruct interface{}
json.Unmarshal([]byte(jsonStr), &unmarshaledJSONstruct)
var unmarshalledJSONstruct interface{}
json.Unmarshal([]byte(jsonStr), &unmarshalledJSONstruct)
invalidJsonStr := `{wrong_json_string}`

// Pointer values.
Expand Down Expand Up @@ -1393,11 +1393,11 @@ func TestDecodeValue(t *testing.T) {
{desc: "decode NULL to []*big.Rat", proto: nullProto(), protoType: listType(numericType()), want: []*big.Rat(nil)},
// JSON
{desc: "decode json to struct", proto: stringProto(jsonStr), protoType: jsonType(), want: msg},
{desc: "decode json to NullJSON", proto: stringProto(jsonStr), protoType: jsonType(), want: NullJSON{unmarshaledJSONstruct, true}},
{desc: "decode json to NullJSON", proto: stringProto(jsonStr), protoType: jsonType(), want: NullJSON{unmarshalledJSONstruct, true}},
{desc: "decode NULL to NullJSON", proto: nullProto(), protoType: jsonType(), want: NullJSON{}},
{desc: "decode an invalid json string", proto: stringProto(invalidJsonStr), protoType: jsonType(), want: msg, wantErr: true},
// JSON ARRAY with []NullJSON
{desc: "decode ARRAY<JSON> to []NullJSON", proto: listProto(stringProto(jsonStr), stringProto(jsonStr), nullProto()), protoType: listType(jsonType()), want: []NullJSON{{unmarshaledJSONstruct, true}, {unmarshaledJSONstruct, true}, {}}},
{desc: "decode ARRAY<JSON> to []NullJSON", proto: listProto(stringProto(jsonStr), stringProto(jsonStr), nullProto()), protoType: listType(jsonType()), want: []NullJSON{{unmarshalledJSONstruct, true}, {unmarshalledJSONstruct, true}, {}}},
{desc: "decode NULL to []NullJSON", proto: nullProto(), protoType: listType(jsonType()), want: []NullJSON(nil)},
// TIMESTAMP
{desc: "decode TIMESTAMP to time.Time", proto: timeProto(t1), protoType: timeType(), want: t1},
Expand Down Expand Up @@ -1609,7 +1609,7 @@ func TestDecodeValue(t *testing.T) {
{desc: "decode BOOL to CustomNullBool", proto: boolProto(true), protoType: boolType(), want: CustomNullBool{true, true}},
{desc: "decode FLOAT64 to CustomNullFloat64", proto: floatProto(6.626), protoType: floatType(), want: CustomNullFloat64{6.626, true}},
{desc: "decode NUMERIC to CustomNullNumeric", proto: numericProto(numValuePtr), protoType: numericType(), want: CustomNullNumeric{*numValuePtr, true}},
{desc: "decode JSON to CustomNullJSON", proto: stringProto(jsonStr), protoType: jsonType(), want: CustomNullJSON{unmarshaledJSONstruct, true}},
{desc: "decode JSON to CustomNullJSON", proto: stringProto(jsonStr), protoType: jsonType(), want: CustomNullJSON{unmarshalledJSONstruct, true}},
{desc: "decode TIMESTAMP to CustomNullTime", proto: timeProto(t1), protoType: timeType(), want: CustomNullTime{t1, true}},
{desc: "decode DATE to CustomNullDate", proto: dateProto(d1), protoType: dateType(), want: CustomNullDate{d1, true}},

Expand Down Expand Up @@ -1657,7 +1657,7 @@ func TestDecodeValue(t *testing.T) {
{desc: "decode ARRAY<NUMERIC> to []CustomNullNumeric", proto: listProto(numericProto(numValuePtr), nullProto(), numericProto(num2ValuePtr)), protoType: listType(numericType()), want: []CustomNullNumeric{{*numValuePtr, true}, {}, {*num2ValuePtr, true}}},
// JSON ARRAY
{desc: "decode NULL to []CustomNullJSON", proto: nullProto(), protoType: listType(jsonType()), want: []CustomNullJSON(nil)},
{desc: "decode ARRAY<JSON> to []CustomNullJSON", proto: listProto(stringProto(jsonStr), stringProto(jsonStr), nullProto()), protoType: listType(jsonType()), want: []CustomNullJSON{{unmarshaledJSONstruct, true}, {unmarshaledJSONstruct, true}, {}}},
{desc: "decode ARRAY<JSON> to []CustomNullJSON", proto: listProto(stringProto(jsonStr), stringProto(jsonStr), nullProto()), protoType: listType(jsonType()), want: []CustomNullJSON{{unmarshalledJSONstruct, true}, {unmarshalledJSONstruct, true}, {}}},
// TIME ARRAY
{desc: "decode NULL to []CustomTime", proto: nullProto(), protoType: listType(timeType()), want: []CustomTime(nil)},
{desc: "decode ARRAY<TIMESTAMP> with NULL values to []CustomTime", proto: listProto(timeProto(t1), nullProto(), timeProto(t2)), protoType: listType(timeType()), want: []CustomTime{}, wantErr: true},
Expand Down

0 comments on commit 1547ebd

Please sign in to comment.