trigger

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 30 Imported by: 2

README

trigger

GoDoc

a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

	decision, err := trigger.NewDecision("this.email.endsWith('acme.com')")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	trigg, err := trigger.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

CEL Macro Extensions

Additional details on the standard CEL spec/library may be found here

function notation description
now now() int64 current timestamp in unix secods
uuid uuid() string random uuidv4 string
sha1 sha1(string) string sha1 hash of the input string
sha256 sha256(string) string sha256 hash of the input string
sha3 sha3(string) string sha3 hash of the input string
base64Encode base64Encode(string) string base64 encoded version of the input
base64Decode base64Decode(string) string base64 decoded version of the input
jsonEncode jsonEncode(string) string json encoded version of the input
jsonDecode jsonDecode(string) string json decoded version of the input
includes includes(arr list(any), element any) bool returns whether the slice includes the element
replace replace(text string, old string, new string) string full string replacement of the old value with the new value
join join(arr list(string), sep string) string joins the array into a single string with the given separator
titleCase titleCase(string) string converts the input into title case string
lowerCase lowerCase(string) string converts the input into lower case string
upperCase upperCase(string) string converts the input into upper case string
trimSpace trimSpace(string) string removes white spaces from the input string
trimPrefix trimPrefix(string) string removes prefix from the input string
trimSuffix trimSuffix(string) string removes suffix from the input string
split split(arr list(string), sep string) string slices s into all substrings separated by sep and returns a slice of the substrings between those separators
geoDistance geoDistance(this list(float64), that list(float64)) float64 haversine distance between two coordinates [lat,lng]
render render(tmplate string, data map[string]interface) string renders the input template with the provided data map
parseClaims parseClaims(jwt string) map[string]interface) string returns the payload of the jwt as a map
typeOf typeOf(any) string returns the go type of the input
encrypt encrypt(secret string, msg string) string aes encrypt a message with a given secret
decrypt decrypt(secret string, msg string) string aes decrypt a message with a given secret

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDecisionDenied   = errors.New("trigger: evaluation = false")
	ErrEmptyExpressions = errors.New("trigger: empty expressions")
)
View Source
var Functions = FuncMap{
	"now": {
		// contains filtered or unexported fields
	},
	"uuid": {
		// contains filtered or unexported fields
	},
	"sha1": {
		// contains filtered or unexported fields
	},
	"sha256": {
		// contains filtered or unexported fields
	},
	"sha3": {
		// contains filtered or unexported fields
	},
	"base64Encode": {
		// contains filtered or unexported fields
	},
	"base64Decode": {
		// contains filtered or unexported fields
	},
	"jsonEncode": {
		// contains filtered or unexported fields
	},
	"jsonDecode": {
		// contains filtered or unexported fields
	},
	"includes": {
		// contains filtered or unexported fields
	},
	"replace": {
		// contains filtered or unexported fields
	},
	"join": {
		// contains filtered or unexported fields
	},
	"titleCase": {
		// contains filtered or unexported fields
	},
	"lowerCase": {
		// contains filtered or unexported fields
	},
	"upperCase": {
		// contains filtered or unexported fields
	},
	"trimSpace": {
		// contains filtered or unexported fields
	},
	"trimPrefix": {
		// contains filtered or unexported fields
	},
	"trimSuffix": {
		// contains filtered or unexported fields
	},
	"split": {
		// contains filtered or unexported fields
	},
	"geoDistance": {
		// contains filtered or unexported fields
	},
	"render": {
		// contains filtered or unexported fields
	},
	"parseClaims": {
		// contains filtered or unexported fields
	},
	"typeOf": {
		// contains filtered or unexported fields
	},
	"encrypt": {
		// contains filtered or unexported fields
	},
	"decrypt": {
		// contains filtered or unexported fields
	},
}

Functions

This section is empty.

Types

type Decision

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

Decision is used to evaluate boolean expressions

func NewDecision

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/trigger"
)

func main() {
	decision, err := trigger.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

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

Eval evaluates the boolean CEL expressions against the Mapper

func (*Decision) Expression

func (e *Decision) Expression() string

Expressions returns the decsions raw expression

type FuncMap added in v0.0.10

type FuncMap map[string]*Function

type Function added in v0.0.10

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

func NewFunction added in v0.0.10

func NewFunction(decl *expr.Decl, overload *functions.Overload) *Function

type Trigger

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

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/trigger"
)

func main() {
	decision, err := trigger.NewDecision("this.email.endsWith('acme.com')")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	trigg, err := trigger.NewTrigger(decision, `
	{
		'admin': true,
		'updated_at': now(),
		'email_sha1': sha1("this.email"),
		'email_sha3': sha3("this.email"),
		'email_sha256': sha256("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_sha1"], data["email_sha3"], data["email_sha256"])
}
Output:

true true bbd5d1877fc1db4e1dc12fbd39dd0989cf422be5 1ec04699856dcbef0f32413a71b6c8a1228de6663f46159f0084b0ecbccb7a8ca3e7928028650ad318f2d52e2ed5b9edecfc46c088557e5fa640f94c3fec8c46 2fee51920dc7672e5c66b328a4b4fff0382c4552f893f7a92747a213085855dd

func (*Trigger) Expression

func (e *Trigger) Expression() string

Expression returns the triggers raw CEL expressions

func (*Trigger) Trigger

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