isit

package module
v0.0.0-...-7001f45 Latest Latest
Warning

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

Go to latest
Published: May 16, 2016 License: MIT Imports: 6 Imported by: 0

README

Is it?

A generic logic/rule testing engine written in Go.

IsIt is a library for contructing rules via JSON or programmatically and then testing those rules against a set of values. This code is in an alpha state right now. The API is still in flux.

Installation
go get github.com/braindev/isit
Docs

See: https://godoc.org/github.com/braindev/isit

Simple Example
rg := isit.RuleGroup{
  Logic: "or",
  Rules: []isit.Rule{
    { Property: "age", Operator: "gt_eq", Value: 18 },
    { Property: "parent_permission": Operator: "eq", Value: true },
  }
}

result, _ := rg.Test(map[string]interface{}{"age": 17, "parent_permission": false})
// result == false

result, _ := rg.Test(map[string]interface{}{"age": 17, "parent_permission": true})
// result == true

result, _ := rg.Test(map[string]interface{}{"age": 18, "parent_permission": true})
// result == true
Rule Operators

These are the types of values that should be used with together. The "Rule Value Type" is the type for the field Value on the struct Rule. The "Property Type" is the type of the value for the property. Using mismatched types will cause errors.

Rule Value Type Operator Property Type
numeric gtgreater than numeric
numeric gt_eqgreater than or equal to numeric
numeric ltless than numeric
numeric lt_eqless than or equal to numeric
numeric eqequal to numeric
numeric not_eqnot equal to numeric
string eqequal to string
string not_eqnot equal to string
string regexmatches regular expression string — the regular expression
string not_regexdoesn't match regular expression string — the regular expression
string inone of a group []string
string not_innot one of a group []string
bool eqequal to bool
bool not_eqnot equal to bool
[]string hasincludes item string
[]string does_not_havedoesn't include item string
Nested Example

Rule logic can be as complex as needed. Suppose the following logic needed to be tested:

if activity == "rock climbing" and (height <= 5 or weight > 280)

It could be written as:

rg := isit.RuleGroup{
  Logic: "and",
  Rules: []isit.Rule{
    { Property: "activity", Operator: "eq", Value: "rock climbing" },
    {
      RuleGroup: &isit.RuleGroup{
      Logic: "or",
      Rules: []isit.Rule{
        { Property: "height", Operator: "lt_eq", Value: 5 },
        { Property: "weight": Operator: "gt", Value: 280 },
      },
    },
  }
}
TODO
  • More tests
  • Add in and not_in for testing inclusion or exclusion of a string to a group of strings
  • Benchmarks

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rule

type Rule struct {
	Property  string      `json:"property,omitempty"`
	Operator  string      `json:"operator,omitempty"`
	Value     interface{} `json:"value,omitempty"`
	RuleGroup *RuleGroup  `json:"rule_group,omitempty"`
}

Rule represents one rule or a sub-collection of rules

type RuleGroup

type RuleGroup struct {
	Logic string `json:"logic"`
	Rules []Rule `json:"rules"`
}

RuleGroup represents a collection of rules

func NewRuleGroupFromJSON

func NewRuleGroupFromJSON(j []byte) (*RuleGroup, error)

NewRuleGroupFromJSON creates a new rule group from JSON

func (*RuleGroup) And

func (rg *RuleGroup) And(andGroup *RuleGroup) *RuleGroup

And allows two rule groups to be "anded" together

func (*RuleGroup) Or

func (rg *RuleGroup) Or(orGroup *RuleGroup) *RuleGroup

Or allows two rule groups to be "or" together

func (*RuleGroup) Test

func (rg *RuleGroup) Test(values map[string]interface{}) (bool, error)

Test runs a rule group against a group of values

Jump to

Keyboard shortcuts

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