Exp-tree is go library for parsing expression tree
go get -u github.com/go-vs/exp-treeExpression tree is in format:
{
"<operator>": [
"<arg1>",
"<arg2>",
{
"<opetator>": [
"<arg>",
"<arg>",
"<@variable>"
]
}
]
}example
{"and":["@a",{"lt":[1,2]}]}is equivalent to @a and (1 < 2) with a is a variable
Variables use format @<string>, and will be replaced as Value from Variables when
call Tree.Calculate(v Variables)
Support for String, Number ( as float64), Bool, Array type
With Bool type, we already define True and False
You could use et.Var(value) to auto convert value into corresponding type
package main
import (
"fmt"
et "github.com/go-vs/exp-tree"
)
func main() {
tree, err := et.ParseTree(`{"and":["@a",{"lt":[1,2]}]}`)
if err != nil {
panic(err)
}
treeJSON, err := tree.JSON()
if err != nil {
panic(err)
}
fmt.Println(treeJSON)
}package main
import (
"fmt"
et "github.com/go-vs/exp-tree"
)
func main() {
tree, err := et.ParseTree(`{"and":["@a",{"lt":[1,2]}]}`)
if err != nil {
panic(err)
}
res, err := tree.Calculate(et.Variables{
"a": et.True, // or et.Var(true)
})
fmt.Println(res) // true
}