predicate

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

README

Predicate

Predicate package 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)
    }
}

func main(){
    // Create a new parser and define the supported operators and methods
    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
}

Documentation

Overview

Predicate package 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

func GetFieldByTag(ival interface{}, tagName string, fieldNames []string) (interface{}, error)

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

Not is a boolean predicate that calls a boolean predicate and returns negated result

func Or

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
	// 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

type GetIdentifierFn func(selector []string) (interface{}, error)

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 reuturns property from a mapVal by key keyVal

type Operators

type Operators struct {
	EQ  interface{}
	NEQ interface{}

	LT interface{}
	GT interface{}

	LE interface{}
	GE interface{}

	OR  interface{}
	AND interface{}
	NOT interface{}
}

Operators contain functions for equality and logical comparison.

type Parser

type Parser interface {
	Parse(string) (interface{}, error)
}

Parser takes the string with expression and calls the operators and functions.

func NewParser

func NewParser(d Def) (Parser, error)

Directories

Path Synopsis
package builder is used to construct predicate expressions using builder functions.
package builder is used to construct predicate expressions using builder functions.

Jump to

Keyboard shortcuts

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