Skip to content

Commit 6e1e72c

Browse files
committed
Update README.md
1 parent 5e64f7b commit 6e1e72c

File tree

2 files changed

+83
-29
lines changed

2 files changed

+83
-29
lines changed

README.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ product.Stock < 15
3838
* Reasonable set of basic operators.
3939
* Builtins `all`, `none`, `any`, `one`, `filter`, `map`.
4040
```coffeescript
41-
all(Tweets, {.Size < 140})
41+
all(Tweets, {.Size <= 280})
4242
```
4343
* Fast ([benchmarks](https://github.com/antonmedv/golang-expression-evaluation-comparison#readme)): uses bytecode virtual machine and optimizing compiler.
4444

@@ -65,48 +65,56 @@ Also, I have an embeddable code editor written in JavaScript which allows editin
6565

6666
## Examples
6767

68-
Executing arbitrary expressions.
68+
[demo.go](./docs/examples/demo.go)
6969

7070
```go
71-
env := map[string]interface{}{
72-
"foo": 1,
73-
"bar": struct{Value int}{1},
74-
}
71+
package main
7572

76-
out, err := expr.Eval("foo + bar.Value", env)
77-
```
73+
import (
74+
"fmt"
75+
"time"
7876

79-
Static type checker with struct as environment.
77+
"github.com/antonmedv/expr"
78+
)
8079

81-
```go
82-
type Env struct {
83-
Foo int
84-
Bar *Bar
80+
var expressions = []string{
81+
"foo > 0",
82+
"bar.Value in ['a', 'b', 'c']",
83+
"name matches '^hello.+$'",
84+
"now().Sub(startedAt).String()",
85+
"all(tweets, {.Size <= 280}) ? '👍' : '👎'",
8586
}
8687

87-
type Bar struct {
88-
Value int
88+
var environment = map[string]interface{}{
89+
"foo": 1,
90+
"bar": struct{ Value string }{"c"},
91+
"name": "hello world",
92+
"startedAt": time.Now(),
93+
"now": func() time.Time { return time.Now() },
94+
"tweets": []tweet{},
8995
}
9096

91-
program, err := expr.Compile("Foo + Bar.Value", expr.Env(&Env{}))
92-
93-
out, err := expr.Run(program, &Env{1, &Bar{2}})
94-
```
97+
type tweet struct {
98+
Message string
99+
Size int
100+
}
95101

96-
Using env's methods as functions inside expressions.
102+
func main() {
103+
for _, input := range expressions {
104+
program, err := expr.Compile(input, expr.Env(environment))
105+
if err != nil {
106+
panic(err)
107+
}
97108

98-
```go
99-
type Env struct {
100-
Name string
101-
}
109+
output, err := expr.Run(program, environment)
110+
if err != nil {
111+
panic(err)
112+
}
102113

103-
func (e *Env) Title() string {
104-
return strings.Title(e.Name)
114+
fmt.Println(output)
115+
}
105116
}
106117

107-
program, err := expr.Compile(`"Hello " + Title()`, expr.Env(&Env{}))
108-
109-
out, err := expr.Run(program, &Env{"world"})
110118
```
111119

112120
## Contributing

docs/examples/demo.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/antonmedv/expr"
8+
)
9+
10+
var expressions = []string{
11+
"foo > 0",
12+
"bar.Value in ['a', 'b', 'c']",
13+
"name matches '^hello.+$'",
14+
"now().Sub(startedAt).String()",
15+
"all(tweets, {.Size <= 280}) ? '👍' : '👎'",
16+
}
17+
18+
var environment = map[string]interface{}{
19+
"foo": 1,
20+
"bar": struct{ Value string }{"c"},
21+
"name": "hello world",
22+
"startedAt": time.Now(),
23+
"now": func() time.Time { return time.Now() },
24+
"tweets": []tweet{},
25+
}
26+
27+
type tweet struct {
28+
Message string
29+
Size int
30+
}
31+
32+
func main() {
33+
for _, input := range expressions {
34+
program, err := expr.Compile(input, expr.Env(environment))
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
output, err := expr.Run(program, environment)
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
fmt.Println(output)
45+
}
46+
}

0 commit comments

Comments
 (0)