-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
NgingUserU2f.go
571 lines (508 loc) · 13.4 KB
/
NgingUserU2f.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// @generated Do not edit this file, which is automatically generated by the generator.
package dbschema
import (
"fmt"
"time"
"github.com/webx-top/db"
"github.com/webx-top/db/lib/factory"
"github.com/webx-top/echo"
"github.com/webx-top/echo/param"
)
type Slice_NgingUserU2f []*NgingUserU2f
func (s Slice_NgingUserU2f) Range(fn func(m factory.Model) error) error {
for _, v := range s {
if err := fn(v); err != nil {
return err
}
}
return nil
}
func (s Slice_NgingUserU2f) RangeRaw(fn func(m *NgingUserU2f) error) error {
for _, v := range s {
if err := fn(v); err != nil {
return err
}
}
return nil
}
func (s Slice_NgingUserU2f) GroupBy(keyField string) map[string][]*NgingUserU2f {
r := map[string][]*NgingUserU2f{}
for _, row := range s {
dmap := row.AsMap()
vkey := fmt.Sprint(dmap[keyField])
if _, y := r[vkey]; !y {
r[vkey] = []*NgingUserU2f{}
}
r[vkey] = append(r[vkey], row)
}
return r
}
func (s Slice_NgingUserU2f) KeyBy(keyField string) map[string]*NgingUserU2f {
r := map[string]*NgingUserU2f{}
for _, row := range s {
dmap := row.AsMap()
vkey := fmt.Sprint(dmap[keyField])
r[vkey] = row
}
return r
}
func (s Slice_NgingUserU2f) AsKV(keyField string, valueField string) param.Store {
r := param.Store{}
for _, row := range s {
dmap := row.AsMap()
vkey := fmt.Sprint(dmap[keyField])
r[vkey] = dmap[valueField]
}
return r
}
func (s Slice_NgingUserU2f) Transform(transfers map[string]param.Transfer) []param.Store {
r := make([]param.Store, len(s))
for idx, row := range s {
r[idx] = row.AsMap().Transform(transfers)
}
return r
}
func (s Slice_NgingUserU2f) FromList(data interface{}) Slice_NgingUserU2f {
values, ok := data.([]*NgingUserU2f)
if !ok {
for _, value := range data.([]interface{}) {
row := &NgingUserU2f{}
row.FromRow(value.(map[string]interface{}))
s = append(s, row)
}
return s
}
s = append(s, values...)
return s
}
func NewNgingUserU2f(ctx echo.Context) *NgingUserU2f {
m := &NgingUserU2f{}
m.SetContext(ctx)
return m
}
// NgingUserU2f 两步验证
type NgingUserU2f struct {
base factory.Base
objects []*NgingUserU2f
Id uint64 `db:"id,omitempty,pk" bson:"id,omitempty" comment:"" json:"id" xml:"id"`
Uid uint `db:"uid" bson:"uid" comment:"用户ID" json:"uid" xml:"uid"`
Token string `db:"token" bson:"token" comment:"签名" json:"token" xml:"token"`
Type string `db:"type" bson:"type" comment:"类型" json:"type" xml:"type"`
Extra string `db:"extra" bson:"extra" comment:"扩展设置" json:"extra" xml:"extra"`
Created uint `db:"created" bson:"created" comment:"绑定时间" json:"created" xml:"created"`
}
// - base function
func (a *NgingUserU2f) Trans() *factory.Transaction {
return a.base.Trans()
}
func (a *NgingUserU2f) Use(trans *factory.Transaction) factory.Model {
a.base.Use(trans)
return a
}
func (a *NgingUserU2f) SetContext(ctx echo.Context) factory.Model {
a.base.SetContext(ctx)
return a
}
func (a *NgingUserU2f) EventON(on ...bool) factory.Model {
a.base.EventON(on...)
return a
}
func (a *NgingUserU2f) EventOFF(off ...bool) factory.Model {
a.base.EventOFF(off...)
return a
}
func (a *NgingUserU2f) Context() echo.Context {
return a.base.Context()
}
func (a *NgingUserU2f) SetConnID(connID int) factory.Model {
a.base.SetConnID(connID)
return a
}
func (a *NgingUserU2f) SetNamer(namer func(factory.Model) string) factory.Model {
a.base.SetNamer(namer)
return a
}
func (a *NgingUserU2f) Namer() func(factory.Model) string {
return a.base.Namer()
}
func (a *NgingUserU2f) SetParam(param *factory.Param) factory.Model {
a.base.SetParam(param)
return a
}
func (a *NgingUserU2f) Param(mw func(db.Result) db.Result, args ...interface{}) *factory.Param {
if a.base.Param() == nil {
return a.NewParam().SetMiddleware(mw).SetArgs(args...)
}
return a.base.Param().SetMiddleware(mw).SetArgs(args...)
}
// - current function
func (a *NgingUserU2f) New(structName string, connID ...int) factory.Model {
if len(connID) > 0 {
return factory.NewModel(structName, connID[0]).Use(a.base.Trans())
}
return factory.NewModel(structName, a.base.ConnID()).Use(a.base.Trans())
}
func (a *NgingUserU2f) Objects() []*NgingUserU2f {
if a.objects == nil {
return nil
}
return a.objects[:]
}
func (a *NgingUserU2f) XObjects() Slice_NgingUserU2f {
return Slice_NgingUserU2f(a.Objects())
}
func (a *NgingUserU2f) NewObjects() factory.Ranger {
return &Slice_NgingUserU2f{}
}
func (a *NgingUserU2f) InitObjects() *[]*NgingUserU2f {
a.objects = []*NgingUserU2f{}
return &a.objects
}
func (a *NgingUserU2f) NewParam() *factory.Param {
return factory.NewParam(factory.DefaultFactory).SetIndex(a.base.ConnID()).SetTrans(a.base.Trans()).SetCollection(a.Name_()).SetModel(a)
}
func (a *NgingUserU2f) Short_() string {
return "nging_user_u2f"
}
func (a *NgingUserU2f) Struct_() string {
return "NgingUserU2f"
}
func (a *NgingUserU2f) Name_() string {
if a.base.Namer() != nil {
return WithPrefix(a.base.Namer()(a))
}
return WithPrefix(factory.TableNamerGet(a.Short_())(a))
}
func (a *NgingUserU2f) CPAFrom(source factory.Model) factory.Model {
a.SetContext(source.Context())
a.Use(source.Trans())
a.SetNamer(source.Namer())
return a
}
func (a *NgingUserU2f) Get(mw func(db.Result) db.Result, args ...interface{}) (err error) {
base := a.base
if !a.base.Eventable() {
err = a.Param(mw, args...).SetRecv(a).One()
a.base = base
return
}
queryParam := a.Param(mw, args...).SetRecv(a)
if err = DBI.FireReading(a, queryParam); err != nil {
return
}
err = queryParam.One()
a.base = base
if err == nil {
err = DBI.FireReaded(a, queryParam)
}
return
}
func (a *NgingUserU2f) List(recv interface{}, mw func(db.Result) db.Result, page, size int, args ...interface{}) (func() int64, error) {
if recv == nil {
recv = a.InitObjects()
}
if !a.base.Eventable() {
return a.Param(mw, args...).SetPage(page).SetSize(size).SetRecv(recv).List()
}
queryParam := a.Param(mw, args...).SetPage(page).SetSize(size).SetRecv(recv)
if err := DBI.FireReading(a, queryParam); err != nil {
return nil, err
}
cnt, err := queryParam.List()
if err == nil {
switch v := recv.(type) {
case *[]*NgingUserU2f:
err = DBI.FireReaded(a, queryParam, Slice_NgingUserU2f(*v))
case []*NgingUserU2f:
err = DBI.FireReaded(a, queryParam, Slice_NgingUserU2f(v))
case factory.Ranger:
err = DBI.FireReaded(a, queryParam, v)
}
}
return cnt, err
}
func (a *NgingUserU2f) GroupBy(keyField string, inputRows ...[]*NgingUserU2f) map[string][]*NgingUserU2f {
var rows Slice_NgingUserU2f
if len(inputRows) > 0 {
rows = Slice_NgingUserU2f(inputRows[0])
} else {
rows = Slice_NgingUserU2f(a.Objects())
}
return rows.GroupBy(keyField)
}
func (a *NgingUserU2f) KeyBy(keyField string, inputRows ...[]*NgingUserU2f) map[string]*NgingUserU2f {
var rows Slice_NgingUserU2f
if len(inputRows) > 0 {
rows = Slice_NgingUserU2f(inputRows[0])
} else {
rows = Slice_NgingUserU2f(a.Objects())
}
return rows.KeyBy(keyField)
}
func (a *NgingUserU2f) AsKV(keyField string, valueField string, inputRows ...[]*NgingUserU2f) param.Store {
var rows Slice_NgingUserU2f
if len(inputRows) > 0 {
rows = Slice_NgingUserU2f(inputRows[0])
} else {
rows = Slice_NgingUserU2f(a.Objects())
}
return rows.AsKV(keyField, valueField)
}
func (a *NgingUserU2f) ListByOffset(recv interface{}, mw func(db.Result) db.Result, offset, size int, args ...interface{}) (func() int64, error) {
if recv == nil {
recv = a.InitObjects()
}
if !a.base.Eventable() {
return a.Param(mw, args...).SetOffset(offset).SetSize(size).SetRecv(recv).List()
}
queryParam := a.Param(mw, args...).SetOffset(offset).SetSize(size).SetRecv(recv)
if err := DBI.FireReading(a, queryParam); err != nil {
return nil, err
}
cnt, err := queryParam.List()
if err == nil {
switch v := recv.(type) {
case *[]*NgingUserU2f:
err = DBI.FireReaded(a, queryParam, Slice_NgingUserU2f(*v))
case []*NgingUserU2f:
err = DBI.FireReaded(a, queryParam, Slice_NgingUserU2f(v))
case factory.Ranger:
err = DBI.FireReaded(a, queryParam, v)
}
}
return cnt, err
}
func (a *NgingUserU2f) Add() (pk interface{}, err error) {
a.Created = uint(time.Now().Unix())
a.Id = 0
if a.base.Eventable() {
err = DBI.Fire("creating", a, nil)
if err != nil {
return
}
}
pk, err = a.Param(nil).SetSend(a).Insert()
if err == nil && pk != nil {
if v, y := pk.(uint64); y {
a.Id = v
} else if v, y := pk.(int64); y {
a.Id = uint64(v)
}
}
if err == nil && a.base.Eventable() {
err = DBI.Fire("created", a, nil)
}
return
}
func (a *NgingUserU2f) Edit(mw func(db.Result) db.Result, args ...interface{}) (err error) {
if !a.base.Eventable() {
return a.Param(mw, args...).SetSend(a).Update()
}
if err = DBI.Fire("updating", a, mw, args...); err != nil {
return
}
if err = a.Param(mw, args...).SetSend(a).Update(); err != nil {
return
}
return DBI.Fire("updated", a, mw, args...)
}
func (a *NgingUserU2f) SetField(mw func(db.Result) db.Result, field string, value interface{}, args ...interface{}) (err error) {
return a.SetFields(mw, map[string]interface{}{
field: value,
}, args...)
}
func (a *NgingUserU2f) SetFields(mw func(db.Result) db.Result, kvset map[string]interface{}, args ...interface{}) (err error) {
if !a.base.Eventable() {
return a.Param(mw, args...).SetSend(kvset).Update()
}
m := *a
m.FromRow(kvset)
var editColumns []string
for column := range kvset {
editColumns = append(editColumns, column)
}
if err = DBI.FireUpdate("updating", &m, editColumns, mw, args...); err != nil {
return
}
if err = a.Param(mw, args...).SetSend(kvset).Update(); err != nil {
return
}
return DBI.FireUpdate("updated", &m, editColumns, mw, args...)
}
func (a *NgingUserU2f) Upsert(mw func(db.Result) db.Result, args ...interface{}) (pk interface{}, err error) {
pk, err = a.Param(mw, args...).SetSend(a).Upsert(func() error {
if !a.base.Eventable() {
return nil
}
return DBI.Fire("updating", a, mw, args...)
}, func() error {
a.Created = uint(time.Now().Unix())
a.Id = 0
if !a.base.Eventable() {
return nil
}
return DBI.Fire("creating", a, nil)
})
if err == nil && pk != nil {
if v, y := pk.(uint64); y {
a.Id = v
} else if v, y := pk.(int64); y {
a.Id = uint64(v)
}
}
if err == nil && a.base.Eventable() {
if pk == nil {
err = DBI.Fire("updated", a, mw, args...)
} else {
err = DBI.Fire("created", a, nil)
}
}
return
}
func (a *NgingUserU2f) Delete(mw func(db.Result) db.Result, args ...interface{}) (err error) {
if !a.base.Eventable() {
return a.Param(mw, args...).Delete()
}
if err = DBI.Fire("deleting", a, mw, args...); err != nil {
return
}
if err = a.Param(mw, args...).Delete(); err != nil {
return
}
return DBI.Fire("deleted", a, mw, args...)
}
func (a *NgingUserU2f) Count(mw func(db.Result) db.Result, args ...interface{}) (int64, error) {
return a.Param(mw, args...).Count()
}
func (a *NgingUserU2f) Exists(mw func(db.Result) db.Result, args ...interface{}) (bool, error) {
return a.Param(mw, args...).Exists()
}
func (a *NgingUserU2f) Reset() *NgingUserU2f {
a.Id = 0
a.Uid = 0
a.Token = ``
a.Type = ``
a.Extra = ``
a.Created = 0
return a
}
func (a *NgingUserU2f) AsMap(onlyFields ...string) param.Store {
r := param.Store{}
if len(onlyFields) == 0 {
r["Id"] = a.Id
r["Uid"] = a.Uid
r["Token"] = a.Token
r["Type"] = a.Type
r["Extra"] = a.Extra
r["Created"] = a.Created
return r
}
for _, field := range onlyFields {
switch field {
case "Id":
r["Id"] = a.Id
case "Uid":
r["Uid"] = a.Uid
case "Token":
r["Token"] = a.Token
case "Type":
r["Type"] = a.Type
case "Extra":
r["Extra"] = a.Extra
case "Created":
r["Created"] = a.Created
}
}
return r
}
func (a *NgingUserU2f) FromRow(row map[string]interface{}) {
for key, value := range row {
switch key {
case "id":
a.Id = param.AsUint64(value)
case "uid":
a.Uid = param.AsUint(value)
case "token":
a.Token = param.AsString(value)
case "type":
a.Type = param.AsString(value)
case "extra":
a.Extra = param.AsString(value)
case "created":
a.Created = param.AsUint(value)
}
}
}
func (a *NgingUserU2f) Set(key interface{}, value ...interface{}) {
switch k := key.(type) {
case map[string]interface{}:
for kk, vv := range k {
a.Set(kk, vv)
}
default:
var (
kk string
vv interface{}
)
if k, y := key.(string); y {
kk = k
} else {
kk = fmt.Sprint(key)
}
if len(value) > 0 {
vv = value[0]
}
switch kk {
case "Id":
a.Id = param.AsUint64(vv)
case "Uid":
a.Uid = param.AsUint(vv)
case "Token":
a.Token = param.AsString(vv)
case "Type":
a.Type = param.AsString(vv)
case "Extra":
a.Extra = param.AsString(vv)
case "Created":
a.Created = param.AsUint(vv)
}
}
}
func (a *NgingUserU2f) AsRow(onlyFields ...string) param.Store {
r := param.Store{}
if len(onlyFields) == 0 {
r["id"] = a.Id
r["uid"] = a.Uid
r["token"] = a.Token
r["type"] = a.Type
r["extra"] = a.Extra
r["created"] = a.Created
return r
}
for _, field := range onlyFields {
switch field {
case "id":
r["id"] = a.Id
case "uid":
r["uid"] = a.Uid
case "token":
r["token"] = a.Token
case "type":
r["type"] = a.Type
case "extra":
r["extra"] = a.Extra
case "created":
r["created"] = a.Created
}
}
return r
}
func (a *NgingUserU2f) BatchValidate(kvset map[string]interface{}) error {
if kvset == nil {
kvset = a.AsRow()
}
return DBI.Fields.BatchValidate(a.Short_(), kvset)
}
func (a *NgingUserU2f) Validate(field string, value interface{}) error {
return DBI.Fields.Validate(a.Short_(), field, value)
}