Skip to content

Commit

Permalink
add ValidateHandlerFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
logandavies181 committed Nov 29, 2022
1 parent 0196bd7 commit bc1e02e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lambda/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ func WithEnableSIGTERM(callbacks ...func()) Option {
})
}

// ValidateHandlerFunc validates the handler against the criteria for Start and returns an error
// if the criteria are not met
func ValidateHandlerFunc(handlerFunc interface{}) error {
if handlerFunc == nil {
return errors.New("handler is nil")
}

handlerType := reflect.TypeOf(handlerFunc)
if handlerType.Kind() != reflect.Func {
return fmt.Errorf("handler kind %s is not %s", handlerType.Kind(), reflect.Func)
}

_, err := validateArguments(handlerType)
if err != nil {
return err
}

err = validateReturns(handlerType)
if err != nil {
return err
}

return nil
}

func validateArguments(handler reflect.Type) (bool, error) {
handlerTakesContext := false
if handler.NumIn() > 2 {
Expand Down
59 changes: 58 additions & 1 deletion lambda/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,68 @@ func TestInvalidHandlers(t *testing.T) {
}
for i, testCase := range testCases {
testCase := testCase
t.Run(fmt.Sprintf("testCase[%d] %s", i, testCase.name), func(t *testing.T) {
t.Run(fmt.Sprintf("testCase[%d] %s part 1", i, testCase.name), func(t *testing.T) {
lambdaHandler := NewHandler(testCase.handler)
_, err := lambdaHandler.Invoke(context.TODO(), make([]byte, 0))
assert.Equal(t, testCase.expected, err)
})

t.Run(fmt.Sprintf("testCase[%d] %s part 2", i, testCase.name), func(t *testing.T) {
err := ValidateHandlerFunc(testCase.handler)
assert.Equal(t, testCase.expected, err)
})
}
}

func TestValidateHandlerFuncValidHandlers(t *testing.T) {
testCases := []struct {
name string
handler interface{}
}{
{
name: "0 arg 0 return",
handler: func() {},
},
{
name: "0 arg, 1 returns",
handler: func() error { return nil },
},
{
name: "1 arg, 0 returns",
handler: func(any) {},
},
{
name: "1 arg, 1 returns",
handler: func(any) error { return nil },
},
{
name: "0 arg, 2 returns",
handler: func() (any, error) { return 1, nil },
},
{
name: "1 arg, 2 returns",
handler: func(any) (any, error) { return 1, nil },
},
{
name: "2 arg, 0 returns",
handler: func(context.Context, any) {},
},
{
name: "2 arg, 1 returns",
handler: func(context.Context, any) error { return nil },
},
{
name: "2 arg, 2 returns",
handler: func(context.Context, any) (any, error) { return 1, nil },
},
}

for i, testCase := range testCases {
testCase := testCase
t.Run(fmt.Sprintf("testCase[%d] %s", i, testCase.name), func(t *testing.T) {
err := ValidateHandlerFunc(testCase.handler)
assert.Nil(t, err)
})
}
}

Expand Down

0 comments on commit bc1e02e

Please sign in to comment.