rules

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MIT Imports: 10 Imported by: 0

README

go-rules

Simple rules engine

Examples

  • From JSON
expression := `
{
    "comparator": "||",
    "rules": [
        {
        "comparator": "&&",
        "rules": [
            { "var": "a", "op": "<", "val": "2019-03-28T11:39:43+07:00" },
            { "var": "b", "op": "in", "val": [1, 2, 3] }
        ]
        },
        {
        "comparator": "&&",
        "rules": [
            { "var": "c", "op": "!=", "val": "string" },
            { "var": "d", "op": ">=", "val": 4 },
            { "var": "strlen(c)", "op": ">", "val": 0 }
        ]
        }
    ]
}
`

parameters := map[string]interface{}{
    "a": time.Now(),
    "b": 1,
    "c": "number",
    "d": 5,
}

functions := map[string]rules.Function{
    "strlen": func(args ...interface{}) (interface{}, error) {
        length := len(args[0].(string))
        return (float64)(length), nil
    },
}

expr, err := rules.ParseFromJSON([]byte(expression))
require.NoError(t, err)

res, err := rules.Evaluate(expr, parameters, functions)
require.NoError(t, err)
assert.True(t, res)
  • From string
input := `a == "2006-01-02T15:04:05Z" && (b > 0 || c == "string") && d < 100 && strlen(c) > 0`

parameters := map[string]interface{}{
    "a": time.Now(),
    "b": 1,
    "c": "number",
    "d": 5,
}

functions := map[string]rules.Function{
    "strlen": func(args ...interface{}) (interface{}, error) {
        length := len(args[0].(string))
        return (float64)(length), nil
    },
}

expr, err := rules.ParseFromString(input)
require.NoError(t, err)
require.NotNil(t, expr)

res, err := rules.Evaluate(expr, parameters, functions)
require.NoError(t, err)
assert.False(t, res)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate

func Evaluate(expr Expr, params map[string]interface{}, funcs map[string]Function) (bool, error)

func Walk

func Walk(expr Expr, fn WalkFunc) error

Types

type Expr

type Expr interface {
	String() string
	Type() string
}

func ParseFromJSON

func ParseFromJSON(raw json.RawMessage) (Expr, error)

func ParseFromString

func ParseFromString(src string) (Expr, error)

type Function

type Function func(args ...interface{}) (interface{}, error)

type Literal

type Literal interface {
	Expr
	Value() interface{}
}

type Op

type Op string
const (
	AND      Op = "&&"
	OR       Op = "||"
	EQ       Op = "=="
	NEQ      Op = "!="
	LT       Op = "<"
	LTE      Op = "<="
	GT       Op = ">"
	GTE      Op = ">="
	IN       Op = "in"
	ADD      Op = "+"
	SUB      Op = "-"
	MUL      Op = "*"
	DIV      Op = "/"
	MOD      Op = "%"
	CONTAINS Op = "contains"
)

type WalkFunc

type WalkFunc func(expr Expr, err error) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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