-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.go
98 lines (80 loc) · 1.68 KB
/
expr.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
package loong
import (
"context"
"fmt"
"strings"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
)
type Evaluator interface {
Eval(ctx context.Context, el string, a any) (any, error)
}
type ActivationEvaluator interface {
Eval(ctx context.Context, el string) (any, error)
}
func eval[T any](ctx context.Context, ae ActivationEvaluator, el string) (val T, a any, err error) {
a, err = ae.Eval(ctx, el)
if err != nil {
return
}
var ok bool
if val, ok = a.(T); !ok {
err = fmt.Errorf("不能将 %T 转换为 %T", a, val)
}
return
}
func eval2[T any](ctx context.Context, e Evaluator, el string, env any) (val T, a any, err error) {
a, err = e.Eval(ctx, el, env)
if err != nil {
return
}
var ok bool
if val, ok = a.(T); !ok {
err = fmt.Errorf("不能将 %T 转换为 %T", a, val)
}
return
}
type ExprEval struct {
inst *vm.VM
}
func NewExprEval() *ExprEval {
return &ExprEval{
inst: &vm.VM{},
}
}
func (e *ExprEval) Eval(ctx context.Context, ex string, a any) (any, error) {
var (
el string
ok bool
)
if el, ok = Expr(ex); !ok {
return el, nil // 不是表达式,直接返回
}
program, err := expr.Compile(el)
if err != nil {
return nil, err
}
return e.inst.Run(program, a)
}
func Expr(s string) (string, bool) {
return fx(s)
}
func fx(s string) (string, bool) {
fx := strings.TrimSpace(s)
s, ok := strings.CutPrefix(fx, "=")
return s, ok && s != ""
}
/*
func exp(s string) (string, bool) {
a, ok1 := fx(s)
b, ok2 := el(a)
return b, (ok1 || ok2)
}
func el(s string) (string, bool) {
fx := strings.TrimSpace(s)
if strings.HasPrefix(fx, "${") && strings.HasSuffix(fx, "}") {
return fx[2 : len(fx)-1], true
}
return fx, false
}
*/