Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Jan 24, 2025
1 parent b2a29c6 commit 3eb5f3b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
58 changes: 56 additions & 2 deletions pkg/codec/encodings/type_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestCodecFromTypeCodecs(t *testing.T) {
t.Run("CreateType works for nested struct values and modifiers", func(t *testing.T) {
itemType := strings.Join([]string{TestItemWithConfigExtra, "AccountStruct", "Account"}, ".")
ts := CreateTestStruct(0, biit)
c := biit.GetCodec(t)
c := biit.GetNestableCodec(t)

encoded, err := c.Encode(tests.Context(t), ts.AccountStruct.Account, itemType)
require.NoError(t, err)
Expand All @@ -141,7 +141,7 @@ func TestCodecFromTypeCodecs(t *testing.T) {
t.Run("CreateType works for nested struct values", func(t *testing.T) {
itemType := strings.Join([]string{TestItemType, "NestedDynamicStruct", "Inner", "S"}, ".")
ts := CreateTestStruct(0, biit)
c := biit.GetCodec(t)
c := biit.GetNestableCodec(t)

encoded, err := c.Encode(tests.Context(t), ts.NestedDynamicStruct.Inner.S, itemType)
require.NoError(t, err)
Expand Down Expand Up @@ -354,6 +354,60 @@ func (b *bigEndianInterfaceTester) GetCodec(t *testing.T) types.Codec {
return modCodec
}

func (b *bigEndianInterfaceTester) GetNestableCodec(t *testing.T) types.Codec {
testStruct := newTestStructCodec(t, binary.BigEndian())
size, err := binary.BigEndian().Int(1)
require.NoError(t, err)
slice, err := encodings.NewSlice(testStruct, size)
require.NoError(t, err)
arr1, err := encodings.NewArray(1, testStruct)
require.NoError(t, err)
arr2, err := encodings.NewArray(2, testStruct)
require.NoError(t, err)

ts := CreateTestStruct(0, b)

tc := &encodings.CodecFromTypeCodec{
TestItemType: testStruct,
TestItemSliceType: slice,
TestItemArray1Type: arr1,
TestItemArray2Type: arr2,
TestItemWithConfigExtra: testStruct,
NilType: encodings.Empty{},
}

require.NoError(t, err)

var c types.RemoteCodec = tc
if b.lenient {
c = (*encodings.LenientCodecFromTypeCodec)(tc)
}

mod, err := codec.NewPathTraverseHardCoder(map[string]any{
"BigField": ts.BigField.String(),
"AccountStruct.Account": ts.AccountStruct.Account,
}, map[string]any{"ExtraField": AnyExtraValue}, true, codec.BigIntHook)
require.NoError(t, err)

byTypeMod, err := codec.NewNestableByItemTypeModifier(map[string]codec.Modifier{
TestItemType: codec.MultiModifier{},
TestItemSliceType: codec.MultiModifier{},
TestItemArray1Type: codec.MultiModifier{},
TestItemArray2Type: codec.MultiModifier{},
TestItemWithConfigExtra: mod,
NilType: codec.MultiModifier{},
})
require.NoError(t, err)

modCodec, err := codec.NewModifierCodec(c, byTypeMod, codec.BigIntHook)
require.NoError(t, err)

_, err = mod.RetypeToOffChain(reflect.PointerTo(testStruct.GetType()), "")
require.NoError(t, err)

return modCodec
}

func (b *bigEndianInterfaceTester) IncludeArrayEncodingSizeEnforcement() bool {
return true
}
10 changes: 10 additions & 0 deletions pkg/codec/modifier_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (m *modifierBase[T]) RetypeToOffChain(onChainType reflect.Type, itemType st
return nil, err
}
default:
// if the types don't match, it means we are attempting to traverse the main struct
if onChainType != m.onChainStructType {
return onChainType, nil
}

return nil, fmt.Errorf("%w: cannot retype the kind %v", types.ErrInvalidType, onChainStructType.Kind())
}

Expand Down Expand Up @@ -213,6 +218,10 @@ func transformWithMaps[T any](
return reflect.Value{}, err
}

if rItem.Type() == toType {
return rItem.Interface(), nil
}

rOutput, err := transformWithMapsHelper(rItem, toType, itemType, fields, fn, hooks)
if err != nil {
return reflect.Value{}, err
Expand Down Expand Up @@ -260,6 +269,7 @@ func transformWithMapsHelper[T any](

return into, err
default:
panic("error")
return reflect.Value{}, fmt.Errorf("%w: cannot retype %v", types.ErrInvalidType, rItem.Type())
}
}
Expand Down

0 comments on commit 3eb5f3b

Please sign in to comment.