Skip to content
Permalink
Browse files
fix(spanner): result from unmarshal of string and spanner.NullString …
…type from json should be consistent. (#5263)

Co-authored-by: Rahul Yadav <irahul@google.com>
  • Loading branch information
rahul2393 and rahul2393 committed Jan 6, 2022
1 parent 22df34b commit 7eaaa470fda5dc7cd1ff041d6a898e35fb54920e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
@@ -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
}

@@ -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},

0 comments on commit 7eaaa47

Please sign in to comment.