From 6d713824cb57c7642bfd27a6b13c6d188ab4e64e Mon Sep 17 00:00:00 2001 From: gKits <56302678+gKits@users.noreply.github.com> Date: Fri, 29 Nov 2024 07:16:58 +0100 Subject: [PATCH] style(encoder): rename option to KeyOriginalPath --- encoder.go | 18 +++++++++--------- encoder_test.go | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/encoder.go b/encoder.go index 9bdf9d0..20fa9f3 100644 --- a/encoder.go +++ b/encoder.go @@ -11,9 +11,9 @@ type encoderFunc func(reflect.Value) string // Encoder encodes values from a struct into url.Values. type Encoder struct { - cache *cache - regenc map[reflect.Type]encoderFunc - keySeparation bool + cache *cache + regenc map[reflect.Type]encoderFunc + keyOriginalPath bool } // NewEncoder returns a new Encoder with defaults. @@ -41,12 +41,12 @@ func (e *Encoder) SetAliasTag(tag string) { e.cache.tag = tag } -// ActivateKeySeparation causes the keys of nested struct fields to be separated -// with a period when encoding. +// KeyOriginalPath causes the keys of nested struct fields to keep their full original +// name consisting of each field name in its path separated with a period. // -// Allows encoded values to be decoded again using schema.Decode. -func (e *Encoder) ActivateKeySeparation() { - e.keySeparation = true +// Allows from nested struct encoded values to be decoded again using schema.Decode. +func (e *Encoder) KeyOriginalPath(originalKeyPath bool) { + e.keyOriginalPath = originalKeyPath } // isValidStructPointer test if input value is a valid struct pointer. @@ -100,7 +100,7 @@ func (e *Encoder) encode(v reflect.Value, dst map[string][]string, prefix string if name == "-" { continue } - if prefix != "" && e.keySeparation { + if prefix != "" && e.keyOriginalPath { name = prefix + "." + name } diff --git a/encoder_test.go b/encoder_test.go index 3f3b0ae..c5913b8 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -524,7 +524,7 @@ func TestRegisterEncoderWithPtrType(t *testing.T) { valExists(t, "DateEnd", "", vals) } -func TestSeparation(t *testing.T) { +func TestEncoder_WithOriginalKeyPath(t *testing.T) { type outter struct { Inner inner } @@ -541,7 +541,7 @@ func TestSeparation(t *testing.T) { } encoder := NewEncoder() - encoder.ActivateKeySeparation() + encoder.OriginalKeyPath(true) vals := map[string][]string{} err := encoder.Encode(nest, vals)