MCPcopy
hub / github.com/aws/aws-lambda-go / reflectHandler

Function reflectHandler

lambda/handler.go:239–351  ·  view source on GitHub ↗
(f interface{}, h *handlerOptions)

Source from the content-addressed store, hash-verified

237}
238
239func 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 {

Callers 1

newHandlerFunction · 0.85

Calls 7

FromContextFunction · 0.92
errorHandlerFunction · 0.85
handlerTakesContextFunction · 0.85
validateReturnsFunction · 0.85
InvokeMethod · 0.65
CloseMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…