diff --git a/pkg/codec/by_item_type_modifier.go b/pkg/codec/by_item_type_modifier.go index 5c10df51e..e2c2522f3 100644 --- a/pkg/codec/by_item_type_modifier.go +++ b/pkg/codec/by_item_type_modifier.go @@ -48,7 +48,6 @@ func (b *byItemTypeModifier) RetypeToOffChain(onChainType reflect.Type, itemType if b.enableNesting { head, tail = ItemTyper(itemType).Next() } - log.Println("byItemTypeModifier", "RetypeToOffChain", onChainType, head, ":", tail) mod, ok := b.modByitemType[head] if !ok { diff --git a/pkg/codec/hard_coder.go b/pkg/codec/hard_coder.go index 291f110fb..f59096961 100644 --- a/pkg/codec/hard_coder.go +++ b/pkg/codec/hard_coder.go @@ -82,10 +82,20 @@ func verifyHardCodeKeys(values map[string]any) error { } func (o *onChainHardCoder) TransformToOnChain(offChainValue any, itemType string) (any, error) { + // set itemType to an ignore value if path traversal is not enabled + if !o.modifierBase.enablePathTraverse { + itemType = "" + } + return transformWithMaps(offChainValue, o.offToOnChainTyper, itemType, o.onChain, hardCode, o.hooks...) } func (o *onChainHardCoder) TransformToOffChain(onChainValue any, itemType string) (any, error) { + // set itemType to an ignore value if path traversal is not enabled + if !o.modifierBase.enablePathTraverse { + itemType = "" + } + allHooks := make([]mapstructure.DecodeHookFunc, len(o.hooks)+1) copy(allHooks, o.hooks) allHooks[len(o.hooks)] = hardCodeManyHook diff --git a/pkg/codec/modifier_base.go b/pkg/codec/modifier_base.go index a3c48a8c3..a269a6e00 100644 --- a/pkg/codec/modifier_base.go +++ b/pkg/codec/modifier_base.go @@ -12,6 +12,7 @@ import ( ) type modifierBase[T any] struct { + enablePathTraverse bool fields map[string]T onToOffChainType map[reflect.Type]reflect.Type offToOnChainType map[reflect.Type]reflect.Type @@ -35,6 +36,12 @@ func (m *modifierBase[T]) RetypeToOffChain(onChainType reflect.Type, itemType st } }() + // path traverse allows an item type of Struct.FieldA.NestedField to isolate modifiers + // associated with the nested field `NestedField`. + if !m.enablePathTraverse { + itemType = "" + } + // if itemType is empty, store the type mappings // if itemType is not empty, assume a sub-field property is expected to be extracted onChainStructType := onChainType diff --git a/pkg/codec/renamer.go b/pkg/codec/renamer.go index 51b4e8f63..0a47d9c81 100644 --- a/pkg/codec/renamer.go +++ b/pkg/codec/renamer.go @@ -32,6 +32,11 @@ type renamer struct { } func (r *renamer) TransformToOffChain(onChainValue any, itemType string) (any, error) { + // set itemType to an ignore value if path traversal is not enabled + if !r.modifierBase.enablePathTraverse { + itemType = "" + } + // itemType references the on-chain type // remap to the off-chain field name if itemType != "" { @@ -62,6 +67,11 @@ func (r *renamer) TransformToOffChain(onChainValue any, itemType string) (any, e } func (r *renamer) TransformToOnChain(offChainValue any, itemType string) (any, error) { + // set itemType to an ignore value if path traversal is not enabled + if !r.modifierBase.enablePathTraverse { + itemType = "" + } + if itemType != "" { var ref string