-
Notifications
You must be signed in to change notification settings - Fork 2
/
ctx.go
143 lines (120 loc) · 3.4 KB
/
ctx.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
package opencypher
import (
"strings"
"unicode"
"github.com/cloudprivacylabs/lpg/v2"
)
type ErrUnknownParameter struct {
Key string
}
func (e ErrUnknownParameter) Error() string { return "Unknown parameter: " + e.Key }
type EvalContext struct {
parent *EvalContext
funcMap map[string]Function
variables map[string]Value
parameters map[string]Value
graph *lpg.Graph
// If this function is non-nil, it will be called to filter property
// values when setting properties of nodes or edges
PropertyValueFromNativeFilter func(string, interface{}) interface{}
}
func NewEvalContext(graph *lpg.Graph) *EvalContext {
return &EvalContext{
funcMap: globalFuncs,
variables: make(map[string]Value),
parameters: make(map[string]Value),
graph: graph,
}
}
// SubContext creates a new subcontext with a new variable set
func (ctx *EvalContext) SubContext() *EvalContext {
return &EvalContext{
parent: ctx,
funcMap: ctx.funcMap,
variables: make(map[string]Value),
parameters: make(map[string]Value),
graph: ctx.graph,
PropertyValueFromNativeFilter: ctx.PropertyValueFromNativeFilter,
}
}
// PropertyValueFromNative calls the ctx.PropertyValueFromNativeFilter
// to give the caller a chance to change property values
func (ctx *EvalContext) PropertyValueFromNative(key string, value interface{}) interface{} {
if ctx.PropertyValueFromNativeFilter != nil {
return ctx.PropertyValueFromNativeFilter(key, value)
}
return value
}
// SetParameter sets a parameter to be used in expressions
func (ctx *EvalContext) SetParameter(key string, value Value) *EvalContext {
ctx.parameters[key] = value
return ctx
}
func (ctx *EvalContext) GetParameter(key string) (Value, error) {
value, ok := ctx.parameters[key]
if ok {
return value, nil
}
if ctx.parent == nil {
return nil, ErrUnknownParameter{Key: key}
}
return ctx.parent.GetParameter(key)
}
type ErrUnknownFunction struct {
Name string
}
func (e ErrUnknownFunction) Error() string { return "Unknown function: " + e.Name }
type ErrUnknownVariable struct {
Name string
}
func (e ErrUnknownVariable) Error() string { return "Unknown variable:" + e.Name }
func (ctx *EvalContext) getFunction(name string) (Function, error) {
f, ok := ctx.funcMap[name]
if !ok {
return Function{}, ErrUnknownFunction{name}
}
return f, nil
}
func (ctx *EvalContext) GetFunction(name []string) (Function, error) {
bld := strings.Builder{}
for i, x := range name {
if i > 0 {
bld.WriteRune('.')
}
bld.WriteString(string(x))
}
return ctx.getFunction(bld.String())
}
func (ctx *EvalContext) GetVar(name string) (Value, error) {
val, ok := ctx.variables[name]
if !ok {
if ctx.parent == nil {
return nil, ErrUnknownVariable{Name: name}
}
return ctx.parent.GetVar(name)
}
if rv, ok := val.(RValue); ok {
rv.Const = false
return rv, nil
}
return val, nil
}
func (ctx *EvalContext) GetVarsNearestScope() map[string]Value {
return ctx.variables
}
func (ctx *EvalContext) SetVar(name string, value Value) {
ctx.variables[name] = value
}
func (ctx *EvalContext) RemoveVar(name string) {
delete(ctx.variables, name)
}
func (ctx *EvalContext) SetVars(m map[string]Value) {
for k, v := range m {
if !unicode.IsDigit(rune(k[0])) {
ctx.SetVar(k, v)
}
}
}
func IsNamedVar(name string) bool {
return !unicode.IsDigit(rune(name[0]))
}