Skip to content

Commit d72d0f4

Browse files
authored
fix(spanner): decode PG JSONB array to PGJsonB struct (#13602)
Decoding arrays of PostgreSQL JSONB values into a slice of PGJsonB structs failed due to a faulty check of the array element type code annotation.
1 parent 337ca07 commit d72d0f4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

spanner/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,7 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO
22422242
if p == nil {
22432243
return errNilDst(p)
22442244
}
2245-
if acode != sppb.TypeCode_JSON || typeAnnotation != sppb.TypeAnnotationCode_PG_JSONB {
2245+
if acode != sppb.TypeCode_JSON || atypeAnnotation != sppb.TypeAnnotationCode_PG_JSONB {
22462246
return errTypeMismatch(code, acode, ptr)
22472247
}
22482248
if isNull {

spanner/value_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,10 +1691,20 @@ func TestDecodeValue(t *testing.T) {
16911691
{desc: "decode an invalid json string", proto: stringProto(invalidJSONStr), protoType: jsonType(), want: NullJSON{}, wantErr: true},
16921692
{desc: "decode a json string with empty array to a NullJSON", proto: stringProto(emptyArrayJSONStr), protoType: jsonType(), want: NullJSON{unmarshalledEmptyJSONArray, true}},
16931693
{desc: "decode a json string with null to a NullJSON", proto: stringProto(nullValueJSONStr), protoType: jsonType(), want: NullJSON{unmarshalledStructWithNull, true}},
1694+
// PG_JSONB
1695+
{desc: "decode json to PGJsonB", proto: stringProto(jsonStr), protoType: pgJsonbType(), want: PGJsonB{Value: unmarshalledJSONStruct, Valid: true}},
1696+
{desc: "decode NULL to PGJsonB", proto: nullProto(), protoType: pgJsonbType(), want: PGJsonB{}},
1697+
{desc: "decode an invalid json string", proto: stringProto(invalidJSONStr), protoType: pgJsonbType(), want: PGJsonB{}, wantErr: true},
1698+
{desc: "decode a json string with empty array to a PGJsonB", proto: stringProto(emptyArrayJSONStr), protoType: pgJsonbType(), want: PGJsonB{Value: unmarshalledEmptyJSONArray, Valid: true}},
1699+
{desc: "decode a json string with null to a PGJsonB", proto: stringProto(nullValueJSONStr), protoType: pgJsonbType(), want: PGJsonB{Value: unmarshalledStructWithNull, Valid: true}},
16941700
// JSON ARRAY with []NullJSON
16951701
{desc: "decode ARRAY<JSON> to []NullJSON", proto: listProto(stringProto(jsonStr), stringProto(jsonStr), nullProto()), protoType: listType(jsonType()), want: []NullJSON{{unmarshalledJSONStruct, true}, {unmarshalledJSONStruct, true}, {}}},
16961702
{desc: "decode ARRAY<JSON> to NullJSON", proto: listProto(stringProto(jsonStr), nullProto(), stringProto("true")), protoType: listType(jsonType()), want: NullJSON{unmarshalledJSONArray, true}},
16971703
{desc: "decode NULL to []NullJSON", proto: nullProto(), protoType: listType(jsonType()), want: []NullJSON(nil)},
1704+
// PG_JSONB ARRAY with []PGJsonB
1705+
{desc: "decode ARRAY<PG_JSONB> to []PGJsonB", proto: listProto(stringProto(jsonStr), stringProto(jsonStr), nullProto()), protoType: listType(pgJsonbType()), want: []PGJsonB{{Value: unmarshalledJSONStruct, Valid: true}, {Value: unmarshalledJSONStruct, Valid: true}, {}}},
1706+
{desc: "decode empty ARRAY<PG_JSONB> to []PGJsonB", proto: listProto(), protoType: listType(pgJsonbType()), want: []PGJsonB{}},
1707+
{desc: "decode NULL to []PGJsonB", proto: nullProto(), protoType: listType(pgJsonbType()), want: []PGJsonB(nil)},
16981708
// PG NUMERIC
16991709
{desc: "decode PG NUMERIC to PGNumeric", proto: stringProto("123.456"), protoType: pgNumericType(), want: PGNumeric{"123.456", true}},
17001710
{desc: "decode NaN to PGNumeric", proto: stringProto("NaN"), protoType: pgNumericType(), want: PGNumeric{"NaN", true}},

0 commit comments

Comments
 (0)