Skip to content

Commit 189d304

Browse files
authored
fix: 修复新版 alist API 响应数据类型变化 (#87)
相关 issue: #85
1 parent 516fe06 commit 189d304

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

internal/service/alist/response.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package alist
22

3+
import (
4+
"encoding/json"
5+
)
6+
37
type AlistResponse[T any] struct {
48
Code int64 `json:"code"` // 状态码
59
Data T `json:"data"` // data
@@ -29,13 +33,33 @@ type FsGetData struct {
2933
}
3034

3135
type UserInfoData struct {
32-
BasePath string `json:"base_path"` // 根目录
33-
Disabled bool `json:"disabled"` // 是否禁用
34-
ID int64 `json:"id"` // id
35-
Otp bool `json:"otp"` // 是否开启二步验证
36-
Password string `json:"password"` // 密码
37-
Permission int64 `json:"permission"` // 权限
38-
Role int64 `json:"role"` // 角色
39-
SsoID string `json:"sso_id"` // sso id
40-
Username string `json:"username"` // 用户名
36+
BasePath string `json:"base_path"` // 根目录
37+
Disabled bool `json:"disabled"` // 是否禁用
38+
ID int64 `json:"id"` // id
39+
Otp bool `json:"otp"` // 是否开启二步验证
40+
Password string `json:"password"` // 密码
41+
Permission int64 `json:"permission"` // 权限
42+
Role Int64Slice `json:"role"` // 角色
43+
SsoID string `json:"sso_id"` // sso id
44+
Username string `json:"username"` // 用户名
45+
}
46+
47+
type Int64Slice []int64
48+
49+
func (s *Int64Slice) UnmarshalJSON(data []byte) error {
50+
if len(data) == 0 || string(data) == "null" {
51+
*s = []int64{}
52+
return nil
53+
}
54+
var single int64
55+
if err := json.Unmarshal(data, &single); err == nil {
56+
*s = []int64{single}
57+
return nil
58+
}
59+
var arr []int64
60+
if err := json.Unmarshal(data, &arr); err != nil {
61+
return err
62+
}
63+
*s = arr
64+
return nil
4165
}

0 commit comments

Comments
 (0)