@@ -38,7 +38,7 @@ product.Stock < 15
38
38
* Reasonable set of basic operators.
39
39
* Builtins ` all ` , ` none ` , ` any ` , ` one ` , ` filter ` , ` map ` .
40
40
``` coffeescript
41
- all (Tweets, {.Size < 140 })
41
+ all (Tweets, {.Size <= 280 })
42
42
```
43
43
* Fast ([ benchmarks] ( https://github.com/antonmedv/golang-expression-evaluation-comparison#readme ) ): uses bytecode virtual machine and optimizing compiler.
44
44
@@ -65,48 +65,56 @@ Also, I have an embeddable code editor written in JavaScript which allows editin
65
65
66
66
## Examples
67
67
68
- Executing arbitrary expressions.
68
+ [ demo.go ] ( ./docs/examples/demo.go )
69
69
70
70
``` go
71
- env := map [string ]interface {}{
72
- " foo" : 1 ,
73
- " bar" : struct {Value int }{1 },
74
- }
71
+ package main
75
72
76
- out , err := expr.Eval (" foo + bar.Value" , env)
77
- ```
73
+ import (
74
+ " fmt"
75
+ " time"
78
76
79
- Static type checker with struct as environment.
77
+ " github.com/antonmedv/expr"
78
+ )
80
79
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}) ? '👍' : '👎'" ,
85
86
}
86
87
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{},
89
95
}
90
96
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
+ }
95
101
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
+ }
97
108
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
+ }
102
113
103
- func ( e * Env ) Title () string {
104
- return strings. Title (e. Name )
114
+ fmt. Println (output)
115
+ }
105
116
}
106
117
107
- program , err := expr.Compile (` "Hello " + Title()` , expr.Env (&Env{}))
108
-
109
- out , err := expr.Run (program, &Env{" world" })
110
118
```
111
119
112
120
## Contributing
0 commit comments