-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathdemo.go
45 lines (37 loc) · 822 Bytes
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"time"
"github.com/antonmedv/expr"
)
var expressions = []string{
"foo > 0",
"bar.Value in ['a', 'b', 'c']",
"name matches '^hello.+$'",
"now().Sub(startedAt).String()",
"all(tweets, {len(.Message) <= 280}) ? '👍' : '👎'",
}
var environment = map[string]interface{}{
"foo": 1,
"bar": struct{ Value string }{"c"},
"name": "hello world",
"startedAt": time.Now(),
"now": func() time.Time { return time.Now() },
"tweets": []tweet{{"first tweet"}},
}
type tweet struct {
Message string
}
func main() {
for _, input := range expressions {
program, err := expr.Compile(input, expr.Env(environment))
if err != nil {
panic(err)
}
output, err := expr.Run(program, environment)
if err != nil {
panic(err)
}
fmt.Println(output)
}
}