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 (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
event "github.com/cloudevents/sdk-go/v2"
|
||||
)
|
||||
|
||||
// Handle a CloudEvent.
|
||||
// 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)
|
||||
// Handle an event.
|
||||
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
|
||||
}
|
||||
fmt.Printf("%v\n", event)
|
||||
|
||||
/*
|
||||
* YOUR CODE HERE
|
||||
*
|
||||
* 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
|
||||
}
|
||||
|
||||
/*
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// 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"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Handle an 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")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error or response write: %v", err)
|
||||
}
|
||||
// Example implementation:
|
||||
fmt.Println("OK") // Print "OK" to standard output (local logs)
|
||||
fmt.Fprintln(res, "OK") // Send "OK" back to the client
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
// TestHandle ensures that Handle executes without error, returns 2000 and sets
|
||||
// and sets a content-type.
|
||||
// TestHandle ensures that Handle executes without error and returns the
|
||||
// HTTP 200 status code indicating no errors.
|
||||
func TestHandle(t *testing.T) {
|
||||
var (
|
||||
w = httptest.NewRecorder()
|
||||
|
@ -21,6 +21,7 @@ func TestHandle(t *testing.T) {
|
|||
func(w http.ResponseWriter, req *http.Request) {
|
||||
Handle(context.Background(), w, req)
|
||||
}(w, req)
|
||||
|
||||
res = w.Result()
|
||||
defer res.Body.Close()
|
||||
|
||||
|
@ -31,8 +32,4 @@ func TestHandle(t *testing.T) {
|
|||
if res.StatusCode != 200 {
|
||||
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