Skip to content

Commit 858a4cf

Browse files
Add support for type-safe Start* function (#468)
* add HandlerFunc interface and StartWithOptionsTypeSafe * add tests for StartWithOptionsTypeSafe * Update test to cover bool from validateArguments * move generic code to dedicated files with build tag * Reduce HandlerFunc interface to just one variant. Rename StartWithOptionsTypeSafe to StartHandlerFunc * add ValidateHandlerFunc * Revert "add ValidateHandlerFunc" This reverts commit bc1e02e. * add context-arg-only reflections back to the godoc * update copyright date in new file to match year of file's creation * update copyright year in new test file to match the year of file's creation Co-authored-by: Bryan Moffatt <[email protected]>
1 parent 666b0d8 commit 858a4cf

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

lambda/entry.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@ import (
1717
// - handler must be a function
1818
// - handler may take between 0 and two arguments.
1919
// - if there are two arguments, the first argument must satisfy the "context.Context" interface.
20-
// - handler may return between 0 and two arguments.
21-
// - if there are two return values, the second argument must be an error.
20+
// - handler may return between 0 and two values.
21+
// - if there are two return values, the second return value must be an error.
2222
// - if there is one return value it must be an error.
2323
//
2424
// Valid function signatures:
2525
//
2626
// func ()
27+
// func (TIn)
2728
// func () error
2829
// func (TIn) error
2930
// func () (TOut, error)
3031
// func (TIn) (TOut, error)
32+
// func (context.Context)
3133
// func (context.Context) error
32-
// func (context.Context, TIn) error
3334
// func (context.Context) (TOut, error)
35+
// func (context.Context, TIn)
36+
// func (context.Context, TIn) error
3437
// func (context.Context, TIn) (TOut, error)
3538
//
3639
// Where "TIn" and "TOut" are types compatible with the "encoding/json" standard library.

lambda/entry_generic.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved
5+
6+
package lambda
7+
8+
import (
9+
"context"
10+
)
11+
12+
// HandlerFunc represents a valid input with two arguments and two returns as described by Start
13+
type HandlerFunc[TIn, TOut any] interface {
14+
func(context.Context, TIn) (TOut, error)
15+
}
16+
17+
// StartHandlerFunc is the same as StartWithOptions except that it takes a generic input
18+
// so that the function signature can be validated at compile time.
19+
//
20+
// Currently only the `func (context.Context, TIn) (TOut, error)` variant is supported
21+
func StartHandlerFunc[TIn any, TOut any, H HandlerFunc[TIn, TOut]](handler H, options ...Option) {
22+
start(newHandler(handler, options...))
23+
}

lambda/entry_generic_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved
5+
6+
package lambda
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"reflect"
12+
"testing"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestStartHandlerFunc(t *testing.T) {
18+
actual := "unexpected"
19+
logFatalf = func(format string, v ...interface{}) {
20+
actual = fmt.Sprintf(format, v...)
21+
}
22+
23+
f := func(context.Context, any) (any, error) { return 1, nil }
24+
StartHandlerFunc(f)
25+
26+
assert.Equal(t, "expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined", actual)
27+
28+
handlerType := reflect.TypeOf(f)
29+
30+
handlerTakesContext, err := validateArguments(handlerType)
31+
assert.NoError(t, err)
32+
assert.True(t, handlerTakesContext)
33+
34+
err = validateReturns(handlerType)
35+
assert.NoError(t, err)
36+
}

0 commit comments

Comments
 (0)