Skip to content

Commit

Permalink
feat: override now() and provide a constant (#61)
Browse files Browse the repository at this point in the history
Having a constant timestamp can be easier to reason about logic.
  • Loading branch information
polds authored Jan 11, 2024
1 parent 1849e4c commit 47fd3ee
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package eval
import (
"encoding/json"
"fmt"
"time"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
Expand All @@ -30,6 +31,12 @@ type RunResponse struct {

var exprEnvOptions = []expr.Option{
expr.AsAny(),

// Provide a constant timestamp to the expression environment.
expr.DisableBuiltin("now"),
expr.Function("now", func(...any) (any, error) {
return time.Date(2024, 2, 26, 0, 0, 0, 0, time.UTC).Format(time.RFC3339), nil
}, new(func() time.Time)),
}

// Eval evaluates the expr expression against the given input.
Expand Down

0 comments on commit 47fd3ee

Please sign in to comment.