-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct.go
268 lines (215 loc) · 6.42 KB
/
struct.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package godd
import (
"errors"
"log"
"net/http"
"strings"
)
// Map type
type Map map[string]interface{}
// MapString type
type MapString map[string]string
// DeferClose struct
type DeferClose struct {
Name string
I InterfaceClose
}
type FuncEnvironment = func(secret MapString) (*Map, *[]DeferClose)
// Handler type
type Handler func(*Context) error
// FrameWork type
type FrameWork string
const (
// FrameWorkGofiber FrameWork
FrameWorkGofiberV2 FrameWork = "gofiber/v2"
FrameWorkNetHTTP FrameWork = "net/http"
)
// Env type
type Env string
const (
// EnvLocalhost Env
EnvLocalhost Env = "localhost"
// EnvDevelopment Env
EnvDevelopment Env = "development"
// EnvTesting Env
EnvTesting Env = "testing"
// EnvStaging Env
EnvStaging Env = "staging"
// EnvProduction Env
EnvProduction Env = "production"
)
// ==============================================================
type Context struct {
i InterfaceContext
service interface{}
serviceOptionList map[string]interface{}
state map[string]interface{}
i18n *I18N
}
func NewContext(i InterfaceContext, service interface{}, serviceOptionList map[string]interface{}, state map[string]interface{}, i18n *I18N) *Context {
if serviceOptionList == nil {
serviceOptionList = map[string]interface{}{}
}
if state == nil {
state = map[string]interface{}{}
}
return &Context{i, service, serviceOptionList, state, i18n}
}
func NewContextDefault() *Context {
return NewContext(nil, nil, nil, nil, nil)
}
func (context *Context) App() InterfaceContext {
return context.i
}
func (context *Context) SetApp(i InterfaceContext) {
context.i = i
}
// GetService func
func (context *Context) GetService() interface{} {
return context.service
}
// GetServiceOptionList func
func (context *Context) GetServiceOptionList(name string) interface{} {
if context.serviceOptionList != nil {
return context.serviceOptionList[name]
}
log.Println("ServiceOptionList is null")
return nil
}
// GetState func
func (context *Context) GetState(name string) interface{} {
if context.state == nil {
context.state = map[string]interface{}{}
}
return context.state[name]
}
// SetState func
func (context *Context) SetState(name string, value interface{}) {
if context.state == nil {
context.state = map[string]interface{}{}
}
context.state[name] = value
}
// ClearState func
func (context *Context) ClearState() {
context.state = map[string]interface{}{}
}
// SetLang func
func (context *Context) SetLang(lang string) {
if context.i18n != nil {
context.i18n.SetLang(lang)
}
}
// GetLang func
func (context *Context) GetLang() string {
return context.i18n.GetLang()
}
// GetI18N func
func (context *Context) GetI18N() *I18N {
return context.i18n
}
//===========
// ValidateStruct func
func (context *Context) ValidateStruct(i interface{}, iType map[string]interface{}) *Error {
return ValidateStruct(context.i18n, i, iType)
}
// SetDefaultStruct func
func (context *Context) SetDefaultStruct(i interface{}) interface{} {
return SetDefaultStruct(i)
}
// ==============================================================
// Error Struct
type Error struct {
Code int // Please Use http.Status
message string
Error error
errorValidate *map[string]ResponseErrorValidate
}
func (e *Error) IsContain(subString string) bool {
return strings.Contains(e.Error.Error(), subString)
}
func (e *Error) SetError(str string) {
e.Error = errors.New(str)
}
func (e *Error) IsContainSetError(subString string, errorString string) {
if e.IsContain(subString) {
e.SetError(errorString)
}
}
func (e *Error) SetMessage(str string) {
e.message = str
}
func (e *Error) GetMessage() string {
if e.message != "" {
return e.message
}
if e.errorValidate != nil {
return http.StatusText(e.Code)
}
if e.Error != nil && e.Error.Error() != "" {
return e.Error.Error()
} else {
return http.StatusText(e.Code)
}
}
func (e *Error) SetErrorValidate(errorValidate *map[string]ResponseErrorValidate) {
e.Code = http.StatusBadRequest
e.errorValidate = errorValidate
}
func (e *Error) GetErrorValidate() *map[string]ResponseErrorValidate {
return e.errorValidate
}
// ==============================================================
// RequestPagination Struct
type RequestPagination struct {
Page int `json:"page" default:"1" swaggertype:"integer"`
PageSize int `json:"page_size" default:"10" swaggertype:"integer"`
}
// RequestFilter Struct
type RequestFilter struct {
DateStart string `json:"date_start"`
DateEnd string `json:"date_end"`
FilterList []RequestFilterType `json:"filter"`
SortList []RequestSort `json:"sort"`
}
// RequestFilterType struct
type RequestFilterType struct {
Field string `json:"field"`
Operator string `json:"op"`
Value string `json:"value"`
}
// RequestSort struct
type RequestSort struct {
Field string `json:"field"`
By string `json:"by"`
}
// ==============================================================
// ResponseDataList for Send Response Message to Encode Response
type ResponseDataList struct {
Code int `json:"code" swaggertype:"integer"`
Success bool `json:"success" swaggertype:"boolean"`
Message string `json:"message" swaggertype:"string"`
Data interface{} `json:"data,omitempty" swaggertype:"object"`
ResponsePagination *ResponsePagination `json:"pagination,omitempty" swaggertype:"object"`
ResponseError *ResponseError `json:"error,omitempty" swaggertype:"object"`
}
// ResponsePagination Struct
type ResponsePagination struct {
Page int `json:"page" swaggertype:"integer"`
PageSize int `json:"page_size" swaggertype:"integer"`
PageCount int `json:"page_count" swaggertype:"integer"`
ItemCount int `json:"item_count" swaggertype:"integer"`
TotalCount int `json:"total_count" swaggertype:"integer"`
}
// ResponseError Struct
type ResponseError struct {
Message string `json:"message" swaggertype:"string"`
Validate *map[string]ResponseErrorValidate `json:"validate,omitempty" swaggertype:"object"`
}
// ResponseErrorValidate struct
type ResponseErrorValidate struct {
Reason string `json:"reason"`
Message string `json:"message"`
Param string `json:"param,omitempty"`
}
// ==============================================================