dynamic convertParameterWithMirror(String parameterValue, TypeMirror typeMirror)

Source

dynamic convertParameterWithMirror(
    String parameterValue, TypeMirror typeMirror) {
  if (parameterValue == null) {
    return null;
  }

  if (typeMirror.isSubtypeOf(reflectType(bool))) {
    return true;
  }

  if (typeMirror.isSubtypeOf(reflectType(String))) {
    return parameterValue;
  }

  if (typeMirror is ClassMirror) {
    var parseDecl = typeMirror.declarations[#parse];
    if (parseDecl != null) {
      try {
        var reflValue =
        typeMirror.invoke(parseDecl.simpleName, [parameterValue]);
        return reflValue.reflectee;
      } catch (e) {
        throw new InternalControllerException(
            "Invalid value for parameter type", HttpStatus.BAD_REQUEST,
            errorMessage: "URI parameter is wrong type");
      }
    }
  }

  // If we get here, then it wasn't a string and couldn't be parsed, and we should throw?
  throw new InternalControllerException(
      "Invalid path parameter type, types must be String or implement parse",
      HttpStatus.INTERNAL_SERVER_ERROR,
      errorMessage: "URI parameter is wrong type");
}