Skip to content

Commit

Permalink
maintain backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Jan 23, 2025
1 parent 670a4b3 commit b368873
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/codec/by_item_type_modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions pkg/codec/hard_coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/codec/modifier_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/codec/renamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit b368873

Please sign in to comment.