Skip to content
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

Add option to encode value keys with '.' separators #229

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +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
cache *cache
regenc map[reflect.Type]encoderFunc
keySeparation bool
}

// NewEncoder returns a new Encoder with defaults.
Expand All @@ -26,7 +27,7 @@ func NewEncoder() *Encoder {
func (e *Encoder) Encode(src interface{}, dst map[string][]string) error {
v := reflect.ValueOf(src)

return e.encode(v, dst)
return e.encode(v, dst, "")
}

// RegisterEncoder registers a converter for encoding a custom type.
Expand All @@ -40,6 +41,14 @@ 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.
//
// Allows encoded values to be decoded again using schema.Decode.
func (e *Encoder) ActivateKeySeparation() {
e.keySeparation = true
}

// isValidStructPointer test if input value is a valid struct pointer.
func isValidStructPointer(v reflect.Value) bool {
return v.Type().Kind() == reflect.Ptr && v.Elem().IsValid() && v.Elem().Type().Kind() == reflect.Struct
Expand Down Expand Up @@ -75,7 +84,7 @@ func isZero(v reflect.Value) bool {
return v.Interface() == z.Interface()
}

func (e *Encoder) encode(v reflect.Value, dst map[string][]string) error {
func (e *Encoder) encode(v reflect.Value, dst map[string][]string, prefix string) error {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
Expand All @@ -91,10 +100,13 @@ func (e *Encoder) encode(v reflect.Value, dst map[string][]string) error {
if name == "-" {
continue
}
if prefix != "" && e.keySeparation {
name = prefix + "." + name
}

// Encode struct pointer types if the field is a valid pointer and a struct.
if isValidStructPointer(v.Field(i)) && !e.hasCustomEncoder(v.Field(i).Type()) {
err := e.encode(v.Field(i).Elem(), dst)
err := e.encode(v.Field(i).Elem(), dst, name)
if err != nil {
errors[v.Field(i).Elem().Type().String()] = err
}
Expand All @@ -115,7 +127,7 @@ func (e *Encoder) encode(v reflect.Value, dst map[string][]string) error {
}

if v.Field(i).Type().Kind() == reflect.Struct {
err := e.encode(v.Field(i), dst)
err := e.encode(v.Field(i), dst, name)
if err != nil {
errors[v.Field(i).Type().String()] = err
}
Expand Down
29 changes: 29 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,32 @@ func TestRegisterEncoderWithPtrType(t *testing.T) {
valExists(t, "DateStart", ss.DateStart.time.String(), vals)
valExists(t, "DateEnd", "", vals)
}

func TestSeparation(t *testing.T) {
type outter struct {
Inner inner
}
type nested struct {
Outter outter
}

nest := nested{
Outter: outter{
Inner: inner{
F12: 12,
},
},
}

encoder := NewEncoder()
encoder.ActivateKeySeparation()

vals := map[string][]string{}
err := encoder.Encode(nest, vals)

t.Log(vals)

noError(t, err)
valsLength(t, 1, vals)
valExists(t, "Outter.Inner.F12", "12", vals)
}
Loading