From c875abea84ee9b6564787192e7150129d275a14d Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 4 Feb 2023 07:28:52 -0600 Subject: [PATCH] Fix encode []any to array https://github.com/jackc/pgx/issues/1488 --- pgtype/array_codec.go | 4 +++- pgtype/array_codec_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pgtype/array_codec.go b/pgtype/array_codec.go index dae12039..56393eb0 100644 --- a/pgtype/array_codec.go +++ b/pgtype/array_codec.go @@ -60,7 +60,9 @@ func (c *ArrayCodec) PlanEncode(m *Map, oid uint32, format int16, value any) Enc elementEncodePlan := m.PlanEncode(c.ElementType.OID, format, elementType) if elementEncodePlan == nil { - return nil + if reflect.TypeOf(elementType) != nil { + return nil + } } switch format { diff --git a/pgtype/array_codec_test.go b/pgtype/array_codec_test.go index ede104ac..f90995b3 100644 --- a/pgtype/array_codec_test.go +++ b/pgtype/array_codec_test.go @@ -103,7 +103,7 @@ func TestArrayCodecArray(t *testing.T) { }) } -func TestArrayCodecAnySlice(t *testing.T) { +func TestArrayCodecNamedSliceType(t *testing.T) { defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { type _int16Slice []int16 @@ -126,6 +126,29 @@ func TestArrayCodecAnySlice(t *testing.T) { }) } +// https://github.com/jackc/pgx/issues/1488 +func TestArrayCodecAnySliceArgument(t *testing.T) { + defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { + type _int16Slice []int16 + + for i, tt := range []struct { + arg any + expected []int16 + }{ + {[]any{1, 2, 3}, []int16{1, 2, 3}}, + } { + var actual []int16 + err := conn.QueryRow( + ctx, + "select $1::smallint[]", + tt.arg, + ).Scan(&actual) + assert.NoErrorf(t, err, "%d", i) + assert.Equalf(t, tt.expected, actual, "%d", i) + } + }) +} + // https://github.com/jackc/pgx/issues/1442 func TestArrayCodecAnyArray(t *testing.T) { defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {