Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HandlerFunc generics support #477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change, as the go compiler isn't smart enough to infer the types when more than one variant is added to the interface. See prior discussion: #468 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thank you for considering this @bmoffatt !
I am still wondering if we can add this changes by adding another function (let's say StartTypedHandlerFunc for now)that accepts "any" type and it calls StartHandlerFunc[any, any](f).
There will be no type-safe though, but user does not need to specify types for StartHandlerFunc . instead, they may want to use new StartHandlerTypedFunc that supports generics with type parameter.

Sorry my English is not good enough to explain this, hope it works!


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

Expand Down