Documentation ¶
Index ¶
- Variables
- type Expr
- func And(v1, v2 Expr, vN ...Expr) Expr
- func Eq(v1, v2 Expr, vN ...Expr) Expr
- func FNV(v Expr) Expr
- func GT(v1, v2 Expr, vN ...Expr) Expr
- func GTE(v1, v2 Expr, vN ...Expr) Expr
- func In(v, e1 Expr, eN ...Expr) Expr
- func LT(v1, v2 Expr, vN ...Expr) Expr
- func LTE(v1, v2 Expr, vN ...Expr) Expr
- func Not(e Expr) Expr
- func Or(v1, v2 Expr, vN ...Expr) Expr
- func Percentile(v, p Expr) Expr
- func True() Expr
- type Param
- type Params
- type Rule
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRulesetNotFound must be returned when no ruleset is found for a given key. ErrRulesetNotFound = errors.New("ruleset not found") // ErrParamTypeMismatch is returned when a parameter type is different from expected. ErrParamTypeMismatch = errors.New("parameter type mismatches") // ErrParamNotFound is returned when a parameter is not defined. ErrParamNotFound = errors.New("parameter not found") // ErrNoMatch is returned when the rule doesn't match the given params. ErrNoMatch = errors.New("rule doesn't match the given params") // ErrRulesetIncoherentType is returned when a ruleset contains rules of different types. ErrRulesetIncoherentType = errors.New("types in ruleset are incoherent") )
Functions ¶
This section is empty.
Types ¶
type Expr ¶
An Expr is a logical expression that can be evaluated to a value.
func And ¶
And creates an expression that takes at least two operands and evaluates to true if all the operands evaluate to true. All the given operands must evaluate to a boolean.
Example ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.And( rule.Eq( rule.Int64Value(10), rule.Int64Param("foo"), ), rule.Not( rule.Eq( rule.Float64Value(1.5), rule.Float64Param("bar"), ), ), ) val, err := tree.Eval(regula.Params{ "foo": int64(10), "bar": 1.6, }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
func Eq ¶
Eq creates an expression that takes at least two operands and evaluates to true if all the operands are equal.
Example (Bool) ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.Eq( rule.BoolValue(false), rule.Not(rule.BoolValue(true)), rule.Eq( rule.StringValue("bar"), rule.StringValue("baz"), ), ) val, err := tree.Eval(regula.Params{ "foo": "bar", }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
Example (Float64) ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.Eq( rule.Float64Value(3.14), rule.Float64Param("foo"), ) val, err := tree.Eval(regula.Params{ "foo": 3.14, }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
Example (Int64) ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.Eq( rule.Int64Value(10), rule.Int64Param("foo"), ) val, err := tree.Eval(regula.Params{ "foo": int64(10), }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
Example (String) ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.Eq( rule.StringValue("bar"), rule.StringValue("bar"), rule.StringParam("foo"), ) val, err := tree.Eval(regula.Params{ "foo": "bar", }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
func FNV ¶ added in v0.6.0
FNV returns an Integer hash of any value it is provided. It uses the Fowler-Noll-Vo non-cryptographic hash function.
func GT ¶ added in v0.6.0
GT creates an expression that takes at least two operands and evaluates to true if each successive operand has a higher value than the next.
func GTE ¶ added in v0.6.0
GTE creates an expression that takes at least two operands and evaluates to true if each successive operand has a greater or equal value compared to the next.
func In ¶
In creates an expression that takes at least two operands and evaluates to true if the first one is equal to one of the others.
Example ¶
package main import ( "fmt" "log" "github.com/heetch/regula/rule" ) func main() { tree := rule.In( rule.StringValue("c"), rule.StringValue("a"), rule.StringValue("b"), rule.StringValue("c"), rule.StringValue("d"), ) val, err := tree.Eval(nil) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
func LT ¶ added in v0.6.0
LT creates an expression that takes at least two operands and evaluates to true if each successive operand has a lower value compared to the next.
func LTE ¶ added in v0.6.0
LTE creates an expression that takes at least two operands and evaluates to true if each successive operand has a lower or equal value compared to the next.
func Not ¶
Not creates an expression that evaluates the given operand e and returns its opposite. e must evaluate to a boolean.
Example ¶
package main import ( "fmt" "log" "github.com/heetch/regula/rule" ) func main() { tree := rule.Not(rule.BoolValue(false)) val, err := tree.Eval(nil) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
func Or ¶
Or creates an expression that takes at least two operands and evaluates to true if one of the operands evaluates to true. All the given operands must evaluate to a boolean.
Example ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.Or( rule.Eq( rule.Float64Value(1.2), rule.Float64Param("foo"), ), rule.Eq( rule.Float64Value(3.14), rule.Float64Param("foo"), ), ) val, err := tree.Eval(regula.Params{ "foo": 3.14, }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: true
func Percentile ¶ added in v0.6.0
Percentile indicates whether the provided value is within a given percentile of the group of all such values. It is intended to be used to assign values to groups for experimentation.
type Param ¶
Param is an expression used to select a parameter passed during evaluation and return its corresponding value.
func BoolParam ¶
BoolParam creates a Param that looks up in the set of params passed during evaluation and returns the value of the variable that corresponds to the given name. The corresponding value must be a boolean. If not found it returns an error.
func Float64Param ¶
Float64Param creates a Param that looks up in the set of params passed during evaluation and returns the value of the variable that corresponds to the given name. The corresponding value must be a float64. If not found it returns an error.
func Int64Param ¶
Int64Param creates a Param that looks up in the set of params passed during evaluation and returns the value of the variable that corresponds to the given name. The corresponding value must be an int64. If not found it returns an error.
func StringParam ¶
StringParam creates a Param that looks up in the set of params passed during evaluation and returns the value of the variable that corresponds to the given name. The corresponding value must be a string. If not found it returns an error.
Example ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { tree := rule.StringParam("foo") val, err := tree.Eval(regula.Params{ "foo": "bar", }) if err != nil { log.Fatal(err) } fmt.Println(val.Data) }
Output: bar
type Params ¶
type Params interface { GetString(key string) (string, error) GetBool(key string) (bool, error) GetInt64(key string) (int64, error) GetFloat64(key string) (float64, error) Keys() []string EncodeValue(key string) (string, error) }
A Params is a set of parameters passed on rule evaluation. It provides type safe methods to query params.
type Rule ¶
A Rule represents a logical boolean expression that evaluates to a result.
Example ¶
package main import ( "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/rule" ) func main() { r := rule.New( rule.Eq( rule.StringValue("foo"), rule.StringParam("bar"), ), rule.StringValue("matched"), ) ret, err := r.Eval(regula.Params{ "bar": "foo", }) if err != nil { log.Fatal(err) } fmt.Println(ret.Data) // Output // matched }
Output:
func New ¶
New creates a rule with the given expression and that returns the given result on evaluation.
func (*Rule) Eval ¶
Eval evaluates the rule against the given params. If it matches it returns a result, otherwise it returns ErrNoMatch or any encountered error.
func (*Rule) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type Value ¶
A Value is the result of the evaluation of an expression.
func Float64Value ¶
Float64Value creates a float64 type value.