Open tombuente opened 6 months ago
@tombuente By passing true to the decoder's ZeroEmpty method, zero values for the field's type will be set.
https://github.com/gorilla/schema/blob/cd59f2f12cbdfa9c06aa63e425d1fe4a806967ff/decoder.go#L38-L48
r.ParseForm()
fmt.Println(r.PostForm["id"], len(r.PostForm["id"])) // [1 15] 3
var decoder = schema.NewDecoder()
// fix
decoder.ZeroEmpty(true)
var i Document
// r.PostForm is a map of our POST form values
err = decoder.Decode(&i, r.PostForm)
if err != nil {
fmt.Println(err)
}
fmt.Println(i.ID, "Length", len(i.ID)) // ID [1 15] Length 2
@tombuente The specific processing is as follows. https://github.com/gorilla/schema/blob/cd59f2f12cbdfa9c06aa63e425d1fe4a806967ff/decoder.go#L355-L358
Current Behavior
When called with
id=1, id=, id=15
The behavior is rather unfortunate when decoding values that matter when equal to
""
. For example, when two slices are related to each other.Now
RowID[0]
andRowName[0]
build a pair. But since gorilla/schema drops empty strings for some reason rather than settings it to Go's default for that value or using the default value defined by tags, the relationship breaks.Expected Behavior