getPathFromName method

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

Tries to find a path from a name

This first asks its stackedRoutes if they have a match Else is tries to see if this VRouteElement matches the name Else return null

Note that not only the name must match but the path parameters must be able to form a valid path afterward too

Implementation

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

  // 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 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);
}