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

Unmarshal() is case insensitive to object attribute names #52

Open
osdrv opened this issue Jun 5, 2019 · 0 comments
Open

Unmarshal() is case insensitive to object attribute names #52

osdrv opened this issue Jun 5, 2019 · 0 comments

Comments

@osdrv
Copy link

osdrv commented Jun 5, 2019

Hey folks,

I happened to bump upon a case where an object source yaml payload contains 2 distinct keys which are being parsed as the same key. Effectively, the only difference in the key names is the case spelling: attrFoo Vs attrfoo (see example).

My struct definition:

type Foo struct {
        Attr int32 `json:"attrFoo"`
}

The yaml payload is defined as:

const data = `
---
bar:
  attrFoo: 42

baz:
  attrfoo: 84

barbaz:
  attrFoo: 122
  attrfoo: 456
`

And the test program:

func main() {
        var foomap map[string]Foo
        err := yaml.Unmarshal([]byte(jsonData), &foomap)
        if err != nil {
                panic(err.Error())
        }
        fmt.Printf("foo map: %+v\n", foomap)
}

The output seems a bit odd:

foo map: map[bar:{Attr:42} barbaz:{Attr:456} baz:{Attr:84}]

Here is a full gist.

On the flip side yaml.v2 behaviour is exactly what I expect: it strips unknown attrfoo attribute:

foo map: map[bar:{Attr:42} barbaz:{Attr:122} baz:{Attr:0}]

Here is a yaml.v2 gist.

As yaml specification implies on key case sensitivity, from the user perspective all 3 objects in my example document look valid, but the parsing result is a bit surprising. Could you please give me an idea if this behaviour is expected? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant