From 1a1a5ac49593b1b11763235d9a9f3169487a7c37 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 6 Oct 2023 08:00:08 +0200 Subject: [PATCH] refactor(dynamite): Remove class prefixes Signed-off-by: jld3103 --- .../dynamite/lib/src/builder/client.dart | 12 +++---- .../lib/src/builder/header_serializer.dart | 14 ++++----- .../dynamite/lib/src/builder/ofs_builder.dart | 31 +++++++++---------- .../lib/src/builder/resolve_enum.dart | 22 ++++++------- .../lib/src/builder/resolve_interface.dart | 4 +-- .../lib/src/builder/resolve_mime_type.dart | 2 +- .../lib/src/builder/resolve_object.dart | 4 +-- .../dynamite/lib/src/builder/state.dart | 8 +---- .../dynamite/lib/src/openapi_builder.dart | 2 +- 9 files changed, 46 insertions(+), 53 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/builder/client.dart b/packages/dynamite/dynamite/lib/src/builder/client.dart index 2eb555b5..54adbe4c 100644 --- a/packages/dynamite/dynamite/lib/src/builder/client.dart +++ b/packages/dynamite/dynamite/lib/src/builder/client.dart @@ -33,7 +33,7 @@ Class buildRootClient( (final b) { b ..extend = refer('DynamiteClient') - ..name = '${state.classPrefix}Client' + ..name = 'Client' ..docs.addAll(spec.formattedTagsFor(null)) ..constructors.addAll([ Constructor( @@ -105,7 +105,7 @@ super( ]); for (final tag in tags.where((final t) => !t.contains('/'))) { - final client = '${state.classPrefix}${clientName(tag)}'; + final client = clientName(tag); b.methods.add( Method( @@ -132,7 +132,7 @@ Class buildClient( Class( (final b) { b - ..name = '${state.classPrefix}${clientName(tag)}' + ..name = clientName(tag) ..docs.addAll(spec.formattedTagsFor(tag)) ..constructors.add( Constructor( @@ -149,7 +149,7 @@ Class buildClient( Field( (final b) => b ..name = '_rootClient' - ..type = refer('${state.classPrefix}Client') + ..type = refer('Client') ..modifier = FieldModifier.final$, ), ); @@ -161,8 +161,8 @@ Class buildClient( ..name = toDartName(t.substring('$tag/'.length)) ..lambda = true ..type = MethodType.getter - ..returns = refer('${state.classPrefix}${clientName(t)}') - ..body = Code('${state.classPrefix}${clientName(t)}(_rootClient)'), + ..returns = refer(clientName(t)) + ..body = Code('${clientName(t)}(_rootClient)'), ), ); } diff --git a/packages/dynamite/dynamite/lib/src/builder/header_serializer.dart b/packages/dynamite/dynamite/lib/src/builder/header_serializer.dart index 2e9121d2..ebe8c6ca 100644 --- a/packages/dynamite/dynamite/lib/src/builder/header_serializer.dart +++ b/packages/dynamite/dynamite/lib/src/builder/header_serializer.dart @@ -14,8 +14,8 @@ Spec buildHeaderSerializer( ) => Class( (final b) => b - ..name = '_\$${state.classPrefix}${identifier}Serializer' - ..implements.add(refer('StructuredSerializer<${state.classPrefix}$identifier>')) + ..name = '_\$${identifier}Serializer' + ..implements.add(refer('StructuredSerializer<$identifier>')) ..fields.addAll([ Field( (final b) => b @@ -23,7 +23,7 @@ Spec buildHeaderSerializer( ..modifier = FieldModifier.final$ ..type = refer('Iterable') ..annotations.add(refer('override')) - ..assignment = Code('const [${state.classPrefix}$identifier, _\$${state.classPrefix}$identifier]'), + ..assignment = Code('const [$identifier, _\$$identifier]'), ), Field( (final b) => b @@ -31,7 +31,7 @@ Spec buildHeaderSerializer( ..modifier = FieldModifier.final$ ..type = refer('String') ..annotations.add(refer('override')) - ..assignment = Code("r'${state.classPrefix}$identifier'"), + ..assignment = Code("r'$identifier'"), ), ]) ..methods.addAll([ @@ -49,7 +49,7 @@ Spec buildHeaderSerializer( Parameter( (final b) => b ..name = 'object' - ..type = refer('${state.classPrefix}$identifier'), + ..type = refer(identifier), ), ]) ..optionalParameters.add( @@ -66,7 +66,7 @@ Spec buildHeaderSerializer( Method((final b) { b ..name = 'deserialize' - ..returns = refer('${state.classPrefix}$identifier') + ..returns = refer(identifier) ..annotations.add(refer('override')) ..requiredParameters.addAll([ Parameter( @@ -90,7 +90,7 @@ Spec buildHeaderSerializer( ), ) ..body = Code(''' -final result = new ${state.classPrefix}${identifier}Builder(); +final result = new ${identifier}Builder(); final iterator = serialized.iterator; while (iterator.moveNext()) { diff --git a/packages/dynamite/dynamite/lib/src/builder/ofs_builder.dart b/packages/dynamite/dynamite/lib/src/builder/ofs_builder.dart index 5d8fe5ea..1639f0bc 100644 --- a/packages/dynamite/dynamite/lib/src/builder/ofs_builder.dart +++ b/packages/dynamite/dynamite/lib/src/builder/ofs_builder.dart @@ -16,7 +16,7 @@ TypeResult resolveAllOf( final bool nullable = false, }) { final result = TypeResultObject( - '${state.classPrefix}$identifier', + identifier, nullable: nullable, ); @@ -55,7 +55,7 @@ TypeResult resolveAllOf( state.output.add( buildBuiltClass( - '${state.classPrefix}$identifier', + identifier, interfaces: interfaces, ), ); @@ -75,7 +75,7 @@ TypeResult resolveOfs( } final result = TypeResultObject( - '${state.classPrefix}$identifier', + identifier, nullable: nullable, ); @@ -94,13 +94,13 @@ TypeResult resolveOfs( final fields = {}; for (final result in results) { - final dartName = toDartName(result.name.replaceFirst(state.classPrefix, '')); - fields[result.name] = toFieldName(dartName, result.name.replaceFirst(state.classPrefix, '')); + final dartName = toDartName(result.name); + fields[result.name] = toFieldName(dartName, result.name); } state.output.addAll([ buildBuiltClass( - '${state.classPrefix}$identifier', + identifier, methods: BuiltList.build((final b) { b.add( Method( @@ -132,8 +132,8 @@ TypeResult resolveOfs( ), Class( (final b) => b - ..name = '_\$${state.classPrefix}${identifier}Serializer' - ..implements.add(refer('PrimitiveSerializer<${state.classPrefix}$identifier>')) + ..name = '_\$${identifier}Serializer' + ..implements.add(refer('PrimitiveSerializer<$identifier>')) ..fields.addAll([ Field( (final b) => b @@ -141,7 +141,7 @@ TypeResult resolveOfs( ..modifier = FieldModifier.final$ ..type = refer('Iterable') ..annotations.add(refer('override')) - ..assignment = Code('const [${state.classPrefix}$identifier, _\$${state.classPrefix}$identifier]'), + ..assignment = Code('const [$identifier, _\$$identifier]'), ), Field( (final b) => b @@ -149,7 +149,7 @@ TypeResult resolveOfs( ..modifier = FieldModifier.final$ ..type = refer('String') ..annotations.add(refer('override')) - ..assignment = Code("r'${state.classPrefix}$identifier'"), + ..assignment = Code("r'$identifier'"), ), ]) ..methods.addAll([ @@ -167,7 +167,7 @@ TypeResult resolveOfs( Parameter( (final b) => b ..name = 'object' - ..type = refer('${state.classPrefix}$identifier'), + ..type = refer(identifier), ), ]) ..optionalParameters.add( @@ -184,7 +184,7 @@ TypeResult resolveOfs( Method((final b) { b ..name = 'deserialize' - ..returns = refer('${state.classPrefix}$identifier') + ..returns = refer(identifier) ..annotations.add(refer('override')) ..requiredParameters.addAll([ Parameter( @@ -209,7 +209,7 @@ TypeResult resolveOfs( ) ..body = Code( [ - 'final result = new ${state.classPrefix}${identifier}Builder()', + 'final result = new ${identifier}Builder()', '..data = JsonObject(data);', if (schema.discriminator != null) ...[ 'if (data is! Iterable) {', @@ -231,12 +231,11 @@ TypeResult resolveOfs( ], for (final result in results) ...[ if (schema.discriminator != null) ...[ - "if (discriminator == '${result.name.replaceFirst(state.classPrefix, '')}'", + "if (discriminator == '${result.name}'", if (schema.discriminator!.mapping != null && schema.discriminator!.mapping!.isNotEmpty) ...[ for (final key in schema.discriminator!.mapping!.entries .where( - (final entry) => - entry.value.endsWith('/${result.name.replaceFirst(state.classPrefix, '')}'), + (final entry) => entry.value.endsWith('/${result.name}'), ) .map((final entry) => entry.key)) ...[ " || discriminator == '$key'", diff --git a/packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart b/packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart index df8199ab..14de57d4 100644 --- a/packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart +++ b/packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart @@ -15,11 +15,11 @@ TypeResult resolveEnum( final TypeResult subResult, { final bool nullable = false, }) { - if (state.resolvedTypes.add(TypeResultEnum('${state.classPrefix}$identifier', subResult))) { + if (state.resolvedTypes.add(TypeResultEnum(identifier, subResult))) { state.output.add( Class( (final b) => b - ..name = '${state.classPrefix}$identifier' + ..name = identifier ..extend = refer('EnumClass') ..constructors.add( Constructor( @@ -50,9 +50,9 @@ TypeResult resolveEnum( ..name = toDartName(value) ..static = true ..modifier = FieldModifier.constant - ..type = refer('${state.classPrefix}$identifier') + ..type = refer(identifier) ..assignment = Code( - '_\$${toCamelCase('${state.classPrefix}$identifier')}${toDartName(value, uppercaseFirstCharacter: true)}', + '_\$${toCamelCase(identifier)}${toDartName(value, uppercaseFirstCharacter: true)}', ); if (toDartName(value) != value) { @@ -60,7 +60,7 @@ TypeResult resolveEnum( throw Exception( 'Sorry enum values are a bit broken. ' 'See https://github.com/google/json_serializable.dart/issues/616. ' - 'Please remove the enum values on ${state.classPrefix}$identifier.', + 'Please remove the enum values on $identifier.', ); } b.annotations.add( @@ -77,16 +77,16 @@ TypeResult resolveEnum( Method( (final b) => b ..name = 'values' - ..returns = refer('BuiltSet<${state.classPrefix}$identifier>') + ..returns = refer('BuiltSet<$identifier>') ..lambda = true ..static = true - ..body = Code('_\$${toCamelCase('${state.classPrefix}$identifier')}Values') + ..body = Code('_\$${toCamelCase(identifier)}Values') ..type = MethodType.getter, ), Method( (final b) => b ..name = 'valueOf' - ..returns = refer('${state.classPrefix}$identifier') + ..returns = refer(identifier) ..lambda = true ..static = true ..requiredParameters.add( @@ -96,15 +96,15 @@ TypeResult resolveEnum( ..type = refer(subResult.name), ), ) - ..body = Code('_\$valueOf${state.classPrefix}$identifier(name)'), + ..body = Code('_\$valueOf$identifier(name)'), ), - buildSerializer('${state.classPrefix}$identifier'), + buildSerializer(identifier), ]), ), ); } return TypeResultEnum( - '${state.classPrefix}$identifier', + identifier, subResult, nullable: nullable, ); diff --git a/packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart b/packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart index 6f93512e..b0b3f092 100644 --- a/packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart +++ b/packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart @@ -14,11 +14,11 @@ TypeResultObject resolveInterface( final openapi.Schema schema, ) { final result = TypeResultObject( - '${state.classPrefix}$identifier', + identifier, ); if (state.resolvedInterfaces.add(result)) { - final className = '${state.classPrefix}$identifier$interfaceSuffix'; + final className = '$identifier$interfaceSuffix'; state.output.add( Class((final b) { diff --git a/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart b/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart index 5da0f29c..3130248d 100644 --- a/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart +++ b/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart @@ -71,7 +71,7 @@ Iterable resolveMimeTypeEncode( mediaType.schema!, nullable: dartParameterNullable, ); - final parameterName = toDartName(result.name.replaceFirst(state.classPrefix, '')); + final parameterName = toDartName(result.name); switch (mimeType) { case 'application/json': case 'application/x-www-form-urlencoded': diff --git a/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart b/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart index 6ad891d2..fdb65624 100644 --- a/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart +++ b/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart @@ -17,7 +17,7 @@ TypeResultObject resolveObject( final bool isHeader = false, }) { final result = TypeResultObject( - '${state.classPrefix}$identifier', + identifier, nullable: nullable, ); if (state.resolvedTypes.add(result)) { @@ -45,7 +45,7 @@ TypeResultObject resolveObject( state.output.addAll([ buildBuiltClass( - '${state.classPrefix}$identifier', + identifier, defaults: defaults, customSerializer: isHeader, interfaces: [ diff --git a/packages/dynamite/dynamite/lib/src/builder/state.dart b/packages/dynamite/dynamite/lib/src/builder/state.dart index b4ebac49..5154683b 100644 --- a/packages/dynamite/dynamite/lib/src/builder/state.dart +++ b/packages/dynamite/dynamite/lib/src/builder/state.dart @@ -1,14 +1,8 @@ import 'package:code_builder/code_builder.dart'; -import 'package:dynamite/src/helpers/dart_helpers.dart'; import 'package:dynamite/src/models/type_result.dart'; class State { - State(final String prefix) - : classPrefix = toDartName(prefix, uppercaseFirstCharacter: true), - variablePrefix = toDartName(prefix); - - final String classPrefix; - final String variablePrefix; + State(); final output = []; final resolvedTypes = {}; diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 4788cac8..1e46af25 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -49,7 +49,7 @@ class OpenAPIBuilder implements Builder { throw Exception('Only OpenAPI 3.0.0 and later are supported'); } - final state = State(spec.info.title); + final state = State(); final output = ListBuilder() ..addAll(generateImports(outputId))