Browse Source

refactor(dynamite): Remove class prefixes

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/935/head
jld3103 1 year ago
parent
commit
1a1a5ac495
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 12
      packages/dynamite/dynamite/lib/src/builder/client.dart
  2. 14
      packages/dynamite/dynamite/lib/src/builder/header_serializer.dart
  3. 31
      packages/dynamite/dynamite/lib/src/builder/ofs_builder.dart
  4. 22
      packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart
  5. 4
      packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart
  6. 2
      packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart
  7. 4
      packages/dynamite/dynamite/lib/src/builder/resolve_object.dart
  8. 8
      packages/dynamite/dynamite/lib/src/builder/state.dart
  9. 2
      packages/dynamite/dynamite/lib/src/openapi_builder.dart

12
packages/dynamite/dynamite/lib/src/builder/client.dart

@ -33,7 +33,7 @@ Class buildRootClient(
(final b) { (final b) {
b b
..extend = refer('DynamiteClient') ..extend = refer('DynamiteClient')
..name = '${state.classPrefix}Client' ..name = 'Client'
..docs.addAll(spec.formattedTagsFor(null)) ..docs.addAll(spec.formattedTagsFor(null))
..constructors.addAll([ ..constructors.addAll([
Constructor( Constructor(
@ -105,7 +105,7 @@ super(
]); ]);
for (final tag in tags.where((final t) => !t.contains('/'))) { for (final tag in tags.where((final t) => !t.contains('/'))) {
final client = '${state.classPrefix}${clientName(tag)}'; final client = clientName(tag);
b.methods.add( b.methods.add(
Method( Method(
@ -132,7 +132,7 @@ Class buildClient(
Class( Class(
(final b) { (final b) {
b b
..name = '${state.classPrefix}${clientName(tag)}' ..name = clientName(tag)
..docs.addAll(spec.formattedTagsFor(tag)) ..docs.addAll(spec.formattedTagsFor(tag))
..constructors.add( ..constructors.add(
Constructor( Constructor(
@ -149,7 +149,7 @@ Class buildClient(
Field( Field(
(final b) => b (final b) => b
..name = '_rootClient' ..name = '_rootClient'
..type = refer('${state.classPrefix}Client') ..type = refer('Client')
..modifier = FieldModifier.final$, ..modifier = FieldModifier.final$,
), ),
); );
@ -161,8 +161,8 @@ Class buildClient(
..name = toDartName(t.substring('$tag/'.length)) ..name = toDartName(t.substring('$tag/'.length))
..lambda = true ..lambda = true
..type = MethodType.getter ..type = MethodType.getter
..returns = refer('${state.classPrefix}${clientName(t)}') ..returns = refer(clientName(t))
..body = Code('${state.classPrefix}${clientName(t)}(_rootClient)'), ..body = Code('${clientName(t)}(_rootClient)'),
), ),
); );
} }

14
packages/dynamite/dynamite/lib/src/builder/header_serializer.dart

@ -14,8 +14,8 @@ Spec buildHeaderSerializer(
) => ) =>
Class( Class(
(final b) => b (final b) => b
..name = '_\$${state.classPrefix}${identifier}Serializer' ..name = '_\$${identifier}Serializer'
..implements.add(refer('StructuredSerializer<${state.classPrefix}$identifier>')) ..implements.add(refer('StructuredSerializer<$identifier>'))
..fields.addAll([ ..fields.addAll([
Field( Field(
(final b) => b (final b) => b
@ -23,7 +23,7 @@ Spec buildHeaderSerializer(
..modifier = FieldModifier.final$ ..modifier = FieldModifier.final$
..type = refer('Iterable<Type>') ..type = refer('Iterable<Type>')
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..assignment = Code('const [${state.classPrefix}$identifier, _\$${state.classPrefix}$identifier]'), ..assignment = Code('const [$identifier, _\$$identifier]'),
), ),
Field( Field(
(final b) => b (final b) => b
@ -31,7 +31,7 @@ Spec buildHeaderSerializer(
..modifier = FieldModifier.final$ ..modifier = FieldModifier.final$
..type = refer('String') ..type = refer('String')
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..assignment = Code("r'${state.classPrefix}$identifier'"), ..assignment = Code("r'$identifier'"),
), ),
]) ])
..methods.addAll([ ..methods.addAll([
@ -49,7 +49,7 @@ Spec buildHeaderSerializer(
Parameter( Parameter(
(final b) => b (final b) => b
..name = 'object' ..name = 'object'
..type = refer('${state.classPrefix}$identifier'), ..type = refer(identifier),
), ),
]) ])
..optionalParameters.add( ..optionalParameters.add(
@ -66,7 +66,7 @@ Spec buildHeaderSerializer(
Method((final b) { Method((final b) {
b b
..name = 'deserialize' ..name = 'deserialize'
..returns = refer('${state.classPrefix}$identifier') ..returns = refer(identifier)
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..requiredParameters.addAll([ ..requiredParameters.addAll([
Parameter( Parameter(
@ -90,7 +90,7 @@ Spec buildHeaderSerializer(
), ),
) )
..body = Code(''' ..body = Code('''
final result = new ${state.classPrefix}${identifier}Builder(); final result = new ${identifier}Builder();
final iterator = serialized.iterator; final iterator = serialized.iterator;
while (iterator.moveNext()) { while (iterator.moveNext()) {

31
packages/dynamite/dynamite/lib/src/builder/ofs_builder.dart

@ -16,7 +16,7 @@ TypeResult resolveAllOf(
final bool nullable = false, final bool nullable = false,
}) { }) {
final result = TypeResultObject( final result = TypeResultObject(
'${state.classPrefix}$identifier', identifier,
nullable: nullable, nullable: nullable,
); );
@ -55,7 +55,7 @@ TypeResult resolveAllOf(
state.output.add( state.output.add(
buildBuiltClass( buildBuiltClass(
'${state.classPrefix}$identifier', identifier,
interfaces: interfaces, interfaces: interfaces,
), ),
); );
@ -75,7 +75,7 @@ TypeResult resolveOfs(
} }
final result = TypeResultObject( final result = TypeResultObject(
'${state.classPrefix}$identifier', identifier,
nullable: nullable, nullable: nullable,
); );
@ -94,13 +94,13 @@ TypeResult resolveOfs(
final fields = <String, String>{}; final fields = <String, String>{};
for (final result in results) { for (final result in results) {
final dartName = toDartName(result.name.replaceFirst(state.classPrefix, '')); final dartName = toDartName(result.name);
fields[result.name] = toFieldName(dartName, result.name.replaceFirst(state.classPrefix, '')); fields[result.name] = toFieldName(dartName, result.name);
} }
state.output.addAll([ state.output.addAll([
buildBuiltClass( buildBuiltClass(
'${state.classPrefix}$identifier', identifier,
methods: BuiltList.build((final b) { methods: BuiltList.build((final b) {
b.add( b.add(
Method( Method(
@ -132,8 +132,8 @@ TypeResult resolveOfs(
), ),
Class( Class(
(final b) => b (final b) => b
..name = '_\$${state.classPrefix}${identifier}Serializer' ..name = '_\$${identifier}Serializer'
..implements.add(refer('PrimitiveSerializer<${state.classPrefix}$identifier>')) ..implements.add(refer('PrimitiveSerializer<$identifier>'))
..fields.addAll([ ..fields.addAll([
Field( Field(
(final b) => b (final b) => b
@ -141,7 +141,7 @@ TypeResult resolveOfs(
..modifier = FieldModifier.final$ ..modifier = FieldModifier.final$
..type = refer('Iterable<Type>') ..type = refer('Iterable<Type>')
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..assignment = Code('const [${state.classPrefix}$identifier, _\$${state.classPrefix}$identifier]'), ..assignment = Code('const [$identifier, _\$$identifier]'),
), ),
Field( Field(
(final b) => b (final b) => b
@ -149,7 +149,7 @@ TypeResult resolveOfs(
..modifier = FieldModifier.final$ ..modifier = FieldModifier.final$
..type = refer('String') ..type = refer('String')
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..assignment = Code("r'${state.classPrefix}$identifier'"), ..assignment = Code("r'$identifier'"),
), ),
]) ])
..methods.addAll([ ..methods.addAll([
@ -167,7 +167,7 @@ TypeResult resolveOfs(
Parameter( Parameter(
(final b) => b (final b) => b
..name = 'object' ..name = 'object'
..type = refer('${state.classPrefix}$identifier'), ..type = refer(identifier),
), ),
]) ])
..optionalParameters.add( ..optionalParameters.add(
@ -184,7 +184,7 @@ TypeResult resolveOfs(
Method((final b) { Method((final b) {
b b
..name = 'deserialize' ..name = 'deserialize'
..returns = refer('${state.classPrefix}$identifier') ..returns = refer(identifier)
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..requiredParameters.addAll([ ..requiredParameters.addAll([
Parameter( Parameter(
@ -209,7 +209,7 @@ TypeResult resolveOfs(
) )
..body = Code( ..body = Code(
<String>[ <String>[
'final result = new ${state.classPrefix}${identifier}Builder()', 'final result = new ${identifier}Builder()',
'..data = JsonObject(data);', '..data = JsonObject(data);',
if (schema.discriminator != null) ...[ if (schema.discriminator != null) ...[
'if (data is! Iterable) {', 'if (data is! Iterable) {',
@ -231,12 +231,11 @@ TypeResult resolveOfs(
], ],
for (final result in results) ...[ for (final result in results) ...[
if (schema.discriminator != null) ...[ if (schema.discriminator != null) ...[
"if (discriminator == '${result.name.replaceFirst(state.classPrefix, '')}'", "if (discriminator == '${result.name}'",
if (schema.discriminator!.mapping != null && schema.discriminator!.mapping!.isNotEmpty) ...[ if (schema.discriminator!.mapping != null && schema.discriminator!.mapping!.isNotEmpty) ...[
for (final key in schema.discriminator!.mapping!.entries for (final key in schema.discriminator!.mapping!.entries
.where( .where(
(final entry) => (final entry) => entry.value.endsWith('/${result.name}'),
entry.value.endsWith('/${result.name.replaceFirst(state.classPrefix, '')}'),
) )
.map((final entry) => entry.key)) ...[ .map((final entry) => entry.key)) ...[
" || discriminator == '$key'", " || discriminator == '$key'",

22
packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart

@ -15,11 +15,11 @@ TypeResult resolveEnum(
final TypeResult subResult, { final TypeResult subResult, {
final bool nullable = false, final bool nullable = false,
}) { }) {
if (state.resolvedTypes.add(TypeResultEnum('${state.classPrefix}$identifier', subResult))) { if (state.resolvedTypes.add(TypeResultEnum(identifier, subResult))) {
state.output.add( state.output.add(
Class( Class(
(final b) => b (final b) => b
..name = '${state.classPrefix}$identifier' ..name = identifier
..extend = refer('EnumClass') ..extend = refer('EnumClass')
..constructors.add( ..constructors.add(
Constructor( Constructor(
@ -50,9 +50,9 @@ TypeResult resolveEnum(
..name = toDartName(value) ..name = toDartName(value)
..static = true ..static = true
..modifier = FieldModifier.constant ..modifier = FieldModifier.constant
..type = refer('${state.classPrefix}$identifier') ..type = refer(identifier)
..assignment = Code( ..assignment = Code(
'_\$${toCamelCase('${state.classPrefix}$identifier')}${toDartName(value, uppercaseFirstCharacter: true)}', '_\$${toCamelCase(identifier)}${toDartName(value, uppercaseFirstCharacter: true)}',
); );
if (toDartName(value) != value) { if (toDartName(value) != value) {
@ -60,7 +60,7 @@ TypeResult resolveEnum(
throw Exception( throw Exception(
'Sorry enum values are a bit broken. ' 'Sorry enum values are a bit broken. '
'See https://github.com/google/json_serializable.dart/issues/616. ' '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( b.annotations.add(
@ -77,16 +77,16 @@ TypeResult resolveEnum(
Method( Method(
(final b) => b (final b) => b
..name = 'values' ..name = 'values'
..returns = refer('BuiltSet<${state.classPrefix}$identifier>') ..returns = refer('BuiltSet<$identifier>')
..lambda = true ..lambda = true
..static = true ..static = true
..body = Code('_\$${toCamelCase('${state.classPrefix}$identifier')}Values') ..body = Code('_\$${toCamelCase(identifier)}Values')
..type = MethodType.getter, ..type = MethodType.getter,
), ),
Method( Method(
(final b) => b (final b) => b
..name = 'valueOf' ..name = 'valueOf'
..returns = refer('${state.classPrefix}$identifier') ..returns = refer(identifier)
..lambda = true ..lambda = true
..static = true ..static = true
..requiredParameters.add( ..requiredParameters.add(
@ -96,15 +96,15 @@ TypeResult resolveEnum(
..type = refer(subResult.name), ..type = refer(subResult.name),
), ),
) )
..body = Code('_\$valueOf${state.classPrefix}$identifier(name)'), ..body = Code('_\$valueOf$identifier(name)'),
), ),
buildSerializer('${state.classPrefix}$identifier'), buildSerializer(identifier),
]), ]),
), ),
); );
} }
return TypeResultEnum( return TypeResultEnum(
'${state.classPrefix}$identifier', identifier,
subResult, subResult,
nullable: nullable, nullable: nullable,
); );

4
packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart

@ -14,11 +14,11 @@ TypeResultObject resolveInterface(
final openapi.Schema schema, final openapi.Schema schema,
) { ) {
final result = TypeResultObject( final result = TypeResultObject(
'${state.classPrefix}$identifier', identifier,
); );
if (state.resolvedInterfaces.add(result)) { if (state.resolvedInterfaces.add(result)) {
final className = '${state.classPrefix}$identifier$interfaceSuffix'; final className = '$identifier$interfaceSuffix';
state.output.add( state.output.add(
Class((final b) { Class((final b) {

2
packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart

@ -71,7 +71,7 @@ Iterable<String> resolveMimeTypeEncode(
mediaType.schema!, mediaType.schema!,
nullable: dartParameterNullable, nullable: dartParameterNullable,
); );
final parameterName = toDartName(result.name.replaceFirst(state.classPrefix, '')); final parameterName = toDartName(result.name);
switch (mimeType) { switch (mimeType) {
case 'application/json': case 'application/json':
case 'application/x-www-form-urlencoded': case 'application/x-www-form-urlencoded':

4
packages/dynamite/dynamite/lib/src/builder/resolve_object.dart

@ -17,7 +17,7 @@ TypeResultObject resolveObject(
final bool isHeader = false, final bool isHeader = false,
}) { }) {
final result = TypeResultObject( final result = TypeResultObject(
'${state.classPrefix}$identifier', identifier,
nullable: nullable, nullable: nullable,
); );
if (state.resolvedTypes.add(result)) { if (state.resolvedTypes.add(result)) {
@ -45,7 +45,7 @@ TypeResultObject resolveObject(
state.output.addAll([ state.output.addAll([
buildBuiltClass( buildBuiltClass(
'${state.classPrefix}$identifier', identifier,
defaults: defaults, defaults: defaults,
customSerializer: isHeader, customSerializer: isHeader,
interfaces: [ interfaces: [

8
packages/dynamite/dynamite/lib/src/builder/state.dart

@ -1,14 +1,8 @@
import 'package:code_builder/code_builder.dart'; import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/models/type_result.dart'; import 'package:dynamite/src/models/type_result.dart';
class State { class State {
State(final String prefix) State();
: classPrefix = toDartName(prefix, uppercaseFirstCharacter: true),
variablePrefix = toDartName(prefix);
final String classPrefix;
final String variablePrefix;
final output = <Spec>[]; final output = <Spec>[];
final resolvedTypes = <TypeResult>{}; final resolvedTypes = <TypeResult>{};

2
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'); throw Exception('Only OpenAPI 3.0.0 and later are supported');
} }
final state = State(spec.info.title); final state = State();
final output = ListBuilder<Spec>() final output = ListBuilder<Spec>()
..addAll(generateImports(outputId)) ..addAll(generateImports(outputId))

Loading…
Cancel
Save