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

Panic on MustInt() #8

Closed
an2deg opened this issue Sep 19, 2013 · 7 comments
Closed

Panic on MustInt() #8

an2deg opened this issue Sep 19, 2013 · 7 comments
Assignees
Labels
Milestone

Comments

@an2deg
Copy link

an2deg commented Sep 19, 2013

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
Copy link
Member

matryer commented Sep 19, 2013

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
Copy link
Contributor

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
Copy link
Author

an2deg commented Sep 19, 2013

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//issues/8#issuecomment-24738795
.

Andrew Degtyarev
DA-RIPE

@an2deg
Copy link
Author

an2deg commented Sep 19, 2013

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//issues/8#issuecomment-24740055
.

Andrew Degtyarev
DA-RIPE

@tylerstillwater
Copy link
Contributor

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

@matryer
Copy link
Member

matryer commented Sep 19, 2013

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
Copy link
Author

an2deg commented Sep 19, 2013

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//issues/8#issuecomment-24756516
.

Andrew Degtyarev
DA-RIPE

@hanzei hanzei added the bug label Jan 26, 2018
@hanzei hanzei added this to the v0.2 milestone Jan 26, 2018
@hanzei hanzei self-assigned this Jan 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants