monoculum / formam

a package for decode form's values into struct in Go
Apache License 2.0
190 stars 19 forks source link

Ignore unknown keys in brackets #16

Closed arp242 closed 7 years ago

arp242 commented 7 years ago

For example:

[Foo]: bar
Foo[bar]: foobar

Previously this would error out with:

--- FAIL: TestIgnoreBracketedKeys (0.00s)
    --- FAIL: TestIgnoreBracketedKeys/struct (0.00s)
        formam_test.go:753: formam: the field "" in path "His[Wife]" has a index for array but it is a struct
    --- FAIL: TestIgnoreBracketedKeys/slice (0.00s)
        formam_test.go:772: formam: the index of slice is not a number in the field "" of path "His[Wife]"

I don't think this is the correct behaviour if IgnoreUnknownKeys is given.

arp242 commented 7 years ago

Looks like the intermittent test failures are due to another bug that my test just happened to stumble upon?

formam_test.go:790: Expected len() 1 got 0

Don't think that should ever happen.

emilgpa commented 7 years ago

A slice requires of a number as index for to set a value, like: s := []string{} vals := url.Values{ "[0]": []string{"Homer"}, "[1]": []string{"Marge"}, "[2]": []string{"Marge"}, }

IgnoreUknownKeys is only for structs. If IgnoreUknownKeys is true, it not will affects in slice, only structs. In a struct, a key like "Boo" is correct and if any field of the struct not matches with "Boo" and IgnoreUknownKeys is true, it will be ignored. But in a slice, a key like "Foo" is not correct, it should be a number, therefore formam will raises an error of bad syntax.

arp242 commented 7 years ago

Ah, that makes sense 👍 Fixed it.

arp242 commented 7 years ago

Sorry to bother you @dashaus, but I think this should be ready to merge now? Or is there anything else you would like to see changed?

Thank you! :-)

emilgpa commented 7 years ago

I'm really sorry @Carpetsmoker. I forgot completely to do the merge.

arp242 commented 7 years ago

Not a problem. Thank you :+1: