diff --git a/aclitem_array.go b/aclitem_array.go index 501074d6..bf7bba93 100644 --- a/aclitem_array.go +++ b/aclitem_array.go @@ -228,6 +228,12 @@ func (src *ACLItemArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/array_test.go b/array_test.go index 2f3b9237..d2120677 100644 --- a/array_test.go +++ b/array_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jackc/pgtype" + "github.com/stretchr/testify/require" ) func TestParseUntypedTextArray(t *testing.T) { @@ -113,3 +114,14 @@ func TestParseUntypedTextArray(t *testing.T) { } } } + +// https://github.com/jackc/pgx/issues/881 +func TestArrayAssignToEmptyToNonSlice(t *testing.T) { + var a pgtype.Int4Array + err := a.Set([]int32{}) + require.NoError(t, err) + + var iface interface{} + err = a.AssignTo(&iface) + require.EqualError(t, err, "cannot assign *pgtype.Int4Array to *interface {}") +} diff --git a/bool_array.go b/bool_array.go index 232863ec..2659321e 100644 --- a/bool_array.go +++ b/bool_array.go @@ -230,6 +230,12 @@ func (src *BoolArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/bpchar_array.go b/bpchar_array.go index aad7c144..d48b2b53 100644 --- a/bpchar_array.go +++ b/bpchar_array.go @@ -230,6 +230,12 @@ func (src *BPCharArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/bytea_array.go b/bytea_array.go index 1dee05fa..14d8afad 100644 --- a/bytea_array.go +++ b/bytea_array.go @@ -202,6 +202,12 @@ func (src *ByteaArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/cidr_array.go b/cidr_array.go index 645c641a..3ac1b183 100644 --- a/cidr_array.go +++ b/cidr_array.go @@ -259,6 +259,12 @@ func (src *CIDRArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/date_array.go b/date_array.go index a546a854..0c623b8f 100644 --- a/date_array.go +++ b/date_array.go @@ -231,6 +231,12 @@ func (src *DateArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/enum_array.go b/enum_array.go index d497dead..cf7c7066 100644 --- a/enum_array.go +++ b/enum_array.go @@ -228,6 +228,12 @@ func (src *EnumArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/float4_array.go b/float4_array.go index c399697d..91b3b0e2 100644 --- a/float4_array.go +++ b/float4_array.go @@ -230,6 +230,12 @@ func (src *Float4Array) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/float8_array.go b/float8_array.go index 9a961c2f..559ee292 100644 --- a/float8_array.go +++ b/float8_array.go @@ -230,6 +230,12 @@ func (src *Float8Array) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/hstore_array.go b/hstore_array.go index 0be072cc..a44ea629 100644 --- a/hstore_array.go +++ b/hstore_array.go @@ -202,6 +202,12 @@ func (src *HstoreArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/inet_array.go b/inet_array.go index d5d0a665..30adeabb 100644 --- a/inet_array.go +++ b/inet_array.go @@ -259,6 +259,12 @@ func (src *InetArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/int2_array.go b/int2_array.go index 8aeb7d46..f4bd64cc 100644 --- a/int2_array.go +++ b/int2_array.go @@ -622,6 +622,12 @@ func (src *Int2Array) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/int4_array.go b/int4_array.go index 76ca811e..528310ff 100644 --- a/int4_array.go +++ b/int4_array.go @@ -622,6 +622,12 @@ func (src *Int4Array) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/int8_array.go b/int8_array.go index 45d8447f..b1e52a97 100644 --- a/int8_array.go +++ b/int8_array.go @@ -622,6 +622,12 @@ func (src *Int8Array) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/jsonb_array.go b/jsonb_array.go index c8ef1fcd..5d658ed5 100644 --- a/jsonb_array.go +++ b/jsonb_array.go @@ -230,6 +230,12 @@ func (src *JSONBArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/macaddr_array.go b/macaddr_array.go index 7f78c304..0ac2618e 100644 --- a/macaddr_array.go +++ b/macaddr_array.go @@ -231,6 +231,12 @@ func (src *MacaddrArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/numeric_array.go b/numeric_array.go index 49c70855..1c2ae489 100644 --- a/numeric_array.go +++ b/numeric_array.go @@ -398,6 +398,12 @@ func (src *NumericArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/text_array.go b/text_array.go index d7125237..afdc507b 100644 --- a/text_array.go +++ b/text_array.go @@ -230,6 +230,12 @@ func (src *TextArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/timestamp_array.go b/timestamp_array.go index bb819017..5256f185 100644 --- a/timestamp_array.go +++ b/timestamp_array.go @@ -231,6 +231,12 @@ func (src *TimestampArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/timestamptz_array.go b/timestamptz_array.go index f028e0f9..47408c02 100644 --- a/timestamptz_array.go +++ b/timestamptz_array.go @@ -231,6 +231,12 @@ func (src *TimestamptzArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/tstzrange_array.go b/tstzrange_array.go index 4f03d838..6d9bfe3b 100644 --- a/tstzrange_array.go +++ b/tstzrange_array.go @@ -183,6 +183,12 @@ func (src *TstzrangeArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/typed_array.go.erb b/typed_array.go.erb index 60665270..52f14592 100644 --- a/typed_array.go.erb +++ b/typed_array.go.erb @@ -203,6 +203,12 @@ func (src *<%= pgtype_array_type %>) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/uuid_array.go b/uuid_array.go index 894bbd40..c6970d52 100644 --- a/uuid_array.go +++ b/uuid_array.go @@ -286,6 +286,12 @@ func (src *UUIDArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0)) diff --git a/varchar_array.go b/varchar_array.go index d515c2a4..f3a9b001 100644 --- a/varchar_array.go +++ b/varchar_array.go @@ -230,6 +230,12 @@ func (src *VarcharArray) AssignTo(dst interface{}) error { value = value.Elem() } + switch value.Kind() { + case reflect.Array, reflect.Slice: + default: + return errors.Errorf("cannot assign %T to %T", src, dst) + } + if len(src.Elements) == 0 { if value.Kind() == reflect.Slice { value.Set(reflect.MakeSlice(value.Type(), 0, 0))