gorilla / schema

Package gorilla/schema fills a struct with form values.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
1.38k stars 229 forks source link

[BUG] Slice encoding drops empty values rather than encoding them as `nil` or default #215

Open tombuente opened 6 months ago

tombuente commented 6 months ago

Current Behavior

r.ParseForm()
fmt.Println(r.PostForm["id"], len(r.PostForm["id"])) // [1  15] 3

var decoder = schema.NewDecoder()
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

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.

type Document struct {
    RowID   []int64  `schema:"row_id"`
    RowName []string `schema:"row_name"`
}

Now RowID[0]and RowName[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

fmt.Println(i.ID, "Length", len(i.ID)) // ID [1 15] Length 3, add "" to struct slice
fmt.Println(i.ID, "Length", len(i.ID)) // ID [&addr nil &addr] Length 3, add nil to struct slice when field is pointer
lll-lll-lll-lll commented 2 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

lll-lll-lll-lll commented 2 months ago
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
lll-lll-lll-lll commented 2 months ago

@tombuente The specific processing is as follows. https://github.com/gorilla/schema/blob/cd59f2f12cbdfa9c06aa63e425d1fe4a806967ff/decoder.go#L355-L358