Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrapper mod for primitves wrapping and extractor modifiers for nested slice of slices #1090

Merged
merged 5 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/codec/property_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/codec/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/codec/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

})

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
Loading