Skip to content

Commit 329cb45

Browse files
committed
XMLCodec: fix DecodeValue to return a []byte
Previously, DecodeValue would always return nil with the default Unmarshal function. fixes #2227
1 parent e877606 commit 329cb45

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

pgtype/xml.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (c *XMLCodec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (an
192192
return nil, nil
193193
}
194194

195-
var dst any
196-
err := c.Unmarshal(src, &dst)
197-
return dst, err
195+
dstBuf := make([]byte, len(src))
196+
copy(dstBuf, src)
197+
return dstBuf, nil
198198
}

pgtype/xml_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,32 @@ func TestXMLCodecPointerToPointerToString(t *testing.T) {
9797
require.Nil(t, s)
9898
})
9999
}
100+
101+
func TestXMLCodecDecodeValue(t *testing.T) {
102+
skipCockroachDB(t, "CockroachDB does not support XML.")
103+
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, _ testing.TB, conn *pgx.Conn) {
104+
for _, tt := range []struct {
105+
sql string
106+
expected any
107+
}{
108+
{
109+
sql: `select '<foo>bar</foo>'::xml`,
110+
expected: []byte("<foo>bar</foo>"),
111+
},
112+
} {
113+
t.Run(tt.sql, func(t *testing.T) {
114+
rows, err := conn.Query(ctx, tt.sql)
115+
require.NoError(t, err)
116+
117+
for rows.Next() {
118+
values, err := rows.Values()
119+
require.NoError(t, err)
120+
require.Len(t, values, 1)
121+
require.Equal(t, tt.expected, values[0])
122+
}
123+
124+
require.NoError(t, rows.Err())
125+
})
126+
}
127+
})
128+
}

0 commit comments

Comments
 (0)