forked from bytedance/go-tagexpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_range.go
150 lines (137 loc) · 3.25 KB
/
spec_range.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
package tagexpr
import (
"context"
"reflect"
"regexp"
)
type rangeCtxKey string
const (
rangeKey rangeCtxKey = "#k"
rangeValue rangeCtxKey = "#v"
rangeLen rangeCtxKey = "##"
)
type rangeKvExprNode struct {
exprBackground
ctxKey rangeCtxKey
boolOpposite *bool
signOpposite *bool
}
func (re *rangeKvExprNode) String() string {
return string(re.ctxKey)
}
func (p *Expr) readRangeKvExprNode(expr *string) ExprNode {
name, boolOpposite, signOpposite, found := findRangeKv(expr)
if !found {
return nil
}
operand := &rangeKvExprNode{
ctxKey: rangeCtxKey(name),
boolOpposite: boolOpposite,
signOpposite: signOpposite,
}
// fmt.Printf("operand: %#v\n", operand)
return operand
}
var rangeKvRegexp = regexp.MustCompile(`^([\!\+\-]*)(#[kv#])([\)\[\],\+\-\*\/%><\|&!=\^ \t\\]|$)`)
func findRangeKv(expr *string) (name string, boolOpposite, signOpposite *bool, found bool) {
raw := *expr
a := rangeKvRegexp.FindAllStringSubmatch(raw, -1)
if len(a) != 1 {
return
}
r := a[0]
name = r[2]
*expr = (*expr)[len(a[0][0])-len(r[3]):]
prefix := r[1]
if len(prefix) == 0 {
found = true
return
}
_, boolOpposite, signOpposite = getBoolAndSignOpposite(&prefix)
found = true
return
}
func (re *rangeKvExprNode) Run(ctx context.Context, _ string, _ *TagExpr) interface{} {
var v interface{}
switch val := ctx.Value(re.ctxKey).(type) {
case reflect.Value:
if !val.IsValid() || !val.CanInterface() {
return nil
}
v = val.Interface()
default:
v = val
}
return realValue(v, re.boolOpposite, re.signOpposite)
}
type rangeFuncExprNode struct {
exprBackground
object ExprNode
elemExprNode ExprNode
boolOpposite *bool
signOpposite *bool
}
func (e *rangeFuncExprNode) String() string {
return "range()"
}
// range($, gt($v,10))
// range($, $v>10)
func readRangeFuncExprNode(p *Expr, expr *string) ExprNode {
boolOpposite, signOpposite, args, found := p.parseFuncSign("range", expr)
if !found {
return nil
}
if len(args) != 2 {
return nil
}
return &rangeFuncExprNode{
boolOpposite: boolOpposite,
signOpposite: signOpposite,
object: args[0],
elemExprNode: args[1],
}
}
func (e *rangeFuncExprNode) Run(ctx context.Context, currField string, tagExpr *TagExpr) interface{} {
var r []interface{}
obj := e.object.Run(ctx, currField, tagExpr)
// fmt.Printf("%v\n", obj)
objval := reflect.ValueOf(obj)
switch objval.Kind() {
case reflect.Array, reflect.Slice:
count := objval.Len()
r = make([]interface{}, count)
ctx = context.WithValue(ctx, rangeLen, count)
for i := 0; i < count; i++ {
// fmt.Printf("%#v, (%v)\n", e.elemExprNode, objval.Index(i))
r[i] = realValue(e.elemExprNode.Run(
context.WithValue(
context.WithValue(
ctx,
rangeKey, i,
),
rangeValue, objval.Index(i),
),
currField, tagExpr,
), e.boolOpposite, e.signOpposite)
}
case reflect.Map:
keys := objval.MapKeys()
count := len(keys)
r = make([]interface{}, count)
ctx = context.WithValue(ctx, rangeLen, count)
for i, key := range keys {
r[i] = realValue(e.elemExprNode.Run(
context.WithValue(
context.WithValue(
ctx,
rangeKey, key,
),
rangeValue, objval.MapIndex(key),
),
currField, tagExpr,
), e.boolOpposite, e.signOpposite)
}
default:
}
return r
}