Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
tombuente opened this issue Apr 13, 2024 · 0 comments
Open
1 task done
Labels

Comments

@tombuente
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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

Steps To Reproduce

No response

Anything else?

No response

@tombuente tombuente added the bug label Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant