-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispose.go
94 lines (84 loc) · 1.83 KB
/
dispose.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 model
import (
"errors"
"strings"
"github.com/sohaha/zlsgo/ztype"
)
type beforeProcess func(interface{}) (string, error)
func (m *Model) GetBeforeProcess(p []string) (fn []beforeProcess, err error) {
for _, v := range p {
switch strings.ToLower(v) {
default:
v := strings.SplitN(v, "|", 2)
if len(v) == 2 {
switch v[0] {
case "date":
if v[1] != "-" {
fn = append(fn, dateMarshalProcess(v[1]))
}
continue
}
}
return nil, errors.New("before name not found")
case "bool":
fn = append(fn, boolMarshalProcess)
case "json":
fn = append(fn, jsonMarshalProcess(false))
case "jsons":
fn = append(fn, jsonMarshalProcess(true))
}
}
return
}
func (m *Model) valuesBeforeProcess(data ztype.Map) (ztype.Map, error) {
var err error
for k := range m.cryptKeys {
if _, ok := data[k]; ok {
data[k], err = m.cryptKeys[k](data.Get(k).String())
if err != nil {
return nil, err
}
}
}
for name, fns := range m.beforeProcess {
val := data.Get(name)
if !val.Exists() {
continue
}
v := val.Value()
for _, fn := range fns {
v, err = fn(v)
if err != nil {
return data, err
}
}
_ = data.Set(name, v)
}
return data, nil
}
type afterProcess func(string) (interface{}, error)
func (m *Model) GetAfterProcess(p []string) (fn []afterProcess, err error) {
for _, v := range p {
switch strings.ToLower(v) {
default:
v := strings.SplitN(v, "|", 2)
if len(v) == 2 {
switch v[0] {
case "date":
if v[1] != "-" {
fn = append(fn, dateUnmarshalProcess(v[1]))
}
continue
}
}
return nil, errors.New("after name not found")
case "json":
fn = append(fn, jsonUnmarshalProcess(false))
case "bool":
fn = append(fn, boolUnmarshalProcess)
case "jsons":
fn = append(fn, jsonUnmarshalProcess(true))
}
}
return
}