sdk-go/sql/v2
github-actions[bot] ebd5561496
chore: update dependencies
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-06-16 03:33:59 +00:00
..
errors fix: cleaned up errors code 2024-07-12 13:29:37 -04:00
expression feat: sql expressions always return a value, along with the correct error types 2024-06-13 15:38:28 -04:00
function fix merge conflicts 2024-06-13 15:43:49 -04:00
gen fix: parser now correctly parses integer literals 2024-06-13 15:35:48 -04:00
parser feat: parser returns parse errors when applicable 2024-06-13 15:37:59 -04:00
runtime fix merge conflicts 2024-06-13 15:43:49 -04:00
test fix merge conflicts 2024-06-13 15:43:49 -04:00
utils feat: sql casting handles booleans correctly and returns errors with correct type 2024-06-13 15:36:38 -04:00
CESQLLexer.g4 fix: parser now correctly parses integer literals 2024-06-13 15:35:48 -04:00
CESQLParser.g4 CESQL (#680) 2021-04-26 09:47:30 +02:00
Makefile Adding workaround to an overflow error in CE SQL parser (#748) 2021-12-27 09:12:29 -08:00
README.md Added: 2024-05-09 18:16:44 +00:00
expression.go CESQL (#680) 2021-04-26 09:47:30 +02:00
function.go all expressions have a defined return type 2024-05-31 11:38:38 -04:00
go.mod chore: update dependencies 2025-06-16 03:33:59 +00:00
go.sum chore: update dependencies 2025-06-16 03:33:59 +00:00
types.go all expressions have a defined return type 2024-05-31 11:38:38 -04:00

README.md

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