1
- # Expr [ ![ Build Status] ( https://travis-ci.org/antonmedv/expr.svg?branch=master )] ( https://travis-ci.org/antonmedv/expr ) [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/antonmedv/expr )] ( https://goreportcard.com/report/github.com/antonmedv/expr ) [ ![ Code Coverage] ( https://scrutinizer-ci.com/g/antonmedv/expr/badges/coverage.png?b=master )] ( https://scrutinizer-ci.com/g/antonmedv/expr/?branch=master ) <a href =" https://stars.medv.io/antonmedv/expr " ><img src =" https://stars.medv.io/antonmedv/expr.svg " alt =" Sparkline " height =" 24 " ></a >
1
+ # Expr
2
+ [ ![ Build Status] ( https://travis-ci.org/antonmedv/expr.svg?branch=master )] ( https://travis-ci.org/antonmedv/expr )
3
+ [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/antonmedv/expr )] ( https://goreportcard.com/report/github.com/antonmedv/expr )
4
+ [ ![ Code Coverage] ( https://scrutinizer-ci.com/g/antonmedv/expr/badges/coverage.png?b=master )] ( https://scrutinizer-ci.com/g/antonmedv/expr/?branch=master )
5
+ [ ![ GoDoc] ( https://godoc.org/github.com/antonmedv/expr?status.svg )] ( https://godoc.org/github.com/antonmedv/expr )
2
6
3
- Expr is an engine that can evaluate expressions.
7
+ Expr is an engine that can evaluate expressions.
4
8
5
9
The purpose of the package is to allow users to use expressions inside configuration for more complex logic.
6
10
It is a perfect candidate for the foundation of a _ business rule engine_ .
@@ -17,27 +21,23 @@ len(article.Comments) > 100 and article.Category not in ["misc"]
17
21
product .Stock < 15
18
22
```
19
23
20
- Inspired by
21
- * Symfony's [ The ExpressionLanguage] ( https://github.com/symfony/expression-language ) component,
22
- * Rob Pike's talk [ Lexical Scanning in Go] ( https://talks.golang.org/2011/lex.slide ) .
23
-
24
24
## Features
25
25
26
- * Works with any valid Go object (structs, maps, etc)
27
- * Static and dynamic typing ([ example] ( https://godoc.org/github.com/antonmedv/expr#example-Define ) )
26
+ * Seamless integration with Go.
27
+ * Static typing ([ example] ( https://godoc.org/github.com/antonmedv/expr#example-Env ) ).
28
28
``` go
29
- code := " groups[0].Title + user.Age"
30
- p , err := expr.Parse (code, expr.Define (" groups" , []Group {}), expr.Define (" user" , User {}))
31
- // err: invalid operation: groups[0].Name + user.Age (mismatched types string and int)
32
- ```
33
- * User-friendly error messages
29
+ out , err := expr.Eval (" 'hello' + 10" )
30
+ // err: invalid operation + (mismatched types string and int64)
31
+ // | 'hello' + 10
32
+ // | ........^
34
33
```
35
- unclosed "("
36
- (boo + bar]
37
- ----------^
34
+ * User-friendly error messages.
35
+ * Reasonable set of basic operators.
36
+ * Builtins ` all ` , ` none ` , ` any ` , ` one ` , ` filter ` , ` map ` .
37
+ ``` coffeescript
38
+ all (Tweets, {.Size < 140 })
38
39
```
39
- * Reasonable set of basic operators
40
- * Fast (faster otto and goja, see [ bench] ( https://github.com/antonmedv/expr/wiki/Benchmarks ) )
40
+ * Fast ([ benchamrks] ( https://github.com/antonmedv/golang-expression-evaluation-comparison ) ).
41
41
42
42
## Install
43
43
@@ -70,57 +70,56 @@ out, err := expr.Eval("foo + bar.Value", env)
70
70
Static type checker with struct as environment.
71
71
72
72
``` go
73
- type env struct {
73
+ type Env struct {
74
74
Foo int
75
75
Bar bar
76
76
}
77
77
78
- type bar struct {
78
+ type Bar struct {
79
79
Value int
80
80
}
81
81
82
- p , err := expr.Parse (" Foo + Bar.Value" , expr.Env (env {}))
82
+ program , err := expr.Compile (" Foo + Bar.Value" , expr.Env (Env {}))
83
83
84
- out , err := expr.Run (p, env {1 , bar {2 }})
84
+ output , err := expr.Run (program, Env {1 , Bar {2 }})
85
85
```
86
86
87
87
Using env's methods as functions inside expressions.
88
88
89
89
``` go
90
- type env struct {
90
+ type Env struct {
91
91
Name string
92
92
}
93
93
94
- func (e env ) Title () string {
94
+ func (e * Env ) Title () string {
95
95
return strings.Title (e.Name )
96
96
}
97
97
98
+ p , err := expr.Parse (` "Hello " + Title()` , expr.Env (&Env{}))
98
99
99
- p , err := expr.Parse (" 'Hello ' ~ Title()" , expr.Env (env{}))
100
-
101
- out , err := expr.Run (p, env{" world" })
100
+ out , err := expr.Run (p, &Env{" world" })
102
101
```
103
102
104
103
Using embedded structs to construct env.
105
104
106
105
``` go
107
- type env struct {
108
- helpers
106
+ type Env struct {
107
+ Helpers
109
108
Name string
110
109
}
111
110
112
- type helpers struct {}
111
+ type Helpers struct {}
113
112
114
- func (h helpers ) Title (s string ) string {
113
+ func (Helpers ) Title (s string ) string {
115
114
return strings.Title (s)
116
115
}
117
116
118
117
119
- p , err := expr.Parse (" ' Hello ' ~ Title(Name)" , expr.Env (env {}))
118
+ p , err := expr.Parse (` " Hello " + Title(Name)` , expr.Env (Env {}))
120
119
121
- out , err := expr.Run (p, env {" world" })
120
+ out , err := expr.Run (p, Env {" world" })
122
121
```
123
122
124
123
## License
125
124
126
- MIT
125
+ [ MIT] ( LICENSE )
0 commit comments