mitchellh / mapstructure

Go library for decoding generic map values into native Go structures and vice versa.
https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
MIT License
7.93k stars 677 forks source link

slice mapping bug fix #266

Closed ozgursoy closed 2 years ago

ozgursoy commented 2 years ago
type Test struct {
    Attributes []byte `json:"attributes"`
}

type TestReq struct {
    Attributes []byte `json:"attributes"`
}

func TestMapping(t *testing.T) {
    req := TestReq{Attributes: []byte(`[{"data": "some data"}]`)}

    var test Test
    test.Attributes = []byte(`[{"a" : "1"}]`)

    err := mapstructure.Decode(req, &test)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(test.Attributes))
    // result will be [{"data": "some data"}]
}

func TestMappingFail(t *testing.T) {
    req := TestReq{Attributes: []byte(`[{"data":"some data"}]`)}

    var test Test
    test.Attributes = []byte(`[{"a":"1111111111111111111111111111111111111111111111"}]`)

    err := mapstructure.Decode(req, &test)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(test.Attributes))
    // result will be [{"data": "some data"}]11111111111111111111111111111111"}]
}
mitchellh commented 2 years ago

Hey this looks really good but I need a failing test case to properly merge this. You almost have that in your PR body but its not quite a test case. Thank you.

ozgursoy commented 2 years ago

Hello @mitchellh,

You are right, I am little lazy person :)

I added a test for that case, I hope this is appropriate. Tested with lastest version of mapstructure that couldn't pass.

Thanks

mitchellh commented 2 years ago

Awesome thank you!