-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (38 loc) · 1013 Bytes
/
main.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
46
47
package main
import (
"fmt"
"log"
"github.com/expr-lang/expr"
)
func main() {
fmt.Println("main start")
env := map[string]any{
"creditScore": 810,
}
expressions := []string{
"creditScore >= 700",
"creditScore >= 680",
"creditScore >= 1000",
}
var programs []any
//var programs []*vm.Program <-- unable to do this since vm.Program is not accessible.
for _, exprStr := range expressions {
program, err := expr.Compile(exprStr)
if err != nil {
log.Fatalf("failed to compile expression: ", exprStr)
}
programs = append(programs, program)
}
for _, program := range programs {
//compiledProgram, ok := program.(*expr.Program) <-- unable to do this. type not accessible
compiledProgram, ok := program.(any)
if !ok {
log.Printf("program has an invalid type")
}
result, err := expr.Run(compiledProgram, env) // causes error: need to assert type, but not able to.
if err != nil {
fmt.Errorf("error evaluating...")
}
fmt.Println("the result is: ", result)
}
}