Giter Site home page Giter Site logo

expr's Introduction

bool expression parser and evaluator

supported grammars

  • true, false, number (float64 and int64), string, identifier
  • compare operations: = > >= < <= !=
  • simple math operations: + - * /
  • prefix: -, !
  • bracket ()
  • custom variable, like $car
  • custom none-variadic functions, like : takeBus()
  • in, like $car in ('bwm','byd')

grammars limits

  • function inputs only supported int64, float64, string, bool
  • functions must have one or two returns, the first one must be one of: int64, float64, string, bool, if the second one exists, it must be error
  • variables support numbers, including all ints and uints, except uint64
  • function does not support variadic args
  • if identifier exists, then the expression can only be parsed, but cannot be evaluated

builtin functions:

  • contains
  • endsWith
  • startsWith
  • length
  • toLower
  • toUpper
  • trim
  • concat
  • geoWithin2d
  • hasIntersection
  • timestampBefore
  • now // return time.Now() rfc3339 formatted string

examples

simple one

func ExampleEvaluate() {
	expr := `$car in ('bwm', 'byd') and (3 + 2) * 2.0 = 10 and startsWith($car, 'b')`

	variables := map[string]interface{}{`car`: `byd`}
	result, err := Evaluate(expr, variables)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
	// Output:
	// true
}

with custom functions

func ExampleParseAndEvaluate() {
	expr := `$car in ('bwm', 'byd') and (3 + echo_int(2)) * 2.0 = 10 and startsWith($car, 'b')`
	var tInt = func(a int64) int64 {
		return a
	}
	if err := RegisterFunc(`echo_int`, tInt); err != nil {
		panic(err)
	}

	variables := map[string]interface{}{`car`: `byd`}
	result, err := Evaluate(expr, variables)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
	// Output:
	// true
}

expr's People

Contributors

echoutopia avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.