-
Notifications
You must be signed in to change notification settings - Fork 11
/
data.go
467 lines (413 loc) · 8.54 KB
/
data.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
Copyright 2016 Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package echo
import (
"encoding/gob"
"fmt"
"net/http"
"strconv"
)
func init() {
gob.Register(&RawData{})
gob.Register(H{})
}
//Status 状态值
type Status struct {
Text string
Code int
}
var (
//States 状态码对应的文本
States = map[State]*Status{
-2: {`Non-Privileged`, http.StatusOK}, //无权限
-1: {`Unauthenticated`, http.StatusOK}, //未登录
0: {`Failure`, http.StatusOK}, //操作失败
1: {`Success`, http.StatusOK}, //操作成功
}
//GetStatus 获取状态值
GetStatus = func(key State) (*Status, bool) {
v, y := States[key]
return v, y
}
)
//State 状态码类型
type State int
func (s State) String() string {
if v, y := GetStatus(s); y {
return v.Text
}
return `Undefined`
}
//Int 返回int类型的自定义状态码
func (s State) Int() int {
return int(s)
}
//HTTPCode 返回HTTP状态码
func (s State) HTTPCode() int {
if v, y := GetStatus(s); y {
return v.Code
}
return http.StatusOK
}
//Data 响应数据
type Data interface {
Assign(key string, val interface{})
Assignx(values *map[string]interface{})
SetTmplFuncs()
SetContext(ctx Context) Data
String() string
Set(code int, args ...interface{}) Data
Reset() Data
SetByMap(Store) Data
SetError(err error, args ...int) Data
SetCode(code int) Data
SetURL(url string, args ...int) Data
SetInfo(info interface{}, args ...int) Data
SetZone(zone interface{}) Data
SetData(data interface{}, args ...int) Data
Gets() (code State, info interface{}, zone interface{}, data interface{})
GetCode() State
GetInfo() interface{}
GetZone() interface{}
GetData() interface{}
GetURL() string
}
type RawData struct {
context Context
Code State
State string `json:",omitempty" xml:",omitempty"`
Info interface{}
URL string `json:",omitempty" xml:",omitempty"`
Zone interface{} `json:",omitempty" xml:",omitempty"`
Data interface{} `json:",omitempty" xml:",omitempty"`
}
func (d *RawData) Error() string {
return fmt.Sprintf(`%v`, d.Info)
}
func (d *RawData) Reset() Data {
d.Code = State(0)
d.State = ``
d.Info = nil
d.URL = ``
d.Zone = nil
d.Data = nil
return d
}
func (d *RawData) String() string {
return fmt.Sprintf(`%v`, d.Info)
}
//Gets 获取全部数据
func (d *RawData) Gets() (State, interface{}, interface{}, interface{}) {
return d.Code, d.Info, d.Zone, d.Data
}
func (d *RawData) GetCode() State {
return d.Code
}
func (d *RawData) GetInfo() interface{} {
return d.Info
}
func (d *RawData) GetZone() interface{} {
return d.Zone
}
func (d *RawData) GetURL() string {
return d.URL
}
//GetData 获取数据
func (d *RawData) GetData() interface{} {
return d.Data
}
//SetError 设置错误
func (d *RawData) SetError(err error, args ...int) Data {
if err != nil {
if len(args) > 0 {
d.SetCode(args[0])
} else {
d.SetCode(0)
}
d.Info = err.Error()
} else {
d.SetCode(1)
}
return d
}
//SetCode 设置状态码
func (d *RawData) SetCode(code int) Data {
d.Code = State(code)
d.State = d.Code.String()
return d
}
//SetURL 设置跳转网址
func (d *RawData) SetURL(url string, args ...int) Data {
d.URL = url
if len(args) > 0 {
d.SetCode(args[0])
}
return d
}
//SetInfo 设置提示信息
func (d *RawData) SetInfo(info interface{}, args ...int) Data {
d.Info = info
if len(args) > 0 {
d.SetCode(args[0])
}
return d
}
//SetByMap 批量设置属性
func (d *RawData) SetByMap(s Store) Data {
if v, y := s["Data"]; y {
d.Data = v
}
if v, y := s["Zone"]; y {
d.Zone = v
}
if v, y := s["Info"]; y {
d.Info = v
}
if v, y := s["URL"]; y {
d.URL, _ = v.(string)
}
var code State
if v, y := s["Code"]; y {
switch c := v.(type) {
case State:
code = c
case int:
code = State(c)
case string:
i, _ := strconv.Atoi(c)
code = State(i)
default:
s := fmt.Sprint(c)
i, _ := strconv.Atoi(s)
code = State(i)
}
}
d.Code = code
return d
}
//SetZone 设置提示区域
func (d *RawData) SetZone(zone interface{}) Data {
d.Zone = zone
return d
}
//SetData 设置正常数据
func (d *RawData) SetData(data interface{}, args ...int) Data {
d.Data = data
if len(args) > 0 {
d.SetCode(args[0])
} else {
d.SetCode(1)
}
return d
}
//SetContext 设置Context
func (d *RawData) SetContext(ctx Context) Data {
d.context = ctx
return d
}
//Assign 赋值
func (d *RawData) Assign(key string, val interface{}) {
data, _ := d.Data.(H)
if data == nil {
data = H{}
}
data[key] = val
d.Data = data
}
//Assignx 批量赋值
func (d *RawData) Assignx(values *map[string]interface{}) {
if values == nil {
return
}
data, _ := d.Data.(H)
if data == nil {
data = H{}
}
for key, val := range *values {
data[key] = val
}
d.Data = data
}
//SetTmplFuncs 设置模板函数
func (d *RawData) SetTmplFuncs() {
flash, ok := d.context.Flash().(*RawData)
if ok {
d.context.Session().Save()
} else {
flash = d
}
d.context.SetFunc(`Code`, func() State {
return flash.Code
})
d.context.SetFunc(`Info`, func() interface{} {
return flash.Info
})
d.context.SetFunc(`Zone`, func() interface{} {
return flash.Zone
})
d.context.SetFunc(`FURL`, func() interface{} {
return flash.URL
})
}
// Set 设置输出(code,info,zone,RawData)
func (d *RawData) Set(code int, args ...interface{}) Data {
d.SetCode(code)
var hasData bool
switch len(args) {
case 3:
d.Data = args[2]
hasData = true
fallthrough
case 2:
d.Zone = args[1]
fallthrough
case 1:
d.Info = args[0]
if !hasData {
flash := &RawData{
context: d.context,
Code: d.Code,
State: d.State,
Info: d.Info,
URL: d.URL,
Zone: d.Zone,
Data: nil,
}
d.context.Session().AddFlash(flash).Save()
}
}
return d
}
func NewData(ctx Context) *RawData {
c := State(1)
return &RawData{
context: ctx,
Code: c,
State: c.String(),
}
}
//KV 键值对
type KV struct {
K string
V string
}
type KVList []*KV
func (list *KVList) Add(k, v string) {
*list = append(*list, &KV{K: k, V: v})
}
func (list *KVList) Del(i int) {
n := len(*list)
if i+1 < n {
*list = append((*list)[0:i], (*list)[i+1:]...)
} else if i < n {
*list = (*list)[0:i]
}
}
func (list *KVList) Reset() {
*list = (*list)[0:0]
}
//NewKVData 键值对数据
func NewKVData() *KVData {
return &KVData{
slice: []*KV{},
index: map[string][]int{},
}
}
//KVData 键值对数据(保持顺序)
type KVData struct {
slice []*KV
index map[string][]int
}
//Slice 返回切片
func (a *KVData) Slice() []*KV {
return a.slice
}
//Index 返回某个key的所有索引值
func (a *KVData) Index(k string) []int {
v, _ := a.index[k]
return v
}
//Indexes 返回所有索引值
func (a *KVData) Indexes() map[string][]int {
return a.index
}
//Reset 重置
func (a *KVData) Reset() *KVData {
a.index = map[string][]int{}
a.slice = []*KV{}
return a
}
//Add 添加键值
func (a *KVData) Add(k, v string) *KVData {
if _, y := a.index[k]; !y {
a.index[k] = []int{}
}
a.index[k] = append(a.index[k], len(a.slice))
a.slice = append(a.slice, &KV{K: k, V: v})
return a
}
//Set 设置首个键值
func (a *KVData) Set(k, v string) *KVData {
a.index[k] = []int{0}
a.slice = []*KV{&KV{K: k, V: v}}
return a
}
func (a *KVData) Get(k string) string {
if indexes, ok := a.index[k]; ok {
if len(indexes) > 0 {
return a.slice[indexes[0]].V
}
}
return ``
}
func (a *KVData) Has(k string) bool {
if _, ok := a.index[k]; ok {
return true
}
return false
}
//Delete 设置某个键的所有值
func (a *KVData) Delete(ks ...string) *KVData {
indexes := []int{}
for _, k := range ks {
v, y := a.index[k]
if !y {
continue
}
for _, key := range v {
indexes = append(indexes, key)
}
}
newSlice := []*KV{}
a.index = map[string][]int{}
for i, v := range a.slice {
var exists bool
for _, idx := range indexes {
if i != idx {
continue
}
exists = true
break
}
if exists {
continue
}
if _, y := a.index[v.K]; !y {
a.index[v.K] = []int{}
}
a.index[v.K] = append(a.index[v.K], len(newSlice))
newSlice = append(newSlice, v)
}
a.slice = newSlice
return a
}