Skip to content

Commit 0c9427d

Browse files
committed
Update docs
1 parent 85c6b10 commit 0c9427d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

doc.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ You can also pass functions into the expression:
7070
7171
ok, err := expr.Eval(`"www" in Values(Request.User.Cookies)`, data)
7272
73+
All methods of passed struct also available as functions inside expr:
74+
75+
type Env struct {
76+
value int
77+
}
78+
79+
func (e Env) Value() int {
80+
return e.value
81+
}
82+
83+
v, err := expr.Eval(`Value()`, env{1})
84+
7385
7486
Parsing and caching
7587
@@ -103,12 +115,18 @@ If you try to use some undeclared variables, or access unknown field, an error w
103115
104116
Also it's possible to define all used variables and functions using expr.Env and struct:
105117
106-
type payload struct {
118+
type MyEnv struct {
119+
Embedded
107120
Request *Request
108-
Values func(xs []Cookie) []string
121+
Url string
122+
//...
123+
}
124+
125+
func (e MyEnv) Values(xs []Cookie) []string {
126+
//...
109127
}
110128
111-
node, err := expr.Parse(expression, expr.Env(payload{}))
129+
node, err := expr.Parse(expression, expr.Env(MyEnv{}))
112130
113131
Or with map:
114132

0 commit comments

Comments
 (0)