Skip to content

Commit

Permalink
Improve comments for wrapper modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 4, 2024
1 parent 637e736 commit 1ff1a0c
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions pkg/codec/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,34 +252,58 @@ func (c *AddressBytesToStringModifierConfig) MarshalJSON() ([]byte, error) {
})
}

// WrapperModifierConfig wraps each field specified in cfg into a struct. These new structs contain the wrapped field as a subfield with a configurable name.
// WrapperModifierConfig replaces each field based on cfg map keys with a struct containing one field with the value of the original field which has is named based on map values.
// Wrapper modifier does not maintain the original pointers.
// Wrapper modifier config shouldn't edit fields that affect each other since the results are not deterministic.
//
// For e.g.
// Example #1:
//
// Based on this input struct:
// type example struct {
// A string
// }
//
// And the wrapper config defined as:
// {"D": "W"}
//
// Result:
// type example struct {
// A string;
// B struct {A, B, C};
// C array [{A, B, C}, {A, B, C}, {A, B, C}...];
// D string;
// D
// }
//
// With transformations defined as:
// {"B.A": "X", "B.C": "Z", "C.A": "Y", "D": "W"}
// where D is a struct that contains the original value of D under the name W:
// type D struct {
// W string
// }
//
// Steps (in non deterministic order), where letters are field names:
// 1. "B.A": "X" -> B: { A: {X}; B; C; }
// 2. "B.C": "Z" -> B: { A: {X}; B; C: {Z}; }
// 3. "C.A": "Y" -> C: [{ A: {Y}; B; C; }, { A: {Y}; B; C; }, ...]
// 4. "D": "W" -> D: {W}
//
// Result:
// {
// A string;
// B struct { A struct {X}; B; C struct {Z}; };
// C array [{ A struct {Y}; B; C; }, { A struct {Y}; B; C; }, ...];
// D struct {W};
// }
// Example #2:
// Wrapper modifier works on any type of field, including nested fields or nested fields in slices etc.!
//
// Based on this input struct:
// type example struct {
// A []B
// }
//
// type B struct {
// C string
// D string
// }
//
// And the wrapper config defined as:
// {"A.C": "E", "A.D": "F"}
//
// Result:
// type example struct {
// A []B
// }
//
// type B struct {
// C type struct { E string }
// D type struct { F string }
// }
//
// Where each element of slice A under fields C.E and D.F retains the values of their respective input slice elements A.C and A.D .
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.
Expand Down

0 comments on commit 1ff1a0c

Please sign in to comment.