-
Notifications
You must be signed in to change notification settings - Fork 1
/
success.go
83 lines (69 loc) · 1.39 KB
/
success.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 response
import (
"time"
)
type resData struct {
total int
perPage int
result interface{}
}
type response struct {
message string
error string
version string
representedAt string
data resData
httpCode int
}
func NewSuccessResponse() *response {
return &response{
version: "v1",
representedAt: time.Now().Format("2006-01-02 15:04:05.000000"),
}
}
func (r *response) Message(msg string) *response {
r.message = msg
return r
}
func (r *response) Version(v string) *response {
r.version = v
return r
}
func (r *response) Data(d interface{}) *response {
r.data.result = d
return r
}
func (r *response) SingleData(d interface{}) *response {
r.data.result = d
return r
}
func (r *response) Total(t int) *response {
r.data.total = t
return r
}
func (r *response) Error(e string) *response {
r.error = e
return r
}
func (r *response) PerPage(pp int) *response {
r.data.perPage = pp
return r
}
func (r *response) HttpCode(hc int) *response {
r.httpCode = hc
return r
}
func (r *response) Generate() map[string]interface{} {
resp := map[string]interface{}{
"message": r.message,
"error": r.error,
"version": r.version,
"represented_at": r.representedAt,
"data": map[string]interface{}{
"total": r.data.total,
"per_page": r.data.perPage,
"result": r.data.result,
},
}
return resp
}