Example_cel_NameResolution showcases scope resolution order and macro variable shadowing as documented in https://celbyexample.com/name-resolution/
()
| 58 | // Example_cel_NameResolution showcases scope resolution order and macro variable shadowing |
| 59 | // as documented in https://celbyexample.com/name-resolution/ |
| 60 | func 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 | } |