diff --git a/.gitignore b/.gitignore index 33337d66d..ed1b48561 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ /coverage.out /bin /target - +.DS_Store node_modules __pycache__ /coverage.out diff --git a/templates/go/cloudevents/README.md b/templates/go/cloudevents/README.md index 23ae0db00..abe0fcc55 100644 --- a/templates/go/cloudevents/README.md +++ b/templates/go/cloudevents/README.md @@ -1,6 +1,6 @@ # Go Cloud Events Function -Welcome to your new Go Function! The boilerplate function code can be found in [`handle.go`](handle.go). This Function is meant to respond exclusively to [Cloud Events](https://cloudevents.io/), but you can remove the check for this in the function and it will respond just fine to plain vanilla incoming HTTP requests. +Welcome to your new Go Function! The boilerplate function code can be found in [`handle.go`](handle.go). This Function is meant to respond exclusively to [Cloud Events](https://cloudevents.io/), but you can remove the check for this in the function and it will respond just fine to plain vanilla incoming HTTP requests. ## Development @@ -9,13 +9,14 @@ Develop new features by adding a test to [`handle_test.go`](handle_test.go) for Update the running analog of the function using the `func` CLI or client library, and it can be invoked using a manually-created CloudEvent: ```console -curl -X POST -d '{"hello": "world"}' \ +curl -v -X POST -d 'hello' \ -H'Content-type: application/json' \ -H'Ce-id: 1' \ -H'Ce-source: cloud-event-example' \ - -H'Ce-type: dev.knative.example' \ + -H'Ce-subject: Echo content' \ + -H'Ce-type: MyEvent' \ -H'Ce-specversion: 1.0' \ - http://myFunction.example.com/ + http://localhost:8080/ ``` For more, see [the complete documentation]('https://github.com/knative-sandbox/kn-plugin-func/tree/main/docs') diff --git a/templates/go/cloudevents/handle.go b/templates/go/cloudevents/handle.go index 81e6b4946..c5d724bdc 100644 --- a/templates/go/cloudevents/handle.go +++ b/templates/go/cloudevents/handle.go @@ -2,13 +2,16 @@ package function import ( "context" + "encoding/json" "fmt" - event "github.com/cloudevents/sdk-go/v2" + "github.com/cloudevents/sdk-go/v2/event" + + cloudevents "github.com/cloudevents/sdk-go/v2" ) // Handle an event. -func Handle(ctx context.Context, event event.Event) error { +func Handle(ctx context.Context, event cloudevents.Event) (*event.Event, error) { /* * YOUR CODE HERE @@ -16,10 +19,23 @@ func Handle(ctx context.Context, event event.Event) error { * Try running `go test`. Add more test as you code in `handle_test.go`. */ - // Example implementation: - fmt.Printf("%v\n", event) // print the received event to standard output + fmt.Printf("Incoming Event: %v\n", event) // print the received event to standard output + payload := "" + err := json.Unmarshal(event.Data(), &payload) + if err != nil { + fmt.Printf("%v\n", err) + return nil, err + } - return nil + payload = "echo " + payload + outputEvent := cloudevents.NewEvent() + outputEvent.SetSource("http://example.com/echo") + outputEvent.SetType("Echo") + outputEvent.SetData(cloudevents.ApplicationJSON, &payload) + + fmt.Printf("Outgoing Event: %v\n", outputEvent) + + return &outputEvent, nil } /* diff --git a/templates/go/cloudevents/handle_test.go b/templates/go/cloudevents/handle_test.go index 5de9c2fa0..5c4bb7a24 100644 --- a/templates/go/cloudevents/handle_test.go +++ b/templates/go/cloudevents/handle_test.go @@ -2,8 +2,11 @@ package function import ( "context" + "encoding/json" "testing" + cloudevents "github.com/cloudevents/sdk-go/v2" + "github.com/cloudevents/sdk-go/v2/event" ) @@ -12,11 +15,30 @@ func TestHandle(t *testing.T) { // A minimal, but valid, event. event := event.New() event.SetID("TEST-EVENT-01") - event.SetType("com.example.event.test") + event.SetType("MyEvent") event.SetSource("http://localhost:8080/") - + event.SetSubject("Echo") + input := "hello" + event.SetData(cloudevents.ApplicationJSON, &input) // Invoke the defined handler. - if err := Handle(context.Background(), event); err != nil { + ce, err := Handle(context.Background(), event) + if err != nil { t.Fatal(err) } + + if ce == nil { + t.Errorf("The output CloudEvent cannot be nil") + } + if ce.Type() != "Echo" { + t.Errorf("Wrong CloudEvent Type received: %v , expected Echo", ce.Type()) + } + output := "" + err = json.Unmarshal(ce.Data(), &output) + if err != nil { + t.Fatal(err) + } + if output != "echo "+input { + t.Errorf("The expected output should be: 'echo hello and it was: %v", output) + } + }