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
{"Could I be wearing any more clothes?", time.Now()},
@@ -129,4 +107,72 @@ func main() {
129
107
}
130
108
```
131
109
110
+
## Configuration
111
+
112
+
Expr can be configured to do more things. For example, with [AllowUndefinedVariables](https://pkg.go.dev/github.com/antonmedv/expr#AllowUndefinedVariables) or [AsBool](https://pkg.go.dev/github.com/antonmedv/expr#AsBool) to expect the boolean result from the expression.
Expr supports any Go functions. For example, you can use `fmt.Sprintf` or methods of your structs.
121
+
Also, Expr supports functions configured via [`expr.Function(name, fn[, ...types])`](https://pkg.go.dev/github.com/antonmedv/expr#Function) option.
122
+
123
+
```go
124
+
atoi:= expr.Function(
125
+
"atoi",
126
+
func(params ...any) (any, error) {
127
+
return strconv.Atoi(params[0].(string))
128
+
},
129
+
)
130
+
131
+
program, err:= expr.Compile(`atoi("42")`, atoi)
132
+
```
133
+
134
+
Expr sees the `atoi` function as a function with a variadic number of arguments of type `any` and returns a value of type `any`. But, we can specify the types of arguments and the return value by adding the correct function
135
+
signature or multiple signatures.
136
+
137
+
```go
138
+
atoi:= expr.Function(
139
+
"atoi",
140
+
func(params ...any) (any, error) {
141
+
return strconv.Atoi(params[0].(string))
142
+
},
143
+
new(func(string) int),
144
+
)
145
+
```
146
+
147
+
Or we can simply reuse the `strconv.Atoi` function.
148
+
149
+
```go
150
+
atoi:= expr.Function(
151
+
"atoi",
152
+
func(params ...any) (any, error) {
153
+
return strconv.Atoi(params[0].(string))
154
+
},
155
+
strconv.Atoi,
156
+
)
157
+
```
158
+
159
+
Here is another example with a few function signatures:
0 commit comments