processBodyAnnotation function

bool processBodyAnnotation(
  1. List<ParamExtractor> positionalArgumentsFunctions,
  2. Map<Symbol, ParamExtractor> namedArgumentsFunctions,
  3. ParameterMirror singleParam,
  4. ObjectMapper om,
)

Implementation

bool processBodyAnnotation(
  List<ParamExtractor> positionalArgumentsFunctions,
  Map<Symbol, ParamExtractor> namedArgumentsFunctions,
  ParameterMirror singleParam,
  ObjectMapper om,
) {
  Body? body = singleParam.metadata
      .firstWhereOrNull(
        (metadata) => metadata.reflectee is Body,
      )
      ?.reflectee;
  if (body != null) {
    bool isRequired = !singleParam.isOptional;

    var defaultValue = singleParam.hasDefaultValue
        ? singleParam.defaultValue?.reflectee
        : null;

    extractor(RequestEntity request) async {
      String rawBody = await request.readAsString();
      if (!isRequired && rawBody.isEmpty) {
        return defaultValue;
      }
      Object parsedBody = body.parser(rawBody, om);
      if (parsedBody is PlainBodyWrapper) {
        return parsedBody.body;
      }
      return parsedBody;
    }

    if (singleParam.isNamed) {
      namedArgumentsFunctions[singleParam.simpleName] = extractor;
    } else {
      positionalArgumentsFunctions.add(extractor);
    }
  }
  return body != null;
}