cel-go

module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2019 License: Apache-2.0

README

Common Expression Language - Go Toolchain

Build Status Go Report Card

This repo contains the Go toolchain for the Common Expression Language (CEL). CEL is a non-Turing complete language designed to be portable and fast. It is best suited to applications where sandboxing a full-fledged language like JavaScript or Lua would be too resource intensive, but side-effect free dynamic computations are strongly desired.

Want to learn more?

Want to integrate CEL?

  • See GoDoc to learn how to integrate CEL into services written in Go. GoDoc
  • See the CEL C++ toolchain (under development) for information about how to integrate CEL evaluation into other environments.

Want to contribute?

How does CEL work?

Write an expression in the CEL syntax, then parse, check, and interpret.

Step Description
Parse Parses a string expression to a protocol buffer representation.
Check Type-checks a parsed expression against a given environment.
Interpret Evaluates parsed expressions against a set of inputs.

Type-checking an expression in an optional, but strongly encouraged step that can be used to reject some expressions as semantically invalid using static analysis. Additionally, the type-check produces some additional metadata related to function overload resolution and object field selection which may be used by the interpret step to speed up execution.

Example

The following example shows the parse, check, and intepretation of a simple program. After the parse and type-check steps, the service checks whether there are any errors that need to be reported.

import(
	"fmt"
	"log"

	"github.com/google/cel-go/cel",
	"github.com/google/cel-go/checker/decls"
)

func main() {
	// Variables used within this expression environment.
	decls := cel.Declarations(
		decls.NewIdent("i", decls.String, nil),
		decls.NewIdent("you", decls.String, nil))
	env, err := cel.NewEnv(decls)
	if err != nil {
		log.Fatalf("environment creation error: %s\n", err)
	}

	// Parse and type-check the expression.
	//
	// Detailed information about which issues/errors were encountered may be
	// found within the `iss`, but a non-nil `iss.Err()` value will contain a
	// detailed error string in human-readable form.
	p, iss := env.Parse(`"Hello " + you + "! I'm " + i + "."`)
	if iss != nil && iss.Err() != nil {
		log.Fatalln(iss.Err())
	}
	c, iss := env.Check(p)
	if iss != nil && iss.Err() != nil {
		log.Fatalln(iss.Err())
	}

	// Create the program, and evaluate it against some input.
	prg, err := env.Program(c)
	if err != nil {
		log.Fatalf("program creation error: %s\n", err)
	}

	// Note, the second result, the EvalDetails will be non-nil when using
	// either cel.OptTrackState or cel.OptExhaustiveEval (not shown). The
	// details value holds information about the evaluation, rather than the
	// output of the evaluation itself.
	out, _, err := prg.Eval(cel.Vars(map[string]interface{}{
		"i": "CEL",
		"you": "world"}))
	if err != nil {
		log.Fatalf("runtime error: %s\n", err)
	}

	// Hello world! I'm CEL.
	fmt.Println(out)
}

More examples like these can be found within the unit tests which can be run using Bazel:

bazel test ...

License

Released under the Apache License.

Disclaimer: This is not an official Google product.

Directories

Path Synopsis
Package cel defines the top-level interface for the Common Expression Language (CEL).
Package cel defines the top-level interface for the Common Expression Language (CEL).
Package checker defines functions to type-checked a parsed expression against a set of identifier and function declarations.
Package checker defines functions to type-checked a parsed expression against a set of identifier and function declarations.
decls
Package decls provides helpers for creating variable and function declarations.
Package decls provides helpers for creating variable and function declarations.
Package common defines types and utilities common to expression parsing, checking, and interpretation
Package common defines types and utilities common to expression parsing, checking, and interpretation
debug
Package debug provides tools to print a parsed expression graph and adorn each expression element with additional metadata.
Package debug provides tools to print a parsed expression graph and adorn each expression element with additional metadata.
operators
Package operators defines the internal function names of operators.
Package operators defines the internal function names of operators.
overloads
Package overloads defines the internal overload identifiers for function and operator overloads.
Package overloads defines the internal overload identifiers for function and operator overloads.
packages
Package packages defines types for interpreting qualified names.
Package packages defines types for interpreting qualified names.
types
Package types contains the types, traits, and utilities common to all components of expression handling.
Package types contains the types, traits, and utilities common to all components of expression handling.
types/pb
Package pb reflects over protocol buffer descriptors to generate objects that simplify type, enum, and field lookup.
Package pb reflects over protocol buffer descriptors to generate objects that simplify type, enum, and field lookup.
types/ref
Package ref contains the reference interfaces used throughout the types components.
Package ref contains the reference interfaces used throughout the types components.
types/traits
Package traits defines interfaces that a type may implement to participate in operator overloads and function dispatch.
Package traits defines interfaces that a type may implement to participate in operator overloads and function dispatch.
Package interpreter provides functions to evaluate parsed expressions with the option to augment the evaluation with inputs and functions supplied at evaluation time.
Package interpreter provides functions to evaluate parsed expressions with the option to augment the evaluation with inputs and functions supplied at evaluation time.
functions
Package functions defines the standard builtin functions supported by the interpreter and as declared within the checker#StandardDeclarations.
Package functions defines the standard builtin functions supported by the interpreter and as declared within the checker#StandardDeclarations.
Package parser declares an expression parser with support for macro expansion.
Package parser declares an expression parser with support for macro expansion.
gen
main
Package main declares the executable entry point for the CEL server.
Package main declares the executable entry point for the CEL server.

Jump to

Keyboard shortcuts

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