Skip to content

Commit 4da811d

Browse files
committed
fix golint warning
1 parent 6b7bb44 commit 4da811d

File tree

15 files changed

+470
-372
lines changed

15 files changed

+470
-372
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ meta:
1919
lint: golint
2020
golint gate/
2121
golint store/
22+
golint client/
23+
golint master/
2224
golint notice/
2325
golint util/
2426

client/candy.aar

-3.04 MB
Binary file not shown.

client/candy.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type MessageHandler interface {
2929
OnUnHealth(msg string)
3030
}
3131

32+
// CandyClient 客户端提供和服务器交互的接口
3233
type CandyClient struct {
3334
host string
3435
stop bool
@@ -39,6 +40,7 @@ type CandyClient struct {
3940
health healthpb.HealthClient
4041
}
4142

43+
// NewCandyClient - create an new CandyClient
4244
func NewCandyClient(host string, handler MessageHandler) *CandyClient {
4345
return &CandyClient{host: host, handler: handler}
4446
}
@@ -70,7 +72,15 @@ func (c *CandyClient) Stop() error {
7072
return c.conn.Close()
7173
}
7274

75+
// Register 用户注册接口
7376
func (c *CandyClient) Register(user, passwd string) (int64, error) {
77+
if err := CheckUserName(user); err != nil {
78+
return -1, err
79+
}
80+
if err := CheckUserPassword(passwd); err != nil {
81+
return -1, err
82+
}
83+
7484
req := &meta.GateRegisterRequest{User: user, Password: passwd}
7585
resp, err := c.api.Register(context.Background(), req)
7686
if err != nil {
@@ -80,7 +90,16 @@ func (c *CandyClient) Register(user, passwd string) (int64, error) {
8090
return resp.ID, resp.Header.Error()
8191
}
8292

93+
// Login 用户登陆
8394
func (c *CandyClient) Login(user, passwd string) (int64, error) {
95+
if err := CheckUserName(user); err != nil {
96+
return -1, err
97+
}
98+
99+
if err := CheckUserPassword(passwd); err != nil {
100+
return -1, err
101+
}
102+
84103
req := &meta.GateUserLoginRequest{User: user, Password: passwd}
85104
resp, err := c.api.Login(context.Background(), req)
86105
if err != nil {
@@ -90,8 +109,9 @@ func (c *CandyClient) Login(user, passwd string) (int64, error) {
90109
return resp.ID, resp.Header.Error()
91110
}
92111

93-
func (c *CandyClient) Logout(user string) error {
94-
req := &meta.GateUserLogoutRequest{User: user}
112+
// Logout 注销登陆
113+
func (c *CandyClient) Logout() error {
114+
req := &meta.GateUserLogoutRequest{}
95115
resp, err := c.api.Logout(context.Background(), req)
96116
if err != nil {
97117
return err
@@ -100,7 +120,16 @@ func (c *CandyClient) Logout(user string) error {
100120
return resp.Header.Error()
101121
}
102122

123+
// UpdateUserInfo 更新用户信息, 昵称/头像
103124
func (c *CandyClient) UpdateUserInfo(user, nickName string, avatar []byte) (int64, error) {
125+
if err := CheckUserName(user); err != nil {
126+
return -1, err
127+
}
128+
129+
if err := CheckNickName(nickName); err != nil {
130+
return -1, err
131+
}
132+
104133
req := &meta.GateUpdateUserInfoRequest{User: user, NickName: nickName, Avatar: avatar}
105134
resp, err := c.api.UpdateUserInfo(context.Background(), req)
106135
if err != nil {
@@ -110,7 +139,16 @@ func (c *CandyClient) UpdateUserInfo(user, nickName string, avatar []byte) (int6
110139
return resp.ID, resp.Header.Error()
111140
}
112141

142+
// UpdateUserPassword 更新用户密码
113143
func (c *CandyClient) UpdateUserPassword(user, passwd string) (int64, error) {
144+
if err := CheckUserName(user); err != nil {
145+
return -1, err
146+
}
147+
148+
if err := CheckUserPassword(passwd); err != nil {
149+
return -1, err
150+
}
151+
114152
req := &meta.GateUpdateUserPasswordRequest{User: user, Password: passwd}
115153
resp, err := c.api.UpdateUserPassword(context.Background(), req)
116154
if err != nil {
@@ -120,6 +158,8 @@ func (c *CandyClient) UpdateUserPassword(user, passwd string) (int64, error) {
120158
return resp.ID, resp.Header.Error()
121159
}
122160

161+
// GetUserInfoByName 根据用户名获取用户信息
162+
//TODO 需要把返回字符串修改成对应的类型
123163
func (c *CandyClient) GetUserInfoByName(user string) (string, error) {
124164
userInfo, err := c.getUserInfoByName(user)
125165
if err != nil {
@@ -135,6 +175,10 @@ func (c *CandyClient) GetUserInfoByName(user string) (string, error) {
135175
}
136176

137177
func (c *CandyClient) getUserInfoByName(user string) (*UserInfo, error) {
178+
if err := CheckUserName(user); err != nil {
179+
return nil, err
180+
}
181+
138182
req := &meta.GateGetUserInfoRequest{Type: 0, UserName: user}
139183
resp, err := c.api.GetUserInfo(context.Background(), req)
140184
if err != nil {
@@ -145,6 +189,8 @@ func (c *CandyClient) getUserInfoByName(user string) (*UserInfo, error) {
145189
return userInfo, resp.Header.Error()
146190
}
147191

192+
// GetUserInfoByID 根据用户ID获取用户信息
193+
//TODO 需要把返回字符串修改成对应的类型
148194
func (c *CandyClient) GetUserInfoByID(userID int64) (string, error) {
149195
userInfo, err := c.getUserInfoByID(userID)
150196
if err != nil {
@@ -170,6 +216,7 @@ func (c *CandyClient) getUserInfoByID(userID int64) (*UserInfo, error) {
170216
return userInfo, resp.Header.Error()
171217
}
172218

219+
// AddFriend 添加好友
173220
func (c *CandyClient) AddFriend(userID int64, confirm bool, msg string) (bool, error) {
174221
req := &meta.GateAddFriendRequest{UserID: userID, Confirm: confirm, Msg: msg}
175222
resp, err := c.api.AddFriend(context.Background(), req)
@@ -180,6 +227,7 @@ func (c *CandyClient) AddFriend(userID int64, confirm bool, msg string) (bool, e
180227
return resp.Confirm, resp.Header.Error()
181228
}
182229

230+
// LoadFriendList 加载好友列表
183231
func (c *CandyClient) LoadFriendList() (string, error) {
184232
req := &meta.GateLoadFriendListRequest{}
185233
resp, err := c.api.LoadFriendList(context.Background(), req)
@@ -196,15 +244,15 @@ func (c *CandyClient) LoadFriendList() (string, error) {
196244
return string(data), nil
197245
}
198246

199-
// 支持模糊查询,返回对应用户的列表
247+
// FindUser 支持模糊查询,返回对应用户的列表
200248
func (c *CandyClient) FindUser(user string) (string, error) {
201249
req := &meta.GateFindUserRequest{User: user}
202250
resp, err := c.api.FindUser(context.Background(), req)
203251
if err != nil {
204252
return empty, err
205253
}
206254

207-
users := make([]*UserInfo, 0)
255+
var users []*UserInfo
208256
for _, matchUser := range resp.Users {
209257
userInfo, err := c.getUserInfoByName(matchUser)
210258
if err != nil {
@@ -221,6 +269,7 @@ func (c *CandyClient) FindUser(user string) (string, error) {
221269
return string(data), resp.Header.Error()
222270
}
223271

272+
// FileExist 判断文件是否存在
224273
func (c *CandyClient) FileExist(key string) (bool, error) {
225274
req := &meta.GateCheckFileRequest{Names: []string{key}}
226275
resp, err := c.api.CheckFile(context.Background(), req)
@@ -239,6 +288,7 @@ func (c *CandyClient) FileExist(key string) (bool, error) {
239288
return false, nil
240289
}
241290

291+
// FileUpload 文件上传
242292
func (c *CandyClient) FileUpload(data []byte) (string, error) {
243293
md5 := string(util.MD5(data))
244294
exist, err := c.FileExist(md5)
@@ -259,6 +309,7 @@ func (c *CandyClient) FileUpload(data []byte) (string, error) {
259309
return md5, resp.Header.Error()
260310
}
261311

312+
// FileDownload 文件下载
262313
func (c *CandyClient) FileDownload(key string) ([]byte, error) {
263314
req := &meta.GateDownloadFileRequest{Names: []string{key}}
264315
resp, err := c.api.DownloadFile(context.Background(), req)
@@ -296,6 +347,7 @@ func (c *CandyClient) loopRecvMessage() {
296347
}
297348
}
298349

350+
// healthCheck 健康检查
299351
func (c *CandyClient) healthCheck() {
300352
for !c.stop {
301353
time.Sleep(time.Second)

client/candy_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestMain(main *testing.M) {
7373
}
7474

7575
for i := 0; i < 10; i++ {
76-
name := fmt.Sprintf("testuser_%v_%d", time.Now().Unix(), i)
76+
name := fmt.Sprintf("testuser_%v_%d@qq.com", time.Now().Unix(), i)
7777
pass := fmt.Sprintf("testpass_%v_%d", time.Now().Unix(), i)
7878
id, err := client.Register(name, pass)
7979
if err != nil {
@@ -175,7 +175,7 @@ func TestUpdateUserPassword(t *testing.T) {
175175
t.Logf("UpdateUserPassword success, userID:%d userName:%v", id, name)
176176

177177
//Logout
178-
err = client.Logout(name)
178+
err = client.Logout()
179179
if err != nil {
180180
t.Fatalf("user Logout error:%v", err)
181181
}
@@ -329,7 +329,7 @@ func TestLoadGroupList(t *testing.T) {
329329
}
330330

331331
for index, group := range groupList.Groups {
332-
fmt.Printf("group:%v {ID:%v, Name:%v, Users:%v}\n", index, group.ID, group.Name, group.Users)
332+
t.Logf("group:%v {ID:%v, Name:%v, Users:%v}\n", index, group.ID, group.Name, group.Users)
333333
}
334334

335335
t.Logf("LoadGroupList success")
@@ -347,7 +347,7 @@ func TestLoadFriendList(t *testing.T) {
347347
}
348348

349349
for index, user := range friendList.Users {
350-
fmt.Printf("friend%v userID:%v\n", index, user)
350+
t.Logf("friend%v userID:%v\n", index, user)
351351
}
352352

353353
t.Logf("LoadFriendList success")

client/client/client.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func notice() {
2424
fmt.Println("10. 加载群组")
2525
fmt.Println("11. 根据用户ID获取用户信息")
2626
fmt.Println("12. 加载好友列表")
27+
fmt.Println("13. 更改用户密码")
2728
fmt.Println("0. 退出")
2829
fmt.Println("---------------------------------")
2930
}
@@ -69,17 +70,13 @@ func login(c *candy.CandyClient, reader *bufio.Reader) {
6970

7071
func logout(c *candy.CandyClient, reader *bufio.Reader) {
7172
fmt.Println("================注销=======================")
72-
fmt.Println("请输入用户名:")
73-
data, _, _ := reader.ReadLine()
74-
userName := string(data)
75-
76-
err := c.Logout(userName)
73+
err := c.Logout()
7774
if err != nil {
7875
log.Errorf("Logout error:%v", err)
7976
return
8077
}
8178

82-
log.Debugf("Logout success, userName:%v", userName)
79+
log.Debugf("Logout success")
8380
fmt.Println("==============================================")
8481
}
8582

@@ -287,7 +284,7 @@ func loadGroupList(c *candy.CandyClient, reader *bufio.Reader) {
287284
fmt.Println("==============================================")
288285
}
289286

290-
func loadFriendList(c *candy.CandyClient, read *bufio.Reader) {
287+
func loadFriendList(c *candy.CandyClient, reader *bufio.Reader) {
291288
fmt.Println("===============加载好友列表===================")
292289

293290
data, err := c.LoadFriendList()
@@ -310,6 +307,25 @@ func loadFriendList(c *candy.CandyClient, read *bufio.Reader) {
310307
fmt.Println("==============================================")
311308
}
312309

310+
func updateUserPasswd(c *candy.CandyClient, reader *bufio.Reader) {
311+
fmt.Println("===============更改用户密码===================")
312+
fmt.Println("请输入用户名:")
313+
data, _, _ := reader.ReadLine()
314+
user := string(data)
315+
316+
fmt.Println("请输入新密码:")
317+
data, _, _ = reader.ReadLine()
318+
pwd := string(data)
319+
320+
id, err := c.UpdateUserPassword(user, pwd)
321+
if err != nil {
322+
log.Errorf("UpdateUserPassword error:%v", err)
323+
return
324+
}
325+
log.Debugf("UpdateUserPassword success, id:%v", id)
326+
fmt.Println("==============================================")
327+
}
328+
313329
type cmdClient struct {
314330
}
315331

@@ -372,6 +388,8 @@ func main() {
372388
getUserInfoByID(c, reader)
373389
} else if command == "12" {
374390
loadFriendList(c, reader)
391+
} else if command == "13" {
392+
updateUserPasswd(c, reader)
375393
} else {
376394
log.Errorf("未知命令")
377395
}

client/common.go

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,31 @@
11
package candy
22

3-
import (
4-
"encoding/json"
5-
"fmt"
6-
)
7-
8-
//用户信息
3+
// UserInfo 用户信息
94
type UserInfo struct {
10-
ID int64
11-
Name string
12-
NickName string
13-
Avatar []byte
5+
ID int64 `json:"ID"`
6+
Name string `json:"Name"`
7+
NickName string `json:"NickName"`
8+
Avatar []byte `json:"Avatar"`
149
}
1510

11+
// UserList 用户列表
1612
type UserList struct {
1713
Users []*UserInfo `json:"Users"`
1814
}
1915

20-
//群组信息
16+
//GroupInfo 群组信息
2117
type GroupInfo struct {
2218
ID int64 `json:"ID"`
2319
Name string `json:"Name"`
2420
Users []int64 `json:"Users"`
2521
}
2622

23+
// GroupList 群组列表
2724
type GroupList struct {
2825
Groups []*GroupInfo `json:"Groups"`
2926
}
3027

31-
//好友信息
28+
//FriendList 好友列表
3229
type FriendList struct {
3330
Users []int64 `json:"Users"`
3431
}
35-
36-
func encodeJSON(data interface{}) ([]byte, error) {
37-
body, err := json.Marshal(data)
38-
if err != nil {
39-
return nil, err
40-
}
41-
return body, nil
42-
}
43-
44-
func DecodeUserInfo(data []byte) (*UserInfo, error) {
45-
userInfo := &UserInfo{}
46-
if err := json.Unmarshal(data, &userInfo); err != nil {
47-
return nil, fmt.Errorf("Decode UserInfo error:%v", err.Error())
48-
}
49-
return userInfo, nil
50-
}
51-
52-
func DecodeUserList(data []byte) (*UserList, error) {
53-
userList := &UserList{}
54-
if err := json.Unmarshal(data, &userList); err != nil {
55-
return nil, fmt.Errorf("Decode UserList error:%v", err.Error())
56-
}
57-
return userList, nil
58-
}
59-
60-
func DecodeFriendList(data []byte) (*FriendList, error) {
61-
friendList := &FriendList{}
62-
if err := json.Unmarshal(data, &friendList); err != nil {
63-
return nil, fmt.Errorf("Decode FriendList error:%v", err.Error())
64-
}
65-
return friendList, nil
66-
}
67-
68-
func DecodeGroupList(data []byte) (*GroupList, error) {
69-
groupList := &GroupList{}
70-
if err := json.Unmarshal(data, &groupList); err != nil {
71-
return nil, fmt.Errorf("Decode GroupList error:%v", err.Error())
72-
}
73-
return groupList, nil
74-
}

0 commit comments

Comments
 (0)