handlerTakesContext returns whether the handler takes a context.Context as its first argument.
(handler reflect.Type)
| 93 | |
| 94 | // handlerTakesContext returns whether the handler takes a context.Context as its first argument. |
| 95 | func handlerTakesContext(handler reflect.Type) (bool, error) { |
| 96 | switch handler.NumIn() { |
| 97 | case 0: |
| 98 | return false, nil |
| 99 | case 1: |
| 100 | contextType := reflect.TypeOf((*context.Context)(nil)).Elem() |
| 101 | argumentType := handler.In(0) |
| 102 | if argumentType.Kind() != reflect.Interface { |
| 103 | return false, nil |
| 104 | } |
| 105 | |
| 106 | // handlers like func(event any) are valid. |
| 107 | if argumentType.NumMethod() == 0 { |
| 108 | return false, nil |
| 109 | } |
| 110 | |
| 111 | if !contextType.Implements(argumentType) || !argumentType.Implements(contextType) { |
| 112 | return false, fmt.Errorf("handler takes an interface, but it is not context.Context: %q", argumentType.Name()) |
| 113 | } |
| 114 | return true, nil |
| 115 | case 2: |
| 116 | contextType := reflect.TypeOf((*context.Context)(nil)).Elem() |
| 117 | argumentType := handler.In(0) |
| 118 | if argumentType.Kind() != reflect.Interface || !contextType.Implements(argumentType) || !argumentType.Implements(contextType) { |
| 119 | return false, fmt.Errorf("handler takes two arguments, but the first is not Context. got %s", argumentType.Kind()) |
| 120 | } |
| 121 | return true, nil |
| 122 | } |
| 123 | return false, fmt.Errorf("handlers may not take more than two arguments, but handler takes %d", handler.NumIn()) |
| 124 | } |
| 125 | |
| 126 | func validateReturns(handler reflect.Type) error { |
| 127 | errorType := reflect.TypeOf((*error)(nil)).Elem() |
no outgoing calls
searching dependent graphs…