stretchr / objx

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

Panic on MustInt() #8

Closed an2deg closed 6 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}

Objx panics on json.Get("d[0].id").Int(). 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.Equal(t, json.Get("d[0].id").Int(), 9879, "...") 
matryer commented 10 years ago

Doesn't JSON make all slices {}interface{}?

Do you think MSISlice() should convert it?

On 19 Sep 2013, at 01:50, Andrew Degtyarev notifications@github.com wrote:

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} Objx panics on json.Get("d[0].id").Int(). 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
}

if !assert.Equal(t, json.Get("d[0].id").Int(), 9879, "...") {
    return
}

— Reply to this email directly or view it on GitHub.

tylerstillwater commented 10 years ago

JSON decides all integer values to float64. Try this again with float64.

Do you think .int() should try to coerce to int?

Sent from my iPhone

On Sep 19, 2013, at 7:36 AM, Mat Ryer notifications@github.com wrote:

Doesn't JSON make all slices {}interface{}?

Do you think MSISlice() should convert it?

On 19 Sep 2013, at 01:50, Andrew Degtyarev notifications@github.com wrote:

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} Objx panics on json.Get("d[0].id").Int(). 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 }

if !assert.Equal(t, json.Get("d[0].id").Int(), 9879, "...") { return } — Reply to this email directly or view it on GitHub. — Reply to this email directly or view it on GitHub.

an2deg commented 10 years ago

No, it is []interface{}. In my case it is []interface{}{ []map[string]interface{} }

On Thu, Sep 19, 2013 at 3:36 PM, Mat Ryer notifications@github.com wrote:

Doesn't JSON make all slices {}interface{}?

Do you think MSISlice() should convert it?

On 19 Sep 2013, at 01:50, Andrew Degtyarev notifications@github.com wrote:

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} Objx panics on json.Get("d[0].id").Int(). 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 }

if !assert.Equal(t, json.Get("d[0].id").Int(), 9879, "...") { return } — Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/stretchr/objx/issues/8#issuecomment-24738795 .

Andrew Degtyarev DA-RIPE

an2deg commented 10 years ago

Yep, I already did it, but for high level library like objx I expect than I asked for Int() it will try return int )

On Thu, Sep 19, 2013 at 3:53 PM, Tyler notifications@github.com wrote:

JSON decides all integer values to float64. Try this again with float64.

Do you think .int() should try to coerce to int?

Sent from my iPhone

On Sep 19, 2013, at 7:36 AM, Mat Ryer notifications@github.com wrote:

Doesn't JSON make all slices {}interface{}?

Do you think MSISlice() should convert it?

On 19 Sep 2013, at 01:50, Andrew Degtyarev notifications@github.com wrote:

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} Objx panics on json.Get("d[0].id").Int(). 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 }

if !assert.Equal(t, json.Get("d[0].id").Int(), 9879, "...") { return } — Reply to this email directly or view it on GitHub. — Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/stretchr/objx/issues/8#issuecomment-24740055 .

Andrew Degtyarev DA-RIPE

tylerstillwater commented 10 years ago

Yes I agree. We should try our hardest to return what is asked for.

matryer commented 10 years ago

Did the MustInt() at least give you a good panic?

On 19 Sep 2013, at 08:16, Tyler notifications@github.com wrote:

Yes I agree. We should try our hardest to return what is asked for.

— Reply to this email directly or view it on GitHub.

an2deg commented 10 years ago

Yes, I made an error in the example, there should be MustInt() instead of Int()

On Thu, Sep 19, 2013 at 7:14 PM, Mat Ryer notifications@github.com wrote:

Did the MustInt() at least give you a good panic?

On 19 Sep 2013, at 08:16, Tyler notifications@github.com wrote:

Yes I agree. We should try our hardest to return what is asked for.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/stretchr/objx/issues/8#issuecomment-24756516 .

Andrew Degtyarev DA-RIPE