Browse Source

refactor(dynamite): unify code generation

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

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

@ -79,20 +79,20 @@ List<Class> generateDynamiteOverrides(final State state) => [
..type = refer('HttpClientResponse'), ..type = refer('HttpClientResponse'),
), ),
) )
..body = Block.of([ ..body = Code('''
const Code('String body;'), String body;
const Code('try {'), try {
const Code('body = await response.body;'), body = await response.body;
const Code('} on FormatException {'), } on FormatException {
const Code("body = 'binary';"), body = 'binary';
const Code('}'), }
const Code(''),
Code('return ${state.classPrefix}ApiException('), return ${state.classPrefix}ApiException(
const Code('response.statusCode,'), response.statusCode,
const Code('response.responseHeaders,'), response.responseHeaders,
const Code('body,'), body,
const Code(');'), );
]), '''),
), ),
Method( Method(
(final b) => b (final b) => b

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

@ -85,26 +85,26 @@ Spec buildHeaderSerializer(final State state, final String identifier, final Ope
..defaultTo = const Code('FullType.unspecified'), ..defaultTo = const Code('FullType.unspecified'),
), ),
) )
..body = Block.of([ ..body = Code('''
Code('final result = new ${state.classPrefix}${identifier}Builder();'), final result = new ${state.classPrefix}${identifier}Builder();
const Code(''),
const Code('final iterator = serialized.iterator;'), final iterator = serialized.iterator;
const Code('while (iterator.moveNext()) {'), while (iterator.moveNext()) {
const Code('final key = iterator.current! as String;'), final key = iterator.current! as String;
const Code('iterator.moveNext();'), iterator.moveNext();
const Code('final value = iterator.current! as String;'), final value = iterator.current! as String;
const Code('switch (key) {'), switch (key) {
...deserializeProperty(state, identifier, spec, schema), ${deserializeProperty(state, identifier, spec, schema).join('\n')}
const Code('}'), }
const Code('}'), }
const Code(''),
const Code('return result.build();'), return result.build();
]); ''');
}), }),
]), ]),
); );
Iterable<Code> deserializeProperty( Iterable<String> deserializeProperty(
final State state, final State state,
final String identifier, final String identifier,
final OpenAPI spec, final OpenAPI spec,
@ -121,15 +121,15 @@ Iterable<Code> deserializeProperty(
nullable: isDartParameterNullable(schema.required?.contains(propertyName), propertySchema), nullable: isDartParameterNullable(schema.required?.contains(propertyName), propertySchema),
); );
yield Code("case '$propertyName':"); yield "case '$propertyName':";
if (result.className != 'String') { if (result.className != 'String') {
if (result is TypeResultBase || result is TypeResultEnum) { if (result is TypeResultBase || result is TypeResultEnum) {
yield Code('result.${toDartName(propertyName)} = ${result.deserialize(result.decode('value!'))};'); yield 'result.${toDartName(propertyName)} = ${result.deserialize(result.decode('value!'))};';
} else { } else {
yield Code('result.${toDartName(propertyName)}.replace(${result.deserialize(result.decode('value!'))});'); yield 'result.${toDartName(propertyName)}.replace(${result.deserialize(result.decode('value!'))});';
} }
} else { } else {
yield Code('result.${toDartName(propertyName)} = value!;'); yield 'result.${toDartName(propertyName)} = value!;';
} }
} }
} }

Loading…
Cancel
Save