forked from bytedance/go-tagexpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
129 lines (111 loc) · 3.2 KB
/
handler.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
package tagexpr
import "reflect"
// FieldHandler field handler
type FieldHandler struct {
selector string
field *fieldVM
expr *TagExpr
}
func newFieldHandler(expr *TagExpr, fieldSelector string, field *fieldVM) *FieldHandler {
return &FieldHandler{
selector: fieldSelector,
field: field,
expr: expr,
}
}
// StringSelector returns the field selector of string type.
func (f *FieldHandler) StringSelector() string {
return f.selector
}
// FieldSelector returns the field selector of FieldSelector type.
func (f *FieldHandler) FieldSelector() FieldSelector {
return FieldSelector(f.selector)
}
// Value returns the field value.
// NOTE:
// If initZero==true, initialize nil pointer to zero value
func (f *FieldHandler) Value(initZero bool) reflect.Value {
return f.field.reflectValueGetter(f.expr.ptr, initZero)
}
// EvalFuncs returns the tag expression eval functions.
func (f *FieldHandler) EvalFuncs() map[ExprSelector]func() interface{} {
targetTagExpr, _ := f.expr.checkout(f.selector)
evals := make(map[ExprSelector]func() interface{}, len(f.field.exprs))
for k, v := range f.field.exprs {
expr := v
exprSelector := ExprSelector(k)
evals[exprSelector] = func() interface{} {
return expr.run(exprSelector.Name(), targetTagExpr)
}
}
return evals
}
// StructField returns the field StructField object.
func (f *FieldHandler) StructField() reflect.StructField {
return f.field.structField
}
// ExprHandler expr handler
type ExprHandler struct {
base string
path string
selector string
expr *TagExpr
targetExpr *TagExpr
}
func newExprHandler(te, tte *TagExpr, base, es string) *ExprHandler {
return &ExprHandler{
base: base,
selector: es,
expr: te,
targetExpr: tte,
}
}
// TagExpr returns the *TagExpr.
func (e *ExprHandler) TagExpr() *TagExpr {
return e.expr
}
// StringSelector returns the expression selector of string type.
func (e *ExprHandler) StringSelector() string {
return e.selector
}
// ExprSelector returns the expression selector of ExprSelector type.
func (e *ExprHandler) ExprSelector() ExprSelector {
return ExprSelector(e.selector)
}
// Path returns the path description of the expression.
func (e *ExprHandler) Path() string {
if e.path == "" {
if e.targetExpr.path == "" {
e.path = e.selector
} else {
e.path = e.targetExpr.path + FieldSeparator + e.selector
}
}
return e.path
}
// Eval evaluate the value of the struct tag expression.
// NOTE:
// result types: float64, string, bool, nil
func (e *ExprHandler) Eval() interface{} {
return e.expr.s.exprs[e.selector].run(e.base, e.targetExpr)
}
// EvalFloat evaluates the value of the struct tag expression.
// NOTE:
// If the expression value type is not float64, return 0.
func (e *ExprHandler) EvalFloat() float64 {
r, _ := e.Eval().(float64)
return r
}
// EvalString evaluates the value of the struct tag expression.
// NOTE:
// If the expression value type is not string, return "".
func (e *ExprHandler) EvalString() string {
r, _ := e.Eval().(string)
return r
}
// EvalBool evaluates the value of the struct tag expression.
// NOTE:
// If the expression value is not 0, '' or nil, return true.
func (e *ExprHandler) EvalBool() bool {
return FakeBool(e.Eval())
}