2
0

RowToStructByPos supports embedded structs

https://github.com/jackc/pgx/issues/1273#issuecomment-1236966785
This commit is contained in:
Jack Christensen
2022-09-06 18:32:10 -05:00
parent f015ced1bf
commit ee2622a8e6
2 changed files with 69 additions and 17 deletions
+44
View File
@@ -329,6 +329,50 @@ func TestRowToStructByPos(t *testing.T) {
})
}
func TestRowToStructByPosEmbeddedStruct(t *testing.T) {
type Name struct {
First string
Last string
}
type person struct {
Name
Age int32
}
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
rows, _ := conn.Query(ctx, `select 'John' as first_name, 'Smith' as last_name, n as age from generate_series(0, 9) n`)
slice, err := pgx.CollectRows(rows, pgx.RowToStructByPos[person])
require.NoError(t, err)
assert.Len(t, slice, 10)
for i := range slice {
assert.Equal(t, "John", slice[i].Name.First)
assert.Equal(t, "Smith", slice[i].Name.Last)
assert.EqualValues(t, i, slice[i].Age)
}
})
}
// Pointer to struct is not supported. But check that we don't panic.
func TestRowToStructByPosEmbeddedPointerToStruct(t *testing.T) {
type Name struct {
First string
Last string
}
type person struct {
*Name
Age int32
}
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
rows, _ := conn.Query(ctx, `select 'John' as first_name, 'Smith' as last_name, n as age from generate_series(0, 9) n`)
_, err := pgx.CollectRows(rows, pgx.RowToStructByPos[person])
require.EqualError(t, err, "got 3 values, but dst struct has only 2 fields")
})
}
func ExampleRowToStructByPos() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()