Skip to content

Commit b593d5a

Browse files
committed
Scan unknown types into *any as string or []byte based on format code
#2399
1 parent b1f7464 commit b593d5a

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

pgtype/pgtype.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,18 @@ func (m *Map) planScan(oid uint32, formatCode int16, target any, depth int) Scan
11351135
}
11361136
}
11371137

1138-
if dt != nil {
1139-
if _, ok := target.(*any); ok {
1140-
return &pointerEmptyInterfaceScanPlan{codec: dt.Codec, m: m, oid: oid, formatCode: formatCode}
1138+
if _, ok := target.(*any); ok {
1139+
var codec Codec
1140+
if dt != nil {
1141+
codec = dt.Codec
1142+
} else {
1143+
if formatCode == TextFormatCode {
1144+
codec = TextCodec{}
1145+
} else {
1146+
codec = ByteaCodec{}
1147+
}
11411148
}
1149+
return &pointerEmptyInterfaceScanPlan{codec: codec, m: m, oid: oid, formatCode: formatCode}
11421150
}
11431151

11441152
return &scanPlanFail{m: m, oid: oid, formatCode: formatCode}

pgtype/pgtype_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ func TestMapScanBinaryFormatInterfacePtr(t *testing.T) {
165165
assert.Equal(t, "foo", got)
166166
}
167167

168+
func TestMapScanUnknownOIDToPtrToAny(t *testing.T) {
169+
unknownOID := uint32(999999)
170+
srcBuf := []byte("foo")
171+
m := pgtype.NewMap()
172+
173+
var a any
174+
err := m.Scan(unknownOID, pgx.TextFormatCode, srcBuf, &a)
175+
assert.NoError(t, err)
176+
assert.Equal(t, "foo", a)
177+
178+
err = m.Scan(unknownOID, pgx.BinaryFormatCode, srcBuf, &a)
179+
assert.NoError(t, err)
180+
assert.Equal(t, []byte("foo"), a)
181+
}
182+
168183
func TestMapScanUnknownOIDToStringsAndBytes(t *testing.T) {
169184
unknownOID := uint32(999999)
170185
srcBuf := []byte("foo")

0 commit comments

Comments
 (0)