Skip to content

Commit f221e70

Browse files
authored
Lists: Fix handling of go arrays (#894)
Go arrays currently cause a panic, because `reflect.MakeSlice` is used to construct them which is invalid, this change fixes that.
1 parent 19b2ad1 commit f221e70

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

common/types/list.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,13 @@ func (l *baseList) ConvertToNative(typeDesc reflect.Type) (any, error) {
190190
// Allow the element ConvertToNative() function to determine whether conversion is possible.
191191
otherElemType := typeDesc.Elem()
192192
elemCount := l.size
193-
nativeList := reflect.MakeSlice(typeDesc, elemCount, elemCount)
193+
var nativeList reflect.Value
194+
if typeDesc.Kind() == reflect.Array {
195+
nativeList = reflect.New(reflect.ArrayOf(elemCount, typeDesc)).Elem().Index(0)
196+
} else {
197+
nativeList = reflect.MakeSlice(typeDesc, elemCount, elemCount)
198+
199+
}
194200
for i := 0; i < elemCount; i++ {
195201
elem := l.NativeToValue(l.get(i))
196202
nativeElemVal, err := elem.ConvertToNative(otherElemType)

ext/native_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func TestNativeTypes(t *testing.T) {
6262
NestedMapVal: {42: true},
6363
},
6464
],
65+
ArrayVal: [
66+
ext.TestNestedType{
67+
NestedListVal:['goodbye', 'cruel', 'world'],
68+
NestedMapVal: {42: true},
69+
},
70+
],
6571
MapVal: {'map-key': ext.TestAllTypes{BoolVal: true}},
6672
CustomSliceVal: [ext.TestNestedSliceType{Value: 'none'}],
6773
CustomMapVal: {'even': ext.TestMapVal{Value: 'more'}},
@@ -85,6 +91,10 @@ func TestNativeTypes(t *testing.T) {
8591
NestedMapVal: map[int64]bool{42: true},
8692
},
8793
},
94+
ArrayVal: [1]*TestNestedType{{
95+
NestedListVal: []string{"goodbye", "cruel", "world"},
96+
NestedMapVal: map[int64]bool{42: true},
97+
}},
8898
MapVal: map[string]TestAllTypes{"map-key": {BoolVal: true}},
8999
CustomSliceVal: []TestNestedSliceType{{Value: "none"}},
90100
CustomMapVal: map[string]TestMapVal{"even": {Value: "more"}},
@@ -688,6 +698,7 @@ type TestAllTypes struct {
688698
Uint32Val uint32
689699
Uint64Val uint64
690700
ListVal []*TestNestedType
701+
ArrayVal [1]*TestNestedType
691702
MapVal map[string]TestAllTypes
692703
PbVal *proto3pb.TestAllTypes
693704
CustomSliceVal []TestNestedSliceType

0 commit comments

Comments
 (0)