Skip to content
This repository has been archived by the owner on Apr 12, 2020. It is now read-only.

braindev/isit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A generic logic/rule testing engine written in Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published