-
Notifications
You must be signed in to change notification settings - Fork 182
/
types.go
94 lines (84 loc) · 2.12 KB
/
types.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
84
85
86
87
88
89
90
91
92
93
94
package common
import (
"encoding/json"
)
// BaseResponse is the main frame of response
type BaseResponse struct {
Code uint32 `json:"code"`
Msg string `json:"msg"`
DetailMsg string `json:"detail_msg"`
Data interface{} `json:"data"`
}
// GetErrorResponse creates an error base response
func GetErrorResponse(code uint32, msg, detailMsg string) *BaseResponse {
return &BaseResponse{
Code: code,
DetailMsg: detailMsg,
Msg: msg,
Data: nil,
}
}
// GetErrorResponseJSON marshals the base response into JSON bytes
func GetErrorResponseJSON(code uint32, msg, detailMsg string) []byte {
res, err := json.Marshal(BaseResponse{
Code: code,
DetailMsg: detailMsg,
Msg: msg,
Data: nil,
})
if err != nil {
return []byte(err.Error())
}
return res
}
// GetBaseResponse gets a default base response
func GetBaseResponse(data interface{}) *BaseResponse {
return &BaseResponse{
Code: 0,
Msg: "",
DetailMsg: "",
Data: data,
}
}
// ParamPage is the struct of params page
type ParamPage struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Total int `json:"total"`
}
// ListDataRes is the struct of list data result
type ListDataRes struct {
Data interface{} `json:"data"`
ParamPage ParamPage `json:"param_page"`
}
// ListResponse is the frame of list response
type ListResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
DetailMsg string `json:"detail_msg"`
Data ListDataRes `json:"data"`
}
// GetListResponse returns a list response
func GetListResponse(total, page, perPage int, data interface{}) *ListResponse {
return &ListResponse{
Code: 0,
Msg: "",
DetailMsg: "",
Data: ListDataRes{
Data: data,
ParamPage: ParamPage{page, perPage, total},
},
}
}
// GetEmptyListResponse returns an empty list response
func GetEmptyListResponse(total, page, perPage int) *ListResponse {
return &ListResponse{
Code: 0,
Msg: "",
DetailMsg: "",
Data: ListDataRes{
Data: []string{},
ParamPage: ParamPage{page, perPage, total},
},
}
}