You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -116,20 +119,16 @@ fib(12+12) // will be transformed to 267914296 during the compilation
116
119
fib(x) // will **not** be transformed and will be evaluated at runtime
117
120
```
118
121
119
-
## Options
122
+
## Timezone
120
123
121
-
Compiler options can be defined as an array:
124
+
By default, the timezone is set to `time.Local`. We can change the timezone via the [`Timezone`](https://pkg.go.dev/github.com/expr-lang/expr#Timezone) option.
Copy file name to clipboardExpand all lines: docs/environment.md
+18-2Lines changed: 18 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,12 +84,28 @@ is the value's type.
84
84
env:=map[string]any{
85
85
"object": map[string]any{
86
86
"field": 42,
87
-
},
87
+
},
88
+
"struct": struct {
89
+
Field int`expr:"field"`
90
+
}{42},
88
91
}
89
92
```
90
93
91
94
Expr will infer the type of the `object` variable as `map[string]any`.
95
+
Accessing fields of the `object` and `struct` variables will return the following results.
92
96
93
-
By default, Expr will return an error if unknown variables are used in the expression.
97
+
```expr
98
+
object.field // 42
99
+
object.unknown // nil (no error)
100
+
101
+
struct.field // 42
102
+
struct.unknown // error (unknown field)
103
+
104
+
foobar // error (unknown variable)
105
+
```
94
106
107
+
:::note
108
+
The `foobar` variable is not defined in the environment.
109
+
By default, Expr will return an error if unknown variables are used in the expression.
95
110
You can disable this behavior by passing [`AllowUndefinedVariables`](https://pkg.go.dev/github.com/expr-lang/expr#AllowUndefinedVariables) option to the compiler.
Copy file name to clipboardExpand all lines: docs/patch.md
+17-6Lines changed: 17 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Patch
2
2
3
3
Sometimes it may be necessary to modify an expression before the compilation.
4
-
Expr provides a powerful mechanism to modify the expression using
5
-
the [`Patch`](https://pkg.go.dev/github.com/expr-lang/expr#Patch) option.
4
+
For example, you may want to replace a variable with a constant, transform an expression into a function call,
5
+
or even modify the expression to use a different operator.
6
6
7
7
## Simple example
8
8
@@ -19,12 +19,15 @@ type FooPatcher struct{}
19
19
20
20
func(FooPatcher) Visit(node *ast.Node) {
21
21
ifn, ok:= (*node).(*ast.IdentifierNode); ok && n.Value == "foo" {
22
+
// highlight-next-line
22
23
ast.Patch(node, &ast.IntegerNode{Value: 42})
23
24
}
24
25
}
25
26
```
26
27
27
-
Now we can use the `FooPatcher` to modify the expression:
28
+
We used the [ast.Patch](https://pkg.go.dev/github.com/expr-lang/expr/ast#Patch) function to replace the `foo` variable with an integer node.
29
+
30
+
Now we can use the `FooPatcher` to modify the expression on compilation via the [expr.Patch](https://pkg.go.dev/github.com/expr-lang/expr#Patch) option:
Copy file name to clipboardExpand all lines: docs/visitor.md
+11-2Lines changed: 11 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,13 @@
1
1
# Visitor
2
2
3
-
Expr provides an interface to traverse the AST of the expression before the compilation.
3
+
Expr provides an interface to traverse the <span title="Abstract Syntax Tree" style={{borderBottom: "1px dotted currentColor"}}>AST</span> of the expression before the compilation.
4
4
The `Visitor` interface allows you to collect information about the expression, modify the expression, or even generate
5
5
a new expression.
6
6
7
7
Let's start with an [ast.Visitor](https://pkg.go.dev/github.com/expr-lang/expr/ast#Visitor) implementation which will
8
-
collect all variables used in the expression:
8
+
collect all variables used in the expression.
9
+
10
+
Visitor must implement a single method `Visit(*ast.Node)`, which will be called for each node in the AST.
0 commit comments