Browse Source

dynamite: migrate enum to built_value

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/194/head
Nikolas Rimikis 2 years ago
parent
commit
6a94a9c21d
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 95
      packages/dynamite/lib/src/openapi_builder.dart
  2. 4
      packages/dynamite/lib/src/type_result/enum.dart

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

@ -63,8 +63,9 @@ class OpenAPIBuilder implements Builder {
"import 'dart:convert';", "import 'dart:convert';",
"import 'dart:typed_data';", "import 'dart:typed_data';",
'', '',
"import 'package:built_collection/built_collection.dart';",
"import 'package:built_value/built_value.dart';",
"import 'package:cookie_jar/cookie_jar.dart';", "import 'package:cookie_jar/cookie_jar.dart';",
"import 'package:json_annotation/json_annotation.dart';",
"import 'package:universal_io/io.dart';", "import 'package:universal_io/io.dart';",
'', '',
"export 'package:cookie_jar/cookie_jar.dart';", "export 'package:cookie_jar/cookie_jar.dart';",
@ -1213,6 +1214,24 @@ String _valueToEscapedValue(final TypeResult result, final dynamic value) {
return value.toString(); return value.toString();
} }
String _toCamelCase(final String name) {
var result = '';
var upperCase = false;
var firstCharacter = true;
for (final char in name.split('')) {
if (char == '_') {
upperCase = true;
} else if (char == r'$') {
result += r'$';
} else {
result += firstCharacter ? char.toLowerCase() : (upperCase ? char.toUpperCase() : char);
upperCase = false;
firstCharacter = false;
}
}
return result;
}
List<String> _descriptionToDocs(final String? description) => [ List<String> _descriptionToDocs(final String? description) => [
if (description != null && description.isNotEmpty) ...[ if (description != null && description.isNotEmpty) ...[
for (final line in description.split('\n')) ...[ for (final line in description.split('\n')) ...[
@ -1808,33 +1827,27 @@ TypeResult resolveType(
if (!state.resolvedTypes.contains('${state.prefix}$identifier')) { if (!state.resolvedTypes.contains('${state.prefix}$identifier')) {
state.resolvedTypes.add('${state.prefix}$identifier'); state.resolvedTypes.add('${state.prefix}$identifier');
state.output.add( state.output.add(
Enum( Class(
(final b) => b (final b) => b
..name = '${state.prefix}$identifier' ..name = '${state.prefix}$identifier'
..extend = refer('EnumClass')
..constructors.add( ..constructors.add(
Constructor( Constructor(
(final b) => b (final b) => b
..name = '_'
..constant = true ..constant = true
..requiredParameters.add( ..requiredParameters.add(
Parameter( Parameter(
(final b) => b (final b) => b
..name = 'value' ..name = 'name'
..toThis = true, ..toSuper = true,
), ),
), ),
), ),
) )
..fields.add( ..fields.addAll(
Field(
(final b) => b
..name = 'value'
..type = refer(result!.name)
..modifier = FieldModifier.final$,
),
)
..values.addAll(
schema.enum_!.map( schema.enum_!.map(
(final value) => EnumValue( (final value) => Field(
(final b) { (final b) {
final result = resolveType( final result = resolveType(
spec, spec,
@ -1846,9 +1859,13 @@ TypeResult resolveType(
); );
b b
..name = _toDartName(value.toString()) ..name = _toDartName(value.toString())
..arguments.add( ..static = true
refer(_valueToEscapedValue(result, value)), ..modifier = FieldModifier.constant
..type = refer('${state.prefix}$identifier')
..assignment = Code(
'_\$${_toCamelCase('${state.prefix}$identifier')}${_toDartName(value.toString(), uppercaseFirstCharacter: true)}',
); );
if (_toDartName(value.toString()) != value.toString()) { if (_toDartName(value.toString()) != value.toString()) {
if (result.name != 'String' && result.name != 'int') { if (result.name != 'String' && result.name != 'int') {
throw Exception( throw Exception(
@ -1858,42 +1875,50 @@ TypeResult resolveType(
); );
} }
b.annotations.add( b.annotations.add(
refer('JsonValue').call([ refer('BuiltValueEnumConst').call([], {
refer(_valueToEscapedValue(result, value.toString())), 'wireName': refer(_valueToEscapedValue(result, value.toString())),
]), }),
); );
} }
}, },
), ),
), ),
) )
..methods.add( ..methods.addAll([
Method( Method(
(final b) => b (final b) => b
..name = 'fromValue' ..name = 'values'
..returns = refer('BuiltSet<${state.prefix}$identifier>')
..lambda = true
..static = true ..static = true
..body = Code('_\$${_toCamelCase('${state.prefix}$identifier')}Values')
..type = MethodType.getter,
),
Method(
(final b) => b
..name = 'valueOf'
..returns = refer('${state.prefix}$identifier') ..returns = refer('${state.prefix}$identifier')
..lambda = true
..static = true
..requiredParameters.add( ..requiredParameters.add(
Parameter( Parameter(
(final b) => b (final b) => b
..name = 'value' ..name = 'name'
..type = refer(result!.name), ..type = refer(result!.name),
), ),
) )
..body = Code( ..body = Code('_\$valueOf${state.prefix}$identifier(name)'),
[
'switch (value) {',
for (final value in schema.enum_!) ...[
'case ${_valueToEscapedValue(result!, value)}:',
'return ${state.prefix}$identifier.${_toDartName(value.toString())};',
],
'default:',
'throw Exception(\'Can not parse ${state.prefix}$identifier from "\$value"\');',
'}',
].join(),
),
), ),
), Method(
(final b) => b
..name = 'serializer'
..returns = refer('Serializer<${state.prefix}$identifier>')
..lambda = true
..static = true
..body = Code("_\$${_toCamelCase('${state.prefix}$identifier')}Serializer")
..type = MethodType.getter,
),
]),
), ),
); );
} }

4
packages/dynamite/lib/src/type_result/enum.dart

@ -9,7 +9,7 @@ class TypeResultEnum extends TypeResult {
final TypeResult subType; final TypeResult subType;
@override @override
String serialize(final String object) => '$object.value'; String serialize(final String object) => '$object.name';
@override @override
String encode( String encode(
@ -20,7 +20,7 @@ class TypeResultEnum extends TypeResult {
subType.encode(object); subType.encode(object);
@override @override
String deserialize(final String object) => '$name.fromValue($object as ${subType.name})'; String deserialize(final String object) => '$name.valueOf($object as ${subType.name})';
@override @override
String decode(final String object) => subType.decode(object); String decode(final String object) => subType.decode(object);

Loading…
Cancel
Save