Skip to content

Commit

Permalink
Lists: Fix handling of go arrays
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 1, 2024
1 parent ba58735 commit a5be570
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}},
}`,
out: &TestAllTypes{
Expand All @@ -83,6 +89,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}},
},
},
Expand Down Expand Up @@ -685,6 +695,7 @@ type TestAllTypes struct {
Uint32Val uint32
Uint64Val uint64
ListVal []*TestNestedType
ArrayVal [1]*TestNestedType
MapVal map[string]TestAllTypes
PbVal *proto3pb.TestAllTypes

Expand Down

0 comments on commit a5be570

Please sign in to comment.