mirror of https://github.com/knative/func.git
src: simplify go templates (#741)
This commit is contained in:
parent
64ba17b4fb
commit
4504b21369
|
@ -3,30 +3,39 @@ package function
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
event "github.com/cloudevents/sdk-go/v2"
|
event "github.com/cloudevents/sdk-go/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handle a CloudEvent.
|
// Handle an event.
|
||||||
// Supported Function signatures:
|
|
||||||
// * func()
|
|
||||||
// * func() error
|
|
||||||
// * func(context.Context)
|
|
||||||
// * func(context.Context) error
|
|
||||||
// * func(event.Event)
|
|
||||||
// * func(event.Event) error
|
|
||||||
// * func(context.Context, event.Event)
|
|
||||||
// * func(context.Context, event.Event) error
|
|
||||||
// * func(event.Event) *event.Event
|
|
||||||
// * func(event.Event) (*event.Event, error)
|
|
||||||
// * func(context.Context, event.Event) *event.Event
|
|
||||||
// * func(context.Context, event.Event) (*event.Event, error)
|
|
||||||
func Handle(ctx context.Context, event event.Event) error {
|
func Handle(ctx context.Context, event event.Event) error {
|
||||||
if err := event.Validate(); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "invalid event received. %v", err)
|
/*
|
||||||
return err
|
* YOUR CODE HERE
|
||||||
}
|
*
|
||||||
fmt.Printf("%v\n", event)
|
* 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
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Other supported function signatures:
|
||||||
|
|
||||||
|
Handle()
|
||||||
|
Handle() error
|
||||||
|
Handle(context.Context)
|
||||||
|
Handle(context.Context) error
|
||||||
|
Handle(event.Event)
|
||||||
|
Handle(event.Event) error
|
||||||
|
Handle(context.Context, event.Event)
|
||||||
|
Handle(context.Context, event.Event) error
|
||||||
|
Handle(event.Event) *event.Event
|
||||||
|
Handle(event.Event) (*event.Event, error)
|
||||||
|
Handle(context.Context, event.Event) *event.Event
|
||||||
|
Handle(context.Context, event.Event) (*event.Event, error)
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
|
@ -20,13 +20,3 @@ func TestHandle(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHandleInvalid ensures that an invalid input event generates an error.
|
|
||||||
func TestInvalidInput(t *testing.T) {
|
|
||||||
invalidEvent := event.New() // missing required fields
|
|
||||||
|
|
||||||
// Attempt to handle the invalid event, ensuring that the handler validats events.
|
|
||||||
if err := Handle(context.Background(), invalidEvent); err == nil {
|
|
||||||
t.Fatalf("handler did not generate error on invalid event. Missing .Validate() check?")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,15 +4,17 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handle an HTTP Request.
|
// Handle an HTTP Request.
|
||||||
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) {
|
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) {
|
||||||
res.Header().Add("Content-Type", "text/plain")
|
/*
|
||||||
|
* YOUR CODE HERE
|
||||||
|
*
|
||||||
|
* Try running `go test`. Add more test as you code in `handle_test.go`.
|
||||||
|
*/
|
||||||
|
|
||||||
_, err := fmt.Fprintf(res, "OK\n")
|
// Example implementation:
|
||||||
if err != nil {
|
fmt.Println("OK") // Print "OK" to standard output (local logs)
|
||||||
fmt.Fprintf(os.Stderr, "error or response write: %v", err)
|
fmt.Fprintln(res, "OK") // Send "OK" back to the client
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestHandle ensures that Handle executes without error, returns 2000 and sets
|
// TestHandle ensures that Handle executes without error and returns the
|
||||||
// and sets a content-type.
|
// HTTP 200 status code indicating no errors.
|
||||||
func TestHandle(t *testing.T) {
|
func TestHandle(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
|
@ -21,6 +21,7 @@ func TestHandle(t *testing.T) {
|
||||||
func(w http.ResponseWriter, req *http.Request) {
|
func(w http.ResponseWriter, req *http.Request) {
|
||||||
Handle(context.Background(), w, req)
|
Handle(context.Background(), w, req)
|
||||||
}(w, req)
|
}(w, req)
|
||||||
|
|
||||||
res = w.Result()
|
res = w.Result()
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
@ -31,8 +32,4 @@ func TestHandle(t *testing.T) {
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
t.Fatalf("unexpected response code: %v", res.StatusCode)
|
t.Fatalf("unexpected response code: %v", res.StatusCode)
|
||||||
}
|
}
|
||||||
if res.Header.Get("Content-Type") == "" {
|
|
||||||
t.Fatal("Handler did not set a content type for the response")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue