From d23f587261ba8cf7ba80e1fe77c4b10b3dceda29 Mon Sep 17 00:00:00 2001 From: HughFace Date: Fri, 14 Jul 2017 12:18:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Different=20situation:=20{=E2=80=9Ctest1?= =?UTF-8?q?=E2=80=9D:=200,=20=E2=80=9Ctest2=E2=80=9D:=20false}=20EncodeToS?= =?UTF-8?q?tring=20need=20to=20test1=3D0&test2=3Dfalse=20not=20test1=3D&te?= =?UTF-8?q?st2=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encode.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/encode.go b/encode.go index 57a0d0a..dd7813c 100644 --- a/encode.go +++ b/encode.go @@ -66,9 +66,13 @@ 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) { +func EncodeToString(dst interface{}, needEmptyValue ...bool) (string, error) { v := reflect.ValueOf(dst) - n, err := encodeToNode(v, false) + z := false + if len(needEmptyValue) != 0 { + z = needEmptyValue[0] + } + n, err := encodeToNode(v, z) if err != nil { return "", err } @@ -77,9 +81,13 @@ func EncodeToString(dst interface{}) (string, error) { } // EncodeToValues encodes dst as a form and returns it as Values. -func EncodeToValues(dst interface{}) (url.Values, error) { +func EncodeToValues(dst interface{}, needEmptyValue ...bool) (url.Values, error) { v := reflect.ValueOf(dst) - n, err := encodeToNode(v, false) + z := false + if len(needEmptyValue) != 0 { + z = needEmptyValue[0] + } + n, err := encodeToNode(v, z) if err != nil { return nil, err } From 923b1d584937d6809612944a5e97f744ac24918b Mon Sep 17 00:00:00 2001 From: HughFace Date: Mon, 17 Jul 2017 18:38:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=94=A8=20form=20?= =?UTF-8?q?=E5=81=9A=E6=A0=87=E7=AD=BE=EF=BC=8C=E6=B2=A1=E6=9C=89=20form?= =?UTF-8?q?=20=E7=9A=84=E6=97=B6=E5=80=99=E7=94=A8=20json=20=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E5=81=9A=E7=94=B3=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encode.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/encode.go b/encode.go index dd7813c..afdcb63 100644 --- a/encode.go +++ b/encode.go @@ -268,15 +268,23 @@ func canIndexOrdinally(v reflect.Value) bool { return false } -func fieldInfo(f reflect.StructField) (k string, oe bool) { +func fieldInfo(f reflect.StructField, tagName ...string) (k string, oe bool) { + _tagName := "form" + if len(tagName) > 0 { + _tagName = tagName[0] + } if f.PkgPath != "" { // Skip private fields. return omittedKey, oe } k = f.Name - tag := f.Tag.Get("form") + tag := f.Tag.Get(_tagName) if tag == "" { - return k, oe + if len(tagName) == 0 { + return fieldInfo(f, "json") // using json as secondary + } else { + return k, oe + } } ps := strings.SplitN(tag, ",", 2) From 0dad4d537c38cfb9e19799a2c7cb84c8f2f93f2d Mon Sep 17 00:00:00 2001 From: HughFace Date: Tue, 18 Jul 2017 12:25:58 +0800 Subject: [PATCH 3/3] using form as default tag, json will be used when there's no form tag. bugfix: avoid endless loop --- encode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encode.go b/encode.go index afdcb63..2eb38fc 100644 --- a/encode.go +++ b/encode.go @@ -280,7 +280,7 @@ func fieldInfo(f reflect.StructField, tagName ...string) (k string, oe bool) { k = f.Name tag := f.Tag.Get(_tagName) if tag == "" { - if len(tagName) == 0 { + if len(tagName) == 0 && _tagName != "json" { return fieldInfo(f, "json") // using json as secondary } else { return k, oe