ZukeIndex.fromJson constructor

ZukeIndex.fromJson(
  1. Map<Object?, Object?> json
)

Implementation

factory ZukeIndex.fromJson(Map<Object?, Object?> json) {
  if (json['kind'] != kind) {
    throw const FormatException('Unsupported Zuke analyzer index schema');
  }
  String requiredString(String field) {
    final value = json[field];
    if (value is! String || value.isEmpty) {
      throw FormatException(
        'Analyzer index $field must be a non-empty string',
      );
    }
    return value;
  }

  Set<String> ids(String field) {
    final value = json[field];
    if (value is! List ||
        value.any((entry) => entry is! String || entry.isEmpty)) {
      throw FormatException('Analyzer index $field must be a list of IDs');
    }
    return Set.unmodifiable(value.cast<String>());
  }

  final inputs = json['inputs'];
  if (inputs is! List) {
    throw const FormatException('Analyzer index inputs missing');
  }
  final patterns = json['inputPatterns'];
  if (patterns is! List || patterns.any((value) => value is! String)) {
    throw const FormatException('Analyzer index inputPatterns missing');
  }
  final patternInputs = json['patternInputs'];
  if (patternInputs is! List ||
      patternInputs.any((value) => value is! String)) {
    throw const FormatException('Analyzer index patternInputs missing');
  }
  return ZukeIndex(
    inputDigest: requiredString('inputDigest'),
    generatedManifestDigest: requiredString('generatedManifestDigest'),
    generatedManifestPath: _validatedRelativePath(
      requiredString('generatedManifestPath'),
    ),
    inputs: List.unmodifiable(
      inputs.map((input) {
        if (input is! Map) {
          throw const FormatException('Invalid analyzer index input');
        }
        return ZukeIndexInput.fromJson(input);
      }),
    ),
    inputPatterns: List.unmodifiable(
      patterns.cast<String>().map(_validatedPattern).toList()..sort(),
    ),
    patternInputs: Set.unmodifiable(
      patternInputs.cast<String>().map(_validatedRelativePath),
    ),
    requirementIds: ids('requirementIds'),
    controlIds: ids('controlIds'),
    bindingIds: ids('bindingIds'),
  );
}