addLinkHandlerToMap method

void addLinkHandlerToMap(
  1. String routeKey,
  2. Map pathsMap,
  3. Map queryMap,
  4. String newParser,
)

Implementation

void addLinkHandlerToMap(
  String routeKey,
  Map pathsMap,
  Map queryMap,
  String newParser,
) {
  if (!routeKey.contains('/')) {
    if (pathsMap[routeKey] == null) {
      pathsMap[routeKey] = {
        '': queryMap.isEmpty ? newParser : queryMap,
      };
    } else if (pathsMap[routeKey][''] == null) {
      pathsMap[routeKey][''] = queryMap.isEmpty ? newParser : queryMap;
    } else {
      if (pathsMap[routeKey][''] is String) {
        if (queryMap.isNotEmpty) {
          pathsMap[routeKey][''] = {
            '': pathsMap[routeKey][''],
            ...queryMap,
          };
        }
      } else {
        // coverage:ignore-start
        final currentMap = pathsMap[routeKey][''] as Map;

        if (queryMap.isEmpty) {
          // should never happen

          if (currentMap.containsKey('')) {
            throw Exception(
              'Multiple handlers detected for the same route: $newParser for $routeKey with path params: $pathsMap and query params $queryMap',
            );
          }

          currentMap.addAll({
            '': newParser,
          });
        } else {
          for (final entry in queryMap.entries) {
            if (currentMap.containsKey(entry.key)) {
              throw Exception(
                'Multiple handlers detected for the same route: $newParser for $routeKey with path params: $pathsMap and query params $queryMap',
              );
            }
          }

          currentMap.addAll({
            ...queryMap,
          });
        }
        // coverage:ignore-end
      }
    }

    return;
  }

  var correctedPathsMap = pathsMap;
  final segments = routeKey.split('/');

  for (final segment in segments) {
    if (correctedPathsMap[segment] == null) {
      correctedPathsMap[segment] = {};
    } else if (correctedPathsMap[segment] is String) {
      // should never happen

      // coverage:ignore-start
      correctedPathsMap[segment] = {
        '': correctedPathsMap[segment],
      };
      // coverage:ignore-end
    }

    correctedPathsMap = correctedPathsMap[segment];
  }

  if (correctedPathsMap[''] is String) {
    if (queryMap.isNotEmpty) {
      correctedPathsMap[''] = {
        '': correctedPathsMap[''],
        ...queryMap,
      };
    }
  } else if (correctedPathsMap[''] is Map) {
    final currentMap = correctedPathsMap[''] as Map;

    if (queryMap.isEmpty) {
      // coverage:ignore-start
      if (currentMap.containsKey('')) {
        throw Exception(
          'Multiple handlers detected for the same route: $newParser for $routeKey with path params: $pathsMap and query params $queryMap',
        );
      }

      currentMap.addAll({
        '': newParser,
      });
      // coverage:ignore-end
    } else {
      for (final entry in queryMap.entries) {
        if (currentMap.containsKey(entry.key)) {
          throw Exception(
            'Multiple handlers detected for the same route: $newParser for $routeKey with path params: $pathsMap and query params $queryMap',
          );
        }
      }

      currentMap.addAll({
        ...queryMap,
      });
    }
  } else {
    correctedPathsMap[''] = queryMap.isEmpty ? newParser : queryMap;
  }
}