-
Notifications
You must be signed in to change notification settings - Fork 164
/
user_info_helper.go
83 lines (74 loc) · 1.89 KB
/
user_info_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package workwx
import (
"fmt"
"strconv"
)
func reshapeDeptInfo(
ids []int64,
orders []uint32,
leaderStatuses []int,
) ([]UserDeptInfo, error) {
if len(ids) != len(orders) {
return nil, fmt.Errorf(
"server API breakage: len(DeptIDs) (%d) != len(DeptOrder) (%d)",
len(ids),
len(orders),
)
}
// sometimes leaderStatuses could be empty, but if not, the arrays should
// have equal length
if len(ids) != len(leaderStatuses) && len(leaderStatuses) != 0 {
return nil, fmt.Errorf(
"server API breakage: len(DeptIDs) (%d) != len(IsLeaderInDept) (%d)",
len(ids),
len(leaderStatuses),
)
}
result := make([]UserDeptInfo, len(ids))
for i := range ids {
result[i].DeptID = ids[i]
result[i].Order = orders[i]
if i < len(leaderStatuses) {
// apparently leaderStatuses could sometimes be empty, don't set
// anybody as leader in that case
// see https://github.com/xen0n/go-workwx/pull/78
result[i].IsLeader = leaderStatuses[i] != 0
}
}
return result, nil
}
func userGenderFromGenderStr(x string) (UserGender, error) {
if x == "" {
return UserGenderUnspecified, nil
}
n, err := strconv.Atoi(x)
if err != nil {
return UserGenderUnspecified, fmt.Errorf("gender string parse failed: %+v", err)
}
return UserGender(n), nil
}
func (x UserDetail) intoUserInfo() (UserInfo, error) {
deptInfo, err := reshapeDeptInfo(x.DeptIDs, x.DeptOrder, x.IsLeaderInDept)
if err != nil {
return UserInfo{}, err
}
gender, err := userGenderFromGenderStr(x.Gender)
if err != nil {
return UserInfo{}, err
}
return UserInfo{
UserID: x.UserID,
Name: x.Name,
Position: x.Position,
Departments: deptInfo,
Mobile: x.Mobile,
Gender: gender,
Email: x.Email,
AvatarURL: x.AvatarURL,
Telephone: x.Telephone,
IsEnabled: x.IsEnabled != 0,
Alias: x.Alias,
Status: UserStatus(x.Status),
QRCodeURL: x.QRCodeURL,
}, nil
}