Skip to content

Commit 958add5

Browse files
committed
refactor: 修改 UserInfoData 中 Role 字段类型为 IntSlice,并优化 UnmarshalJSON 方法
1 parent 189d304 commit 958add5

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

internal/service/alist/response.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,27 @@ type FsGetData struct {
3333
}
3434

3535
type UserInfoData struct {
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"` // 用户名
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 IntSlice `json:"role"` // 角色
43+
SsoID string `json:"sso_id"` // sso id
44+
Username string `json:"username"` // 用户名
4545
}
4646

47-
type Int64Slice []int64
47+
type IntSlice []int
4848

49-
func (s *Int64Slice) UnmarshalJSON(data []byte) error {
50-
if len(data) == 0 || string(data) == "null" {
51-
*s = []int64{}
52-
return nil
49+
func (s *IntSlice) UnmarshalJSON(data []byte) error {
50+
if len(data) > 0 && data[0] == '[' {
51+
return json.Unmarshal(data, (*[]int)(s))
5352
}
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 {
53+
var single int
54+
if err := json.Unmarshal(data, &single); err != nil {
6155
return err
6256
}
63-
*s = arr
57+
*s = []int{single}
6458
return nil
6559
}

0 commit comments

Comments
 (0)