-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasteval.go
More file actions
376 lines (348 loc) · 9.37 KB
/
Copy pathasteval.go
File metadata and controls
376 lines (348 loc) · 9.37 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package oktaexpr
import (
"fmt"
"regexp"
"strings"
"github.com/stevenewson/okta-expression-parser/expressionclasses"
)
// evalContext holds the evaluation environment for a single Eval call: the
// user profile, group memberships, and expression classes a Parser was
// configured with (see Parser's fields) — nothing here affects how an
// expression is parsed (see astparse.go), only how an already-built Node
// resolves against a specific user/org's data.
type evalContext struct {
userProfile map[string]any
groupIDs []string
groupData map[string]any
classes expressionclasses.Registry
strict bool
}
// eval dispatches to the node-type-specific evaluator. Every node type
// astparse.go can produce is handled; a type outside that set (only
// possible from a hand-constructed Node) is a runtime error rather than a
// panic.
func (c *evalContext) eval(n Node) (any, error) {
switch v := n.(type) {
case Literal:
return v.Value, nil
case PathExpr:
return c.evalPath(v)
case MemberOfExpr:
return c.evalMemberOf(v)
case ClassCall:
return c.evalClassCall(v)
case ArrayLit:
return c.evalArray(v)
case CommaList:
return c.evalCommaList(v)
case Ternary:
return c.evalTernary(v)
case Comparison:
return c.evalComparison(v)
case Additive:
return c.evalAdditive(v)
case Logical:
return c.evalLogical(v)
case Not:
return c.evalNot(v)
default:
return nil, fmt.Errorf("unsupported AST node type %T", n)
}
}
// evalPath resolves a PathExpr's root plus its chain of hops. See
// PathExpr's doc comment for the "bare name always nil" quirk (RootUser
// false leaves val nil throughout, since only a "user"-rooted chain ever
// resolves against real profile data) and NameHop's for strict mode.
func (c *evalContext) evalPath(n PathExpr) (any, error) {
var val any
if n.RootUser {
val = any(c.userProfile)
}
for _, hop := range n.Hops {
switch h := hop.(type) {
case MemberOfHop:
memberVal, err := c.evalMemberOf(*h.Call)
if err != nil {
return nil, err
}
val = memberVal
case NameHop:
m, ok := val.(map[string]any)
if !ok {
val = nil
continue
}
res, exists := m[h.Name]
if !exists {
if c.strict {
return nil, fmt.Errorf("property %q does not exist at character %d", h.Name, h.Pos)
}
val = nil
continue
}
if s, ok := res.(string); ok {
// The source strips literal quote characters from any
// string resolved through a "." access; preserved here for
// fidelity.
res = strings.ReplaceAll(s, `"`, "")
}
val = res
}
}
return val, nil
}
func (c *evalContext) evalMemberOf(n MemberOfExpr) (any, error) {
argVal, err := c.eval(n.Arg)
if err != nil {
return nil, err
}
switch n.Kind {
case MemberOf:
return c.evalIsMemberOf(argVal)
case MemberOfAny:
return c.evalIsMemberOfAny(argVal)
case MemberOfName:
return c.evalIsMemberOfGroupData(argVal, func(name, target string) bool { return name == target })
case MemberOfGroupStartsWith:
return c.evalIsMemberOfGroupData(argVal, strings.HasPrefix)
case MemberOfGroupContains:
return c.evalIsMemberOfGroupData(argVal, func(name, target string) bool { return strings.Contains(name, target) })
case MemberOfGroupNameRegex:
return c.evalIsMemberOfGroupNameRegex(argVal)
default:
return nil, fmt.Errorf("unhandled isMemberOf builtin %q", memberOfNames[n.Kind])
}
}
// evalIsMemberOf implements isMemberOfGroup: true if arg is one of the
// parser's configured group IDs.
func (c *evalContext) evalIsMemberOf(arg any) (any, error) {
if len(c.groupIDs) == 0 {
return false, nil
}
s, ok := arg.(string)
if !ok {
return false, nil
}
for _, id := range c.groupIDs {
if id == s {
return true, nil
}
}
return false, nil
}
// evalIsMemberOfAny implements isMemberOfAnyGroup: true if any of arg's
// (possibly multiple, comma-joined) values is one of the configured group
// IDs.
func (c *evalContext) evalIsMemberOfAny(arg any) (any, error) {
if len(c.groupIDs) == 0 {
return false, nil
}
ids := make(map[string]bool, len(c.groupIDs))
for _, id := range c.groupIDs {
ids[id] = true
}
for _, v := range spreadArgs(arg) {
if s, ok := v.(string); ok && ids[s] {
return true, nil
}
}
return false, nil
}
// groupProfileName extracts group["profile"]["name"] from a group_data
// entry. Note this shape (nested under "profile") differs from what
// expressionclasses.Groups.getFilteredGroups expects (a flat map); that
// inconsistency exists in the Python source and is preserved rather than
// papered over, since it's the caller's group_data to shape either way.
func groupProfileName(group any) (string, bool) {
m, ok := group.(map[string]any)
if !ok {
return "", false
}
profile, ok := m["profile"].(map[string]any)
if !ok {
return "", false
}
name, ok := profile["name"].(string)
return name, ok
}
// evalIsMemberOfGroupData implements the isMemberOfGroupName* family: true
// if match(groupName, arg) holds for any configured group's profile name.
func (c *evalContext) evalIsMemberOfGroupData(arg any, match func(groupName, target string) bool) (any, error) {
target, ok := arg.(string)
if !ok {
return false, nil
}
for _, group := range c.groupData {
if name, ok := groupProfileName(group); ok && match(name, target) {
return true, nil
}
}
return false, nil
}
func (c *evalContext) evalIsMemberOfGroupNameRegex(arg any) (any, error) {
pattern, ok := arg.(string)
if !ok {
return false, nil
}
re, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("isMemberOfGroupNameRegex: invalid regular expression %q: %w", pattern, err)
}
for _, group := range c.groupData {
name, ok := groupProfileName(group)
if !ok {
continue
}
if loc := re.FindStringIndex(name); loc != nil && loc[0] == 0 {
return true, nil
}
}
return false, nil
}
func (c *evalContext) evalClassCall(n ClassCall) (any, error) {
class, ok := c.classes[n.Class]
if !ok {
return nil, fmt.Errorf("class %q has not been implemented in the parser", n.Class)
}
var args []any
if n.Arg != nil {
argVal, err := c.eval(n.Arg)
if err != nil {
return nil, err
}
args = spreadArgs(argVal)
}
result, err := class.Call(n.Method, args...)
if err != nil {
return nil, fmt.Errorf("%s.%s: %w", n.Class, n.Method, err)
}
return result, nil
}
func (c *evalContext) evalArray(n ArrayLit) (any, error) {
elems := make(Array, len(n.Elements))
for i, e := range n.Elements {
v, err := c.eval(e)
if err != nil {
return nil, err
}
elems[i] = v
}
return elems, nil
}
// evalCommaList folds mergeOperands across its evaluated elements in
// order — identical to parseOperandBranch's incremental build in the old
// fused parser, just applied at Eval time instead of parse time.
func (c *evalContext) evalCommaList(n CommaList) (any, error) {
var result any
for i, e := range n.Elements {
v, err := c.eval(e)
if err != nil {
return nil, err
}
if i == 0 {
result = v
} else {
result = mergeOperands(result, v)
}
}
return result, nil
}
// evalTernary evaluates Cond, True, and False unconditionally (no
// short-circuit — a preserved quirk, since the original bottom-up parser
// evaluated every subexpression as it reduced, before the ternary's own
// action ever ran) before picking a branch by Cond's truthiness.
func (c *evalContext) evalTernary(n Ternary) (any, error) {
condVal, err := c.eval(n.Cond)
if err != nil {
return nil, err
}
trueVal, err := c.eval(n.True)
if err != nil {
return nil, err
}
falseVal, err := c.eval(n.False)
if err != nil {
return nil, err
}
if truthy(condVal) {
return trueVal, nil
}
return falseVal, nil
}
func (c *evalContext) evalComparison(n Comparison) (any, error) {
left, err := c.eval(n.Left)
if err != nil {
return nil, err
}
right, err := c.eval(n.Right)
if err != nil {
return nil, err
}
result, err := applyComparison(n.Op, left, right)
if err != nil {
return nil, fmt.Errorf("%s at character %d", err, n.Pos)
}
return result, nil
}
func applyComparison(op string, a, b any) (bool, error) {
switch op {
case "==":
return equalOperands(a, b), nil
case "!=":
return !equalOperands(a, b), nil
default:
return compareOperands(op, a, b)
}
}
func (c *evalContext) evalAdditive(n Additive) (any, error) {
left, err := c.eval(n.Left)
if err != nil {
return nil, err
}
right, err := c.eval(n.Right)
if err != nil {
return nil, err
}
result, err := addOperands(left, right)
if err != nil {
return nil, fmt.Errorf("%s at character %d", err, n.Pos)
}
return result, nil
}
// evalLogical evaluates every operand unconditionally, left to right,
// stopping at the first error — then reduces the results with the same
// truthy-chaining logic the old fused AND/OR loops applied incrementally.
// Evaluating every operand regardless of an earlier one's truthiness is
// the preserved "AND/OR never short-circuit" quirk; see the doc comment on
// Logical and the package README.
func (c *evalContext) evalLogical(n Logical) (any, error) {
values := make([]any, len(n.Operands))
for i, o := range n.Operands {
v, err := c.eval(o)
if err != nil {
return nil, err
}
values[i] = v
}
result := values[0]
for _, v := range values[1:] {
switch n.Op {
case "AND":
if truthy(result) {
result = v
}
case "OR":
if !truthy(result) {
result = v
}
}
}
return result, nil
}
func (c *evalContext) evalNot(n Not) (any, error) {
v, err := c.eval(n.Operand)
if err != nil {
return nil, err
}
return !truthy(v), nil
}