-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add codec wrapper modifier * Fix WrapperModifierConfig description comment * Improve comments for wrapper modifier
- Loading branch information
Showing
3 changed files
with
530 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package codec | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func NewWrapperModifier(fields map[string]string) Modifier { | ||
m := &wrapperModifier{ | ||
modifierBase: modifierBase[string]{ | ||
fields: fields, | ||
onToOffChainType: map[reflect.Type]reflect.Type{}, | ||
offToOnChainType: map[reflect.Type]reflect.Type{}, | ||
}, | ||
} | ||
|
||
m.modifyFieldForInput = func(_ string, field *reflect.StructField, _ string, fieldName string) error { | ||
field.Type = reflect.StructOf([]reflect.StructField{{ | ||
Name: fieldName, | ||
Type: field.Type, | ||
}}) | ||
return nil | ||
} | ||
|
||
return m | ||
} | ||
|
||
type wrapperModifier struct { | ||
modifierBase[string] | ||
} | ||
|
||
func (t *wrapperModifier) TransformToOnChain(offChainValue any, _ string) (any, error) { | ||
return transformWithMaps(offChainValue, t.offToOnChainType, t.fields, unwrapFieldMapAction) | ||
} | ||
|
||
func (t *wrapperModifier) TransformToOffChain(onChainValue any, _ string) (any, error) { | ||
return transformWithMaps(onChainValue, t.onToOffChainType, t.fields, wrapFieldMapAction) | ||
} | ||
|
||
func wrapFieldMapAction(typesMap map[string]any, fieldName string, wrappedFieldName string) error { | ||
field, exists := typesMap[fieldName] | ||
if !exists { | ||
return fmt.Errorf("field %s does not exist", fieldName) | ||
} | ||
|
||
typesMap[fieldName] = map[string]any{wrappedFieldName: field} | ||
return nil | ||
} | ||
|
||
func unwrapFieldMapAction(typesMap map[string]any, fieldName string, wrappedFieldName string) error { | ||
_, exists := typesMap[fieldName] | ||
if !exists { | ||
return fmt.Errorf("field %s does not exist", fieldName) | ||
} | ||
val, isOk := typesMap[fieldName].(map[string]any)[wrappedFieldName] | ||
if !isOk { | ||
return fmt.Errorf("field %s.%s does not exist", fieldName, wrappedFieldName) | ||
} | ||
|
||
typesMap[fieldName] = val | ||
return nil | ||
} |
Oops, something went wrong.