(f interface{}, h *handlerOptions)
| 237 | } |
| 238 | |
| 239 | func reflectHandler(f interface{}, h *handlerOptions) handlerFunc { |
| 240 | if f == nil { |
| 241 | return errorHandler(errors.New("handler is nil")) |
| 242 | } |
| 243 | |
| 244 | // back-compat: types with reciever `Invoke(context.Context, []byte) ([]byte, error)` need the return bytes wrapped |
| 245 | if handler, ok := f.(Handler); ok { |
| 246 | return func(ctx context.Context, payload []byte) (io.Reader, error) { |
| 247 | b, err := handler.Invoke(ctx, payload) |
| 248 | if err != nil { |
| 249 | return nil, err |
| 250 | } |
| 251 | return bytes.NewBuffer(b), nil |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | handler := reflect.ValueOf(f) |
| 256 | handlerType := reflect.TypeOf(f) |
| 257 | if handlerType.Kind() != reflect.Func { |
| 258 | return errorHandler(fmt.Errorf("handler kind %s is not %s", handlerType.Kind(), reflect.Func)) |
| 259 | } |
| 260 | |
| 261 | takesContext, err := handlerTakesContext(handlerType) |
| 262 | if err != nil { |
| 263 | return errorHandler(err) |
| 264 | } |
| 265 | |
| 266 | if err := validateReturns(handlerType); err != nil { |
| 267 | return errorHandler(err) |
| 268 | } |
| 269 | |
| 270 | return func(ctx context.Context, payload []byte) (outFinal io.Reader, _ error) { |
| 271 | in := bytes.NewBuffer(payload) |
| 272 | decoder := json.NewDecoder(in) |
| 273 | if h.jsonRequestUseNumber { |
| 274 | decoder.UseNumber() |
| 275 | } |
| 276 | if h.jsonRequestDisallowUnknownFields { |
| 277 | decoder.DisallowUnknownFields() |
| 278 | } |
| 279 | |
| 280 | out := h.jsonOutBufferPool.Get().(*jsonOutBuffer) |
| 281 | defer func() { |
| 282 | // If the final return value is not our buffer, reset and return it to the pool. |
| 283 | // The caller of the handlerFunc does this otherwise. |
| 284 | if outFinal != out { |
| 285 | out.Close() |
| 286 | } |
| 287 | }() |
| 288 | encoder := json.NewEncoder(out) |
| 289 | encoder.SetEscapeHTML(h.jsonResponseEscapeHTML) |
| 290 | encoder.SetIndent(h.jsonResponseIndentPrefix, h.jsonResponseIndentValue) |
| 291 | |
| 292 | trace := handlertrace.FromContext(ctx) |
| 293 | |
| 294 | // construct arguments |
| 295 | var args []reflect.Value |
| 296 | if takesContext { |
no test coverage detected
searching dependent graphs…