lang

package
v0.0.0-...-8534d30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Simple bool expressions

A simple yet awesome bool expression language.

Expression

An expression is - usually - in the form of <lhs> <operator> <rhs>.

Logical operators

  • &&: AND
  • ||: OR

Comparison operators

  • ==: equals to
  • !=: not equals to
  • ~~: matches (regex)

Fields

The <lhs> and <rhs> can be fields, even both at the same time, or other expressions

For example status == status is a valid expression.

The field syntax is as per buger/jsonparser.

So, you can assert conditions also on nested fields!

Let's say you want to log if the user agent starts starts with some value...

You'd need to traverse the request object, its headers child (another object), and its "User-Agent" child (array).

Sounds difficult. But, it isn't! Express this field as: request>headers>User-Agent>[0].

Values

The <lhs> and <rhs> can be values, of course.

The expressions support the following types for the values: boolean, (raw) string, and numbers (int64, and float64).

The language provides two boolean constants (case-insensitive): true and false.

Constants

This means that <lhs> and <rhs> (whether are they fields or constants) need to be of one of those types.

Operator precedence

In order of evaluation:

  1. ()
  2. ||
  3. &&
  4. ==, !=, ~~

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Fields []string
View Source
var Lang = gval.NewLanguage(

	gval.InfixShortCircuit("&&", func(lhs interface{}) (interface{}, bool) { return false, lhs == false }),
	gval.InfixShortCircuit("||", func(lhs interface{}) (interface{}, bool) { return true, lhs == true }),

	gval.InfixBoolOperator("&&", func(lhs, rhs bool) (interface{}, error) { return lhs && rhs, nil }),
	gval.InfixBoolOperator("||", func(lhs, rhs bool) (interface{}, error) { return lhs || rhs, nil }),

	gval.InfixBoolOperator("==", func(lhs, rhs bool) (interface{}, error) { return lhs == rhs, nil }),
	gval.InfixBoolOperator("!=", func(lhs, rhs bool) (interface{}, error) { return lhs != rhs, nil }),

	gval.InfixNumberOperator("==", func(lhs, rhs float64) (interface{}, error) { return lhs == rhs, nil }),
	gval.InfixNumberOperator("!=", func(lhs, rhs float64) (interface{}, error) { return lhs != rhs, nil }),

	gval.InfixEvalOperator("~~", regEx),

	gval.InfixOperator("==", func(lhs, rhs interface{}) (interface{}, error) { return reflect.DeepEqual(lhs, rhs), nil }),
	gval.InfixOperator("!=", func(lhs, rhs interface{}) (interface{}, error) { return !reflect.DeepEqual(lhs, rhs), nil }),

	gval.PrefixExtension(scanner.Int, parseNumber),
	gval.PrefixExtension(scanner.Float, parseNumber),
	gval.PrefixExtension(scanner.RawString, parseString),

	gval.Constant("true", true),
	gval.Constant("false", false),

	gval.Parentheses(),

	gval.Precedence("||", 20),
	gval.Precedence("&&", 21),

	gval.Precedence("==", 40),
	gval.Precedence("!=", 40),
	gval.Precedence("~~", 40),

	gval.PrefixMetaPrefix(scanner.Ident, parseIdent),
)

Functions

func Compile

func Compile(expression string) (gval.Evaluable, error)

func Execute

func Execute(eval gval.Evaluable, data map[string]interface{}) (interface{}, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL