-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat) Add PreCodec modifier #961
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,113 @@ | ||
package codec | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/go-viper/mapstructure/v2" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
// PreCodec creates a modifier that will run a preliminary encoding/decoding step. | ||
// This is useful when wanting to move nested data as generic bytes. | ||
func NewPreCodec(fields map[string]string, codecs map[string]types.RemoteCodec) (Modifier, error) { | ||
m := &preCodec{ | ||
modifierBase: modifierBase[string]{ | ||
fields: fields, | ||
onToOffChainType: map[reflect.Type]reflect.Type{}, | ||
offToOnChainType: map[reflect.Type]reflect.Type{}, | ||
}, | ||
codecs: codecs, | ||
} | ||
|
||
// validate that there is a codec for each unique type definition | ||
for _, typeDef := range fields { | ||
if _, ok := m.codecs[typeDef]; ok { | ||
continue | ||
} | ||
return nil, fmt.Errorf("codec not supplied for: %s", typeDef) | ||
} | ||
|
||
m.modifyFieldForInput = func(_ string, field *reflect.StructField, _ string, typeDef string) error { | ||
if field.Type != reflect.SliceOf(reflect.TypeFor[uint8]()) { | ||
return fmt.Errorf("can only decode []byte from on-chain: %s", field.Type) | ||
} | ||
|
||
codec, ok := m.codecs[typeDef] | ||
if !ok || codec == nil { | ||
return fmt.Errorf("codec not found for type definition: '%s'", typeDef) | ||
} | ||
|
||
newType, err := codec.CreateType("", false) | ||
if err != nil { | ||
return err | ||
} | ||
field.Type = reflect.TypeOf(newType) | ||
|
||
return nil | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
type preCodec struct { | ||
modifierBase[string] | ||
codecs map[string]types.RemoteCodec | ||
} | ||
|
||
func (pc *preCodec) TransformToOffChain(onChainValue any, _ string) (any, error) { | ||
allHooks := make([]mapstructure.DecodeHookFunc, 1) | ||
allHooks[0] = hardCodeManyHook | ||
|
||
return transformWithMaps(onChainValue, pc.onToOffChainType, pc.fields, pc.decodeFieldMapAction, allHooks...) | ||
} | ||
|
||
func (pc *preCodec) decodeFieldMapAction(extractMap map[string]any, key string, typeDef string) error { | ||
_, exists := extractMap[key] | ||
if !exists { | ||
return fmt.Errorf("field %s does not exist", key) | ||
} | ||
|
||
codec, ok := pc.codecs[typeDef] | ||
if !ok || codec == nil { | ||
return fmt.Errorf("codec not found for type definition: '%s'", typeDef) | ||
} | ||
|
||
to, err := codec.CreateType("", false) | ||
if err != nil { | ||
return err | ||
} | ||
err = codec.Decode(context.Background(), extractMap[key].([]byte), &to, "") | ||
if err != nil { | ||
return err | ||
} | ||
extractMap[key] = to | ||
return nil | ||
} | ||
|
||
func (pc *preCodec) TransformToOnChain(offChainValue any, _ string) (any, error) { | ||
allHooks := make([]mapstructure.DecodeHookFunc, 1) | ||
allHooks[0] = hardCodeManyHook | ||
|
||
return transformWithMaps(offChainValue, pc.offToOnChainType, pc.fields, pc.encodeFieldMapAction, allHooks...) | ||
} | ||
|
||
func (pc *preCodec) encodeFieldMapAction(extractMap map[string]any, key string, typeDef string) error { | ||
_, exists := extractMap[key] | ||
if !exists { | ||
return fmt.Errorf("field %s does not exist", key) | ||
} | ||
|
||
codec, ok := pc.codecs[typeDef] | ||
if !ok || codec == nil { | ||
return fmt.Errorf("codec not found for type definition: '%s'", typeDef) | ||
} | ||
|
||
encoded, err := codec.Encode(context.Background(), extractMap[key], "") | ||
if err != nil { | ||
return err | ||
} | ||
extractMap[key] = encoded | ||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add more detailed documentation and probably examples in the doc?. Currently codec and codec modifiers are complex from an usability standpoint and it would be great to have more docs/examples for this new codec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an example! Let me know what else you think is missing and I can add more words