Skip to content

Commit 820c64e

Browse files
authored
Update README.md
1 parent e4f3b41 commit 820c64e

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

README.md

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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)
26

3-
Expr is an engine that can evaluate expressions.
7+
Expr is an engine that can evaluate expressions.
48

59
The purpose of the package is to allow users to use expressions inside configuration for more complex logic.
610
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"]
1721
product.Stock < 15
1822
```
1923

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-
2424
## Features
2525

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)).
2828
```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+
// | ........^
3433
```
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})
3839
```
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)).
4141

4242
## Install
4343

@@ -70,57 +70,56 @@ out, err := expr.Eval("foo + bar.Value", env)
7070
Static type checker with struct as environment.
7171

7272
```go
73-
type env struct {
73+
type Env struct {
7474
Foo int
7575
Bar bar
7676
}
7777

78-
type bar struct {
78+
type Bar struct {
7979
Value int
8080
}
8181

82-
p, err := expr.Parse("Foo + Bar.Value", expr.Env(env{}))
82+
program, err := expr.Compile("Foo + Bar.Value", expr.Env(Env{}))
8383

84-
out, err := expr.Run(p, env{1, bar{2}})
84+
output, err := expr.Run(program, Env{1, Bar{2}})
8585
```
8686

8787
Using env's methods as functions inside expressions.
8888

8989
```go
90-
type env struct {
90+
type Env struct {
9191
Name string
9292
}
9393

94-
func (e env) Title() string {
94+
func (e *Env) Title() string {
9595
return strings.Title(e.Name)
9696
}
9797

98+
p, err := expr.Parse(`"Hello " + Title()`, expr.Env(&Env{}))
9899

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"})
102101
```
103102

104103
Using embedded structs to construct env.
105104

106105
```go
107-
type env struct {
108-
helpers
106+
type Env struct {
107+
Helpers
109108
Name string
110109
}
111110

112-
type helpers struct{}
111+
type Helpers struct{}
113112

114-
func (h helpers) Title(s string) string {
113+
func (Helpers) Title(s string) string {
115114
return strings.Title(s)
116115
}
117116

118117

119-
p, err := expr.Parse("'Hello ' ~ Title(Name)", expr.Env(env{}))
118+
p, err := expr.Parse(`"Hello " + Title(Name)`, expr.Env(Env{}))
120119

121-
out, err := expr.Run(p, env{"world"})
120+
out, err := expr.Run(p, Env{"world"})
122121
```
123122

124123
## License
125124

126-
MIT
125+
[MIT](LICENSE)

0 commit comments

Comments
 (0)