-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
220 lines (187 loc) · 4.58 KB
/
function.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
package eval
import (
"bytes"
"go/ast"
"strings"
"time"
)
func init() {
funcMap["left_pad"] = leftPad
funcMap["right_pad"] = rightPad
funcMap["date_parse"] = dateParse
funcMap["sub_str"] = subStr
funcMap["contact"] = contact
funcMap["index_of"] = indexOf
}
func leftPad(args []ast.Expr, data map[string]any) (any, error) {
str, padStr, toLength, err := _padParam(args, data)
if err != nil {
return nil, err
}
if len(str) >= toLength {
return str, nil
}
needPad := toLength - len(str)
var buffer bytes.Buffer
for i := 0; i < needPad; i++ {
buffer.WriteString(padStr)
}
buffer.WriteString(str)
return buffer.String(), nil
}
func rightPad(args []ast.Expr, data map[string]any) (any, error) {
str, padStr, toLength, err := _padParam(args, data)
if err != nil {
return nil, err
}
if len(str) >= toLength {
return str, nil
}
needPad := toLength - len(str)
var buffer bytes.Buffer
buffer.WriteString(str)
for i := 0; i < needPad; i++ {
buffer.WriteString(padStr)
}
return buffer.String(), nil
}
func subStr(args []ast.Expr, data map[string]any) (any, error) {
if len(args) != 3 {
return nil, illegalParams
}
strArg, err := eval(args[0], data)
if err != nil {
return nil, err
}
startArg, err := eval(args[1], data)
if err != nil {
return nil, err
}
endArg, err := eval(args[2], data)
if err != nil {
return nil, err
}
str := String(strArg)
start, err := Int(startArg)
if err != nil {
return nil, err
}
end, err := Int(endArg)
if err != nil {
return nil, err
}
if start > end || start > len(str) || end > len(str) {
return nil, illegalParams
}
return str[start:end], nil
}
func contact(args []ast.Expr, data map[string]any) (any, error) {
ss := make([]string, 0, len(args))
for _, arg := range args {
strArg, err := eval(arg, data)
if err != nil {
return nil, err
}
ss = append(ss, String(strArg))
}
return strings.Join(ss, ""), nil
}
func indexOf(args []ast.Expr, data map[string]any) (any, error) {
if len(args) != 2 {
return nil, illegalParams
}
strArg, err := eval(args[0], data)
if err != nil {
return nil, err
}
subStrArg, err := eval(args[1], data)
if err != nil {
return nil, err
}
str := String(strArg)
sub := String(subStrArg)
return strings.Index(str, sub), nil
}
func dateParse(args []ast.Expr, data map[string]any) (any, error) {
if len(args) != 2 {
return nil, illegalParams
}
dateArg, err := eval(args[0], data)
if err != nil {
return nil, err
}
formatArg, err := eval(args[1], data)
if err != nil {
return nil, err
}
date := String(dateArg)
format := String(formatArg) // YYYY MM DD 格式
return _parseDate(format, date)
}
func _padParam(args []ast.Expr, data map[string]any) (string, string, int, error) {
if len(args) != 3 {
return "", "", 0, illegalParams
}
strArg, err := eval(args[0], data)
if err != nil {
return "", "", 0, err
}
padStrArg, err := eval(args[1], data)
if err != nil {
return "", "", 0, err
}
toLengthArg, err := eval(args[2], data)
if err != nil {
return "", "", 0, err
}
str := String(strArg)
padStr := String(padStrArg)
toLength, err := Int(toLengthArg)
return str, padStr, toLength, err
}
func _parseDate(format, date string) (time.Time, error) {
if strings.Contains(format, "YYYY") {
format = strings.ReplaceAll(format, "YYYY", "2006")
}
if strings.Contains(format, "yyyy") {
format = strings.ReplaceAll(format, "yyyy", "2006")
}
if strings.Contains(format, "MM") {
format = strings.ReplaceAll(format, "MM", "01")
}
if strings.Contains(format, "DD") {
format = strings.ReplaceAll(format, "DD", "02")
}
if strings.Contains(format, "dd") {
format = strings.ReplaceAll(format, "dd", "02")
}
if strings.Contains(format, "HH") {
format = strings.ReplaceAll(format, "HH", "15")
}
if strings.Contains(format, "hh") {
format = strings.ReplaceAll(format, "hh", "15")
}
if strings.Contains(format, "mm") {
format = strings.ReplaceAll(format, "mm", "04")
}
if strings.Contains(format, "ss") {
format = strings.ReplaceAll(format, "ss", "05")
}
// 纳秒
format, date = _nsFormatStyle(format, date, "SSSSSSSSS", "000000000")
// 微秒
format, date = _nsFormatStyle(format, date, "SSSSSS", "000000")
// 毫秒
format, date = _nsFormatStyle(format, date, "SSS", "000")
return time.Parse(format, date)
}
func _nsFormatStyle(format, date string, old, new string) (string, string) {
if index := strings.Index(format, old); index > 0 {
format = strings.ReplaceAll(format, old, new)
if format[index-1:index] != "." {
format = format[:index] + "." + format[index:]
date = date[:index] + "." + date[index:]
}
}
return format, date
}