Categorygithub.com/graphikDB/trigger
modulepackage
0.0.19
Repository: https://github.com/graphikdb/trigger.git
Documentation: pkg.go.dev

# README

trigger

https://graphikdb.github.io/trigger/

GoDoc

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

  • Full Text Search Expression Macros/Functions(startsWith, endsWith, contains)
  • RegularExp Expression Macros/Functions(matches)
  • Geographic Expression Macros/Functions(geoDistance)
  • Cryptographic Expression Macros/Functions(encrypt, decrypt, sha1, sha256, sha3)
  • JWT Expression Macros/Functions(parseClaims, parseHeader, parseSignature)
  • Collection Expression Macros/Functions(in, map, filter, exists)
  • String Manipulation Expression Macros/Functions(replace, join, titleCase, lowerCase, upperCase, trimSpace, trimPrefix, trimSuffix, render)
  • URL Introspection Expression Macros/Functions(parseHost, parseScheme, parseQuery, parsePath)

Use Case:

Since this expression language requires just input data(map[string]interface) and an expression string, Go programs may use it to embed flexible logic that may be changed at runtime without having to recompile.

  • Authorization Middleware/Policy Evaluation/Rule Engine

  • Database or API "triggers" for mutating data before its commited

  • Search Engine(filter something based on a decision)

Examples

restrict access based on domain

	decision, err := trigger.NewDecision("this.email.endsWith('acme.com')")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	err = decision.Eval(map[string]interface{}{
		"email": "[email protected]",
	})
	fmt.Println(err == trigger.ErrDecisionDenied) 
    // Output: true 

create a trigger based on signup event that adds updated_at timestamp & hashes a password

	// create a trigger based on the new decision that hashes a password and creates an updated_at timestamp
	// this would in theory be applied to a newly created user after signup
	trigg, err := trigger.NewArrowTrigger(`
	this.event == 'signup' && has(this.email) =>
	{
		'updated_at': now(),
		'password': this.password.sha1()
	}
`)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	user := map[string]interface{}{
		"event": "signup",
		"name":  "bob",
		"email": "[email protected]",
		"password": "123456",
	}
	data, err := trigg.Trigger(user)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(data["updated_at"].(int64) > 0, data["password"])
	// Output: true 7c4a8d09ca3762af61e59520943dc26494f8941b

CEL Macro Extensions

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

functionnotationdescription
nownow() int64current timestamp in unix secods
uuiduuid() stringrandom uuidv4 string
sha1sha1(string) stringsha1 hash of the input string
sha256sha256(string) stringsha256 hash of the input string
sha3sha3(string) stringsha3 hash of the input string
base64Encodebase64Encode(string) stringbase64 encoded version of the input
base64Decodebase64Decode(string) stringbase64 decoded version of the input
jsonEncodejsonEncode(string) stringjson encoded version of the input
jsonDecodejsonDecode(string) stringjson decoded version of the input
replacereplace(text string, old string, new string) stringfull string replacement of the old value with the new value
joinjoin(arr list(string), sep string) stringjoins the array into a single string with the given separator
titleCasetitleCase(string) stringconverts the input into title case string
lowerCaselowerCase(string) stringconverts the input into lower case string
upperCaseupperCase(string) stringconverts the input into upper case string
trimSpacetrimSpace(string) stringremoves white spaces from the input string
trimPrefixtrimPrefix(string) stringremoves prefix from the input string
trimSuffixtrimSuffix(string) stringremoves suffix from the input string
geoDistancegeoDistance(this list(float64), that list(float64)) float64haversine distance between two coordinates [lat,lng]
renderrender(tmplate string, data map[string]interface) stringrenders the input template with the provided data map
parseClaimsparseClaims(jwt string) map[string]interface)returns the payload of the jwt as a map
parseHeaderparseHeader(jwt string) map[string]interfacereturns the header of the jwt as a map
parseSignatureparseSignature(jwt string) stringreturns the signature of the jwt as a string
typeOftypeOf(any) stringreturns the go type of the input
encryptencrypt(secret string, msg string) stringaes encrypt a message with a given secret
decryptdecrypt(secret string, msg string) stringaes decrypt a message with a given secret

# Functions

NewArrowTrigger creates a trigger from arrow syntax ${decision} => ${mutation}.
NewDecision creates a new Decision with the given boolean CEL expressions.
No description provided by the author
NewTrigger creates a new trigger instance from the decision & trigger expressions.

# Constants

No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Decision is used to evaluate boolean expressions.
No description provided by the author
Trigger creates values as map[string]interface{} if it's decisider returns no errors against a Mapper.

# Type aliases

No description provided by the author