Skip to content

Commit

Permalink
Lists: Fix handling of go arrays (#894)
Browse files Browse the repository at this point in the history
Go arrays currently cause a panic, because `reflect.MakeSlice` is used
to construct them which is invalid, this change fixes that.
  • Loading branch information
alvaroaleman committed Feb 2, 2024
1 parent 19b2ad1 commit f221e70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion common/types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ func (l *baseList) ConvertToNative(typeDesc reflect.Type) (any, error) {
// Allow the element ConvertToNative() function to determine whether conversion is possible.
otherElemType := typeDesc.Elem()
elemCount := l.size
nativeList := reflect.MakeSlice(typeDesc, elemCount, elemCount)
var nativeList reflect.Value
if typeDesc.Kind() == reflect.Array {
nativeList = reflect.New(reflect.ArrayOf(elemCount, typeDesc)).Elem().Index(0)
} else {
nativeList = reflect.MakeSlice(typeDesc, elemCount, elemCount)

}
for i := 0; i < elemCount; i++ {
elem := l.NativeToValue(l.get(i))
nativeElemVal, err := elem.ConvertToNative(otherElemType)
Expand Down
11 changes: 11 additions & 0 deletions ext/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func TestNativeTypes(t *testing.T) {
NestedMapVal: {42: true},
},
],
ArrayVal: [
ext.TestNestedType{
NestedListVal:['goodbye', 'cruel', 'world'],
NestedMapVal: {42: true},
},
],
MapVal: {'map-key': ext.TestAllTypes{BoolVal: true}},
CustomSliceVal: [ext.TestNestedSliceType{Value: 'none'}],
CustomMapVal: {'even': ext.TestMapVal{Value: 'more'}},
Expand All @@ -85,6 +91,10 @@ func TestNativeTypes(t *testing.T) {
NestedMapVal: map[int64]bool{42: true},
},
},
ArrayVal: [1]*TestNestedType{{
NestedListVal: []string{"goodbye", "cruel", "world"},
NestedMapVal: map[int64]bool{42: true},
}},
MapVal: map[string]TestAllTypes{"map-key": {BoolVal: true}},
CustomSliceVal: []TestNestedSliceType{{Value: "none"}},
CustomMapVal: map[string]TestMapVal{"even": {Value: "more"}},
Expand Down Expand Up @@ -688,6 +698,7 @@ type TestAllTypes struct {
Uint32Val uint32
Uint64Val uint64
ListVal []*TestNestedType
ArrayVal [1]*TestNestedType
MapVal map[string]TestAllTypes
PbVal *proto3pb.TestAllTypes
CustomSliceVal []TestNestedSliceType
Expand Down

0 comments on commit f221e70

Please sign in to comment.