From 96c426601c00c73cc7cc87f8cbd7d8fb8777a151 Mon Sep 17 00:00:00 2001 From: wubin48435 Date: Sun, 11 Aug 2024 13:45:19 +0800 Subject: [PATCH] fix form decode bug --- toolkit/form/_examples/decoder/full.go | 33 +++++++++++++++++++------- toolkit/form/form_decoder.go | 1 + 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/toolkit/form/_examples/decoder/full.go b/toolkit/form/_examples/decoder/full.go index 39d2d5fc..78d8e76d 100644 --- a/toolkit/form/_examples/decoder/full.go +++ b/toolkit/form/_examples/decoder/full.go @@ -57,17 +57,19 @@ func main() { decoder.SetNamespaceSuffix("]") // this simulates the results of http.Request's ParseForm() function - values := parseForm2() + values := parseForm3() - var user User + for i := 0; i < 20; i++ { + var user ParameterWrapper - // must pass a pointer - err := decoder.Decode(&user, values) - if err != nil { - log.Panic(err) - } + // must pass a pointer + err := decoder.Decode(&user, values) + if err != nil { + log.Panic(err) + } - fmt.Printf("%#v\n", user) + fmt.Printf("%#v\n", user) + } //userMap := make(map[string]interface{}) //// must pass a pointer @@ -153,3 +155,18 @@ func parseForm2() url.Values { "f[1][0]": []string{"value10"}, } } + +type A struct { + B string `json:"b" form:"b"` +} + +type ParameterWrapper struct { + Parameter A `json:"parameter" form:"parameter"` +} + +func parseForm3() url.Values { + // parameter[product_code] + return url.Values{ + "parameter[b]": []string{"b"}, + } +} diff --git a/toolkit/form/form_decoder.go b/toolkit/form/form_decoder.go index 14e6ae8b..1273db4e 100644 --- a/toolkit/form/form_decoder.go +++ b/toolkit/form/form_decoder.go @@ -185,6 +185,7 @@ func (d *Decoder) Decode(v interface{}, values url.Values) (err error) { } dec.values = valuesCopy dec.dm = dec.dm[0:0] + dec.decoded = dec.decoded[0:0] val = val.Elem() typ := val.Type()