Strange type assertion error

159 views
Skip to first unread message

Jeff Rooney

unread,
Jan 26, 2016, 7:23:04 PM1/26/16
to golan...@googlegroups.com
Hi there, I ran into a strange type assertion error today and was hoping someone could help point me in the right direction. I have a uint32 that I am storing in a net/context but when I try and access it, I am getting the following error:

panic: interface conversion: interface is float64, not uint32

Here is how I store it in the context:
ctx = context.WithValue(ctx, "auth.uid", token.Claims["sub"])

If I dump the context I see:
WithValue("auth.uid", 1)

Then when I access the value from the context I assert uint32 and receive the error.
uid := ctx.Value("auth.uid).(uint32)

I'm sure I am missing something obvious, but i have been spinning on this for a bit now. Thanks for any pointers

Jeff

Dan Kortschak

unread,
Jan 26, 2016, 7:32:13 PM1/26/16
to Jeff Rooney, golan...@googlegroups.com
Presumably token.Claims["sub"] is a float64. How was it created?

Jeff Rooney

unread,
Jan 26, 2016, 7:56:11 PM1/26/16
to Dan Kortschak, golan...@googlegroups.com
That is was I was thinking, but it is set via:
        token.Claims["sub"] = uint32(user.Id)

where user.Id is defined as a uint32 as well.

Caleb Spare

unread,
Jan 26, 2016, 8:00:13 PM1/26/16
to Jeff Rooney, Dan Kortschak, golang-nuts
Can you show a complete, runnable example?
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jeff Rooney

unread,
Jan 26, 2016, 8:52:20 PM1/26/16
to Caleb Spare, Dan Kortschak, golang-nuts
I'll see if I can put together a runnable example, but it will take some reworking. I'm using github.com/dgrijalva/jwt-go to parse a json web token that has "sub": 1 The claims are stored as map[string]interface{} and when I try and convert the "sub" value to uint32 I receive an error that the interface is a float64 and I am unable to type assert it.

Dan Kortschak

unread,
Jan 26, 2016, 9:04:52 PM1/26/16
to Jeff Rooney, Caleb Spare, golang-nuts
On Tue, 2016-01-26 at 19:51 -0600, Jeff Rooney wrote:
> I'll see if I can put together a runnable example, but it will take
> some reworking. I'm using github.com/dgrijalva/jwt-go to parse a json
> web token that has "sub": 1 The claims are stored as
> map[string]interface{} and when I try and convert the "sub" value to
> uint32 I receive an error that the interface is a float64 and I am
> unable to type assert it.
>
The most likely thing is that encoding/json is decoding the JSON number
as float64 (as documented).

You should be able to just `ctx = context.WithValue(ctx, "auth.uid",
uint32(token.Claims["sub"].(float64)))` to get the behaviour you want.

Jeff Rooney

unread,
Jan 26, 2016, 9:14:51 PM1/26/16
to Dan Kortschak, golang-nuts
Thank you very much, I was so focused as on why I wasnt able to to assert using .(uint32) that I missed that encoding/json parses numbers as float64.

Thanks again!
Reply all
Reply to author
Forward
0 new messages