From a5be570ca311ac5bd78854e1b77d6616f7fcb697 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Wed, 31 Jan 2024 20:41:03 -0500 Subject: [PATCH] Lists: Fix handling of go arrays Go arrays currently cause a panic, because `reflect.MakeSlice` is used to construct them which is invalid, this change fixes that. --- common/types/list.go | 8 +++++++- ext/native_test.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/common/types/list.go b/common/types/list.go index d4932b4a9..06f48dde7 100644 --- a/common/types/list.go +++ b/common/types/list.go @@ -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) diff --git a/ext/native_test.go b/ext/native_test.go index ead7bc1c1..9da6e0bf9 100644 --- a/ext/native_test.go +++ b/ext/native_test.go @@ -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{ @@ -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}}, }, }, @@ -685,6 +695,7 @@ type TestAllTypes struct { Uint32Val uint32 Uint64Val uint64 ListVal []*TestNestedType + ArrayVal [1]*TestNestedType MapVal map[string]TestAllTypes PbVal *proto3pb.TestAllTypes