-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmacro.go
385 lines (340 loc) · 10.5 KB
/
macro.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
package macro
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"unicode"
)
type (
// ParamEvaluator is the signature for param type evaluator.
// It accepts the param's value as string and returns
// the <T> value (which its type is used for the input argument of the parameter functions, if any)
// and a true value for passed, otherwise nil and false should be returned.
ParamEvaluator func(paramValue string) (interface{}, bool)
)
var goodEvaluatorFuncs = []reflect.Type{
reflect.TypeOf(func(string) (interface{}, bool) { return nil, false }),
reflect.TypeOf(ParamEvaluator(func(string) (interface{}, bool) { return nil, false })),
}
func goodParamFunc(typ reflect.Type) bool {
if typ.Kind() == reflect.Func { // it should be a func which returns a func (see below check).
if typ.NumOut() == 1 {
typOut := typ.Out(0)
if typOut.Kind() != reflect.Func {
return false
}
if typOut.NumOut() == 2 { // if it's a type of EvaluatorFunc, used for param evaluator.
for _, fType := range goodEvaluatorFuncs {
if typOut == fType {
return true
}
}
return false
}
if typOut.NumIn() == 1 && typOut.NumOut() == 1 { // if it's a type of func(paramValue [int,string...]) bool, used for param funcs.
return typOut.Out(0).Kind() == reflect.Bool
}
}
}
return false
}
// Regexp accepts a regexp "expr" expression
// and returns its MatchString.
// The regexp is compiled before return.
//
// Returns a not-nil error on regexp compile failure.
func Regexp(expr string) (func(string) bool, error) {
if expr == "" {
return nil, fmt.Errorf("empty regex expression")
}
// add the last $ if missing (and not wildcard(?))
if i := expr[len(expr)-1]; i != '$' && i != '*' {
expr += "$"
}
r, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
return r.MatchString, nil
}
// MustRegexp same as Regexp
// but it panics on the "expr" parse failure.
func MustRegexp(expr string) func(string) bool {
r, err := Regexp(expr)
if err != nil {
panic(err)
}
return r
}
// goodParamFuncName reports whether the function name is a valid identifier.
func goodParamFuncName(name string) bool {
if name == "" {
return false
}
// valid names are only letters and _
for _, r := range name {
switch {
case r == '_':
case !unicode.IsLetter(r):
return false
}
}
return true
}
// the convertBuilderFunc return value is generating at boot time.
// convertFunc converts an interface to a valid full param function.
func convertBuilderFunc(fn interface{}) ParamFuncBuilder {
typFn := reflect.TypeOf(fn)
if !goodParamFunc(typFn) {
// it's not a function which returns a function,
// it's not a a func(compileArgs) func(requestDynamicParamValue) bool
// but it's a func(requestDynamicParamValue) bool, such as regexp.Compile.MatchString
if typFn.NumIn() == 1 && typFn.In(0).Kind() == reflect.String && typFn.NumOut() == 1 && typFn.Out(0).Kind() == reflect.Bool {
fnV := reflect.ValueOf(fn)
// let's convert it to a ParamFuncBuilder which its combile route arguments are empty and not used at all.
// the below return function runs on each route that this param type function is used in order to validate the function,
// if that param type function is used wrongly it will be panic like the rest,
// indeed the only check is the len of arguments not > 0, no types of values or conversions,
// so we return it as soon as possible.
return func(args []string) reflect.Value {
if n := len(args); n > 0 {
panic(fmt.Sprintf("%T does not allow any input arguments from route but got [len=%d,values=%s]", fn, n, strings.Join(args, ", ")))
}
return fnV
}
}
return nil
}
numFields := typFn.NumIn()
panicIfErr := func(i int, err error) {
if err != nil {
panic(fmt.Sprintf("on field index: %d: %v", i, err))
}
}
return func(args []string) reflect.Value {
if len(args) != numFields {
// no variadics support, for now.
panic(fmt.Sprintf("args(len=%d) should be the same len as numFields(%d) for: %s", len(args), numFields, typFn))
}
var argValues []reflect.Value
for i := 0; i < numFields; i++ {
field := typFn.In(i)
arg := args[i]
// try to convert the string literal as we get it from the parser.
var (
val interface{}
)
// try to get the value based on the expected type.
switch field.Kind() {
case reflect.Int:
v, err := strconv.Atoi(arg)
panicIfErr(i, err)
val = v
case reflect.Int8:
v, err := strconv.ParseInt(arg, 10, 8)
panicIfErr(i, err)
val = int8(v)
case reflect.Int16:
v, err := strconv.ParseInt(arg, 10, 16)
panicIfErr(i, err)
val = int16(v)
case reflect.Int32:
v, err := strconv.ParseInt(arg, 10, 32)
panicIfErr(i, err)
val = int32(v)
case reflect.Int64:
v, err := strconv.ParseInt(arg, 10, 64)
panicIfErr(i, err)
val = v
case reflect.Uint:
v, err := strconv.ParseUint(arg, 10, strconv.IntSize)
panicIfErr(i, err)
val = uint(v)
case reflect.Uint8:
v, err := strconv.ParseUint(arg, 10, 8)
panicIfErr(i, err)
val = uint8(v)
case reflect.Uint16:
v, err := strconv.ParseUint(arg, 10, 16)
panicIfErr(i, err)
val = uint16(v)
case reflect.Uint32:
v, err := strconv.ParseUint(arg, 10, 32)
panicIfErr(i, err)
val = uint32(v)
case reflect.Uint64:
v, err := strconv.ParseUint(arg, 10, 64)
panicIfErr(i, err)
val = v
case reflect.Float32:
v, err := strconv.ParseFloat(arg, 32)
panicIfErr(i, err)
val = float32(v)
case reflect.Float64:
v, err := strconv.ParseFloat(arg, 64)
panicIfErr(i, err)
val = v
case reflect.Bool:
v, err := strconv.ParseBool(arg)
panicIfErr(i, err)
val = v
case reflect.Slice:
if len(arg) > 1 {
if arg[0] == '[' && arg[len(arg)-1] == ']' {
// it is a single argument but as slice.
val = strings.Split(arg[1:len(arg)-1], ",") // only string slices.
}
}
default:
val = arg
}
argValue := reflect.ValueOf(val)
if expected, got := field.Kind(), argValue.Kind(); expected != got {
panic(fmt.Sprintf("func's input arguments should have the same type: [%d] expected %s but got %s", i, expected, got))
}
argValues = append(argValues, argValue)
}
evalFn := reflect.ValueOf(fn).Call(argValues)[0]
// var evaluator EvaluatorFunc
// // check for typed and not typed
// if _v, ok := evalFn.(EvaluatorFunc); ok {
// evaluator = _v
// } else if _v, ok = evalFn.(func(string) bool); ok {
// evaluator = _v
// }
// return func(paramValue interface{}) bool {
// return evaluator(paramValue)
// }
return evalFn
}
}
type (
// Macro represents the parsed macro,
// which holds
// the evaluator (param type's evaluator + param functions evaluators)
// and its param functions.
//
// Any type contains its own macro
// instance, so an String type
// contains its type evaluator
// which is the "Evaluator" field
// and it can register param functions
// to that macro which maps to a parameter type.
Macro struct {
indent string
alias string
master bool
trailing bool
Evaluator ParamEvaluator
handleError interface{}
funcs []ParamFunc
goType reflect.Type
}
// ParamFuncBuilder is a func
// which accepts a param function's arguments (values)
// and returns a function as value, its job
// is to make the macros to be registered
// by user at the most generic possible way.
ParamFuncBuilder func([]string) reflect.Value // the func(<T>) bool
// ParamFunc represents the parsed
// parameter function, it holds
// the parameter's name
// and the function which will build
// the evaluator func.
ParamFunc struct {
Name string
Func ParamFuncBuilder
}
)
// NewMacro creates and returns a Macro that can be used as a registry for
// a new customized parameter type and its functions.
func NewMacro(indent, alias string, valueType any, master, trailing bool, evaluator ParamEvaluator) *Macro {
return &Macro{
indent: indent,
alias: alias,
master: master,
trailing: trailing,
goType: reflect.TypeOf(valueType),
Evaluator: evaluator,
}
}
// Indent returns the name of the parameter type.
func (m *Macro) Indent() string {
return m.indent
}
// Alias returns the alias of the parameter type, if any.
func (m *Macro) Alias() string {
return m.alias
}
// Master returns true if that macro's parameter type is the
// default one if not :type is followed by a parameter type inside the route path.
func (m *Macro) Master() bool {
return m.master
}
// Trailing returns true if that macro's parameter type
// is wildcard and can accept one or more path segments as one parameter value.
// A wildcard should be registered in the last path segment only.
func (m *Macro) Trailing() bool {
return m.trailing
}
// GoType returns the type of the parameter type's evaluator.
// string if it's a string evaluator, int if it's an int evaluator etc.
func (m *Macro) GoType() reflect.Type {
return m.goType
}
// HandleError registers a handler which will be executed
// when a parameter evaluator returns false and a non nil value which is a type of `error`.
// The "fnHandler" value MUST BE a type of `func(iris.Context, paramIndex int, err error)`,
// otherwise the program will receive a panic before server startup.
// The status code of the ErrCode (`else` literal) is set
// before the error handler but it can be modified inside the handler itself.
func (m *Macro) HandleError(fnHandler interface{}) *Macro { // See handler.MakeFilter.
m.handleError = fnHandler
return m
}
// func (m *Macro) SetParamResolver(fn func(memstore.Entry) interface{}) *Macro {
// m.ParamResolver = fn
// return m
// }
// RegisterFunc registers a parameter function
// to that macro.
// Accepts the func name ("range")
// and the function body, which should return an EvaluatorFunc
// a bool (it will be converted to EvaluatorFunc later on),
// i.e RegisterFunc("min", func(minValue int) func(paramValue string) bool){})
func (m *Macro) RegisterFunc(funcName string, fn interface{}) *Macro {
fullFn := convertBuilderFunc(fn)
// if it's not valid then not register it at all.
if fullFn != nil {
m.registerFunc(funcName, fullFn)
}
return m
}
func (m *Macro) registerFunc(funcName string, fullFn ParamFuncBuilder) {
if !goodParamFuncName(funcName) {
return
}
for _, fn := range m.funcs {
if fn.Name == funcName {
fn.Func = fullFn
return
}
}
m.funcs = append(m.funcs, ParamFunc{
Name: funcName,
Func: fullFn,
})
}
func (m *Macro) getFunc(funcName string) ParamFuncBuilder {
for _, fn := range m.funcs {
if fn.Name == funcName {
if fn.Func == nil {
continue
}
return fn.Func
}
}
return nil
}