2
0

Fix encode to json ignoring driver.Valuer

https://github.com/jackc/pgx/issues/1430
This commit is contained in:
Jack Christensen
2022-12-23 13:44:09 -06:00
parent c4ac6d810f
commit e66ad1bcec
3 changed files with 23 additions and 0 deletions
+6
View File
@@ -24,6 +24,12 @@ func (c JSONCodec) PlanEncode(m *Map, oid uint32, format int16, value any) Encod
return encodePlanJSONCodecEitherFormatString{}
case []byte:
return encodePlanJSONCodecEitherFormatByteSlice{}
// Cannot rely on driver.Valuer being handled later because anything can be marshalled.
//
// https://github.com/jackc/pgx/issues/1430
case driver.Valuer:
return &encodePlanDriverValuer{m: m, oid: oid, formatCode: format}
}
// Because anything can be marshalled the normal wrapping in Map.PlanScan doesn't get a chance to run. So try the
+3
View File
@@ -52,6 +52,9 @@ func TestJSONCodec(t *testing.T) {
// Test sql.Scanner. (https://github.com/jackc/pgx/issues/1418)
{"42", new(sql.NullInt64), isExpectedEq(sql.NullInt64{Int64: 42, Valid: true})},
// Test driver.Valuer. (https://github.com/jackc/pgx/issues/1430)
{sql.NullInt64{Int64: 42, Valid: true}, new(sql.NullInt64), isExpectedEq(sql.NullInt64{Int64: 42, Valid: true})},
})
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, "json", []pgxtest.ValueRoundTripTest{