MCPcopy Create free account
hub / github.com/cel-expr/cel-go / Example_cel_NameResolution

Function Example_cel_NameResolution

examples/example_cel_advanced_test.go:60–85  ·  view source on GitHub ↗

Example_cel_NameResolution showcases scope resolution order and macro variable shadowing as documented in https://celbyexample.com/name-resolution/

()

Source from the content-addressed store, hash-verified

58// Example_cel_NameResolution showcases scope resolution order and macro variable shadowing
59// as documented in https://celbyexample.com/name-resolution/
60func Example_cel_NameResolution() {
61 vars := map[string]any{
62 "x": 100,
63 "items": []int64{1, 2, 3},
64 }
65
66 // In items.map(x, x * 2), the iteration variable 'x' shadows the outer variable 'x'
67 prg, err := cel.Compile(`items.map(x, x * 2)`,
68 cel.Variable("x", cel.IntType),
69 cel.Variable("items", cel.ListType(cel.IntType)),
70 )
71 if err != nil {
72 fmt.Printf("cel.Compile() error: %v\n", err)
73 return
74 }
75 out, _, err := prg.Eval(vars)
76 if err != nil {
77 fmt.Printf("prg.Eval() error: %v\n", err)
78 return
79 }
80
81 fmt.Printf("Macro iteration variable shadows outer x: %v\n", out)
82
83 // Output:
84 // Macro iteration variable shadows outer x: [2, 4, 6]
85}

Callers

nothing calls this directly

Calls 3

CompileFunction · 0.92
VariableFunction · 0.92
EvalMethod · 0.65

Tested by

no test coverage detected