simple_rule

package
v0.0.0-...-aeb4a1d Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package simple_rule is a Go implementation of json-logic represented rules that can be used to evaluate data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func A

func A(n ...interface{}) interface{}

A returns normalized data as an []interface{}.

Example
normalizedDataSingle := A(1)
fmt.Println(normalizedDataSingle)

normalizedDataMulti := A(1, "a", "b")
fmt.Println(normalizedDataMulti)
Output:

[1]
[1 a b]

func D

func D(n ...interface{}) interface{}

D returns normalized data as an interface{}. If more than one value is supplied, it returns a normalized []interface{}.

Example
normalizedDataSingle := D(1)
fmt.Println(normalizedDataSingle)

normalizedDataMulti := D(1, "a", "b")
fmt.Println(normalizedDataMulti)
Output:

1
[1 a b]

func M

func M(n ...interface{}) map[string]interface{}

M returns a map[string]interface{} from a list of values with a []interface{key, value} format.

Example
M(D("name", "Tom"), D("age", 32), D("sex", "male"))
// returns map[name:Tom age:32 sex:male]
Output:

func R

func R(oper string, val ...interface{}) map[string]interface{}

R returns a rule expression given an operator and a set of arguments.

Example
r := R("+", 1, 2.4, 6)
fmt.Println(r)
Output:

map[+:[1 2.4 6]]

func ToJSON

func ToJSON(e interface{}) string

ToJSON returns data as a JSON string.

Example
data := `{
		"name": {"last": "Smith", "first": "Jo"},
		"age": 45
	  }`
rule := NewRule(R("var", "age"))
resultMap, _ := rule.Apply(data)

resultJSON := ToJSON(resultMap)

fmt.Println(resultJSON)
Output:

{"$result":45,"age":45,"name":{"first":"Jo","last":"Smith"}}

Types

type Rule

type Rule struct {
	// contains filtered or unexported fields
}

Rule represents a rule that can be evaluated over a set of data.

func CombineRules

func CombineRules(operator string, ruleComponents []map[string]interface{}) (*Rule, error)

CombineRules accepts a list of rules and combines them into a single rule with the given operator.

func NewRule

func NewRule(n ...interface{}) Rule

NewRule creates a new rule. It takes two parameters, rule_expr and init_data (optional), both of which can be either a JSON string or a map[string]interface{}. expr is a rule expression. It has the following format in JSON:

{ "operator" : [ arg1, arg2, ... ] }

The value of args can be any final value (number, string, boolean, array, or map), or a rule expression. Rule expressions are only evaluted when Apply() is called, never during creation. All numeric data in a rule_expression are treated as float64 values. However, if an integer value is required (e.g. % operator), the value is converted to an int64 for internal computation. If the value is not a whole number when an integer is required, an error is retuned.

init_data is initial data that can be optionally provided. If provided, it will later be merged with the data supplied for actual rule evaluation.

Example
rule1 := NewRule(`{"+": [1, 2.4, 6]}`)
fmt.Println(rule1)

e := make(map[string]interface{})
e["+"] = []interface{}{1, 2.4, 6}
rule2 := NewRule(e)
fmt.Println(rule2)

rule3 := NewRule(R("+", 1, 2.4, 6))
fmt.Println(rule3)
Output:

{map[+:[1 2.4 6]] map[]}
{map[+:[1 2.4 6]] map[]}
{map[+:[1 2.4 6]] map[]}

func (*Rule) Apply

func (r *Rule) Apply(val interface{}) (map[string]interface{}, error)

Apply applies the rule to val and returns the result as a map[string]interface{}. val can be a map[string]interface{}, a JSON string, or nil if the data is not needed. The evaluted value is stored in the "$result" key of the returned data map.

Example
data := `{
		"name": {"last": "Smith", "first": "Jo"},
		"age": 45
	  }`
rule := NewRule(R("var", "age"))
resultMap, _ := rule.Apply(data)

fmt.Println(resultMap["$result"])
Output:

45

func (*Rule) GetExpr

func (r *Rule) GetExpr() interface{}

GetExpr returns the rule expression as an interface{}.

Example
rule := NewRule(R("+", 1, 2.4, 6))

expr := rule.GetExpr()
fmt.Println(expr)
Output:

map[+:[1 2.4 6]]

func (*Rule) GetExprJSON

func (r *Rule) GetExprJSON() string

GetExprJSON returns the rule expression as a JSON string.

Example
rule := NewRule(R("+", 1, 2.4, 6))

exprJSON := rule.GetExprJSON()
fmt.Println(exprJSON)
Output:

{"+":[1,2.4,6]}

func (*Rule) GetInit

func (r *Rule) GetInit() map[string]interface{}

GetInit returns the rule's init data as a map[string]interface{}.

Example
init_data := M(D("my list", D(1, 2, 3, 4.0, "my val", 1, "test")))
rule := NewRule(R("in", "my val", R("var", "my list")), init_data)

init := rule.GetInit()

fmt.Println(init)
Output:

map[my list:[1 2 3 4 my val 1 test]]

func (*Rule) GetInitJSON

func (r *Rule) GetInitJSON() string

GetInitJSON returns the rule's init data as a JSON string.

Example
init_data := M(D("my list", D(1, 2, 3, 4.0, "my val", 1, "test")))
rule := NewRule(R("in", "my val", R("var", "my list")), init_data)

initJSON := rule.GetInitJSON()

fmt.Println(initJSON)
Output:

{"my list":[1,2,3,4,"my val",1,"test"]}

Jump to

Keyboard shortcuts

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