Skip to content

Commit

Permalink
Add codec wrapper modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Oct 31, 2024
1 parent f2d327f commit 48e0c53
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/codec/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// - extract element -> [ElementExtractorModifierConfig]
// - epoch to time -> [EpochToTimeModifierConfig]
// - address to string -> [AddressBytesToStringModifierConfig]
// - field wrapper -> [WrapperModifierConfig]
type ModifiersConfig []ModifierConfig

func (m *ModifiersConfig) UnmarshalJSON(data []byte) error {
Expand Down Expand Up @@ -55,6 +56,8 @@ func (m *ModifiersConfig) UnmarshalJSON(data []byte) error {
(*m)[i] = &PropertyExtractorConfig{}
case ModifierAddressToString:
(*m)[i] = &AddressBytesToStringModifierConfig{}
case ModifierWrapper:
(*m)[i] = &ModifiersConfig{}
default:
return fmt.Errorf("%w: unknown modifier type: %s", types.ErrInvalidConfig, mType)
}
Expand Down Expand Up @@ -88,6 +91,7 @@ const (
ModifierEpochToTime ModifierType = "epoch to time"
ModifierExtractProperty ModifierType = "extract property"
ModifierAddressToString ModifierType = "address to string"
ModifierWrapper ModifierType = "wrapper"
)

type ModifierConfig interface {
Expand Down Expand Up @@ -248,6 +252,29 @@ func (c *AddressBytesToStringModifierConfig) MarshalJSON() ([]byte, error) {
})
}

// WrapperModifierConfig wraps fields into subfields of a new struct.
type WrapperModifierConfig struct {
// Fields key defines the fields to be wrapped and the name of the wrapper struct.
// The field becomes a subfield of the wrapper struct where the name of the subfield is map value.
Fields map[string]string
}

func (r *WrapperModifierConfig) ToModifier(_ ...mapstructure.DecodeHookFunc) (Modifier, error) {
fields := map[string]string{}
for i, f := range r.Fields {
// using a private variable will make the field not serialize, essentially dropping the field
fields[upperFirstCharacter(f)] = fmt.Sprintf("dropFieldPrivateName%d", i)

Check failure on line 266 in pkg/codec/config.go

View workflow job for this annotation

GitHub Actions / build-test

fmt.Sprintf format %d has arg i of wrong type string

Check failure on line 266 in pkg/codec/config.go

View workflow job for this annotation

GitHub Actions / benchmark

fmt.Sprintf format %d has arg i of wrong type string
}
return NewWrapperModifier(r.Fields), nil
}

func (r *WrapperModifierConfig) MarshalJSON() ([]byte, error) {
return json.Marshal(&modifierMarshaller[WrapperModifierConfig]{
Type: ModifierWrapper,
T: r,
})
}

type typer struct {
Type string
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/codec/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package codec

import (
"fmt"
"reflect"
)

// NewWrapperModifier wraps fields into subfields of a new struct where the name of the struct is field name and the subfield name is map value.
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, wrapFieldMapAction)
}

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
}

0 comments on commit 48e0c53

Please sign in to comment.