Vinshop expression tree

Status

Exp-tree is go library for parsing expression tree

Installation

go get -u github.com/vinshop/exp-tree

Quick start

Expression 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

Parse tree

package main

import (
	"fmt"
	et "github.com/vinshop/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)
}

Calculate

package main

import (
	"fmt"
	et "github.com/vinshop/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
}

Current only support for String, Float64, Bool type

With Bool type, we already define True and False
You could use et.Var(value) to auto convert value into corresponding type

GitHub

https://github.com/vinshop/exp-tree