From 1ff1a0c294910ffa0c7629c08fe9740101e67223 Mon Sep 17 00:00:00 2001 From: ilija Date: Mon, 4 Nov 2024 15:50:53 +0100 Subject: [PATCH] Improve comments for wrapper modifier --- pkg/codec/config.go | 64 +++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/pkg/codec/config.go b/pkg/codec/config.go index f1bfa657b..0921d7790 100644 --- a/pkg/codec/config.go +++ b/pkg/codec/config.go @@ -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.