diff --git a/pkg/codec/property_extractor.go b/pkg/codec/property_extractor.go index 39945201a..b7ab8771a 100644 --- a/pkg/codec/property_extractor.go +++ b/pkg/codec/property_extractor.go @@ -200,7 +200,7 @@ func (e *propertyExtractor) getPropTypeFromStruct(onChainType reflect.Type) (ref // if value for extraction is nested under a slice return [][] with type of the value to be extracted var prevIsSlice bool - if prevLocations != nil && len(parts) > 1 { + if prevLocations != nil && len(parts) > 0 { prevLocation, ok := prevLocations.fieldByName(parts[len(parts)-1]) if !ok { return nil, fmt.Errorf("%w: field not found in on-chain type %s", types.ErrInvalidType, e.fieldName) diff --git a/pkg/codec/wrapper.go b/pkg/codec/wrapper.go index 0ce7d3fd4..4cc948a05 100644 --- a/pkg/codec/wrapper.go +++ b/pkg/codec/wrapper.go @@ -55,6 +55,7 @@ func (m *wrapperModifier) RetypeToOffChain(onChainType reflect.Type, _ string) ( Type: onChainType, }}) + offChainTyp = reflect.PointerTo(offChainTyp) m.onToOffChainType[onChainType] = offChainTyp m.offToOnChainType[offChainTyp] = onChainType return offChainTyp, nil diff --git a/pkg/codec/wrapper_test.go b/pkg/codec/wrapper_test.go index f4dc414af..8263cc4c5 100644 --- a/pkg/codec/wrapper_test.go +++ b/pkg/codec/wrapper_test.go @@ -70,11 +70,11 @@ func TestWrapper(t *testing.T) { t.Run("RetypeToOffChain works for whole values", func(t *testing.T) { offChainType, err := wholeValueWrapper.RetypeToOffChain(reflect.TypeOf(primitiveToWrap), "") require.NoError(t, err) - assert.Equal(t, reflect.StructOf([]reflect.StructField{{Name: "X", Type: reflect.TypeOf("")}}).Kind(), offChainType.Kind()) + assert.Equal(t, reflect.StructOf([]reflect.StructField{{Name: "X", Type: reflect.TypeOf("")}}).Kind(), offChainType.Elem().Kind()) offChainType, err = wholeValueWrapper.RetypeToOffChain(reflect.TypeOf(testStruct{}), "") require.NoError(t, err) - assert.Equal(t, reflect.StructOf([]reflect.StructField{{Name: "X", Type: reflect.TypeOf(testStruct{})}}).Kind(), offChainType.Kind()) + assert.Equal(t, reflect.StructOf([]reflect.StructField{{Name: "X", Type: reflect.TypeOf(testStruct{})}}).Kind(), offChainType.Elem().Kind()) }) @@ -154,7 +154,7 @@ func TestWrapper(t *testing.T) { offChainType, err := wholeValueWrapper.RetypeToOffChain(reflect.TypeOf(primitiveToWrap), "") require.NoError(t, err) - offChain := reflect.New(offChainType).Elem() + offChain := reflect.New(offChainType.Elem()).Elem() type wrappedPrimitive struct { X string } @@ -176,7 +176,7 @@ func TestWrapper(t *testing.T) { offChainType, err = wholeValueWrapper.RetypeToOffChain(reflect.TypeOf(testStruct{}), "") require.NoError(t, err) - iOffchain := reflect.New(offChainType).Elem() + iOffchain := reflect.New(offChainType.Elem()).Elem() iOffchain.FieldByName("X").FieldByName("A").SetString("foo") iOffchain.FieldByName("X").FieldByName("B").SetInt(10) iOffchain.FieldByName("X").FieldByName("C").SetInt(20)