eval

package module
v0.0.7 Latest Latest
Warning

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

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

README

eval

GoDoc

a decision & trigger 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")
	ErrEmptyExpressions = errors.New("eval: empty 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(expression string) (*Decision, error)

NewDecision creates a new Decision with the given boolean CEL expressions

Example
package main

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

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

this.email.endsWith('acme.com')

func (*Decision) Eval added in v0.0.1

func (n *Decision) Eval(data map[string]interface{}) error

Eval evaluates the boolean CEL expressions against the Mapper

func (*Decision) Expression added in v0.0.6

func (e *Decision) Expression() string

Expressions returns the decsions raw expression

type Trigger added in v0.0.2

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

Trigger creates values as map[string]interface{} if it's decisider returns no errors against a Mapper

func NewTrigger added in v0.0.2

func NewTrigger(decision *Decision, triggerExpression string) (*Trigger, error)

NewTrigger creates a new trigger instance from the decision & trigger expressions

Example
package main

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

func main() {
	decision, err := eval.NewDecision("this.email.endsWith('acme.com')")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	trigg, err := eval.NewTrigger(decision, `
	{
		'admin': true,
		'updated_at': now(),
		'email_hash': sha1(this.email)
	}
`)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	person := map[string]interface{}{
		"name":  "bob",
		"email": "bob@acme.com",
	}
	data, err := trigg.Trigger(person)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(data["admin"], data["updated_at"].(int64) > 0, data["email_hash"])
}
Output:

true true 6fd706dd2d151c2bf79218a2acd764a7d3eed7e3

func (*Trigger) Expression added in v0.0.6

func (e *Trigger) Expression() string

Expression returns the triggers raw CEL expressions

func (*Trigger) Trigger added in v0.0.2

func (t *Trigger) Trigger(data map[string]interface{}) (map[string]interface{}, error)

Trigger executes it's decision against the Mapper and then overwrites the

Jump to

Keyboard shortcuts

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