celpp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: Apache-2.0 Imports: 5 Imported by: 1

README

CEL Pre-processor

Go Reference

This repo contains a simple CEL pre-processor. This allows passing in higher-level CEL expressions, and emitting standard ones.

This is done using CEL's own parser and macro system. The pre-processor allows defining custom macros, expands them, and emits the expanded expression. Builtin macros are not expanded, so the result is not overly bloated.

Examples

Below shows some examples. This is an incomplete list, see the Go pkg documentation for an exhaustive list.

Note this is also just the builtin ones; library users can define their own as well.

Default

Input: has(self.x) && default(self.y, 0)

Output: has(self.x) && (has(self.y) ? self.y : 0)

Index

Input: self.index({}, x, z, b)

Output: (has(self.x) && has(self.x.z) && has(self.x.z.b)) ? self.x.z.b : {}

Future additions

  • Custom operator overloads
  • Schema awareness. For instance, rather than default(self.y, 0), can we do default(self.y) if we know y is an int?

Documentation

Overview

Example (BuiltinDefault)
package main

import (
	"fmt"

	"github.com/howardjohn/celpp"
	"github.com/howardjohn/celpp/macros"
)

func main() {
	parser, _ := celpp.New(macros.All...)
	output, _ := parser.Process("default(self.x, 'DEF')")
	fmt.Println(output)
}
Output:

has(self.x) ? self.x : "DEF"
Example (BuiltinIndex)
package main

import (
	"fmt"

	"github.com/howardjohn/celpp"
	"github.com/howardjohn/celpp/macros"
)

func main() {
	parser, _ := celpp.New(macros.All...)
	output, _ := parser.Process("self.index({}, x, z, b)")
	fmt.Println(output)
}
Output:

(has(self.x) && has(self.x.z) && has(self.x.z.b)) ? self.x.z.b : {}
Example (BuiltinOneof)
package main

import (
	"fmt"

	"github.com/howardjohn/celpp"
	"github.com/howardjohn/celpp/macros"
)

func main() {
	parser, _ := celpp.New(macros.All...)
	output, _ := parser.Process("oneof(self.x, self.y, self.z)")
	fmt.Println(output)
}
Output:

(has(self.x) ? 1 : 0) + (has(self.y) ? 1 : 0) + (has(self.z) ? 1 : 0) <= 1
Example (BuiltinUnrollmap)
package main

import (
	"fmt"

	"github.com/howardjohn/celpp"
	"github.com/howardjohn/celpp/macros"
)

func main() {
	parser, _ := celpp.New(macros.All...)
	output, _ := parser.Process("self.unrollmap(0, 3, x, x.matches.size()).sum()<=128")
	fmt.Println(output)
}
Output:

[(size(self) > 0) ? ([self[0]].map(x, x.matches.size())[0]) : 0, (size(self) > 1) ? ([self[1]].map(x, x.matches.size())[0]) : 0, (size(self) > 2) ? ([self[2]].map(x, x.matches.size())[0]) : 0].sum() <= 128

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Preprocessor

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

func New

func New(macros ...cel.Macro) (*Preprocessor, error)

func (*Preprocessor) Process

func (p *Preprocessor) Process(expr string) (string, error)

func (*Preprocessor) ProcessToAST

func (p *Preprocessor) ProcessToAST(expr string) (*celast.AST, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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