Skip to content

Commit

Permalink
Add HandlerFunc generics support
Browse files Browse the repository at this point in the history
  • Loading branch information
shugo.kawamura committed Dec 26, 2022
1 parent 99a9a4b commit 627275f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions lambda/entry_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ import (
"context"
)

// HandlerFunc represents a valid input with two arguments and two returns as described by Start
// HandlerFunc represents a valid input with arguments and returns as described by Start
type HandlerFunc[TIn, TOut any] interface {
func(context.Context, TIn) (TOut, error)
~func(context.Context, TIn) (TOut, error) |
~func() |
~func(TIn) |
~func() error |
~func(TIn) error |
~func() (TOut, error) |
~func(TIn) (TOut, error) |
~func(context.Context) |
~func(context.Context) error |
~func(context.Context) (TOut, error) |
~func(context.Context, TIn) |
~func(context.Context, TIn) 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...))
}
2 changes: 1 addition & 1 deletion lambda/entry_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestStartHandlerFunc(t *testing.T) {
}

f := func(context.Context, any) (any, error) { return 1, nil }
StartHandlerFunc(f)
StartHandlerFunc[any, any](f)

assert.Equal(t, "expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined", actual)

Expand Down

0 comments on commit 627275f

Please sign in to comment.