@@ -48,6 +48,7 @@ func TestJSONCodec(t *testing.T) {
48
48
Age int `json:"age"`
49
49
}
50
50
51
+ var str string
51
52
pgxtest .RunValueRoundTripTests (context .Background (), t , defaultConnTestRunner , nil , "json" , []pgxtest.ValueRoundTripTest {
52
53
{nil , new (* jsonStruct ), isExpectedEq ((* jsonStruct )(nil ))},
53
54
{map [string ]any (nil ), new (* string ), isExpectedEq ((* string )(nil ))},
@@ -65,6 +66,9 @@ func TestJSONCodec(t *testing.T) {
65
66
{Issue1805 (7 ), new (Issue1805 ), isExpectedEq (Issue1805 (7 ))},
66
67
// Test driver.Scanner is used before json.Unmarshaler (https://github.com/jackc/pgx/issues/2146)
67
68
{Issue2146 (7 ), new (* Issue2146 ), isPtrExpectedEq (Issue2146 (7 ))},
69
+
70
+ // Test driver.Scanner without pointer receiver (https://github.com/jackc/pgx/issues/2204)
71
+ {NonPointerJSONScanner {V : stringPtr ("{}" )}, NonPointerJSONScanner {V : & str }, func (a any ) bool { return str == "{}" }},
68
72
})
69
73
70
74
pgxtest .RunValueRoundTripTests (context .Background (), t , defaultConnTestRunner , pgxtest .KnownOIDQueryExecModes , "json" , []pgxtest.ValueRoundTripTest {
@@ -136,6 +140,27 @@ func (i Issue2146) Value() (driver.Value, error) {
136
140
return string (b ), err
137
141
}
138
142
143
+ type NonPointerJSONScanner struct {
144
+ V * string
145
+ }
146
+
147
+ func (i NonPointerJSONScanner ) Scan (src any ) error {
148
+ switch c := src .(type ) {
149
+ case string :
150
+ * i .V = c
151
+ case []byte :
152
+ * i .V = string (c )
153
+ default :
154
+ return errors .New ("unknown source type" )
155
+ }
156
+
157
+ return nil
158
+ }
159
+
160
+ func (i NonPointerJSONScanner ) Value () (driver.Value , error ) {
161
+ return i .V , nil
162
+ }
163
+
139
164
// https://github.com/jackc/pgx/issues/1273#issuecomment-1221414648
140
165
func TestJSONCodecUnmarshalSQLNull (t * testing.T ) {
141
166
defaultConnTestRunner .RunTest (context .Background (), t , func (ctx context.Context , t testing.TB , conn * pgx.Conn ) {
@@ -267,7 +292,8 @@ func TestJSONCodecCustomMarshal(t *testing.T) {
267
292
Unmarshal : func (data []byte , v any ) error {
268
293
return json .Unmarshal ([]byte (`{"custom":"value"}` ), v )
269
294
},
270
- }})
295
+ },
296
+ })
271
297
}
272
298
273
299
pgxtest .RunValueRoundTripTests (context .Background (), t , connTestRunner , pgxtest .KnownOIDQueryExecModes , "json" , []pgxtest.ValueRoundTripTest {
@@ -278,3 +304,20 @@ func TestJSONCodecCustomMarshal(t *testing.T) {
278
304
}},
279
305
})
280
306
}
307
+
308
+ func TestJSONCodecScanToNonPointerValues (t * testing.T ) {
309
+ defaultConnTestRunner .RunTest (context .Background (), t , func (ctx context.Context , t testing.TB , conn * pgx.Conn ) {
310
+ n := 44
311
+ err := conn .QueryRow (ctx , "select '42'::jsonb" ).Scan (n )
312
+ require .Error (t , err )
313
+
314
+ var i * int
315
+ err = conn .QueryRow (ctx , "select '42'::jsonb" ).Scan (i )
316
+ require .Error (t , err )
317
+
318
+ m := 0
319
+ err = conn .QueryRow (ctx , "select '42'::jsonb" ).Scan (& m )
320
+ require .NoError (t , err )
321
+ require .Equal (t , 42 , m )
322
+ })
323
+ }
0 commit comments