-
Notifications
You must be signed in to change notification settings - Fork 235
Open
Description
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!
Metadata
Metadata
Assignees
Labels
No labels