Skip to content

Commit

Permalink
Use Env instead of With
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Oct 9, 2018
1 parent 9cd6e2c commit 1dc7443
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 8 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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("<nil>")
}
Expand Down

0 comments on commit 1dc7443

Please sign in to comment.