Skip to content

Commit 5846dec

Browse files
committed
Update docs
1 parent fb34ea4 commit 5846dec

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,73 @@ go get -u github.com/antonmedv/expr
5050
* See [![GoDoc](https://godoc.org/github.com/antonmedv/expr?status.svg)](https://godoc.org/github.com/antonmedv/expr) for developer documentation,
5151
* See [The Expression Syntax](https://github.com/antonmedv/expr/wiki/The-Expression-Syntax) page to learn the syntax of the Expr expressions.
5252

53+
## Examples
54+
55+
Executing arbitrary expressions.
56+
57+
```go
58+
env := map[string]interface{}{
59+
"foo": 1,
60+
"bar": struct{Value int}{1},
61+
}
62+
63+
out, err := expr.Eval("foo + bar.Value", env)
64+
```
65+
66+
Static type checker with struct as environment.
67+
68+
```go
69+
type env struct {
70+
Foo int
71+
Bar bar
72+
}
73+
74+
type bar struct {
75+
Value int
76+
}
77+
78+
p, err := expr.Parse("Foo + Bar.Value", expr.Env(env{}))
79+
80+
out, err := expr.Run(p, env{1, bar{2}})
81+
```
82+
83+
Using env's methods as functions inside expressions.
84+
85+
```go
86+
type env struct {
87+
Name string
88+
}
89+
90+
func (e env) Title() string {
91+
return strings.Title(e.Name)
92+
}
93+
94+
95+
p, err := expr.Parse("'Hello ' ~ Title()", expr.Env(env{}))
96+
97+
out, err := expr.Run(p, env{"world"})
98+
```
99+
100+
Using embedded structs to construct env.
101+
102+
```go
103+
type env struct {
104+
helpers
105+
Name string
106+
}
107+
108+
type helpers struct{}
109+
110+
func (h helpers) Title(s string) string {
111+
return strings.Title(s)
112+
}
113+
114+
115+
p, err := expr.Parse("'Hello ' ~ Title(Name)", expr.Env(env{}))
116+
117+
out, err := expr.Run(p, env{"world"})
118+
```
119+
53120
## License
54121

55122
MIT

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ All methods of passed struct also available as functions inside expr:
8080
return e.value
8181
}
8282
83-
v, err := expr.Eval(`Value()`, env{1})
83+
v, err := expr.Eval(`Value()`, Env{1})
8484
8585
8686
Parsing and caching

0 commit comments

Comments
 (0)