Skip to content

Commit

Permalink
XMLCodec: fix DecodeValue to return a []byte
Browse files Browse the repository at this point in the history
Previously, DecodeValue would always return nil with the default
Unmarshal function.

fixes #2227
  • Loading branch information
jackc committed Jan 11, 2025
1 parent e877606 commit 6e4f2eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pgtype/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *XMLCodec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (an
return nil, nil
}

var dst any
err := c.Unmarshal(src, &dst)
return dst, err
dstBuf := make([]byte, len(src))
copy(dstBuf, src)
return dstBuf, nil
}
28 changes: 28 additions & 0 deletions pgtype/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,31 @@ func TestXMLCodecPointerToPointerToString(t *testing.T) {
require.Nil(t, s)
})
}

func TestXMLCodecDecodeValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, _ testing.TB, conn *pgx.Conn) {
for _, tt := range []struct {
sql string
expected any
}{
{
sql: `select '<foo>bar</foo>'::xml`,
expected: []byte("<foo>bar</foo>"),
},
} {
t.Run(tt.sql, func(t *testing.T) {
rows, err := conn.Query(ctx, tt.sql)
require.NoError(t, err)

for rows.Next() {
values, err := rows.Values()
require.NoError(t, err)
require.Len(t, values, 1)
require.Equal(t, tt.expected, values[0])
}

require.NoError(t, rows.Err())
})
}
})
}

0 comments on commit 6e4f2eb

Please sign in to comment.