Browse Source

dynamite: migrate other classes to built_value

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

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

@ -1272,74 +1272,42 @@ TypeResult resolveObject(
state.output.add( state.output.add(
Class( Class(
(final b) { (final b) {
final sortedParameterKeys = schema.properties!.keys.toList()
..sort(
(final a, final b) => sortRequiredElements(
_isDartParameterRequired(
schema.required?.contains(a),
schema.properties![a]!.default_,
),
_isDartParameterRequired(
schema.required?.contains(b),
schema.properties![b]!.default_,
),
),
);
b b
..name = '${state.prefix}$identifier' ..name = '${state.prefix}$identifier'
..docs.addAll(_descriptionToDocs(schema.description)) ..docs.addAll(_descriptionToDocs(schema.description))
..annotations.add( ..abstract = true
refer('JsonSerializable').call( ..implements.add(
[], refer(
{ 'Built<${state.prefix}$identifier, ${state.prefix}${identifier}Builder>',
if (schema.additionalProperties != null) ...{
'disallowUnrecognizedKeys': refer('false'),
},
if (extraJsonSerializableValues != null) ...{
for (final key in extraJsonSerializableValues.keys) ...{
key: refer(extraJsonSerializableValues[key]!),
},
},
},
), ),
) )
..constructors.addAll( ..constructors.addAll([
[
Constructor( Constructor(
(final b) => b (final b) => b
..optionalParameters.addAll( ..name = '_'
sortedParameterKeys.map( ..constant = true,
(final propertyName) => Parameter(
(final b) {
final propertySchema = schema.properties![propertyName]!;
b
..name = _toDartName(propertyName)
..toThis = true
..named = true
..required = _isDartParameterRequired(
schema.required?.contains(propertyName),
propertySchema.default_,
);
if (propertySchema.default_ != null) {
final value = propertySchema.default_!.toString();
final result = resolveType(
spec,
state,
propertySchema.type!,
propertySchema,
);
b.defaultTo = Code(_valueToEscapedValue(result, value));
}
},
),
),
),
), ),
Constructor( Constructor(
(final b) => b (final b) => b
..factory = true ..factory = true
..lambda = true
..optionalParameters.add(
Parameter(
(final b) => b
..name = 'b'
..type = refer('void Function(${state.prefix}${identifier}Builder)?'),
),
)
..redirect = refer('_\$${state.prefix}$identifier'),
),
])
..methods.addAll([
Method(
(final b) => b
..static = true
..name = 'fromJson' ..name = 'fromJson'
..lambda = true ..lambda = true
..returns = refer('${state.prefix}$identifier')
..requiredParameters.add( ..requiredParameters.add(
Parameter( Parameter(
(final b) => b (final b) => b
@ -1347,13 +1315,14 @@ TypeResult resolveObject(
..type = refer('Map<String, dynamic>'), ..type = refer('Map<String, dynamic>'),
), ),
) )
..body = Code('_\$${state.prefix}${identifier}FromJson(json)'), ..body = const Code('serializers.deserializeWith(serializer, json)!'),
), ),
Constructor( Method(
(final b) => b (final b) => b
..factory = true ..static = true
..name = 'fromJsonString' ..name = 'fromJsonString'
..lambda = true ..lambda = true
..returns = refer('${state.prefix}$identifier')
..requiredParameters.add( ..requiredParameters.add(
Parameter( Parameter(
(final b) => b (final b) => b
@ -1361,17 +1330,14 @@ TypeResult resolveObject(
..type = refer('String'), ..type = refer('String'),
), ),
) )
..body = Code('${state.prefix}$identifier.fromJson(json.decode(data) as Map<String, dynamic>)'), ..body = const Code('serializers.fromJson(serializer, data)!'),
), ),
],
)
..methods.addAll([
Method( Method(
(final b) => b (final b) => b
..name = 'toJson' ..name = 'toJson'
..returns = refer('Map<String, dynamic>') ..returns = refer('Map<String, dynamic>')
..lambda = true ..lambda = true
..body = Code('_\$${state.prefix}${identifier}ToJson(this)'), ..body = const Code('serializers.serializeWith(serializer, this)! as Map<String, dynamic>'),
), ),
Method( Method(
(final b) => b (final b) => b
@ -1386,12 +1352,10 @@ TypeResult resolveObject(
..type = refer(_makeNullable('${state.prefix}$identifier', true)), ..type = refer(_makeNullable('${state.prefix}$identifier', true)),
), ),
) )
..body = const Code('data == null ? null : json.encode(data.toJson())'), ..body = const Code('data == null ? null : serializers.toJson(serializer, data)'),
), ),
])
..fields.addAll([
for (final propertyName in schema.properties!.keys) ...[ for (final propertyName in schema.properties!.keys) ...[
Field( Method(
(final b) { (final b) {
final propertySchema = schema.properties![propertyName]!; final propertySchema = schema.properties![propertyName]!;
final result = resolveType( final result = resolveType(
@ -1404,7 +1368,7 @@ TypeResult resolveObject(
b b
..name = _toDartName(propertyName) ..name = _toDartName(propertyName)
..type = refer( ..returns = refer(
_makeNullable( _makeNullable(
result.name, result.name,
_isDartParameterNullable( _isDartParameterNullable(
@ -1414,7 +1378,7 @@ TypeResult resolveObject(
), ),
), ),
) )
..modifier = FieldModifier.final$ ..type = MethodType.getter
..docs.addAll(_descriptionToDocs(propertySchema.description)); ..docs.addAll(_descriptionToDocs(propertySchema.description));
final hasDifferentName = _toDartName(propertyName) != propertyName; final hasDifferentName = _toDartName(propertyName) != propertyName;
final isContentString = propertySchema.isContentString; final isContentString = propertySchema.isContentString;
@ -1423,7 +1387,7 @@ TypeResult resolveObject(
final arguments = <String, Expression>{ final arguments = <String, Expression>{
if (hasDifferentName) ...{ if (hasDifferentName) ...{
'name': refer("'$propertyName'"), 'wireName': literalString(propertyName),
}, },
if (hasExtraJsonKeyValues) ...{ if (hasExtraJsonKeyValues) ...{
for (final key in extraJsonKeyValues[propertyName]!.keys) ...{ for (final key in extraJsonKeyValues[propertyName]!.keys) ...{
@ -1492,12 +1456,21 @@ TypeResult resolveObject(
if (arguments.isNotEmpty) { if (arguments.isNotEmpty) {
b.annotations.add( b.annotations.add(
refer('JsonKey').call([], arguments), refer('BuiltValueField').call([], arguments),
); );
} }
}, },
) ),
], ],
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,
),
]); ]);
}, },
), ),

Loading…
Cancel
Save