eval

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

eval

GoDoc

an evaluation framework backed by Google's Common Expression Language

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDecisionDenied = errors.New("eval: evaluation = false")
	ErrNoExpressions  = errors.New("eval: no expressions")
)

Functions

This section is empty.

Types

type Decision added in v0.0.1

type Decision struct {
	// contains filtered or unexported fields
}

Decision is used to evaluate boolean expressions

func NewDecision added in v0.0.1

func NewDecision(expressions []string) (*Decision, error)

NewDecision creates a new Decision with the given boolean CEL expressions

Example
package main

import (
	"fmt"
	"github.com/graphikDB/eval"
	"strings"
)

func main() {
	decision, err := eval.NewDecision([]string{"this.email.endsWith('acme.com')"})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	if err := decision.AddExpression("this.name != ''"); err != nil {
		fmt.Println(err.Error())
		return
	}
	if err := decision.Eval(eval.MapperFunc(func() map[string]interface{} {
		return map[string]interface{}{
			"name":  "bob",
			"email": "bob@acme.com",
		}
	}), eval.AllTrue); err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(strings.Join(decision.Expressions(), ","))
}
Output:

this.email.endsWith('acme.com'),this.name != ''

func (*Decision) AddExpression added in v0.0.1

func (n *Decision) AddExpression(expression string) error

AddExpression adds an expression to the decision tree

func (*Decision) Eval added in v0.0.1

func (n *Decision) Eval(mapper Mapper, typ DecisionType) error

Eval evaluates the boolean CEL expressions against the Mapper

func (*Decision) Expressions added in v0.0.1

func (e *Decision) Expressions() []string

Expressions returns the decsions raw expressions

type DecisionType added in v0.0.1

type DecisionType int
const (
	AllTrue DecisionType = 0
	AnyTrue DecisionType = 1
)

type Mapper

type Mapper interface {
	AsMap() map[string]interface{}
}

Mapper returns it's values as a map[string]interface{}

type MapperFunc

type MapperFunc func() map[string]interface{}

MapperFunc implements Mapper

func (MapperFunc) AsMap

func (m MapperFunc) AsMap() map[string]interface{}

Jump to

Keyboard shortcuts

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