This repository was archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
77 lines (69 loc) · 3.4 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package wflambda
import (
"context"
"fmt"
"reflect"
)
// validateLambdaHandler validates the lambdaHandler is a valid handler function. When the handler is a valid handler function,
// the function will check whether one of the arguments is a context. The function returns a non-nil error if lambdaHandler is
// not a valid handler. When the lambdaHandler is valid, the boolean indicates whether the lambdaHandler has a context parameter.
// is returned. The code of the function is based on source code at
// https://github.com/aws/aws-lambda-go/blob/ea03c2814414b2223eff860ed2286a83ed8a195c/lambda/handler.go#L75
func validateLambdaHandler(lambdaHandler interface{}) (bool, error) {
if lambdaHandler == nil {
return false, fmt.Errorf("handler is nil")
}
handlerType := reflect.TypeOf(lambdaHandler)
// Validate lambdaHandler Kind.
if handlerType.Kind() != reflect.Func {
return false, fmt.Errorf("handler kind %s is not %s", handlerType.Kind(), reflect.Func)
}
// Check if the lambdaHandler takes a context argument.
takesContext, err := validateArguments(handlerType)
if err != nil {
return false, err
}
if err := validateReturns(handlerType); err != nil {
return false, err
}
return takesContext, nil
}
// validateArguments validates whether the arguments passed as part of the lambdaHandler are valid. A valid lambdaHandler
// has a maximum of two arguments. When there are two arguments, the first one must be a Context. The function returns
// true or false depending on whether the lambdaHandler has a context argument. If the arguments are not valid, an error
// is returned. Detailed information on the valid handler signatures can be found in the AWS Lambda documentation
// https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-handler-types.html
func validateArguments(handler reflect.Type) (bool, error) {
handlerTakesContext := false
if handler.NumIn() > 2 {
return false, fmt.Errorf("handlers may not take more than two arguments, but handler takes %d", handler.NumIn())
} else if handler.NumIn() > 0 {
contextType := reflect.TypeOf((*context.Context)(nil)).Elem()
argumentType := handler.In(0)
handlerTakesContext = argumentType.Implements(contextType)
if handler.NumIn() > 1 && !handlerTakesContext {
return false, fmt.Errorf("handler takes two arguments, but the first is not Context. got %s", argumentType.Kind())
}
}
return handlerTakesContext, nil
}
// validateReturns validates whether the arguments returned by the lambdaHandler are valid or not. A valid lambdaHandler
// returns a maximum of two arguments. When there are two arguments, the second argument must be of type error. When there
// is only one argument, that one must be of type error. Detailed information on the valid handler signatures can be found
// in the AWS Lambda documentation
// https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-handler-types.html
func validateReturns(handler reflect.Type) error {
errorType := reflect.TypeOf((*error)(nil)).Elem()
if handler.NumOut() > 2 {
return fmt.Errorf("handler may not return more than two values")
} else if handler.NumOut() > 1 {
if !handler.Out(1).Implements(errorType) {
return fmt.Errorf("handler returns two values, but the second does not implement error")
}
} else if handler.NumOut() == 1 {
if !handler.Out(0).Implements(errorType) {
return fmt.Errorf("handler returns a single value, but it does not implement error")
}
}
return nil
}