From 1dc744393547d26e04403bc8efacfc4aff2c088f Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Tue, 9 Oct 2018 23:00:05 +0700 Subject: [PATCH] Use Env instead of With --- doc.go | 6 +++--- doc_test.go | 4 ++-- eval_test.go | 2 +- parser.go | 11 ++++++++--- type_test.go | 4 ++-- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/doc.go b/doc.go index c63074da3..575bb7600 100644 --- a/doc.go +++ b/doc.go @@ -101,14 +101,14 @@ If you try to use some undeclared variables, or access unknown field, an error w // err: Request.User.Cookies[0].Timestamp undefined (type expr_test.cookie has no field Timestamp) -Also it's possible to define all used variables and functions using expr.With and struct: +Also it's possible to define all used variables and functions using expr.Env and struct: type payload struct { Request *Request Values func(xs []Cookie) []string } - node, err := expr.Parse(expression, expr.With(payload{})) + node, err := expr.Parse(expression, expr.Env(payload{})) Or with map: @@ -117,7 +117,7 @@ Or with map: "Values": func(xs []Cookie) []string {...}, } - node, err := expr.Parse(expression, expr.With(data)) + node, err := expr.Parse(expression, expr.Env(data)) Printing diff --git a/doc_test.go b/doc_test.go index 1e5e9e6ce..5eedef4fc 100644 --- a/doc_test.go +++ b/doc_test.go @@ -158,7 +158,7 @@ func ExampleDefine() { // Output: err: invalid operation: groups[0].Name + user.Age (mismatched types string and int) } -func ExampleWith() { +func ExampleEnv() { type Segment struct { Origin string } @@ -173,7 +173,7 @@ func ExampleWith() { } code := `Segments[0].Origin == "MOW" && Passengers.Adults == 2 && Marker == "test" && Meta["accept"]` - ast, err := expr.Parse(code, expr.With(&Request{})) + ast, err := expr.Parse(code, expr.Env(&Request{})) if err != nil { fmt.Printf("err: %v", err) diff --git a/eval_test.go b/eval_test.go index 7c62cd4f8..91913ebdc 100644 --- a/eval_test.go +++ b/eval_test.go @@ -552,7 +552,7 @@ func TestEval_complex(t *testing.T) { } input := `Request.User.UserAgent matches "Mozilla" && "www" in Values(Request.User.Cookies)` - node, err := expr.Parse(input, expr.With(p)) + node, err := expr.Parse(input, expr.Env(p)) if err != nil { t.Fatal(err) } diff --git a/parser.go b/parser.go index eb57eb8f3..19226a2e7 100644 --- a/parser.go +++ b/parser.go @@ -115,13 +115,18 @@ func Define(name string, t interface{}) OptionFn { } } -// With sets variables for type checks during parsing. +// Deprecated: Use expr.Env instead. +func With(i interface{}) OptionFn { + return Env(i) +} + +// Env sets variables for type checks during parsing. // If struct is passed, all fields will be treated as variables, -// as well as all fields of embedded structs. +// as well as all fields of embedded structs and struct itself. // // If map is passed, all items will be treated as variables // (key as name, value as type). -func With(i interface{}) OptionFn { +func Env(i interface{}) OptionFn { return func(p *parser) { p.strict = true for k, v := range p.createTypesTable(i) { diff --git a/type_test.go b/type_test.go index 17700edbb..11943c1a0 100644 --- a/type_test.go +++ b/type_test.go @@ -333,7 +333,7 @@ type payload struct { func TestType(t *testing.T) { for _, test := range typeTests { - _, err := expr.Parse(string(test), expr.With(&payload{})) + _, err := expr.Parse(string(test), expr.Env(&payload{})) if err != nil { t.Errorf("%s:\n\t%+v", test, err.Error()) } @@ -342,7 +342,7 @@ func TestType(t *testing.T) { func TestType_error(t *testing.T) { for _, test := range typeErrorTests { - _, err := expr.Parse(test.input, expr.With(&payload{})) + _, err := expr.Parse(test.input, expr.Env(&payload{})) if err == nil { err = fmt.Errorf("") }