@@ -339,49 +339,102 @@ func ExampleOperator() {
339
339
// Output: true
340
340
}
341
341
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 ,
345
347
}
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.
350
352
}
351
353
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
+ }
353
359
354
- program , err := expr .Compile ( code , expr . Env ( & Request {}), expr . Operator ( "<" , "Before" ) )
360
+ output , err := expr .Run ( program , env )
355
361
if err != nil {
356
362
fmt .Printf ("%v" , err )
357
363
return
358
364
}
365
+ fmt .Printf ("%v\n " , output )
359
366
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
374
373
}
374
+ fmt .Printf ("%v\n " , output )
375
375
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 ... )
377
393
if err != nil {
378
394
fmt .Printf ("%v" , err )
379
395
return
380
396
}
381
397
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
+ }
382
408
fmt .Printf ("%v" , output )
383
409
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]
385
438
}
386
439
387
440
func TestOperator_struct (t * testing.T ) {
0 commit comments