Skip to content

Commit 0cf3b1f

Browse files
committed
Add AllowUndefinedVariables option examples
1 parent b72131b commit 0cf3b1f

File tree

1 file changed

+78
-25
lines changed

1 file changed

+78
-25
lines changed

expr_test.go

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -339,49 +339,102 @@ func ExampleOperator() {
339339
// Output: true
340340
}
341341

342-
func ExampleOperator_time() {
343-
type Segment struct {
344-
Date time.Time
342+
func ExampleAllowUndefinedVariables() {
343+
code := `name == nil ? "Hello, world!" : sprintf("Hello, %v!", name)`
344+
345+
env := map[string]interface{}{
346+
"sprintf": fmt.Sprintf,
345347
}
346-
type Request struct {
347-
Segments []Segment
348-
Before func(a, b time.Time) bool
349-
Date func(s string) time.Time
348+
349+
options := []expr.Option{
350+
expr.Env(env),
351+
expr.AllowUndefinedVariables(), // Allow to use undefined variables.
350352
}
351353

352-
code := `Date("2001-01-01") < Segments[0].Date`
354+
program, err := expr.Compile(code, options...)
355+
if err != nil {
356+
fmt.Printf("%v", err)
357+
return
358+
}
353359

354-
program, err := expr.Compile(code, expr.Env(&Request{}), expr.Operator("<", "Before"))
360+
output, err := expr.Run(program, env)
355361
if err != nil {
356362
fmt.Printf("%v", err)
357363
return
358364
}
365+
fmt.Printf("%v\n", output)
359366

360-
request := &Request{
361-
Segments: []Segment{
362-
{Date: time.Date(2019, 7, 1, 0, 0, 0, 0, time.UTC)},
363-
},
364-
Before: func(a, b time.Time) bool {
365-
return a.Before(b)
366-
},
367-
Date: func(s string) time.Time {
368-
date, err := time.Parse("2006-01-02", s)
369-
if err != nil {
370-
panic(err)
371-
}
372-
return date
373-
},
367+
env["name"] = "you" // Define variables later on.
368+
369+
output, err = expr.Run(program, env)
370+
if err != nil {
371+
fmt.Printf("%v", err)
372+
return
374373
}
374+
fmt.Printf("%v\n", output)
375375

376-
output, err := expr.Run(program, request)
376+
// Output: Hello, world!
377+
// Hello, you!
378+
}
379+
380+
func ExampleAllowUndefinedVariables_zero_value() {
381+
code := `name == "" ? foo + bar : foo + name`
382+
383+
// If environment has different zero values, then undefined variables
384+
// will have it as default value.
385+
env := map[string]string{}
386+
387+
options := []expr.Option{
388+
expr.Env(env),
389+
expr.AllowUndefinedVariables(), // Allow to use undefined variables.
390+
}
391+
392+
program, err := expr.Compile(code, options...)
377393
if err != nil {
378394
fmt.Printf("%v", err)
379395
return
380396
}
381397

398+
env = map[string]string{
399+
"foo": "Hello, ",
400+
"bar": "world!",
401+
}
402+
403+
output, err := expr.Run(program, env)
404+
if err != nil {
405+
fmt.Printf("%v", err)
406+
return
407+
}
382408
fmt.Printf("%v", output)
383409

384-
// Output: true
410+
// Output: Hello, world!
411+
}
412+
413+
func ExampleAllowUndefinedVariables_zero_value_functions() {
414+
code := `words == "" ? Split("foo,bar", ",") : Split(words, ",")`
415+
416+
// Env is map[string]string type on which methods are defined.
417+
env := mockMapStringStringEnv{}
418+
419+
options := []expr.Option{
420+
expr.Env(env),
421+
expr.AllowUndefinedVariables(), // Allow to use undefined variables.
422+
}
423+
424+
program, err := expr.Compile(code, options...)
425+
if err != nil {
426+
fmt.Printf("%v", err)
427+
return
428+
}
429+
430+
output, err := expr.Run(program, env)
431+
if err != nil {
432+
fmt.Printf("%v", err)
433+
return
434+
}
435+
fmt.Printf("%v", output)
436+
437+
// Output: [foo bar]
385438
}
386439

387440
func TestOperator_struct(t *testing.T) {

0 commit comments

Comments
 (0)