Help us shape the future of Go! Take the 2026 Go Developer Survey

v2

package module
v2.16.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: Apache-2.0 Imports: 1 Imported by: 2

README

CloudEvents Expression Language Go implementation

CloudEvents Expression Language implementation.

Note: this package is a work in progress, APIs might break in future releases.

User guide

To start using it:

import cesqlparser "github.com/cloudevents/sdk-go/sql/v2/parser"

// Parse the expression
expression, err := cesqlparser.Parse("subject = 'Hello world'")

// Res can be either int32, bool or string
res, err := expression.Evaluate(event)

Add a user defined function

import (
    cesql "github.com/cloudevents/sdk-go/sql/v2"
    cefn "github.com/cloudevents/sdk-go/sql/v2/function"
    cesqlparser "github.com/cloudevents/sdk-go/sql/v2/parser"
    ceruntime "github.com/cloudevents/sdk-go/sql/v2/runtime"
    cloudevents "github.com/cloudevents/sdk-go/v2"
)

// Create a test event
event := cloudevents.NewEvent()
event.SetID("aaaa-bbbb-dddd")
event.SetSource("https://my-source")
event.SetType("dev.tekton.event")

// Create and add a new user defined function
var HasPrefixFunction cesql.Function = cefn.NewFunction(
    "HASPREFIX",
    []cesql.Type{cesql.StringType, cesql.StringType},
    nil,
    func(event cloudevents.Event, i []interface{}) (interface{}, error) {
        str := i[0].(string)
        prefix := i[1].(string)

        return strings.HasPrefix(str, prefix), nil
    },
)

err := ceruntime.AddFunction(HasPrefixFunction)

// parse the expression 
expression, err := cesqlparser.Parse("HASPREFIX(type, 'dev.tekton.event')")
	if err != nil {
		fmt.Println("parser err: ", err)
		os.Exit(1)
	}

// Evalute the expression with the test event
res, err := expression.Evaluate(event)

if res.(bool) {
    fmt.Println("Event type has the prefix")
} else {
    fmt.Println("Event type doesn't have the prefix")
}

Development guide

To regenerate the parser, make sure you have ANTLR4 installed and then run:

antlr4 -v 4.10.1 -Dlanguage=Go -package gen -o gen -visitor -no-listener CESQLParser.g4

Then you need to run this sed command as a workaround until this ANTLR issue is resolved. Without this, building for 32bit platforms will throw an int overflow error:

sed -i 's/(1<</(int64(1)<</g' gen/cesqlparser_parser.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expression

type Expression interface {

	// Evaluate the expression using the provided input type.
	// The return value can be either int32, bool or string.
	// The evaluation fails as soon as an error arises.
	Evaluate(event cloudevents.Event) (interface{}, error)
}

Expression represents a parsed CloudEvents SQL Expression.

type Function

type Function interface {
	Name() string
	Arity() int
	IsVariadic() bool
	ArgType(index int) *Type
	ReturnType() Type

	Run(event cloudevents.Event, arguments []interface{}) (interface{}, error)
}

type Type

type Type uint8
const (
	StringType Type = iota
	IntegerType
	BooleanType
	AnyType
)

func TypeFromVal

func TypeFromVal(val interface{}) Type

func TypePtr

func TypePtr(t Type) *Type

func (Type) IsSameType

func (t Type) IsSameType(val interface{}) bool

func (Type) String

func (t Type) String() string

func (Type) ZeroValue added in v2.16.1

func (t Type) ZeroValue() interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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