SWPConfig.fromYaml constructor
Creates a SWPConfig from YamlMap.
Implementation
factory SWPConfig.fromYaml(
YamlMap yamlMap, {
bool isRootConfig = false,
SWPConfig? rootConfig,
}) {
final schemaPath =
yamlMap['schema_path']?.toString() ?? rootConfig?.schemaPath;
final schemaUrl =
yamlMap['schema_url']?.toString() ?? rootConfig?.schemaUrl;
if (schemaUrl != null) {
final uri = Uri.tryParse(schemaUrl);
if (uri == null) {
throw const ConfigException(
"Config parameter 'schema_url' must be valid URL.",
);
}
}
if (!isRootConfig && schemaPath == null && schemaUrl == null) {
throw const ConfigException(
"Config parameters 'schema_path' or 'schema_url' are required.",
);
}
var outputDirectory =
yamlMap['output_directory']?.toString() ?? rootConfig?.outputDirectory;
if (isRootConfig && outputDirectory == null) {
outputDirectory = '';
}
if (outputDirectory == null) {
if (rootConfig == null) {
throw const ConfigException(
"Config parameter 'output_directory' is required.",
);
} else if (rootConfig.outputDirectory == '') {
throw ConfigException(
"Config parameter 'output_directory' for $schemaPath was not found.\n"
"Add the 'output_directory' parameter under 'swagger_parser:' or set it for each schema.",
);
}
outputDirectory = rootConfig.outputDirectory;
}
final rawName = yamlMap['name']?.toString();
final name = rawName == null || rawName.isEmpty
? (schemaPath ?? schemaUrl ?? '').split('/').last.split('.').first
: rawName;
final defaultContentType = yamlMap['default_content_type'] as String? ??
rootConfig?.defaultContentType;
final extrasParameterByDefault =
yamlMap['extras_parameter_by_default'] as bool? ??
rootConfig?.extrasParameterByDefault;
final dioOptionsParameterByDefault =
yamlMap['dio_options_parameter_by_default'] as bool? ??
rootConfig?.dioOptionsParameterByDefault;
final addOpenApiMetadata = yamlMap['add_openapi_metadata'] as bool? ??
rootConfig?.addOpenApiMetadata;
final pathMethodName =
yamlMap['path_method_name'] as bool? ?? rootConfig?.pathMethodName;
final mergeClients =
yamlMap['merge_clients'] as bool? ?? rootConfig?.mergeClients;
final enumsParentPrefix = yamlMap['enums_parent_prefix'] as bool? ??
rootConfig?.enumsParentPrefix;
final rawSkippedParameters = yamlMap['skipped_parameters'] as YamlList?;
List<String>? skippedParameters;
if (rawSkippedParameters != null) {
skippedParameters = [];
for (final p in rawSkippedParameters) {
if (p is! String) {
throw const ConfigException(
"Config parameter 'skipped_parameters' values must be List of String.",
);
}
skippedParameters.add(p);
}
} else if (rootConfig?.skippedParameters != null) {
skippedParameters = List.from(rootConfig!.skippedParameters);
}
final rawLanguage = yamlMap['language']?.toString();
final language = rawLanguage == null
? rootConfig?.language
: ProgrammingLanguage.fromString(rawLanguage);
JsonSerializer? jsonSerializer;
final rawJsonSerializer = yamlMap['json_serializer']?.toString();
if (rawJsonSerializer != null) {
jsonSerializer = JsonSerializer.fromString(rawJsonSerializer);
} else if (rootConfig?.jsonSerializer != null) {
jsonSerializer = rootConfig!.jsonSerializer;
}
final rootClient =
yamlMap['root_client'] as bool? ?? rootConfig?.rootClient;
final rootClientName =
yamlMap['root_client_name'] as String? ?? rootConfig?.rootClientName;
final clientPostfix =
yamlMap['client_postfix'] as String? ?? rootConfig?.clientPostfix;
final exportFile =
yamlMap['export_file'] as bool? ?? rootConfig?.exportFile;
final putClientsInFolder = yamlMap['put_clients_in_folder'] as bool? ??
rootConfig?.putClientsInFolder;
final enumsToJson =
yamlMap['enums_to_json'] as bool? ?? rootConfig?.enumsToJson;
final unknownEnumValue =
yamlMap['unknown_enum_value'] as bool? ?? rootConfig?.unknownEnumValue;
final markFilesAsGenerated = yamlMap['mark_files_as_generated'] as bool? ??
rootConfig?.markFilesAsGenerated;
final originalHttpResponse = yamlMap['original_http_response'] as bool? ??
rootConfig?.originalHttpResponse;
final rawReplacementRules = yamlMap['replacement_rules'] as YamlList?;
List<ReplacementRule>? replacementRules;
if (rawReplacementRules != null) {
replacementRules = [];
for (final r in rawReplacementRules) {
if (r is! YamlMap ||
r['pattern'] is! String ||
r['replacement'] is! String) {
throw const ConfigException(
"Config parameter 'replacement_rules' values must be maps of strings "
"and contain 'pattern' and 'replacement'.",
);
}
replacementRules.add(
ReplacementRule(
pattern: RegExp(r['pattern'].toString()),
replacement: r['replacement'].toString(),
),
);
}
} else if (rootConfig?.replacementRules != null) {
replacementRules = List.from(rootConfig!.replacementRules);
}
final rawReplacementRulesForRawSchema =
yamlMap['replacement_rules_for_raw_schema'] as YamlList?;
List<ReplacementRule>? replacementRulesForRawSchema;
if (rawReplacementRulesForRawSchema != null) {
replacementRulesForRawSchema = [];
for (final r in rawReplacementRulesForRawSchema) {
if (r is! YamlMap ||
r['pattern'] is! String ||
r['replacement'] is! String) {
throw const ConfigException(
"Config parameter 'replacement_rules_for_raw_schema' values must be maps of strings "
"and contain 'pattern' and 'replacement'.",
);
}
replacementRulesForRawSchema.add(
ReplacementRule(
pattern: RegExp(r['pattern'].toString()),
replacement: r['replacement'].toString(),
),
);
}
} else if (rootConfig?.replacementRulesForRawSchema != null) {
replacementRulesForRawSchema =
List.from(rootConfig!.replacementRulesForRawSchema);
}
final generateValidator =
yamlMap['generate_validator'] as bool? ?? rootConfig?.generateValidator;
final useXNullable =
yamlMap['use_x_nullable'] as bool? ?? rootConfig?.useXNullable;
final useFreezed3 =
yamlMap['use_freezed3'] as bool? ?? rootConfig?.useFreezed3;
final freezedJsonSerialization =
yamlMap['freezed_json_serialization'] as bool? ??
rootConfig?.freezedJsonSerialization;
final useMultipartFile =
yamlMap['use_multipart_file'] as bool? ?? rootConfig?.useMultipartFile;
final fallbackUnion =
yamlMap['fallback_union'] as String? ?? rootConfig?.fallbackUnion;
final dartMappableConvenientWhen =
yamlMap['dart_mappable_convenient_when'] as bool? ??
rootConfig?.dartMappableConvenientWhen ??
true;
final useDartMappableNaming =
yamlMap['use_dart_mappable_naming'] as bool? ??
rootConfig?.useDartMappableNaming;
final excludedTagsYaml = yamlMap['exclude_tags'] as YamlList?;
List<String>? excludedTags;
if (excludedTagsYaml != null) {
excludedTags = [];
for (final t in excludedTagsYaml) {
if (t is! String) {
throw const ConfigException(
"Config parameter 'exclude_tags' values must be List of String.",
);
}
excludedTags.add(t);
}
} else if (rootConfig?.excludeTags != null) {
excludedTags = List.from(rootConfig!.excludeTags);
}
final includedTagsYaml = yamlMap['include_tags'] as YamlList?;
List<String>? includedTags;
if (includedTagsYaml != null) {
includedTags = [];
for (final t in includedTagsYaml) {
if (t is! String) {
throw const ConfigException(
"Config parameter 'include_tags' values must be List of String.",
);
}
includedTags.add(t);
}
} else if (rootConfig?.includeTags != null) {
includedTags = List.from(rootConfig!.includeTags);
}
final includePaths = yamlMap['include_paths'] as YamlList?;
List<String>? includePathsList;
if (includePaths != null) {
includePathsList = [];
for (final p in includePaths) {
if (p is! String) {
throw const ConfigException(
"Config parameter 'include_paths' values must be List of String.",
);
}
includePathsList.add(p);
}
} else if (rootConfig?.includePaths case final paths?) {
includePathsList = List.from(paths);
}
final fallbackClient =
yamlMap['fallback_client'] as String? ?? rootConfig?.fallbackClient;
final mergeOutputs =
yamlMap['merge_outputs'] as bool? ?? rootConfig?.mergeOutputs;
final includeIfNull =
yamlMap['include_if_null'] as bool? ?? rootConfig?.includeIfNull;
final inferRequiredFromNullable =
yamlMap['infer_required_from_nullable'] as bool? ??
rootConfig?.inferRequiredFromNullable;
final useFlutterCompute = yamlMap['use_flutter_compute'] as bool? ??
rootConfig?.useFlutterCompute;
final generateUrlsConstants = yamlMap['generate_urls_constants'] as bool? ??
rootConfig?.generateUrlsConstants;
final preserveSchemaCasing = yamlMap['preserve_schema_casing'] as bool? ??
rootConfig?.preserveSchemaCasing;
final rawFieldParsers = yamlMap['field_parsers'] as YamlList?;
List<FieldParser>? fieldParsers;
if (rawFieldParsers != null) {
fieldParsers = [];
for (final p in rawFieldParsers) {
if (p is! YamlMap ||
p['apply_to_type'] is! String ||
p['parser_name'] is! String ||
p['parser_absolute_path'] is! String) {
throw const ConfigException(
"Config parameter 'field_parsers' values must be List of maps with 'apply_to_type', 'parser_name', and 'parser_absolute_path'.",
);
}
fieldParsers.add(
FieldParser(
applyToType: p['apply_to_type'].toString(),
parserName: p['parser_name'].toString(),
parserAbsolutePath: p['parser_absolute_path'].toString(),
),
);
}
} else if (rootConfig?.fieldParsers != null) {
fieldParsers = List.from(rootConfig!.fieldParsers);
}
// Default config
final dc = SWPConfig(name: name, outputDirectory: outputDirectory);
return SWPConfig._(
schemaPath: schemaPath,
schemaUrl: schemaUrl,
fieldParsers: fieldParsers ?? dc.fieldParsers,
outputDirectory: outputDirectory,
name: name,
pathMethodName: pathMethodName ?? dc.pathMethodName,
defaultContentType: defaultContentType ?? dc.defaultContentType,
extrasParameterByDefault:
extrasParameterByDefault ?? dc.extrasParameterByDefault,
dioOptionsParameterByDefault:
dioOptionsParameterByDefault ?? dc.dioOptionsParameterByDefault,
addOpenApiMetadata: addOpenApiMetadata ?? dc.addOpenApiMetadata,
mergeClients: mergeClients ?? dc.mergeClients,
enumsParentPrefix: enumsParentPrefix ?? dc.enumsParentPrefix,
skippedParameters: skippedParameters ?? dc.skippedParameters,
exportFile: exportFile ?? dc.exportFile,
language: language ?? dc.language,
jsonSerializer: jsonSerializer ?? dc.jsonSerializer,
rootClient: rootClient ?? dc.rootClient,
rootClientName: rootClientName ?? dc.rootClientName,
clientPostfix: clientPostfix?.trim() ?? dc.clientPostfix,
putClientsInFolder: putClientsInFolder ?? dc.putClientsInFolder,
enumsToJson: enumsToJson ?? dc.enumsToJson,
unknownEnumValue: unknownEnumValue ?? dc.unknownEnumValue,
markFilesAsGenerated: markFilesAsGenerated ?? dc.markFilesAsGenerated,
originalHttpResponse: originalHttpResponse ?? dc.originalHttpResponse,
replacementRules: replacementRules ?? dc.replacementRules,
replacementRulesForRawSchema:
replacementRulesForRawSchema ?? dc.replacementRulesForRawSchema,
generateValidator: generateValidator ?? dc.generateValidator,
useXNullable: useXNullable ?? dc.useXNullable,
useFreezed3: useFreezed3 ?? dc.useFreezed3,
freezedJsonSerialization:
freezedJsonSerialization ?? dc.freezedJsonSerialization,
useMultipartFile: useMultipartFile ?? dc.useMultipartFile,
fallbackUnion: fallbackUnion,
excludeTags: excludedTags ?? dc.excludeTags,
includeTags: includedTags ?? dc.includeTags,
fallbackClient: fallbackClient ?? dc.fallbackClient,
mergeOutputs: mergeOutputs ?? dc.mergeOutputs,
dartMappableConvenientWhen: dartMappableConvenientWhen,
useDartMappableNaming: useDartMappableNaming ?? dc.useDartMappableNaming,
includeIfNull: includeIfNull ?? dc.includeIfNull,
inferRequiredFromNullable:
inferRequiredFromNullable ?? dc.inferRequiredFromNullable,
useFlutterCompute: useFlutterCompute ?? dc.useFlutterCompute,
includePaths: includePathsList ?? dc.includePaths,
generateUrlsConstants: generateUrlsConstants ?? dc.generateUrlsConstants,
preserveSchemaCasing: preserveSchemaCasing ?? dc.preserveSchemaCasing,
);
}