From 9639a69d451f55456f598c1aa8b93053f8df3088 Mon Sep 17 00:00:00 2001 From: Simo Haasanen Date: Tue, 20 Oct 2020 19:52:05 +0100 Subject: [PATCH] Adds checks for zero length arrays. Assigning values from nil or zero length elements or dimensions now return immediately as there are no values to assign. --- aclitem_array.go | 4 ++++ aclitem_array_test.go | 5 +++++ bool_array.go | 4 ++++ bool_array_test.go | 5 +++++ bpchar_array.go | 4 ++++ bytea_array.go | 4 ++++ bytea_array_test.go | 5 +++++ cidr_array.go | 4 ++++ cidr_array_test.go | 10 ++++++++++ date_array.go | 4 ++++ date_array_test.go | 5 +++++ enum_array.go | 4 ++++ enum_array_test.go | 5 +++++ float4_array.go | 4 ++++ float4_array_test.go | 5 +++++ float8_array.go | 4 ++++ float8_array_test.go | 5 +++++ hstore_array.go | 4 ++++ hstore_array_test.go | 3 +++ inet_array.go | 4 ++++ inet_array_test.go | 10 ++++++++++ int2_array.go | 4 ++++ int2_array_test.go | 5 +++++ int4_array.go | 4 ++++ int4_array_test.go | 5 +++++ int8_array.go | 4 ++++ int8_array_test.go | 5 +++++ jsonb_array.go | 4 ++++ macaddr_array.go | 4 ++++ macaddr_array_test.go | 5 +++++ numeric_array.go | 4 ++++ numeric_array_test.go | 5 +++++ text_array.go | 4 ++++ text_array_test.go | 5 +++++ timestamp_array.go | 4 ++++ timestamp_array_test.go | 5 +++++ timestamptz_array.go | 4 ++++ timestamptz_array_test.go | 5 +++++ tstzrange_array.go | 4 ++++ typed_array.go.erb | 4 ++++ uuid_array.go | 4 ++++ uuid_array_test.go | 11 +++++++++++ varchar_array.go | 4 ++++ varchar_array_test.go | 5 +++++ 44 files changed, 210 insertions(+) diff --git a/aclitem_array.go b/aclitem_array.go index 673951a6..95f74aa7 100644 --- a/aclitem_array.go +++ b/aclitem_array.go @@ -190,6 +190,10 @@ func (dst ACLItemArray) Get() interface{} { func (src *ACLItemArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/aclitem_array_test.go b/aclitem_array_test.go index 73e9ce71..8ebb8c8d 100644 --- a/aclitem_array_test.go +++ b/aclitem_array_test.go @@ -189,6 +189,11 @@ func TestACLItemArrayAssignTo(t *testing.T) { dst: &stringSlice, expected: (([]string)(nil)), }, + { + src: pgtype.ACLItemArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, { src: pgtype.ACLItemArray{ Elements: []pgtype.ACLItem{ diff --git a/bool_array.go b/bool_array.go index ed411a28..c9447db1 100644 --- a/bool_array.go +++ b/bool_array.go @@ -192,6 +192,10 @@ func (dst BoolArray) Get() interface{} { func (src *BoolArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/bool_array_test.go b/bool_array_test.go index 7f31e252..4ff26bb5 100644 --- a/bool_array_test.go +++ b/bool_array_test.go @@ -168,6 +168,11 @@ func TestBoolArrayAssignTo(t *testing.T) { dst: &boolSlice, expected: (([]bool)(nil)), }, + { + src: pgtype.BoolArray{Status: pgtype.Present}, + dst: &boolSlice, + expected: (([]bool)(nil)), + }, { src: pgtype.BoolArray{ Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, diff --git a/bpchar_array.go b/bpchar_array.go index 0b92ba30..f814930f 100644 --- a/bpchar_array.go +++ b/bpchar_array.go @@ -192,6 +192,10 @@ func (dst BPCharArray) Get() interface{} { func (src *BPCharArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/bytea_array.go b/bytea_array.go index f80980bd..618a2f4b 100644 --- a/bytea_array.go +++ b/bytea_array.go @@ -173,6 +173,10 @@ func (dst ByteaArray) Get() interface{} { func (src *ByteaArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/bytea_array_test.go b/bytea_array_test.go index f40005a2..4964771c 100644 --- a/bytea_array_test.go +++ b/bytea_array_test.go @@ -157,6 +157,11 @@ func TestByteaArrayAssignTo(t *testing.T) { dst: &byteByteSlice, expected: (([][]byte)(nil)), }, + { + src: pgtype.ByteaArray{Status: pgtype.Present}, + dst: &byteByteSlice, + expected: (([][]byte)(nil)), + }, { src: pgtype.ByteaArray{ Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}}, diff --git a/cidr_array.go b/cidr_array.go index 0b902cca..8ea7d7a6 100644 --- a/cidr_array.go +++ b/cidr_array.go @@ -212,6 +212,10 @@ func (dst CIDRArray) Get() interface{} { func (src *CIDRArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/cidr_array_test.go b/cidr_array_test.go index b1769c38..aa933b62 100644 --- a/cidr_array_test.go +++ b/cidr_array_test.go @@ -217,11 +217,21 @@ func TestCIDRArrayAssignTo(t *testing.T) { dst: &ipnetSlice, expected: (([]*net.IPNet)(nil)), }, + { + src: pgtype.CIDRArray{Status: pgtype.Present}, + dst: &ipnetSlice, + expected: (([]*net.IPNet)(nil)), + }, { src: pgtype.CIDRArray{Status: pgtype.Null}, dst: &ipSlice, expected: (([]net.IP)(nil)), }, + { + src: pgtype.CIDRArray{Status: pgtype.Present}, + dst: &ipSlice, + expected: (([]net.IP)(nil)), + }, { src: pgtype.CIDRArray{ Elements: []pgtype.CIDR{ diff --git a/date_array.go b/date_array.go index b306589e..dc4cb2e3 100644 --- a/date_array.go +++ b/date_array.go @@ -193,6 +193,10 @@ func (dst DateArray) Get() interface{} { func (src *DateArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/date_array_test.go b/date_array_test.go index 089c7dd4..8791c31f 100644 --- a/date_array_test.go +++ b/date_array_test.go @@ -182,6 +182,11 @@ func TestDateArrayAssignTo(t *testing.T) { dst: &timeSlice, expected: (([]time.Time)(nil)), }, + { + src: pgtype.DateArray{Status: pgtype.Present}, + dst: &timeSlice, + expected: (([]time.Time)(nil)), + }, { src: pgtype.DateArray{ Elements: []pgtype.Date{ diff --git a/enum_array.go b/enum_array.go index 4b6d2af4..f5312a04 100644 --- a/enum_array.go +++ b/enum_array.go @@ -190,6 +190,10 @@ func (dst EnumArray) Get() interface{} { func (src *EnumArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/enum_array_test.go b/enum_array_test.go index 91a81ab6..9db8b49f 100644 --- a/enum_array_test.go +++ b/enum_array_test.go @@ -167,6 +167,11 @@ func TestEnumArrayArrayAssignTo(t *testing.T) { dst: &stringSlice, expected: (([]string)(nil)), }, + { + src: pgtype.EnumArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, { src: pgtype.EnumArray{ Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, diff --git a/float4_array.go b/float4_array.go index 22577023..88dd84ab 100644 --- a/float4_array.go +++ b/float4_array.go @@ -192,6 +192,10 @@ func (dst Float4Array) Get() interface{} { func (src *Float4Array) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/float4_array_test.go b/float4_array_test.go index 23a94ee8..88d35fd6 100644 --- a/float4_array_test.go +++ b/float4_array_test.go @@ -167,6 +167,11 @@ func TestFloat4ArrayAssignTo(t *testing.T) { dst: &float32Slice, expected: (([]float32)(nil)), }, + { + src: pgtype.Float4Array{Status: pgtype.Present}, + dst: &float32Slice, + expected: (([]float32)(nil)), + }, { src: pgtype.Float4Array{ Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, diff --git a/float8_array.go b/float8_array.go index 6c309700..9d79a449 100644 --- a/float8_array.go +++ b/float8_array.go @@ -192,6 +192,10 @@ func (dst Float8Array) Get() interface{} { func (src *Float8Array) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/float8_array_test.go b/float8_array_test.go index 052ab3f3..d7bf6ac3 100644 --- a/float8_array_test.go +++ b/float8_array_test.go @@ -143,6 +143,11 @@ func TestFloat8ArrayAssignTo(t *testing.T) { dst: &float64Slice, expected: (([]float64)(nil)), }, + { + src: pgtype.Float8Array{Status: pgtype.Present}, + dst: &float64Slice, + expected: (([]float64)(nil)), + }, { src: pgtype.Float8Array{ Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, diff --git a/hstore_array.go b/hstore_array.go index 413e3993..d0b34b3c 100644 --- a/hstore_array.go +++ b/hstore_array.go @@ -173,6 +173,10 @@ func (dst HstoreArray) Get() interface{} { func (src *HstoreArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/hstore_array_test.go b/hstore_array_test.go index fac66b4a..3d85545a 100644 --- a/hstore_array_test.go +++ b/hstore_array_test.go @@ -302,6 +302,9 @@ func TestHstoreArrayAssignTo(t *testing.T) { { src: pgtype.HstoreArray{Status: pgtype.Null}, dst: &hstoreSlice, expected: (([]map[string]string)(nil)), }, + { + src: pgtype.HstoreArray{Status: pgtype.Present}, dst: &hstoreSlice, expected: (([]map[string]string)(nil)), + }, { src: pgtype.HstoreArray{ Elements: []pgtype.Hstore{ diff --git a/inet_array.go b/inet_array.go index c4368ebc..2058db81 100644 --- a/inet_array.go +++ b/inet_array.go @@ -212,6 +212,10 @@ func (dst InetArray) Get() interface{} { func (src *InetArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/inet_array_test.go b/inet_array_test.go index d78b91c0..5beab960 100644 --- a/inet_array_test.go +++ b/inet_array_test.go @@ -217,11 +217,21 @@ func TestInetArrayAssignTo(t *testing.T) { dst: &ipnetSlice, expected: (([]*net.IPNet)(nil)), }, + { + src: pgtype.InetArray{Status: pgtype.Present}, + dst: &ipnetSlice, + expected: (([]*net.IPNet)(nil)), + }, { src: pgtype.InetArray{Status: pgtype.Null}, dst: &ipSlice, expected: (([]net.IP)(nil)), }, + { + src: pgtype.InetArray{Status: pgtype.Present}, + dst: &ipSlice, + expected: (([]net.IP)(nil)), + }, { src: pgtype.InetArray{ Elements: []pgtype.Inet{ diff --git a/int2_array.go b/int2_array.go index 71ccc0c4..bf6a6284 100644 --- a/int2_array.go +++ b/int2_array.go @@ -458,6 +458,10 @@ func (dst Int2Array) Get() interface{} { func (src *Int2Array) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/int2_array_test.go b/int2_array_test.go index dfe84c19..da669f7d 100644 --- a/int2_array_test.go +++ b/int2_array_test.go @@ -219,6 +219,11 @@ func TestInt2ArrayAssignTo(t *testing.T) { dst: &int16Slice, expected: (([]int16)(nil)), }, + { + src: pgtype.Int2Array{Status: pgtype.Present}, + dst: &int16Slice, + expected: (([]int16)(nil)), + }, { src: pgtype.Int2Array{ Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, diff --git a/int4_array.go b/int4_array.go index 09b23c2f..05e10dc3 100644 --- a/int4_array.go +++ b/int4_array.go @@ -458,6 +458,10 @@ func (dst Int4Array) Get() interface{} { func (src *Int4Array) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/int4_array_test.go b/int4_array_test.go index 35b791d3..a5aad827 100644 --- a/int4_array_test.go +++ b/int4_array_test.go @@ -233,6 +233,11 @@ func TestInt4ArrayAssignTo(t *testing.T) { dst: &int32Slice, expected: (([]int32)(nil)), }, + { + src: pgtype.Int4Array{Status: pgtype.Present}, + dst: &int32Slice, + expected: (([]int32)(nil)), + }, { src: pgtype.Int4Array{ Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, diff --git a/int8_array.go b/int8_array.go index 93a902b0..d149558f 100644 --- a/int8_array.go +++ b/int8_array.go @@ -458,6 +458,10 @@ func (dst Int8Array) Get() interface{} { func (src *Int8Array) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/int8_array_test.go b/int8_array_test.go index d65b875a..b0ee97ee 100644 --- a/int8_array_test.go +++ b/int8_array_test.go @@ -226,6 +226,11 @@ func TestInt8ArrayAssignTo(t *testing.T) { dst: &int64Slice, expected: (([]int64)(nil)), }, + { + src: pgtype.Int8Array{Status: pgtype.Present}, + dst: &int64Slice, + expected: (([]int64)(nil)), + }, { src: pgtype.Int8Array{ Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, diff --git a/jsonb_array.go b/jsonb_array.go index 98970dcf..36411b9d 100644 --- a/jsonb_array.go +++ b/jsonb_array.go @@ -192,6 +192,10 @@ func (dst JSONBArray) Get() interface{} { func (src *JSONBArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/macaddr_array.go b/macaddr_array.go index eafa5482..2ec5971e 100644 --- a/macaddr_array.go +++ b/macaddr_array.go @@ -193,6 +193,10 @@ func (dst MacaddrArray) Get() interface{} { func (src *MacaddrArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/macaddr_array_test.go b/macaddr_array_test.go index 647db8cf..6359a374 100644 --- a/macaddr_array_test.go +++ b/macaddr_array_test.go @@ -166,6 +166,11 @@ func TestMacaddrArrayAssignTo(t *testing.T) { dst: &macaddrSlice, expected: (([]net.HardwareAddr)(nil)), }, + { + src: pgtype.MacaddrArray{Status: pgtype.Present}, + dst: &macaddrSlice, + expected: (([]net.HardwareAddr)(nil)), + }, { src: pgtype.MacaddrArray{ Elements: []pgtype.Macaddr{ diff --git a/numeric_array.go b/numeric_array.go index 806557bc..7c044c8c 100644 --- a/numeric_array.go +++ b/numeric_array.go @@ -306,6 +306,10 @@ func (dst NumericArray) Get() interface{} { func (src *NumericArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/numeric_array_test.go b/numeric_array_test.go index 29300bf0..def8150d 100644 --- a/numeric_array_test.go +++ b/numeric_array_test.go @@ -190,6 +190,11 @@ func TestNumericArrayAssignTo(t *testing.T) { dst: &float32Slice, expected: (([]float32)(nil)), }, + { + src: pgtype.NumericArray{Status: pgtype.Present}, + dst: &float32Slice, + expected: (([]float32)(nil)), + }, { src: pgtype.NumericArray{ Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, diff --git a/text_array.go b/text_array.go index 03f72d37..01b5e6e6 100644 --- a/text_array.go +++ b/text_array.go @@ -192,6 +192,10 @@ func (dst TextArray) Get() interface{} { func (src *TextArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/text_array_test.go b/text_array_test.go index 125d6034..a538c617 100644 --- a/text_array_test.go +++ b/text_array_test.go @@ -168,6 +168,11 @@ func TestTextArrayAssignTo(t *testing.T) { dst: &stringSlice, expected: (([]string)(nil)), }, + { + src: pgtype.TextArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, { src: pgtype.TextArray{ Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, diff --git a/timestamp_array.go b/timestamp_array.go index 27f6e867..ee6037b0 100644 --- a/timestamp_array.go +++ b/timestamp_array.go @@ -193,6 +193,10 @@ func (dst TimestampArray) Get() interface{} { func (src *TimestampArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/timestamp_array_test.go b/timestamp_array_test.go index c6f32d20..85db94bb 100644 --- a/timestamp_array_test.go +++ b/timestamp_array_test.go @@ -162,6 +162,11 @@ func TestTimestampArrayAssignTo(t *testing.T) { dst: &timeSlice, expected: (([]time.Time)(nil)), }, + { + src: pgtype.TimestampArray{Status: pgtype.Present}, + dst: &timeSlice, + expected: (([]time.Time)(nil)), + }, { src: pgtype.TimestampArray{ Elements: []pgtype.Timestamp{ diff --git a/timestamptz_array.go b/timestamptz_array.go index 4db5c979..327b3ebc 100644 --- a/timestamptz_array.go +++ b/timestamptz_array.go @@ -193,6 +193,10 @@ func (dst TimestamptzArray) Get() interface{} { func (src *TimestamptzArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/timestamptz_array_test.go b/timestamptz_array_test.go index f4e80413..a4e1dded 100644 --- a/timestamptz_array_test.go +++ b/timestamptz_array_test.go @@ -198,6 +198,11 @@ func TestTimestamptzArrayAssignTo(t *testing.T) { dst: &timeSlice, expected: (([]time.Time)(nil)), }, + { + src: pgtype.TimestamptzArray{Status: pgtype.Present}, + dst: &timeSlice, + expected: (([]time.Time)(nil)), + }, { src: pgtype.TimestamptzArray{ Elements: []pgtype.Timestamptz{ diff --git a/tstzrange_array.go b/tstzrange_array.go index 2c9492f4..cac377af 100644 --- a/tstzrange_array.go +++ b/tstzrange_array.go @@ -154,6 +154,10 @@ func (dst TstzrangeArray) Get() interface{} { func (src *TstzrangeArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/typed_array.go.erb b/typed_array.go.erb index c4c797de..6d34b0e1 100644 --- a/typed_array.go.erb +++ b/typed_array.go.erb @@ -174,6 +174,10 @@ func (dst <%= pgtype_array_type %>) Get() interface{} { func (src *<%= pgtype_array_type %>) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1{ // Attempt to match to select common types: switch v := dst.(type) { diff --git a/uuid_array.go b/uuid_array.go index 035fb114..33f2e62c 100644 --- a/uuid_array.go +++ b/uuid_array.go @@ -230,6 +230,10 @@ func (dst UUIDArray) Get() interface{} { func (src *UUIDArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/uuid_array_test.go b/uuid_array_test.go index cdb212bb..a1e14a04 100644 --- a/uuid_array_test.go +++ b/uuid_array_test.go @@ -214,6 +214,7 @@ func TestUUIDArrayAssignTo(t *testing.T) { var byteArraySlice [][16]byte var byteSliceSlice [][]byte var stringSlice []string + var byteSlice []byte var byteArraySliceDim2 [][][16]byte var stringSliceDim4 [][][][]string var byteArrayDim2 [2][1][16]byte @@ -252,6 +253,16 @@ func TestUUIDArrayAssignTo(t *testing.T) { dst: &byteSliceSlice, expected: ([][]byte)(nil), }, + { + src: pgtype.UUIDArray{Status: pgtype.Present}, + dst: &byteSlice, + expected: ([]byte)(nil), + }, + { + src: pgtype.UUIDArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, { src: pgtype.UUIDArray{ Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, diff --git a/varchar_array.go b/varchar_array.go index 95ab48f3..b3b030b8 100644 --- a/varchar_array.go +++ b/varchar_array.go @@ -192,6 +192,10 @@ func (dst VarcharArray) Get() interface{} { func (src *VarcharArray) AssignTo(dst interface{}) error { switch src.Status { case Present: + if len(src.Elements) == 0 || len(src.Dimensions) == 0 { + // No values to assign + return nil + } if len(src.Dimensions) <= 1 { // Attempt to match to select common types: switch v := dst.(type) { diff --git a/varchar_array_test.go b/varchar_array_test.go index 3b0e65ed..ca9a15b7 100644 --- a/varchar_array_test.go +++ b/varchar_array_test.go @@ -168,6 +168,11 @@ func TestVarcharArrayAssignTo(t *testing.T) { dst: &stringSlice, expected: (([]string)(nil)), }, + { + src: pgtype.VarcharArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, { src: pgtype.VarcharArray{ Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}},