getPathFromName method
GetPathFromNameResult
getPathFromName(
- String nameToMatch, {
- required Map<
String, String> pathParameters, - required GetNewParentPathResult parentPathResult,
- required Map<
String, String> remainingPathParameters,
override
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,
}) {
// A variable to store the new parentPath from the path and aliases
final List<GetNewParentPathResult> newParentPathResults = [];
final List<Map<String, String>> newRemainingPathParameters = [];
final List<GetPathFromNameResult> nameErrorResults = [];
// Check if any subroute matches the name using path
// Get the new parent path by taking this path into account
newParentPathResults.add(
getNewParentPath(
parentPathResult,
thisPath: path,
thisPathParametersKeys: pathParametersKeys,
pathParameters: pathParameters,
),
);
newRemainingPathParameters.add(
(path != null && path!.startsWith('/'))
? Map<String, String>.from(pathParameters)
: Map<String, String>.from(remainingPathParameters)
..removeWhere((key, value) => pathParametersKeys.contains(key)),
);
for (var vRouteElement in stackedRoutes) {
GetPathFromNameResult childPathFromNameResult =
vRouteElement.getPathFromName(
nameToMatch,
pathParameters: pathParameters,
parentPathResult: newParentPathResults.last,
remainingPathParameters: newRemainingPathParameters.last,
);
if (childPathFromNameResult is ValidNameResult) {
return childPathFromNameResult;
} else {
nameErrorResults.add(childPathFromNameResult);
}
}
// Check if any subroute matches the name using aliases
for (var i = 0; i < aliases.length; i++) {
// Get the new parent path by taking this alias into account
newParentPathResults.add(getNewParentPath(
parentPathResult,
thisPath: aliases[i],
thisPathParametersKeys: aliasesPathParametersKeys[i],
pathParameters: pathParameters,
));
newRemainingPathParameters.add(
(aliases[i].startsWith('/'))
? Map<String, String>.from(pathParameters)
: Map<String, String>.from(remainingPathParameters)
..removeWhere(
(key, value) => aliasesPathParametersKeys[i].contains(key)),
);
for (var vRouteElement in stackedRoutes) {
GetPathFromNameResult childPathFromNameResult =
vRouteElement.getPathFromName(
nameToMatch,
pathParameters: pathParameters,
parentPathResult: newParentPathResults.last,
remainingPathParameters: newRemainingPathParameters.last,
);
if (childPathFromNameResult is ValidNameResult) {
return childPathFromNameResult;
} else {
nameErrorResults.add(childPathFromNameResult);
}
}
}
// // 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
// for (int i = 0; i < newParentPathResults.length; i++) {
// var newParentPathResult = newParentPathResults[i];
// if (newParentPathResult is ValidParentPathResult) {
// if (newParentPathResult.path == null) {
// // If this path is null, we add a NullPathErrorNameResult
// nameErrorResults.add(NullPathErrorNameResult(name: nameToMatch));
// } else {
// final newRemainingPathParameter = newRemainingPathParameters[i];
// if (newRemainingPathParameter.isNotEmpty) {
// // If there are path parameters remaining, wee add a PathParamsErrorsNameResult
// nameErrorResults.add(
// PathParamsErrorsNameResult(
// name: nameToMatch,
// values: [
// OverlyPathParamsError(
// pathParams: pathParameters.keys.toList(),
// expectedPathParams: newParentPathResult.pathParameters.keys.toList(),
// ),
// ],
// ),
// );
// } else {
// // Else the result is valid
// return ValidNameResult(path: newParentPathResult.path!);
// }
// }
// } else {
// assert(newParentPathResult is PathParamsErrorNewParentPath);
// nameErrorResults.add(
// PathParamsErrorsNameResult(
// name: nameToMatch,
// values: [
// MissingPathParamsError(
// pathParams: pathParameters.keys.toList(),
// missingPathParams:
// (newParentPathResult as PathParamsErrorNewParentPath).pathParameters,
// ),
// ],
// ),
// );
// }
// }
// }
// If we don't have any valid result
// If some stackedRoute returned PathParamsPopError, aggregate them
final pathParamsNameErrors = PathParamsErrorsNameResult(
name: nameToMatch,
values: nameErrorResults.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 (nameErrorResults.indexWhere(
(childNameResult) => childNameResult is NullPathErrorNameResult) !=
-1) {
return NullPathErrorNameResult(name: nameToMatch);
}
// Else return a NotFoundError
return NotFoundErrorNameResult(name: nameToMatch);
}