Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions data/abi/abi_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,8 @@ func inferToSlice(value interface{}) ([]interface{}, error) {
if reflectVal.Kind() != reflect.Slice && reflectVal.Kind() != reflect.Array {
return nil, fmt.Errorf("cannot infer an interface value as a slice of interface element")
}
if reflectVal.IsNil() {
if reflectVal.Kind() == reflect.Slice {
return nil, nil
}
return nil, fmt.Errorf("cannot infer nil value for array kind interface")
}
// * if input is a slice, with nil, then reflectVal.Len() == 0
// * if input is an array, it is not possible it is nil
values := make([]interface{}, reflectVal.Len())
for i := 0; i < reflectVal.Len(); i++ {
values[i] = reflectVal.Index(i).Interface()
Expand Down
41 changes: 41 additions & 0 deletions data/abi/abi_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,3 +1224,44 @@ func TestParseMethodSignature(t *testing.T) {
})
}
}

func TestInferToSlice(t *testing.T) {
partitiontest.PartitionTest(t)

var emptySlice []int
tests := []struct {
toBeInferred interface{}
length int
}{
{
toBeInferred: []int{},
length: 0,
},
{
toBeInferred: make([]int, 0),
length: 0,
},
{
toBeInferred: emptySlice,
length: 0,
},
{
toBeInferred: [0]int{},
length: 0,
},
{
toBeInferred: [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
length: 32,
},
{
toBeInferred: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
length: 32,
},
}

for i, test := range tests {
inferredSlice, err := inferToSlice(test.toBeInferred)
require.NoError(t, err, "inferToSlice on testcase %d failed to successfully infer %v", i, test.toBeInferred)
require.Equal(t, test.length, len(inferredSlice), "inferToSlice on testcase %d inferred different length, expected %d", i, test.length)
}
}