af0ca3a39b
Original issue https://github.com/jackc/pgtype/issues/68
This crash occurred in the recursive assignment system used to support
multidimensional arrays.
This was fixed in 9639a69d45. However,
that fix incorrectly used nil instead of an empty slice.
In hindsight, it appears the fundamental error is that an assignment to
a slice of a type that is not specified is handled with the recursive /
reflection path. Or another way of looking at it is as an unexpected
feature where []T can now be scanned if individual elements are
assignable to T even if []T is not specifically handled.
But this new reflection / recursive path did not handle empty arrays.
This fix handles the reflection path for an empty slice by allocating an
empty slice.
230 lines
7.6 KiB
Go
230 lines
7.6 KiB
Go
package pgtype_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgtype"
|
|
"github.com/jackc/pgtype/testutil"
|
|
)
|
|
|
|
func TestByteaArrayTranscode(t *testing.T) {
|
|
testutil.TestSuccessfulTranscode(t, "bytea[]", []interface{}{
|
|
&pgtype.ByteaArray{
|
|
Elements: nil,
|
|
Dimensions: nil,
|
|
Status: pgtype.Present,
|
|
},
|
|
&pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Status: pgtype.Null},
|
|
},
|
|
Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
|
|
Status: pgtype.Present,
|
|
},
|
|
&pgtype.ByteaArray{Status: pgtype.Null},
|
|
&pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{}, Status: pgtype.Present},
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Status: pgtype.Null},
|
|
{Bytes: []byte{1}, Status: pgtype.Present},
|
|
},
|
|
Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
|
|
Status: pgtype.Present,
|
|
},
|
|
&pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{}, Status: pgtype.Present},
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{1}, Status: pgtype.Present},
|
|
},
|
|
Dimensions: []pgtype.ArrayDimension{
|
|
{Length: 2, LowerBound: 4},
|
|
{Length: 2, LowerBound: 2},
|
|
},
|
|
Status: pgtype.Present,
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestByteaArraySet(t *testing.T) {
|
|
successfulTests := []struct {
|
|
source interface{}
|
|
result pgtype.ByteaArray
|
|
}{
|
|
{
|
|
source: [][]byte{{1, 2, 3}},
|
|
result: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
|
|
Status: pgtype.Present},
|
|
},
|
|
{
|
|
source: (([][]byte)(nil)),
|
|
result: pgtype.ByteaArray{Status: pgtype.Null},
|
|
},
|
|
{
|
|
source: [][][]byte{{{1}}, {{2}}},
|
|
result: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
|
|
Status: pgtype.Present},
|
|
},
|
|
{
|
|
source: [][][][][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
|
|
result: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
|
|
{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
|
|
{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
|
|
{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
|
|
{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{
|
|
{LowerBound: 1, Length: 2},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 3}},
|
|
Status: pgtype.Present},
|
|
},
|
|
{
|
|
source: [2][1][]byte{{{1}}, {{2}}},
|
|
result: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
|
|
Status: pgtype.Present},
|
|
},
|
|
{
|
|
source: [2][1][1][3][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
|
|
result: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
|
|
{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
|
|
{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
|
|
{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
|
|
{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{
|
|
{LowerBound: 1, Length: 2},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 3}},
|
|
Status: pgtype.Present},
|
|
},
|
|
}
|
|
|
|
for i, tt := range successfulTests {
|
|
var r pgtype.ByteaArray
|
|
err := r.Set(tt.source)
|
|
if err != nil {
|
|
t.Errorf("%d: %v", i, err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(r, tt.result) {
|
|
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestByteaArrayAssignTo(t *testing.T) {
|
|
var byteByteSlice [][]byte
|
|
var byteByteSliceDim2 [][][]byte
|
|
var byteByteSliceDim4 [][][][][]byte
|
|
var byteByteArraySliceDim2 [2][1][]byte
|
|
var byteByteArraySliceDim4 [2][1][1][3][]byte
|
|
|
|
simpleTests := []struct {
|
|
src pgtype.ByteaArray
|
|
dst interface{}
|
|
expected interface{}
|
|
}{
|
|
{
|
|
src: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
|
|
Status: pgtype.Present,
|
|
},
|
|
dst: &byteByteSlice,
|
|
expected: [][]byte{{1, 2, 3}},
|
|
},
|
|
{
|
|
src: pgtype.ByteaArray{Status: pgtype.Null},
|
|
dst: &byteByteSlice,
|
|
expected: (([][]byte)(nil)),
|
|
},
|
|
{
|
|
src: pgtype.ByteaArray{Status: pgtype.Present},
|
|
dst: &byteByteSlice,
|
|
expected: [][]byte{},
|
|
},
|
|
{
|
|
src: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
|
|
Status: pgtype.Present},
|
|
dst: &byteByteSliceDim2,
|
|
expected: [][][]byte{{{1}}, {{2}}},
|
|
},
|
|
{
|
|
src: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
|
|
{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
|
|
{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
|
|
{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
|
|
{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{
|
|
{LowerBound: 1, Length: 2},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 3}},
|
|
Status: pgtype.Present},
|
|
dst: &byteByteSliceDim4,
|
|
expected: [][][][][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
|
|
},
|
|
{
|
|
src: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
|
|
Status: pgtype.Present},
|
|
dst: &byteByteArraySliceDim2,
|
|
expected: [2][1][]byte{{{1}}, {{2}}},
|
|
},
|
|
{
|
|
src: pgtype.ByteaArray{
|
|
Elements: []pgtype.Bytea{
|
|
{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
|
|
{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
|
|
{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
|
|
{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
|
|
{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
|
|
{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
|
|
Dimensions: []pgtype.ArrayDimension{
|
|
{LowerBound: 1, Length: 2},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 1},
|
|
{LowerBound: 1, Length: 3}},
|
|
Status: pgtype.Present},
|
|
dst: &byteByteArraySliceDim4,
|
|
expected: [2][1][1][3][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
|
|
},
|
|
}
|
|
|
|
for i, tt := range simpleTests {
|
|
err := tt.src.AssignTo(tt.dst)
|
|
if err != nil {
|
|
t.Errorf("%d: %v", i, err)
|
|
}
|
|
|
|
if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
|
|
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
|
|
}
|
|
}
|
|
}
|