Documentation
¶
Overview ¶
Package predicate used to create interpreted mini languages with Go syntax - mostly to define various predicates for configuration, e.g. Latency() > 40 || ErrorRate() > 0.5.
Here's an example of fully functional predicate language to deal with division remainders:
// takes number and returns true or false type numberPredicate func(v int) bool // Converts one number to another type numberMapper func(v int) int // Function that creates predicate to test if the remainder is 0 func divisibleBy(divisor int) numberPredicate { return func(v int) bool { return v%divisor == 0 } } // Function - logical operator AND that combines predicates func numberAND(a, b numberPredicate) numberPredicate { return func(v int) bool { return a(v) && b(v) } } p, err := NewParser(Def{ Operators: Operators{ AND: numberAND, }, Functions: map[string]interface{}{ "DivisibleBy": divisibleBy, }, }) pr, err := p.Parse("DivisibleBy(2) && DivisibleBy(3)") if err == nil { fmt.Fatalf("Error: %v", err) } pr.(numberPredicate)(2) // false pr.(numberPredicate)(3) // false pr.(numberPredicate)(6) // true
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFieldByTag ¶
GetFieldByTag returns a field from the object based on the tag.
func GetStringMapValue ¶
func GetStringMapValue(mapVal, keyVal interface{}) (interface{}, error)
GetStringMapValue is a helper function that returns property from map[string]string or map[string][]string the function returns empty value in case if key not found In case if map is nil, returns empty value as well.
Types ¶
type BoolPredicate ¶
type BoolPredicate func() bool
BoolPredicate is a function without arguments that returns boolean value when called.
func And ¶
func And(a, b BoolPredicate) BoolPredicate
And is a boolean predicate that calls two boolean predicates and returns result of && operation on their return values.
func Contains ¶
func Contains(a interface{}, b interface{}) BoolPredicate
Contains checks if string slice contains a string Contains([]string{"a", "b"}, "b") -> true.
func Equals ¶
func Equals(a interface{}, b interface{}) BoolPredicate
Equals can compare complex objects, e.g. arrays of strings and strings together.
func Not ¶ added in v1.1.0
func Not(a BoolPredicate) BoolPredicate
Not is a boolean predicate that calls a boolean predicate and returns negated result.
func Or ¶
func Or(a, b BoolPredicate) BoolPredicate
Or is a boolean predicate that calls two boolean predicates and returns result of || operation on their return values.
type Def ¶
type Def struct { Operators Operators // Function matching is case sensitive, e.g. Len is different from len Functions map[string]interface{} // GetIdentifier returns value of any identifier passed in the form []string{"id", "field", "subfield"} GetIdentifier GetIdentifierFn // GetProperty returns property from a map GetProperty GetPropertyFn }
Def contains supported operators (e.g. LT, GT) and functions passed in as a map.
type GetIdentifierFn ¶
GetIdentifierFn function returns identifier based on selector e.g. id.field.subfield will be passed as. GetIdentifierFn([]string{"id", "field", "subfield"}).
type GetPropertyFn ¶
type GetPropertyFn func(mapVal, keyVal interface{}) (interface{}, error)
GetPropertyFn returns property from a mapVal by key keyVal.