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.
go get github.com/braindev/isit
See: https://godoc.org/github.com/braindev/isit
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 == trueThese 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 | gt — greater than |
numeric |
| numeric | gt_eq — greater than or equal to |
numeric |
| numeric | lt — less than |
numeric |
| numeric | lt_eq — less than or equal to |
numeric |
| numeric | eq — equal to |
numeric |
| numeric | not_eq — not equal to |
numeric |
| string | eq — equal to |
string |
| string | not_eq — not equal to |
string |
| string | regex — matches regular expression |
string — the regular expression |
| string | not_regex — doesn't match regular expression |
string — the regular expression |
| string | in — one of a group |
[]string |
| string | not_in — not one of a group |
[]string |
| bool | eq — equal to |
bool |
| bool | not_eq — not equal to |
bool |
| []string | has — includes item |
string |
| []string | does_not_have — doesn't include item |
string |
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 },
},
},
}
}- More tests
Addinandnot_infor testing inclusion or exclusion of a string to a group of strings- Benchmarks