getPathFromName method

GetPathFromNameResult getPathFromName(
  1. String nameToMatch, {
  2. required Map<String, String> pathParameters,
  3. required GetNewParentPathResult parentPathResult,
  4. required Map<String, String> remainingPathParameters,
})
override

This function takes a name and tries to find the path corresponding to the route matching this name

The deeper nested the route the better The given path parameters have to include at least every path parameters of the final path

Implementation

GetPathFromNameResult getPathFromName(
  String nameToMatch, {
  required Map<String, String> pathParameters,
  required GetNewParentPathResult parentPathResult,
  required Map<String, String> remainingPathParameters,
}) {
  final childNameResults = <GetPathFromNameResult>[];

  // Check if any nestedRoute matches the name
  for (var vRouteElement in nestedRoutes) {
    childNameResults.add(
      vRouteElement.getPathFromName(
        nameToMatch,
        pathParameters: pathParameters,
        parentPathResult: parentPathResult,
        remainingPathParameters: remainingPathParameters,
      ),
    );
    if (childNameResults.last is ValidNameResult) {
      return childNameResults.last;
    }
  }

  // Check if any subroute matches the name
  for (var vRouteElement in stackedRoutes) {
    childNameResults.add(
      vRouteElement.getPathFromName(
        nameToMatch,
        pathParameters: pathParameters,
        parentPathResult: parentPathResult,
        remainingPathParameters: remainingPathParameters,
      ),
    );
    if (childNameResults.last is ValidNameResult) {
      return childNameResults.last;
    }
  }

  // If no subroute or stackedRoute matches the name, try to match this name
  if (name == nameToMatch) {
    // If path or any alias is valid considering the given path parameters, return this
    if (parentPathResult is ValidParentPathResult) {
      if (parentPathResult.path == null) {
        // If this path is null, we add a NullPathErrorNameResult
        childNameResults.add(NullPathErrorNameResult(name: nameToMatch));
      } else {
        if (remainingPathParameters.isNotEmpty) {
          // If there are path parameters remaining, wee add a PathParamsErrorsNameResult
          childNameResults.add(
            PathParamsErrorsNameResult(
              name: nameToMatch,
              values: [
                OverlyPathParamsError(
                  pathParams: pathParameters.keys.toList(),
                  expectedPathParams:
                      parentPathResult.pathParameters.keys.toList(),
                ),
              ],
            ),
          );
        } else {
          // Else the result is valid
          return ValidNameResult(path: parentPathResult.path!);
        }
      }
    } else {
      assert(parentPathResult is PathParamsErrorNewParentPath);
      childNameResults.add(
        PathParamsErrorsNameResult(
          name: nameToMatch,
          values: [
            MissingPathParamsError(
              pathParams: pathParameters.keys.toList(),
              missingPathParams:
                  (parentPathResult as PathParamsErrorNewParentPath)
                      .pathParameters,
            ),
          ],
        ),
      );
    }
  }

  // If we don't have any valid result

  // If some stackedRoute returned PathParamsPopError, aggregate them
  final pathParamsNameErrors = PathParamsErrorsNameResult(
    name: nameToMatch,
    values: childNameResults.fold<List<PathParamsError>>(
      <PathParamsError>[],
      (previousValue, element) {
        return previousValue +
            ((element is PathParamsErrorsNameResult) ? element.values : []);
      },
    ),
  );

  // If there was any PathParamsPopError, we have some pathParamsPopErrors.values
  // and therefore should return this
  if (pathParamsNameErrors.values.isNotEmpty) {
    return pathParamsNameErrors;
  }

  // Else try to find a NullPathError
  if (childNameResults.indexWhere(
          (childNameResult) => childNameResult is NullPathErrorNameResult) !=
      -1) {
    return NullPathErrorNameResult(name: nameToMatch);
  }

  // Else return a NotFoundError
  return NotFoundErrorNameResult(name: nameToMatch);
}