Skip to content

Repository files navigation

GO-VS expression tree

Status

Exp-tree is go library for parsing expression tree

Installation

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

Quick start

Format

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

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

Data type

Bool

Number

Array

Operator

Operator

Parse tree

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)
}

Calculate

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
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages