Skip to content

Commit be55ef0

Browse files
committed
Add more examples
1 parent 2541a82 commit be55ef0

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

expr_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func ExampleEval_matches() {
121121
// | ............^
122122
}
123123

124-
func ExampleRun() {
124+
func ExampleCompile() {
125125
env := map[string]interface{}{
126126
"foo": 1,
127127
"bar": 99,
@@ -188,6 +188,68 @@ func ExampleEnv() {
188188
// Output: true
189189
}
190190

191+
func ExampleAsBool() {
192+
env := map[string]int{
193+
"foo": 0,
194+
}
195+
196+
program, err := expr.Compile("foo >= 0", expr.Env(env), expr.AsBool())
197+
if err != nil {
198+
fmt.Printf("%v", err)
199+
return
200+
}
201+
202+
output, err := expr.Run(program, env)
203+
if err != nil {
204+
fmt.Printf("%v", err)
205+
return
206+
}
207+
208+
fmt.Printf("%v", output.(bool))
209+
210+
// Output: true
211+
}
212+
213+
func ExampleAsFloat64() {
214+
program, err := expr.Compile("42", expr.AsFloat64())
215+
if err != nil {
216+
fmt.Printf("%v", err)
217+
return
218+
}
219+
220+
output, err := expr.Run(program, nil)
221+
if err != nil {
222+
fmt.Printf("%v", err)
223+
return
224+
}
225+
226+
fmt.Printf("%v", output.(float64))
227+
228+
// Output: 42
229+
}
230+
231+
func ExampleAsInt64() {
232+
env := map[string]float64{
233+
"foo": 3,
234+
}
235+
236+
program, err := expr.Compile("foo + 2", expr.Env(env), expr.AsInt64())
237+
if err != nil {
238+
fmt.Printf("%v", err)
239+
return
240+
}
241+
242+
output, err := expr.Run(program, env)
243+
if err != nil {
244+
fmt.Printf("%v", err)
245+
return
246+
}
247+
248+
fmt.Printf("%v", output.(int64))
249+
250+
// Output: 5
251+
}
252+
191253
func ExampleOperator() {
192254
type Place struct {
193255
Code string

0 commit comments

Comments
 (0)