Skip to content

Commit

Permalink
replace erroneous reflect.New with reflect.Zero in TryWrapStructScanPlan
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-roehrich authored and jackc committed Feb 11, 2023
1 parent fa5fbed commit 5cd8468
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgtype/pgtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ func TryWrapStructScanPlan(target any) (plan WrappedScanPlanNextSetter, nextValu

var targetElemValue reflect.Value
if targetValue.IsNil() {
targetElemValue = reflect.New(targetValue.Type().Elem())
targetElemValue = reflect.Zero(targetValue.Type().Elem())
} else {
targetElemValue = targetValue.Elem()
}
Expand Down
33 changes: 33 additions & 0 deletions pgtype/pgtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,39 @@ func TestScanPlanInterface(t *testing.T) {
assert.Error(t, err)
}

func TestPointerPointerStructScan(t *testing.T) {
m := pgtype.NewMap()
type composite struct {
ID int
}

int4Type, _ := m.TypeForOID(pgtype.Int4OID)
pgt := &pgtype.Type{
Codec: &pgtype.CompositeCodec{
Fields: []pgtype.CompositeCodecField{
{
Name: "id",
Type: int4Type,
},
},
},
Name: "composite",
OID: 215333,
}
m.RegisterType(pgt)

var c *composite
plan := m.PlanScan(pgt.OID, pgtype.BinaryFormatCode, &c)
err := plan.Scan([]byte{
0, 0, 0, 1,
0, 0, 0, 23,
0, 0, 0, 4,
0, 0, 0, 1,
}, &c)
require.NoError(t, err)
require.Equal(t, c.ID, 1)
}

// https://github.com/jackc/pgx/issues/1263
func TestMapScanPtrToPtrToSlice(t *testing.T) {
m := pgtype.NewMap()
Expand Down

0 comments on commit 5cd8468

Please sign in to comment.