YmlGeneratorConfig constructor

YmlGeneratorConfig(
  1. PubspecConfig pubspecConfig,
  2. String configContent
)

Implementation

YmlGeneratorConfig(PubspecConfig pubspecConfig, String configContent) {
  loadYaml(configContent).forEach((key, value) {
    final String baseDirectory =
        value['base_directory'] ?? pubspecConfig.baseDirectory;
    final String? path = value['path'];
    final bool generateForGenerics =
        value['generate_for_generics'] ?? pubspecConfig.generateForGenerics;

    final extraImports =
        value.containsKey('extra_imports') ? <String>[] : null;
    final extraImportsVal = value['extra_imports'];
    extraImportsVal?.forEach((e) {
      if (e != null) {
        extraImports!.add(e.toString());
      }
    });

    final extraAnnotations =
        value.containsKey('extra_annotations') ? <String>[] : null;
    final extraAnnotationsVal = value['extra_annotations'];
    extraAnnotationsVal?.forEach((e) {
      if (e != null) {
        extraAnnotations!.add(e.toString());
      }
    });

    final dynamic properties = value['properties'];
    final YamlList? converters = value['converters'];
    final String? type = value['type'];
    if (type == 'custom') {
      models.add(CustomModel(
        name: key,
        path: path,
        baseDirectory: baseDirectory,
        extraImports: extraImports,
        extraAnnotations: extraAnnotations,
      ));
      return;
    } else if (type == 'custom_from_to_json') {
      models.add(CustomFromToJsonModel(
        name: key,
        path: path,
        baseDirectory: baseDirectory,
        extraImports: extraImports,
        extraAnnotations: extraAnnotations,
      ));
      return;
    } else if (type == 'json_converter') {
      models.add(JsonConverterModel(
        name: key,
        path: path,
        baseDirectory: baseDirectory,
        extraImports: extraImports,
        extraAnnotations: extraAnnotations,
      ));
      return;
    }
    if (properties == null) {
      throw Exception('Properties can not be null. model: $key');
    }
    if (!(properties is YamlMap)) {
      throw Exception(
          'Properties should be a map, right now you are using a ${properties.runtimeType}. model: $key');
    }
    if (type == 'enum') {
      final fields = <EnumField>[];
      properties.forEach((propertyKey, propertyValue) {
        if (propertyValue != null && !(propertyValue is YamlMap)) {
          throw Exception('$propertyKey should be an object');
        }
        fields.add(EnumField(
          name: propertyKey,
          value: propertyValue == null ? null : propertyValue['value'],
        ));
      });
      models.add(EnumModel(
        name: key,
        path: path,
        generateMap: value['generate_map'] == true,
        generateExtensions: value['generate_extensions'] == true,
        baseDirectory: baseDirectory,
        fields: fields,
        extraImports: extraImports,
        extraAnnotations: extraAnnotations,
      ));
    } else {
      final fields = <Field>[];
      properties.forEach((propertyKey, propertyValue) {
        if (!(propertyValue is YamlMap)) {
          throw Exception('$propertyKey should be an object');
        }
        fields.add(getField(propertyKey, propertyValue));
      });
      final mappedConverters =
          converters?.map((element) => element.toString()).toList();
      models.add(ObjectModel(
        name: key,
        path: path,
        baseDirectory: baseDirectory,
        generateForGenerics: generateForGenerics,
        fields: fields,
        converters: mappedConverters ?? [],
        extraImports: extraImports,
        extraAnnotations: extraAnnotations,
        equalsAndHashCode: value['equals_and_hash_code'],
        generateToString: value['to_string'],
      ));
    }
  });

  checkIfTypesAvailable();
}