stretchr / objx

Go package for dealing with maps, slices, JSON and other data.
MIT License
695 stars 75 forks source link

IsMSISlice() doesn't work properly #7

Closed an2deg closed 5 years ago

an2deg commented 10 years ago

Hi!

I have the following JSON structure:

{"d":[{"author":{"displayName":"DemoUser3","id":2},"classes":null,"id":9879,"v":{"code":"","created":"2013-09-19T09:38:50+02:00","published":"0001-01-01T00:00:00Z","updated":"2013-09-19T09:38:50+02:00"}}],"s":200}

And method IsMSISlice() returns false. Test:

    json, err := objx.FromJSON(`{"d":[{"author":{"displayName":"DemoUser3","id":2},"classes":null,"id":9879,"v":{"code":"","created":"2013-09-19T09:38:50+02:00","published":"0001-01-01T00:00:00Z","updated":"2013-09-19T09:38:50+02:00"}}],"s":200}`)

    if !assert.NoError(t, err, "Can't decode output") {
        return
    }
    assert.True(t, json.Has("d"), json.MustJSON())

    if !assert.True(t, json.Get("d").IsMSISlice(), json.MustJSON()) {
        return
    }
    if !assert.Equal(t, 1, len(json.Get("d").MSISlice()), "...") {
        return
    }
tylerstillwater commented 10 years ago

The JSON unmarshaller unpacks all slices to []interface{}. To make it an MSI, I think we'd have to iterate and copy to the new slice.