PubEntry.fromYamlMap constructor

PubEntry.fromYamlMap(
  1. String name,
  2. YamlMap map
)

Factory constructor that creates a Hosted or Git version if needed; for other sources it creates a generic PubEntry.

Implementation

factory PubEntry.fromYamlMap(String name, YamlMap map) {
  if (map['dependency'] is! String ||
      map['source'] is! String ||
      map['version'] is! String) {
    throw ArgumentError('Invalid types in YamlMap');
  }
  if (!map.containsKey('dependency') ||
      !map.containsKey('source') ||
      !map.containsKey('version') ||
      !map.containsKey('description')) {
    throw ArgumentError('Missing required keys in YamlMap');
  }

  String source = map['source'] as String;
  if (source == 'hosted') {
    return HostedPubEntry.fromYamlMap(name, map);
  } else if (source == 'git') {
    return GitPubEntry.fromYamlMap(name, map);
  } else {
    // For other types, just return the base class instance
    PubDesc? desc;
    if (map['description'] is YamlMap) {
      desc = PubDesc.fromYamlMap(map['description'] as YamlMap);
    }
    return PubEntry(
      name: name,
      dependency: map['dependency'] as String,
      description: desc,
      source: source,
      version: map['version'] as String,
    );
  }
}