15
15
package tagexpr
16
16
17
17
import (
18
+ "context"
18
19
"fmt"
19
20
"reflect"
20
21
"regexp"
@@ -44,44 +45,54 @@ func RegFunc(funcName string, fn func(...interface{}) interface{}, force ...bool
44
45
return nil
45
46
}
46
47
47
- func newFunc ( funcName string , fn func ( ... interface {}) interface {}) func ( * Expr , * string ) ExprNode {
48
+ func ( p * Expr ) parseFuncSign ( funcName string , expr * string ) ( boolOpposite * bool , signOpposite * bool , args [] ExprNode , found bool ) {
48
49
prefix := funcName + "("
49
50
length := len (funcName )
50
- return func (p * Expr , expr * string ) ExprNode {
51
- last , boolOpposite := getBoolOpposite (expr )
52
- if ! strings .HasPrefix (last , prefix ) {
53
- return nil
51
+ last , boolOpposite , signOpposite := getBoolAndSignOpposite (expr )
52
+ if ! strings .HasPrefix (last , prefix ) {
53
+ return
54
+ }
55
+ * expr = last [length :]
56
+ lastStr := * expr
57
+ subExprNode := readPairedSymbol (expr , '(' , ')' )
58
+ if subExprNode == nil {
59
+ return
60
+ }
61
+ * subExprNode = "," + * subExprNode
62
+ for {
63
+ if strings .HasPrefix (* subExprNode , "," ) {
64
+ * subExprNode = (* subExprNode )[1 :]
65
+ operand := newGroupExprNode ()
66
+ _ , err := p .parseExprNode (trimLeftSpace (subExprNode ), operand )
67
+ if err != nil {
68
+ * expr = lastStr
69
+ return
70
+ }
71
+ sortPriority (operand .RightOperand ())
72
+ args = append (args , operand )
73
+ } else {
74
+ * expr = lastStr
75
+ return
76
+ }
77
+ trimLeftSpace (subExprNode )
78
+ if len (* subExprNode ) == 0 {
79
+ found = true
80
+ return
54
81
}
55
- * expr = last [length :]
56
- lastStr := * expr
57
- subExprNode := readPairedSymbol (expr , '(' , ')' )
58
- if subExprNode == nil {
82
+ }
83
+ }
84
+
85
+ func newFunc (funcName string , fn func (... interface {}) interface {}) func (* Expr , * string ) ExprNode {
86
+ return func (p * Expr , expr * string ) ExprNode {
87
+ boolOpposite , signOpposite , args , found := p .parseFuncSign (funcName , expr )
88
+ if ! found {
59
89
return nil
60
90
}
61
- f := & funcExprNode {
91
+ return & funcExprNode {
62
92
fn : fn ,
63
93
boolOpposite : boolOpposite ,
64
- }
65
- * subExprNode = "," + * subExprNode
66
- for {
67
- if strings .HasPrefix (* subExprNode , "," ) {
68
- * subExprNode = (* subExprNode )[1 :]
69
- operand := newGroupExprNode ()
70
- _ , err := p .parseExprNode (trimLeftSpace (subExprNode ), operand )
71
- if err != nil {
72
- * expr = lastStr
73
- return nil
74
- }
75
- sortPriority (operand .RightOperand ())
76
- f .args = append (f .args , operand )
77
- } else {
78
- * expr = lastStr
79
- return nil
80
- }
81
- trimLeftSpace (subExprNode )
82
- if len (* subExprNode ) == 0 {
83
- return f
84
- }
94
+ signOpposite : signOpposite ,
95
+ args : args ,
85
96
}
86
97
}
87
98
}
@@ -91,23 +102,25 @@ type funcExprNode struct {
91
102
args []ExprNode
92
103
fn func (... interface {}) interface {}
93
104
boolOpposite * bool
105
+ signOpposite * bool
94
106
}
95
107
96
- func (f * funcExprNode ) Run (currField string , tagExpr * TagExpr ) interface {} {
108
+ func (f * funcExprNode ) Run (ctx context. Context , currField string , tagExpr * TagExpr ) interface {} {
97
109
var args []interface {}
98
110
if n := len (f .args ); n > 0 {
99
111
args = make ([]interface {}, n )
100
112
for k , v := range f .args {
101
- args [k ] = v .Run (currField , tagExpr )
113
+ args [k ] = v .Run (ctx , currField , tagExpr )
102
114
}
103
115
}
104
- return realValue (f .fn (args ... ), f .boolOpposite )
116
+ return realValue (f .fn (args ... ), f .boolOpposite , f . signOpposite )
105
117
}
106
118
107
119
// --------------------------- Built-in function ---------------------------
108
120
func init () {
109
121
funcList ["regexp" ] = readRegexpFuncExprNode
110
122
funcList ["sprintf" ] = readSprintfFuncExprNode
123
+ funcList ["range" ] = readRangeFuncExprNode
111
124
err := RegFunc ("len" , func (args ... interface {}) (n interface {}) {
112
125
if len (args ) != 1 {
113
126
return 0
@@ -159,7 +172,7 @@ type regexpFuncExprNode struct {
159
172
}
160
173
161
174
func readRegexpFuncExprNode (p * Expr , expr * string ) ExprNode {
162
- last , boolOpposite := getBoolOpposite (expr )
175
+ last , boolOpposite , _ := getBoolAndSignOpposite (expr )
163
176
if ! strings .HasPrefix (last , "regexp(" ) {
164
177
return nil
165
178
}
@@ -207,8 +220,8 @@ func readRegexpFuncExprNode(p *Expr, expr *string) ExprNode {
207
220
return e
208
221
}
209
222
210
- func (re * regexpFuncExprNode ) Run (currField string , tagExpr * TagExpr ) interface {} {
211
- param := re .rightOperand .Run (currField , tagExpr )
223
+ func (re * regexpFuncExprNode ) Run (ctx context. Context , currField string , tagExpr * TagExpr ) interface {} {
224
+ param := re .rightOperand .Run (ctx , currField , tagExpr )
212
225
switch v := param .(type ) {
213
226
case string :
214
227
bol := re .re .MatchString (v )
@@ -276,12 +289,12 @@ func readSprintfFuncExprNode(p *Expr, expr *string) ExprNode {
276
289
}
277
290
}
278
291
279
- func (se * sprintfFuncExprNode ) Run (currField string , tagExpr * TagExpr ) interface {} {
292
+ func (se * sprintfFuncExprNode ) Run (ctx context. Context , currField string , tagExpr * TagExpr ) interface {} {
280
293
var args []interface {}
281
294
if n := len (se .args ); n > 0 {
282
295
args = make ([]interface {}, n )
283
296
for i , e := range se .args {
284
- args [i ] = e .Run (currField , tagExpr )
297
+ args [i ] = e .Run (ctx , currField , tagExpr )
285
298
}
286
299
}
287
300
return fmt .Sprintf (se .format , args ... )
0 commit comments