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 String? extendsModel = value['extends'];
    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 description = value['description']?.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 uppercaseEnums =
          (value['uppercase_enums'] ?? pubspecConfig.uppercaseEnums) == true;

      final fields = <EnumField>[];
      properties.forEach((propertyKey, propertyValue) {
        if (propertyValue != null && propertyValue is! YamlMap) {
          throw Exception('$propertyKey should be an object');
        }
        fields.add(EnumField(
          name: uppercaseEnums ? propertyKey.toUpperCase() : propertyKey,
          rawName: propertyKey,
          value: propertyValue == null ? null : propertyValue['value'],
          description:
              propertyValue == null ? null : propertyValue['description'],
        ));
      });
      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,
        description: description,
      ));
    } else {
      final staticCreate = (value['static_create'] ?? false) == true;
      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,
        extendsModel: extendsModel,
        baseDirectory: baseDirectory,
        generateForGenerics: generateForGenerics,
        fields: fields,
        converters: mappedConverters ?? [],
        extraImports: extraImports,
        extraAnnotations: extraAnnotations,
        staticCreate: staticCreate,
        equalsAndHashCode: value['equals_and_hash_code'],
        explicitToJson: value['explicit_to_json'],
        generateToString: value['to_string'],
        description: description,
      ));
    }
  });

  checkIfTypesAvailable();
}