Skip to content

Commit

Permalink
style(encoder): rename option to KeyOriginalPath
Browse files Browse the repository at this point in the history
  • Loading branch information
gKits committed Nov 29, 2024
1 parent 24dd5c2 commit 6d71382
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 6d71382

Please sign in to comment.