WorkspaceCoverageConfig.fromMap constructor

WorkspaceCoverageConfig.fromMap(
  1. Map<Object?, Object?> value
)

Implementation

factory WorkspaceCoverageConfig.fromMap(Map<Object?, Object?> value) {
  for (final key in ['input', 'baseline', 'changedSince']) {
    final raw = value[key];
    if (raw != null && (raw is! String || raw.trim().isEmpty)) {
      throw FormatException('coverage.$key must be a non-empty string');
    }
  }
  double? threshold(String key) {
    final raw = value[key];
    if (raw == null) return null;
    final parsed = raw is num
        ? raw.toDouble()
        : raw is String
        ? double.tryParse(raw)
        : null;
    if (parsed == null || !parsed.isFinite || parsed < 0 || parsed > 100) {
      throw FormatException('coverage.$key must be between 0 and 100');
    }
    return parsed;
  }

  final rawRoots = value['includedRoots'];
  if (rawRoots != null &&
      (rawRoots is! List ||
          rawRoots.any((item) => item is! String || item.isEmpty))) {
    throw const FormatException(
      'coverage.includedRoots must be string values',
    );
  }
  final rawLayers = value['layers'];
  if (rawLayers != null && rawLayers is! Map) {
    throw const FormatException('coverage.layers must be a mapping');
  }
  if (rawLayers is Map) {
    for (final entry in rawLayers.entries) {
      if (entry.key is! String ||
          (entry.key as String).trim().isEmpty ||
          entry.value is! List ||
          (entry.value as List).any(
            (item) => item is! String || item.trim().isEmpty,
          )) {
        throw const FormatException(
          'coverage.layers must map non-empty names to string lists',
        );
      }
    }
  }
  return WorkspaceCoverageConfig(
    input: value['input'] as String?,
    baseline: value['baseline'] as String?,
    changedSince: value['changedSince'] as String?,
    minimumTotal: threshold('minimumTotal'),
    changedLineMinimum: threshold('changedLineMinimum'),
    includedRoots: rawRoots == null
        ? const []
        : List<String>.unmodifiable(rawRoots as List),
    layers: rawLayers == null
        ? const {}
        : Map.unmodifiable({
            for (final entry in (rawLayers as Map).entries)
              entry.key as String: List<String>.unmodifiable(
                entry.value as List,
              ),
          }),
    extensions: Map.unmodifiable({
      for (final entry in value.entries)
        if (entry.key is String && !_knownKeys.contains(entry.key))
          entry.key as String: _freeze(entry.value),
    }),
  );
}