8

I am getting the following error

panic: interface conversion: interface {} is float64, not int64

I am not sure where float64 is coming from here I have type set to int64 but not sure where float64 came from

type AccessDetails struct {
    AccessUuid   string  `json:"access_uuid"`
    Email        string  `json:"email"`
    Refresh      int64    `json:"refresh"`
    Expiry       int64   `json:"expiry"`
    Permission   string  `json:"permission"`
    Scope        string  `json:"scope"`
}

func GetAccessDetails(c *fiber.Ctx) (*AccessDetails, error) {
    ad := &AccessDetails{}

    cookie := c.Cookies("access_token")

    var err error
    token, err := jwt.Parse(cookie, func(token *jwt.Token) (interface{}, error) {
        return []byte(os.Getenv("ACCESS_SECRET")), nil
    })

    if err != nil {
        return nil, err
    }

    payload := token.Claims.(jwt.MapClaims)
    
    ad.Email = payload["sub"].(string)
    ad.AccessUuid = payload["access_uuid"].(string)
    ad.Refresh = payload["refresh"].(int64)
    ad.Expiry = payload["exp"].(int64)
    ad.Permission = payload["permission"].(string)
    ad.Scope = payload["scope"].(string)

    return ad, nil
}

The error seems to be from the line ad.Refresh = payload["refresh"].(int64) I think i just need to know how to convert types from float64 to int64 or vice versa for interfaces {}

I have tried everything to change the type back to int64 but i get one error or the other and now need help to move forward

here is example of what the payload data looks like from the cookie after it is jwt decoded

{
  "access_uuid": "c307ac76-e591-41d0-a638-6dcc2f963704",
  "exp": 1642130687,
  "permission": "user",
  "refresh": 1642734587,
  "sub": "test3@example.com"
}
uberrebu
  • 3,597
  • 9
  • 38
  • 73
  • Can you show how you attempted to convert from float64 to int64? – Hymns For Disco Jan 14 '22 at 03:28
  • i tried this for example `ad.Refresh = int64(payload["refresh"])` and others..i just want to have int64 that is all – uberrebu Jan 14 '22 at 03:30
  • 1
    `int64(payload["refresh"].(float64))` -- [jwt.MapClaims](https://pkg.go.dev/github.com/dgrijalva/jwt-go#MapClaims) is a `map[string]interface{}` and presumably it is unmarshaled from json using [`encoding/json`](https://pkg.go.dev/encoding/json@go1.17.5) which, by default, unmarshals json numbers into `interface{}` as `float64`. – mkopriva Jan 14 '22 at 03:41
  • 2
    Explaining @mkopriva's comment, you must first perform type assertion to the actual type, and then perform a conversion to the desired type. – Hymns For Disco Jan 14 '22 at 03:43
  • @mkopriva does not work...tried what you mentioned `ad.Refresh = int64(payload["refresh"].(float64))` – uberrebu Jan 14 '22 at 03:49
  • 2
    @uberrebu what does "does not work" mean? what error are you getting? – mkopriva Jan 14 '22 at 03:50
  • getting same error still `panic: interface conversion: interface {} is float64, not int64` – uberrebu Jan 14 '22 at 03:51
  • Then you're doing it wrong. You're not doing what I've suggested. See [here](https://go.dev/play/p/sXTVwQH9y7S), no panic. – mkopriva Jan 14 '22 at 03:52
  • can you post as answer what i need to do? @mkopriva – uberrebu Jan 14 '22 at 03:54
  • I assume, after you fixed `refresh`, you're now getting the same panic for `exp`. – mkopriva Jan 14 '22 at 03:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241057/discussion-between-uberrebu-and-mkopriva). – uberrebu Jan 14 '22 at 03:56

1 Answers1

21
ad.Refresh = int64(payload["refresh"].(float64))
ad.Expiry = int64(payload["exp"].(float64))

You need to first assert the accurate dynamic type of the interface value and then, if successful, you can convert it to your desired type.

Note that the reason the interface values are float64 is because that's the default setting of the encoding/json decoder when unmarshaling JSON numbers into interface{} values.

To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null
mkopriva
  • 35,176
  • 4
  • 57
  • 71