Skip to content

Commit 1dc7443

Browse files
committed
Use Env instead of With
1 parent 9cd6e2c commit 1dc7443

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ If you try to use some undeclared variables, or access unknown field, an error w
101101
102102
// err: Request.User.Cookies[0].Timestamp undefined (type expr_test.cookie has no field Timestamp)
103103
104-
Also it's possible to define all used variables and functions using expr.With and struct:
104+
Also it's possible to define all used variables and functions using expr.Env and struct:
105105
106106
type payload struct {
107107
Request *Request
108108
Values func(xs []Cookie) []string
109109
}
110110
111-
node, err := expr.Parse(expression, expr.With(payload{}))
111+
node, err := expr.Parse(expression, expr.Env(payload{}))
112112
113113
Or with map:
114114
@@ -117,7 +117,7 @@ Or with map:
117117
"Values": func(xs []Cookie) []string {...},
118118
}
119119
120-
node, err := expr.Parse(expression, expr.With(data))
120+
node, err := expr.Parse(expression, expr.Env(data))
121121
122122
123123
Printing

doc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func ExampleDefine() {
158158
// Output: err: invalid operation: groups[0].Name + user.Age (mismatched types string and int)
159159
}
160160

161-
func ExampleWith() {
161+
func ExampleEnv() {
162162
type Segment struct {
163163
Origin string
164164
}
@@ -173,7 +173,7 @@ func ExampleWith() {
173173
}
174174

175175
code := `Segments[0].Origin == "MOW" && Passengers.Adults == 2 && Marker == "test" && Meta["accept"]`
176-
ast, err := expr.Parse(code, expr.With(&Request{}))
176+
ast, err := expr.Parse(code, expr.Env(&Request{}))
177177

178178
if err != nil {
179179
fmt.Printf("err: %v", err)

eval_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ func TestEval_complex(t *testing.T) {
552552
}
553553

554554
input := `Request.User.UserAgent matches "Mozilla" && "www" in Values(Request.User.Cookies)`
555-
node, err := expr.Parse(input, expr.With(p))
555+
node, err := expr.Parse(input, expr.Env(p))
556556
if err != nil {
557557
t.Fatal(err)
558558
}

parser.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,18 @@ func Define(name string, t interface{}) OptionFn {
115115
}
116116
}
117117

118-
// With sets variables for type checks during parsing.
118+
// Deprecated: Use expr.Env instead.
119+
func With(i interface{}) OptionFn {
120+
return Env(i)
121+
}
122+
123+
// Env sets variables for type checks during parsing.
119124
// If struct is passed, all fields will be treated as variables,
120-
// as well as all fields of embedded structs.
125+
// as well as all fields of embedded structs and struct itself.
121126
//
122127
// If map is passed, all items will be treated as variables
123128
// (key as name, value as type).
124-
func With(i interface{}) OptionFn {
129+
func Env(i interface{}) OptionFn {
125130
return func(p *parser) {
126131
p.strict = true
127132
for k, v := range p.createTypesTable(i) {

type_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ type payload struct {
333333

334334
func TestType(t *testing.T) {
335335
for _, test := range typeTests {
336-
_, err := expr.Parse(string(test), expr.With(&payload{}))
336+
_, err := expr.Parse(string(test), expr.Env(&payload{}))
337337
if err != nil {
338338
t.Errorf("%s:\n\t%+v", test, err.Error())
339339
}
@@ -342,7 +342,7 @@ func TestType(t *testing.T) {
342342

343343
func TestType_error(t *testing.T) {
344344
for _, test := range typeErrorTests {
345-
_, err := expr.Parse(test.input, expr.With(&payload{}))
345+
_, err := expr.Parse(test.input, expr.Env(&payload{}))
346346
if err == nil {
347347
err = fmt.Errorf("<nil>")
348348
}

0 commit comments

Comments
 (0)