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) {
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)'),
),
);
}

14
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<Type>')
..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()) {

31
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 = <String, String>{};
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<Type>')
..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(
<String>[
'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'",

22
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,
);

4
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) {

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

@ -71,7 +71,7 @@ Iterable<String> 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':

4
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: [

8
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 = <Spec>[];
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');
}
final state = State(spec.info.title);
final state = State();
final output = ListBuilder<Spec>()
..addAll(generateImports(outputId))

Loading…
Cancel
Save