Skip to content

Commit

Permalink
fix(spanner): result from unmarshal of string and spanner.NullString …
Browse files Browse the repository at this point in the history
…type from json should be consistent. (#5263)

Co-authored-by: Rahul Yadav <irahul@google.com>
  • Loading branch information
rahul2393 and rahul2393 authored Jan 6, 2022
1 parent 22df34b commit 7eaaa47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions spanner/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,17 @@ func (n *NullString) UnmarshalJSON(payload []byte) error {
n.Valid = false
return nil
}
payload, err := trimDoubleQuotes(payload)
if err != nil {
var s *string
if err := json.Unmarshal(payload, &s); err != nil {
return err
}
n.StringVal = string(payload)
n.Valid = true
if s != nil {
n.StringVal = *s
n.Valid = true
} else {
n.StringVal = ""
n.Valid = false
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions spanner/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,7 @@ func TestJSONUnmarshal_NullTypes(t *testing.T) {
{input: []byte(`"this is a test string"`), got: NullString{}, isNull: false, expect: "this is a test string", expectError: false},
{input: []byte(`""`), got: NullString{}, isNull: false, expect: "", expectError: false},
{input: []byte("null"), got: NullString{}, isNull: true, expect: nullString, expectError: false},
{input: []byte(`"{\"sub_a\": \"value_1\"}"`), got: NullString{}, isNull: false, expect: `{"sub_a": "value_1"}`, expectError: false},
{input: nil, got: NullString{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullString{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullString{}, isNull: true, expect: nullString, expectError: true},
Expand Down

0 comments on commit 7eaaa47

Please sign in to comment.