MCPcopy
hub / github.com/expr-lang/expr / ExampleOperator

Function ExampleOperator

expr_test.go:378–419  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

376}
377
378func ExampleOperator() {
379 code := `
380 Now() > CreatedAt &&
381 (Now() - CreatedAt).Hours() > 24
382 `
383
384 type Env struct {
385 CreatedAt time.Time
386 Now func() time.Time
387 Sub func(a, b time.Time) time.Duration
388 After func(a, b time.Time) bool
389 }
390
391 options := []expr.Option{
392 expr.Env(Env{}),
393 expr.Operator(">", "After"),
394 expr.Operator("-", "Sub"),
395 }
396
397 program, err := expr.Compile(code, options...)
398 if err != nil {
399 fmt.Printf("%v", err)
400 return
401 }
402
403 env := Env{
404 CreatedAt: time.Date(2018, 7, 14, 0, 0, 0, 0, time.UTC),
405 Now: func() time.Time { return time.Now() },
406 Sub: func(a, b time.Time) time.Duration { return a.Sub(b) },
407 After: func(a, b time.Time) bool { return a.After(b) },
408 }
409
410 output, err := expr.Run(program, env)
411 if err != nil {
412 fmt.Printf("%v", err)
413 return
414 }
415
416 fmt.Printf("%v", output)
417
418 // Output: true
419}
420
421func ExampleOperator_with_decimal() {
422 type Decimal struct{ N float64 }

Callers

nothing calls this directly

Calls 7

EnvStruct · 0.92
OperatorFunction · 0.92
CompileFunction · 0.92
RunFunction · 0.92
PrintfMethod · 0.80
SubMethod · 0.80
AfterMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…