getNewParentPath method
The goal is that, considering thisPath
and parentPath
we can form a new parentPath
For more details, see GetNewParentPathResult
Implementation
GetNewParentPathResult getNewParentPath(
GetNewParentPathResult parentPathResult, {
required String? thisPath,
required List<String> thisPathParametersKeys,
required Map<String, String> pathParameters,
}) {
// First check that we have the path parameters needed to have this path
final missingPathParameters = thisPathParametersKeys
.where((key) => !pathParameters.containsKey(key))
.toList();
if (missingPathParameters.isNotEmpty) {
if (thisPath!.startsWith('/')) {
return PathParamsErrorNewParentPath(
pathParameters: missingPathParameters);
} else {
return PathParamsErrorNewParentPath(
pathParameters: [
if (parentPathResult is PathParamsErrorNewParentPath)
...parentPathResult.pathParameters,
...missingPathParameters
],
);
}
}
if (thisPath == null) {
// If the path is null, the new parent path is the same as the previous one
return parentPathResult;
}
final localPath =
replacePathParameters(replaceWildcards(thisPath), pathParameters);
final thisPathParameters = Map<String, String>.from(pathParameters)
..removeWhere((key, value) => !thisPathParametersKeys.contains(key));
// If the path is absolute
if (thisPath.startsWith('/')) {
return ValidParentPathResult(
path: localPath,
pathParameters: thisPathParameters,
);
}
// Else the path is relative
// If the path is relative and the parent path is invalid, then this path is invalid
if (parentPathResult is PathParamsErrorNewParentPath) {
return parentPathResult;
}
// Else this path is valid
final parentPathValue =
(parentPathResult as ValidParentPathResult).path ?? '';
return ValidParentPathResult(
path: parentPathValue +
(!parentPathValue.endsWith('/') ? '/' : '') +
localPath,
pathParameters: {
...parentPathResult.pathParameters,
...thisPathParameters
},
);
}