-
-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathconfig.go
More file actions
121 lines (106 loc) · 2.9 KB
/
config.go
File metadata and controls
121 lines (106 loc) · 2.9 KB
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
package conf
import (
"fmt"
"reflect"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/builtin"
"github.com/expr-lang/expr/checker/nature"
"github.com/expr-lang/expr/vm/runtime"
)
var (
// DefaultMemoryBudget represents default maximum allowed memory usage by the vm.VM.
DefaultMemoryBudget uint = 1e6
// DefaultMaxNodes represents default maximum allowed AST nodes by the compiler.
DefaultMaxNodes uint = 1e4
// DefaultTag defines the default tag to use to determine field names. Override
// with the WithTag method.
DefaultTag string = "expr"
)
type FunctionsTable map[string]*builtin.Function
type Config struct {
EnvObject any
Env nature.Nature
Expect reflect.Kind
ExpectAny bool
Optimize bool
Strict bool
ShortCircuit bool
Profile bool
MaxNodes uint
ConstFns map[string]reflect.Value
Visitors []ast.Visitor
Functions FunctionsTable
Builtins FunctionsTable
Disabled map[string]bool // disabled builtins
NtCache nature.Cache
Tag string
// DisableIfOperator disables the built-in `if ... { } else { }` operator syntax
// so that users can use a custom function named `if(...)` without conflicts.
// When enabled, the lexer treats `if`/`else` as identifiers and the parser
// will not parse `if` statements.
DisableIfOperator bool
}
// CreateNew creates new config with default values.
func CreateNew() *Config {
c := &Config{
Optimize: true,
ShortCircuit: true,
MaxNodes: DefaultMaxNodes,
ConstFns: make(map[string]reflect.Value),
Functions: make(map[string]*builtin.Function),
Builtins: make(map[string]*builtin.Function),
Disabled: make(map[string]bool),
}
for _, f := range builtin.Builtins {
c.Builtins[f.Name] = f
}
c.SetTag(DefaultTag)
return c
}
// SetTag sets the struct tag key used for field name resolution in expressions.
// It updates the config, the nature cache, and the get() builtin atomically.
func (c *Config) SetTag(tag string) {
c.Tag = tag
c.NtCache.SetTag(tag)
}
// New creates new config with environment.
func New(env any) *Config {
c := CreateNew()
c.WithEnv(env)
return c
}
func (c *Config) WithEnv(env any) {
c.EnvObject = env
c.Env = EnvWithCache(&c.NtCache, env)
c.Strict = c.Env.Strict
}
func (c *Config) ConstExpr(name string) {
if c.EnvObject == nil {
panic("no environment is specified for ConstExpr()")
}
ctx := runtime.New(c.Tag)
fn := reflect.ValueOf(ctx.Fetch(c.EnvObject, name))
if fn.Kind() != reflect.Func {
panic(fmt.Errorf("const expression %q must be a function", name))
}
c.ConstFns[name] = fn
}
type Checker interface {
Check()
}
func (c *Config) Check() {
for _, v := range c.Visitors {
if c, ok := v.(Checker); ok {
c.Check()
}
}
}
func (c *Config) IsOverridden(name string) bool {
if _, ok := c.Functions[name]; ok {
return true
}
if _, ok := c.Env.Get(&c.NtCache, name); ok {
return true
}
return false
}