-
Notifications
You must be signed in to change notification settings - Fork 8
/
eval_func.go
245 lines (217 loc) · 7.5 KB
/
eval_func.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
/*
Copyright 2023 eatmoreapple
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 juice
import (
"errors"
"fmt"
"reflect"
"strings"
)
// return the length of the string or array
func length(v any) (int, error) {
switch t := v.(type) {
case nil:
return 0, nil
case string:
return len(t), nil
default:
rv := reflect.Indirect(reflect.ValueOf(v))
switch rv.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan:
return rv.Len(), nil
}
}
return 0, errors.New("length: invalid argument type")
}
// strSub returns a substring of the string.
// The first parameter is the string to be processed.
// The second parameter is the start position of the substring.
// The third parameter is the length of the substring.
func strSub(str string, start, count int) (string, error) {
if start < 0 {
start = len(str) + start
}
if start < 0 {
start = 0
}
if start > len(str) {
start = len(str)
}
if count < 0 {
count = len(str) + count
}
if count < 0 {
count = 0
}
if start+count > len(str) {
count = len(str) - start
}
return str[start : start+count], nil
}
// strJoin joins the elements of the array into a string.
// The first parameter is the array to be processed.
// The second parameter is the separator.
// Returns a string.
func strJoin(v any, sep string) (string, error) {
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Array, reflect.Slice:
if rv.Type().Elem().Kind() == reflect.String {
list := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
list = append(list, rv.Index(i).String())
}
return strings.Join(list, sep), nil
}
}
return "", errors.New("join: invalid argument type")
}
// contains returns true if the value is in the array or string.
func contains(s any, v any) (bool, error) {
switch t := s.(type) {
case string:
value, ok := v.(string)
if !ok {
value = fmt.Sprintf("%v", v)
}
return strings.Contains(t, value), nil
default:
rv := reflect.Indirect(reflect.ValueOf(s))
switch rv.Kind() {
case reflect.Array, reflect.Slice, reflect.Map:
for i := 0; i < rv.Len(); i++ {
if rv.Index(i).Interface() == v {
return true, nil
}
}
return false, nil
}
}
return false, errors.New("contains: invalid argument type")
}
// slice returns a slice of the array or string.
func slice(v any, start, count int) ([]any, error) {
rv := reflect.Indirect(reflect.ValueOf(v))
switch rv.Kind() {
case reflect.Array, reflect.Slice:
rt := rv.Slice(start, start+count)
var ret = make([]any, 0, rt.Len())
for i := 0; i < rt.Len(); i++ {
ret = append(ret, rt.Index(i).Interface())
}
return ret, nil
}
return nil, errors.New("slice: invalid argument type")
}
// lower returns a copy of the string s with all Unicode letters mapped to their lower case.
func lower(text string) (string, error) {
return strings.ToLower(text), nil
}
// upper returns a copy of the string s with all Unicode letters mapped to their upper case.
func upper(text string) (string, error) {
return strings.ToUpper(text), nil
}
// trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
func trim(text, cutest string) (string, error) {
return strings.Trim(text, cutest), nil
}
// trimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
func trimLeft(text, cutest string) (string, error) {
return strings.TrimLeft(text, cutest), nil
}
// trimRight returns a slice of the string s with all trailing Unicode code points contained in cutset removed.
func trimRight(text, cutest string) (string, error) {
return strings.TrimRight(text, cutest), nil
}
// replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new.
// If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
func replace(text, old, new string, n int) (string, error) {
return strings.Replace(text, old, new, n), nil
}
// replaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new.
// If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
func replaceAll(text, old, new string) (string, error) {
return strings.ReplaceAll(text, old, new), nil
}
// split returns a slice of strings after splitting the string s at each instance of sep.
func split(text, sep string) ([]string, error) {
return strings.Split(text, sep), nil
}
// splitN returns a slice of strings after splitting the string s at each instance of sep, at most n times.
// If n == 0, SplitN returns an unlimited number of substrings.
// If n < 0, SplitN splits after each instance of sep.
func splitN(text, sep string, n int) ([]string, error) {
return strings.SplitN(text, sep, n), nil
}
// splitAfter returns a slice of strings after splitting the string s after each instance of sep.
func splitAfter(text, sep string) ([]string, error) {
return strings.SplitAfter(text, sep), nil
}
// errType is the reflect.Type of error.
var errType = reflect.TypeOf((*error)(nil)).Elem()
// RegisterEvalFunc registers a function for eval.
// The function must be a function with one return value.
// And Allowed to overwrite the built-in function.
func RegisterEvalFunc(name string, v any) error {
rv := reflect.Indirect(reflect.ValueOf(v))
if rv.Kind() != reflect.Func {
return errors.New("RegisterEvalFunc: v must be a function type")
}
if rv.Type().NumOut() != 2 {
return errors.New("RegisterEvalFunc: v must be a function with two return value")
}
// if last return value is error
if !rv.Type().Out(rv.Type().NumOut() - 1).Implements(errType) {
return errors.New("RegisterEvalFunc: v must be a function with an error return value")
}
builtins[name] = rv
return nil
}
// MustRegisterEvalFunc registers a function for eval.
// If an error occurs, it will panic.
func MustRegisterEvalFunc(name string, v any) {
err := RegisterEvalFunc(name, v)
if err != nil {
panic(err)
}
}
// builtins is a map of built-in functions.
var builtins = map[string]reflect.Value{}
var (
// trueValue is the reflect.Value of true.
trueValue = reflect.ValueOf(true)
// falseValue is the reflect.Value of false.
falseValue = reflect.ValueOf(false)
// nilValue is the reflect.Value of nil.
nilValue = reflect.ValueOf(nil)
)
func init() {
builtins["true"] = trueValue
builtins["false"] = falseValue
builtins["nil"] = nilValue
MustRegisterEvalFunc("len", length)
MustRegisterEvalFunc("substr", strSub)
MustRegisterEvalFunc("join", strJoin)
MustRegisterEvalFunc("contains", contains)
MustRegisterEvalFunc("slice", slice)
MustRegisterEvalFunc("lower", lower)
MustRegisterEvalFunc("upper", upper)
MustRegisterEvalFunc("trim", trim)
MustRegisterEvalFunc("trimLeft", trimLeft)
MustRegisterEvalFunc("trimRight", trimRight)
MustRegisterEvalFunc("replace", replace)
MustRegisterEvalFunc("replaceAll", replaceAll)
MustRegisterEvalFunc("split", split)
MustRegisterEvalFunc("splitN", splitN)
MustRegisterEvalFunc("splitAfter", splitAfter)
}