Skip to content

Commit

Permalink
Fix composite decoder with array field
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaf committed Apr 20, 2021
1 parent 65057b2 commit 346ef9a
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 62 deletions.
2 changes: 2 additions & 0 deletions example/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func TestExamples(t *testing.T) {
args: []string{
"--schema-glob", "example/nested/schema.sql",
"--query-glob", "example/nested/query.sql",
"--go-type", "int4=int",
"--go-type", "text=string",
},
},
{
Expand Down
4 changes: 4 additions & 0 deletions example/nested/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func TestGenerate_Go_Example_nested(t *testing.T) {
OutputDir: tmpDir,
GoPackage: "nested",
Language: pggen.LangGo,
TypeOverrides: map[string]string{
"int4": "int",
"text": "string",
},
})
if err != nil {
t.Fatalf("Generate() example/nested: %s", err)
Expand Down
18 changes: 17 additions & 1 deletion example/nested/query.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
-- name: ArrayNested2 :one
SELECT
ARRAY [
ROW ('img2', ROW (22, 22)::dimensions)::product_image_type,
ROW ('img3', ROW (33, 33)::dimensions)::product_image_type
] AS images;

-- name: Nested3 :many
SELECT ROW (ROW ('item_name', ROW ('sku_id')::sku)::inventory_item, 88)::qux AS qux;
SELECT
ROW (
'name', -- name
ROW ('img1', ROW (11, 11)::dimensions)::product_image_type, -- orig_image
ARRAY [ --images
ROW ('img2', ROW (22, 22)::dimensions)::product_image_type,
ROW ('img3', ROW (33, 33)::dimensions)::product_image_type
]
)::product_image_set_type;

150 changes: 108 additions & 42 deletions example/nested/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 48 additions & 11 deletions example/nested/query.sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,68 @@ import (
"testing"
)

func TestQuerier(t *testing.T) {
func TestNewQuerier_ArrayNested2(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()

q := NewQuerier(conn)
ctx := context.Background()

item := "item_name"
sku := "sku_id"
foo := 88
want := []Qux{
want := []ProductImageType{
{Source: "img2", Dimensions: Dimensions{22, 22}},
{Source: "img3", Dimensions: Dimensions{33, 33}},
}
t.Run("ArrayNested2", func(t *testing.T) {
rows, err := q.ArrayNested2(ctx)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, want, rows)
})

t.Run("ArrayNested2Batch", func(t *testing.T) {
batch := &pgx.Batch{}
q.ArrayNested2Batch(batch)
results := conn.SendBatch(ctx, batch)
rows, err := q.ArrayNested2Scan(results)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, want, rows)
})
}

func TestNewQuerier_Nested3(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()

q := NewQuerier(conn)
ctx := context.Background()

want := []ProductImageSetType{
{
InvItem: InventoryItem{ItemName: &item, Sku: Sku{SkuID: &sku}},
Foo: &foo,
Name: "name",
OrigImage: ProductImageType{
Source: "img1",
Dimensions: Dimensions{Width: 11, Height: 11},
},
Images: []ProductImageType{
{Source: "img2", Dimensions: Dimensions{22, 22}},
{Source: "img3", Dimensions: Dimensions{33, 33}},
},
},
}
{
t.Run("Nested3", func(t *testing.T) {
t.Skipf("https://github.com/jackc/pgx/issues/874")
rows, err := q.Nested3(ctx)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, want, rows)
}
})

{
t.Run("Nested3Batch", func(t *testing.T) {
t.Skipf("https://github.com/jackc/pgx/issues/874")
batch := &pgx.Batch{}
q.Nested3Batch(batch)
results := conn.SendBatch(ctx, batch)
Expand All @@ -41,5 +78,5 @@ func TestQuerier(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, want, rows)
}
})
}
18 changes: 10 additions & 8 deletions example/nested/schema.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE TYPE SKU AS (
sku_id text
CREATE TYPE dimensions AS (
width int4,
height int4
);

CREATE TYPE inventory_item AS (
item_name text,
sku SKU
CREATE TYPE product_image_type AS (
source text,
dimensions dimensions
);

CREATE TABLE qux (
inv_item inventory_item,
foo int8
CREATE TYPE product_image_set_type AS (
name text,
orig_image product_image_type,
images product_image_type[]
);
3 changes: 3 additions & 0 deletions internal/codegen/golang/declarer_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func (c CompositeDecoderDeclarer) Declare(pkgPath string) (string, error) {
case gotype.EnumType:
sb.WriteString(NameEnumDecoderFunc(fieldType))
sb.WriteString("(),")
case gotype.ArrayType:
sb.WriteString(NameArrayDecoderFunc(fieldType))
sb.WriteString("(),")
case gotype.VoidType:
// skip
default:
Expand Down

0 comments on commit 346ef9a

Please sign in to comment.