Skip to content

Commit

Permalink
fix form decode bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin48435 committed Aug 11, 2024
1 parent 1bcbe7b commit 96c4266
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
33 changes: 25 additions & 8 deletions toolkit/form/_examples/decoder/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
}
}
1 change: 1 addition & 0 deletions toolkit/form/form_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 96c4266

Please sign in to comment.