From 3640aadeab5a77f8a7b8a8e442bf12a135c9d2ea Mon Sep 17 00:00:00 2001 From: "Alvaro J. Genial" Date: Tue, 18 Jul 2017 00:26:15 -0400 Subject: [PATCH 1/4] Add defaultKeepZeros (false) constant --- encode.go | 4 ++-- form.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/encode.go b/encode.go index 57a0d0a..603964b 100644 --- a/encode.go +++ b/encode.go @@ -68,7 +68,7 @@ func (e Encoder) Encode(dst interface{}) error { // EncodeToString encodes dst as a form and returns it as a string. func EncodeToString(dst interface{}) (string, error) { v := reflect.ValueOf(dst) - n, err := encodeToNode(v, false) + n, err := encodeToNode(v, defaultKeepZeros) if err != nil { return "", err } @@ -79,7 +79,7 @@ func EncodeToString(dst interface{}) (string, error) { // EncodeToValues encodes dst as a form and returns it as Values. func EncodeToValues(dst interface{}) (url.Values, error) { v := reflect.ValueOf(dst) - n, err := encodeToNode(v, false) + n, err := encodeToNode(v, defaultKeepZeros) if err != nil { return nil, err } diff --git a/form.go b/form.go index 4052369..0e9bdb8 100644 --- a/form.go +++ b/form.go @@ -11,4 +11,5 @@ const ( defaultDelimiter = '.' defaultEscape = '\\' + defaultKeepZeros = false ) From c01f843076e882864c30bcea64566041739f2e88 Mon Sep 17 00:00:00 2001 From: "Alvaro J. Genial" Date: Tue, 18 Jul 2017 00:34:39 -0400 Subject: [PATCH 2/4] Add EncodeToStringWith --- encode.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/encode.go b/encode.go index 603964b..a95e794 100644 --- a/encode.go +++ b/encode.go @@ -67,12 +67,17 @@ func (e Encoder) Encode(dst interface{}) error { // EncodeToString encodes dst as a form and returns it as a string. func EncodeToString(dst interface{}) (string, error) { + return EncodeToStringWith(dst, defaultDelimiter, defaultEscape, defaultKeepZeros) +} + +// EncodeToStringWith encodes dst as a form with delimiter d, escape e, keeping zero values if z, and returns it as a string. +func EncodeToStringWith(dst interface{}, d rune, e rune, z bool) (string, error) { v := reflect.ValueOf(dst) - n, err := encodeToNode(v, defaultKeepZeros) + n, err := encodeToNode(v, z) if err != nil { return "", err } - vs := n.values(defaultDelimiter, defaultEscape) + vs := n.values(d, e) return vs.Encode(), nil } From 3d8ee21ad60649f3696daa85cca549be4f593ee1 Mon Sep 17 00:00:00 2001 From: "Alvaro J. Genial" Date: Tue, 18 Jul 2017 00:34:57 -0400 Subject: [PATCH 3/4] Add EncodeToValuesWith --- encode.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/encode.go b/encode.go index a95e794..b310f9b 100644 --- a/encode.go +++ b/encode.go @@ -83,12 +83,17 @@ func EncodeToStringWith(dst interface{}, d rune, e rune, z bool) (string, error) // EncodeToValues encodes dst as a form and returns it as Values. func EncodeToValues(dst interface{}) (url.Values, error) { + return EncodeToValuesWith(dst, defaultDelimiter, defaultEscape, defaultKeepZeros) +} + +// EncodeToValuesWith encodes dst as a form with delimiter d, escape e, keeping zero values if z, and returns it as Values. +func EncodeToValuesWith(dst interface{}, d rune, e rune, z bool) (url.Values, error) { v := reflect.ValueOf(dst) - n, err := encodeToNode(v, defaultKeepZeros) + n, err := encodeToNode(v, z) if err != nil { return nil, err } - vs := n.values(defaultDelimiter, defaultEscape) + vs := n.values(d, e) return vs, nil } From 5f6a2d96b23dcb0d06b06400a7f8ed2c3474f97c Mon Sep 17 00:00:00 2001 From: "Alvaro J. Genial" Date: Tue, 18 Jul 2017 00:40:25 -0400 Subject: [PATCH 4/4] Update zero values section in README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index ad99be4..2b2e92d 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,29 @@ While encouraged, it is not necessary to define a type (e.g. a `struct`) in orde By default, and without custom marshaling, zero values (also known as empty/default values) are encoded as the empty string. To disable this behavior, meaning to keep zero values in their literal form (e.g. `0` for integral types), `Encoder` offers a `KeepZeros` setter method, which will do just that when set to `true`. +For instance, given... + +```go +foo := map[string]interface{}{"b": false, "i": 0} +``` + +...the following... + +```go +form.EncodeToString(foo) // i.e. keepZeros == false +``` + +...will result in `"b=&i="`, whereas... + +```go +keepZeros := true +delimiter := '.' +escape := '\\' +form.EncodeToStringWith(foo, delimiter, escape, keepZeros) +``` + +...will result in `"b=false&i=0"`. + ### Unsupported Values Values of the following kinds aren't supported and, if present, must be ignored.