parseControllerMethod function

ControllerMethod parseControllerMethod(
  1. ControllerMethodDefinition defn
)

Implementation

ControllerMethod parseControllerMethod(ControllerMethodDefinition defn) {
  final type = defn.$1;
  final method = defn.$2;

  final ctrlMirror = inject.reflectType(type) as r.ClassMirror;
  if (ctrlMirror.superclass?.reflectedType != HTTPController) {
    throw ArgumentError('$type must extend BaseController');
  }

  final methods = ctrlMirror.instanceMembers.values.whereType<r.MethodMirror>();
  final actualMethod =
      methods.firstWhereOrNull((e) => e.simpleName == symbolToString(method));
  if (actualMethod == null) {
    throw ArgumentError(
        '$type does not have method  #${symbolToString(method)}');
  }

  final parameters = actualMethod.parameters;
  if (parameters.isEmpty) return ControllerMethod(defn);

  if (parameters.any((e) => e.metadata.length > 1)) {
    throw ArgumentError(
        'Multiple annotations using on $type #${symbolToString(method)} parameter');
  }

  final params = parameters.map((e) {
    final meta = e.metadata.first;
    if (meta is! RequestAnnotation) {
      throw ArgumentError(
        'Invalid annotation $meta used on $type #${symbolToString(method)} parameter',
      );
    }

    final paramType = e.reflectedType;
    final maybeDto = _tryResolveDtoInstance(paramType);

    return ControllerMethodParam(
      e.simpleName,
      paramType,
      defaultValue: e.defaultValue,
      optional: e.isOptional,
      meta: meta,
      dto: maybeDto,
    );
  });

  return ControllerMethod(defn, params);
}