Browse Source

refactor(dynamite): iterate over Map.entries where possible

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
pull/694/head
Nikolas Rimikis 1 year ago
parent
commit
d65cff6e2d
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 88
      packages/dynamite/dynamite/lib/src/openapi_builder.dart

88
packages/dynamite/dynamite/lib/src/openapi_builder.dart

@ -177,19 +177,16 @@ class OpenAPIBuilder implements Builder {
final paths = <String, PathItem>{}; final paths = <String, PathItem>{};
if (spec.paths != null) { if (spec.paths != null) {
for (final path in spec.paths!.keys) { for (final path in spec.paths!.entries) {
final pathItem = spec.paths![path]!; for (final operationEntry in path.value.operations.entries) {
for (final method in pathItem.operations.keys) { final operation = operationEntry.value;
final operation = pathItem.operations[method]!;
if ((tag != null && operation.tags != null && operation.tags!.contains(tag)) || if ((tag != null && operation.tags != null && operation.tags!.contains(tag)) ||
(tag == null && (operation.tags == null || operation.tags!.isEmpty))) { (tag == null && (operation.tags == null || operation.tags!.isEmpty))) {
if (paths[path] == null) { paths[path.key] ??= PathItem(
paths[path] = PathItem( description: path.value.description,
description: pathItem.description, parameters: path.value.parameters,
parameters: pathItem.parameters,
); );
} paths[path.key] = paths[path.key]!.copyWithOperations({operationEntry.key: operation});
paths[path] = paths[path]!.copyWithOperations({method: operation});
} }
} }
} }
@ -316,18 +313,16 @@ class OpenAPIBuilder implements Builder {
..body = Code('$classPrefix${_clientName(t)}(${isRootClient ? 'this' : '_rootClient'})'), ..body = Code('$classPrefix${_clientName(t)}(${isRootClient ? 'this' : '_rootClient'})'),
), ),
], ],
for (final path in paths.keys) ...[ for (final pathEntry in paths.entries) ...[
for (final httpMethod in paths[path]!.operations.keys) ...[ for (final operationEntry in pathEntry.value.operations.entries) ...[
Method( Method(
(final b) { (final b) {
final operation = paths[path]!.operations[httpMethod]!; final httpMethod = operationEntry.key;
final operationId = operation.operationId ?? _toDartName('$httpMethod-$path'); final operation = operationEntry.value;
final pathParameters = <spec_parameter.Parameter>[ final operationId = operation.operationId ?? _toDartName('$httpMethod-${pathEntry.key}');
if (paths[path]!.parameters != null) ...paths[path]!.parameters!,
];
final parameters = <spec_parameter.Parameter>[ final parameters = <spec_parameter.Parameter>[
...pathParameters, ...?pathEntry.value.parameters,
if (operation.parameters != null) ...operation.parameters!, ...?operation.parameters,
]..sort( ]..sort(
(final a, final b) => sortRequiredElements( (final a, final b) => sortRequiredElements(
_isDartParameterRequired( _isDartParameterRequired(
@ -354,15 +349,15 @@ class OpenAPIBuilder implements Builder {
b.annotations.add(refer('Deprecated').call([refer("''")])); b.annotations.add(refer('Deprecated').call([refer("''")]));
} }
final acceptHeader = (operation.responses?.values final acceptHeader = operation.responses?.values
.map((final response) => response.content?.keys) .map((final response) => response.content?.keys)
.whereNotNull() .whereNotNull()
.expand((final element) => element) .expand((final element) => element)
.toSet() ?? .toSet()
{}) .join(',') ??
.join(','); '';
final code = StringBuffer(''' final code = StringBuffer('''
var _path = '$path'; var _path = '${pathEntry.key}';
final _queryParameters = <String, dynamic>{}; final _queryParameters = <String, dynamic>{};
final _headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}}; final _headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}};
Uint8List? _body; Uint8List? _body;
@ -498,8 +493,9 @@ class OpenAPIBuilder implements Builder {
if (operation.requestBody!.content!.length > 1) { if (operation.requestBody!.content!.length > 1) {
throw Exception('Can not work with multiple mime types right now'); throw Exception('Can not work with multiple mime types right now');
} }
for (final mimeType in operation.requestBody!.content!.keys) { for (final content in operation.requestBody!.content!.entries) {
final mediaType = operation.requestBody!.content![mimeType]!; final mimeType = content.key;
final mediaType = content.value;
code.write("_headers['Content-Type'] = '$mimeType';"); code.write("_headers['Content-Type'] = '$mimeType';");
@ -565,8 +561,9 @@ class OpenAPIBuilder implements Builder {
if (operation.responses!.length > 1) { if (operation.responses!.length > 1) {
throw Exception('Can not work with multiple status codes right now'); throw Exception('Can not work with multiple status codes right now');
} }
for (final statusCode in operation.responses!.keys) { for (final responseEntry in operation.responses!.entries) {
final response = operation.responses![statusCode]!; final statusCode = responseEntry.key;
final response = responseEntry.value;
code.write('if (_response.statusCode == $statusCode) {'); code.write('if (_response.statusCode == $statusCode) {');
String? headersType; String? headersType;
@ -600,8 +597,9 @@ class OpenAPIBuilder implements Builder {
if (response.content!.length > 1) { if (response.content!.length > 1) {
throw Exception('Can not work with multiple mime types right now'); throw Exception('Can not work with multiple mime types right now');
} }
for (final mimeType in response.content!.keys) { for (final content in response.content!.entries) {
final mediaType = response.content![mimeType]!; final mimeType = content.key;
final mediaType = content.value;
final result = resolveType( final result = resolveType(
spec, spec,
@ -681,11 +679,9 @@ class OpenAPIBuilder implements Builder {
} }
if (spec.components?.schemas != null) { if (spec.components?.schemas != null) {
for (final name in spec.components!.schemas!.keys) { for (final schema in spec.components!.schemas!.entries) {
final schema = spec.components!.schemas![name]!; final identifier = _toDartName(schema.key, uppercaseFirstCharacter: true);
if (schema.value.type == null && schema.value.ref == null && schema.value.ofs == null) {
final identifier = _toDartName(name, uppercaseFirstCharacter: true);
if (schema.type == null && schema.ref == null && schema.ofs == null) {
output.add('typedef $identifier = dynamic;'); output.add('typedef $identifier = dynamic;');
} else { } else {
final result = resolveType( final result = resolveType(
@ -693,7 +689,7 @@ class OpenAPIBuilder implements Builder {
variablePrefix, variablePrefix,
state, state,
identifier, identifier,
schema, schema.value,
); );
if (result is TypeResultBase) { if (result is TypeResultBase) {
output.add('typedef $identifier = ${result.name};'); output.add('typedef $identifier = ${result.name};');
@ -988,10 +984,11 @@ TypeResult resolveObject(
..lambda = true ..lambda = true
..body = const Code('_jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>'), ..body = const Code('_jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>'),
), ),
for (final propertyName in schema.properties!.keys) ...[ for (final property in schema.properties!.entries) ...[
Method( Method(
(final b) { (final b) {
final propertySchema = schema.properties![propertyName]!; final propertyName = property.key;
final propertySchema = property.value;
final result = resolveType( final result = resolveType(
spec, spec,
variablePrefix, variablePrefix,
@ -1040,8 +1037,8 @@ TypeResult resolveObject(
]); ]);
final defaults = <String>[]; final defaults = <String>[];
for (final propertyName in schema.properties!.keys) { for (final property in schema.properties!.entries) {
final propertySchema = schema.properties![propertyName]!; final propertySchema = property.value;
if (propertySchema.default_ != null) { if (propertySchema.default_ != null) {
final value = propertySchema.default_!.toString(); final value = propertySchema.default_!.toString();
final result = resolveType( final result = resolveType(
@ -1051,7 +1048,7 @@ TypeResult resolveObject(
propertySchema.type!, propertySchema.type!,
propertySchema, propertySchema,
); );
defaults.add('..${_toDartName(propertyName)} = ${_valueToEscapedValue(result, value)}'); defaults.add('..${_toDartName(property.key)} = ${_valueToEscapedValue(result, value)}');
} }
} }
if (defaults.isNotEmpty) { if (defaults.isNotEmpty) {
@ -1168,8 +1165,9 @@ TypeResult resolveObject(
..defaultTo = const Code('FullType.unspecified'), ..defaultTo = const Code('FullType.unspecified'),
), ),
); );
List<Code> deserializeProperty(final String propertyName) { List<Code> deserializeProperty(final MapEntry<String, Schema> property) {
final propertySchema = schema.properties![propertyName]!; final propertyName = property.key;
final propertySchema = property.value;
final result = resolveType( final result = resolveType(
spec, spec,
variablePrefix, variablePrefix,
@ -1212,9 +1210,7 @@ TypeResult resolveObject(
const Code('iterator.moveNext();'), const Code('iterator.moveNext();'),
const Code('final value = iterator.current! as String;'), const Code('final value = iterator.current! as String;'),
const Code('switch (key) {'), const Code('switch (key) {'),
for (final propertyName in schema.properties!.keys) ...[ for (final property in schema.properties!.entries) ...deserializeProperty(property),
...deserializeProperty(propertyName),
],
const Code('}'), const Code('}'),
const Code('}'), const Code('}'),
const Code(''), const Code(''),

Loading…
Cancel
Save