Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CESQL user defined function support #1110

Open
csc-piscopo opened this issue Dec 6, 2024 · 0 comments
Open

CESQL user defined function support #1110

csc-piscopo opened this issue Dec 6, 2024 · 0 comments

Comments

@csc-piscopo
Copy link

Docs seem to indicate adding a user defined function is available and supported but I can't get this to work: (lifted from the README)

package main

import (
	"fmt"
	"os"
	"strings"

	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"
)

func main() {
	// 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")
	}
}

Referencing v2.15.2 for the sdk. Maybe I am missing something obvious, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant