-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
666b0d8
commit 858a4cf
Showing
3 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved | ||
|
||
package lambda | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// HandlerFunc represents a valid input with two arguments and two returns as described by Start | ||
type HandlerFunc[TIn, TOut any] interface { | ||
func(context.Context, TIn) (TOut, error) | ||
} | ||
|
||
// StartHandlerFunc is the same as StartWithOptions except that it takes a generic input | ||
// so that the function signature can be validated at compile time. | ||
// | ||
// Currently only the `func (context.Context, TIn) (TOut, error)` variant is supported | ||
func StartHandlerFunc[TIn any, TOut any, H HandlerFunc[TIn, TOut]](handler H, options ...Option) { | ||
start(newHandler(handler, options...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved | ||
|
||
package lambda | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStartHandlerFunc(t *testing.T) { | ||
actual := "unexpected" | ||
logFatalf = func(format string, v ...interface{}) { | ||
actual = fmt.Sprintf(format, v...) | ||
} | ||
|
||
f := func(context.Context, any) (any, error) { return 1, nil } | ||
StartHandlerFunc(f) | ||
|
||
assert.Equal(t, "expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined", actual) | ||
|
||
handlerType := reflect.TypeOf(f) | ||
|
||
handlerTakesContext, err := validateArguments(handlerType) | ||
assert.NoError(t, err) | ||
assert.True(t, handlerTakesContext) | ||
|
||
err = validateReturns(handlerType) | ||
assert.NoError(t, err) | ||
} |