rule

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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

type Expr interface {
	Eval(Params) (*Value, error)
}

An Expr is a logical expression that can be evaluated to a value.

func And

func And(v1, v2 Expr, vN ...Expr) Expr

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

func Eq(v1, v2 Expr, vN ...Expr) Expr

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

func FNV(v Expr) Expr

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

func GT(v1, v2 Expr, vN ...Expr) Expr

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

func GTE(v1, v2 Expr, vN ...Expr) Expr

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

func In(v, e1 Expr, eN ...Expr) Expr

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

func LT(v1, v2 Expr, vN ...Expr) Expr

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

func LTE(v1, v2 Expr, vN ...Expr) Expr

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

func Not(e Expr) Expr

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

func Or(v1, v2 Expr, vN ...Expr) Expr

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

func Percentile(v, p Expr) Expr

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.

func True

func True() Expr

True creates an expression that always evaluates to true.

Example
package main

import (
	"fmt"
	"log"

	"github.com/heetch/regula/rule"
)

func main() {
	tree := rule.True()

	val, err := tree.Eval(nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(val.Data)
}
Output:

true

type Param

type Param struct {
	Kind string `json:"kind"`
	Type string `json:"type"`
	Name string `json:"name"`
}

Param is an expression used to select a parameter passed during evaluation and return its corresponding value.

func BoolParam

func BoolParam(name string) *Param

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

func Float64Param(name string) *Param

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

func Int64Param(name string) *Param

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

func StringParam(name string) *Param

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

func (*Param) Eval

func (p *Param) Eval(params Params) (*Value, error)

Eval extracts a value from the given parameters.

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

type Rule struct {
	Expr   Expr   `json:"expr"`
	Result *Value `json:"result"`
}

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

func New(expr Expr, result *Value) *Rule

New creates a rule with the given expression and that returns the given result on evaluation.

func (*Rule) Eval

func (r *Rule) Eval(params Params) (*Value, error)

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) Params

func (r *Rule) Params() []Param

Params returns a list of all the parameters expected by this rule.

func (*Rule) UnmarshalJSON

func (r *Rule) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Value

type Value struct {
	Kind string `json:"kind"`
	Type string `json:"type"`
	Data string `json:"data"`
}

A Value is the result of the evaluation of an expression.

func BoolValue

func BoolValue(value bool) *Value

BoolValue creates a bool type value.

func Float64Value

func Float64Value(value float64) *Value

Float64Value creates a float64 type value.

func Int64Value

func Int64Value(value int64) *Value

Int64Value creates an int64 type value.

func StringValue

func StringValue(value string) *Value

StringValue creates a string type value.

func (*Value) Equal

func (v *Value) Equal(other *Value) bool

Equal reports whether v and other represent the same value.

func (*Value) Eval

func (v *Value) Eval(Params) (*Value, error)

Eval evaluates the value to itself.

func (*Value) GT added in v0.6.0

func (v *Value) GT(other *Value) (bool, error)

GT reports whether v is greater than other.

func (*Value) GTE added in v0.6.0

func (v *Value) GTE(other *Value) (bool, error)

GTE reports whether v is greater or equal than other.

func (*Value) LT added in v0.6.0

func (v *Value) LT(other *Value) (bool, error)

LT reports whether v is less than other.

func (*Value) LTE added in v0.6.0

func (v *Value) LTE(other *Value) (bool, error)

LTE reports whether v is less or equal than other.

Jump to

Keyboard shortcuts

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