@@ -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
0 commit comments