Skip to content

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

Open
@tombuente

Description

@tombuente

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions