恐慌界面转换界面{}是float64而不是int64

huangapple go评论80阅读模式
英文:

panic interface conversion interface {} is float64 not int64

问题

我遇到了以下错误:

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

我不确定为什么会出现float64。我已经将类型设置为int64,但不确定float64是从哪里来的。

以下是代码片段:

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
}

错误似乎来自于ad.Refresh = payload["refresh"].(int64)这一行。我认为我只需要知道如何在接口{}中将类型从float64转换为int64,或者反之。

我已经尝试了各种方法将类型更改回int64,但是总是遇到其他错误,现在需要帮助继续前进。

以下是解码jwt后从cookie中获取的payload数据示例:

{
  "access_uuid": "c307ac76-e591-41d0-a638-6dcc2f963704",
  "exp": 1642130687,
  "permission": "user",
  "refresh": 1642734587,
  "sub": "test3@example.com"
}
英文:

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"
}

答案1

得分: 21

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

你需要首先 断言 准确的接口值的动态类型,然后,如果成功,你可以将其转换为你想要的类型。

请注意,接口值为 float64 的原因是因为当将 JSON 数字解组为 interface{} 值时,encoding/json 解码器的默认设置。

为了将 JSON 解组为接口值,Unmarshal 将以下之一存储在接口值中:

bool,用于 JSON 布尔值
float64,用于 JSON 数字
string,用于 JSON 字符串
[]interface{},用于 JSON 数组
map[string]interface{},用于 JSON 对象
nil,用于 JSON null
英文:
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:
>
> none
> 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
>

huangapple
  • 本文由 发表于 2022年1月14日 11:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/70705673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定