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. 167
      packages/dynamite/lib/src/openapi_builder.dart

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

@ -1272,106 +1272,72 @@ 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 ..name = '_'
..optionalParameters.addAll( ..constant = true,
sortedParameterKeys.map( ),
(final propertyName) => Parameter( Constructor(
(final b) { (final b) => b
final propertySchema = schema.properties![propertyName]!; ..factory = true
b ..lambda = true
..name = _toDartName(propertyName) ..optionalParameters.add(
..toThis = true Parameter(
..named = true (final b) => b
..required = _isDartParameterRequired( ..name = 'b'
schema.required?.contains(propertyName), ..type = refer('void Function(${state.prefix}${identifier}Builder)?'),
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( ..redirect = refer('_\$${state.prefix}$identifier'),
(final b) => b ),
..factory = true ])
..name = 'fromJson'
..lambda = true
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'json'
..type = refer('Map<String, dynamic>'),
),
)
..body = Code('_\$${state.prefix}${identifier}FromJson(json)'),
),
Constructor(
(final b) => b
..factory = true
..name = 'fromJsonString'
..lambda = true
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'data'
..type = refer('String'),
),
)
..body = Code('${state.prefix}$identifier.fromJson(json.decode(data) as Map<String, dynamic>)'),
),
],
)
..methods.addAll([ ..methods.addAll([
Method(
(final b) => b
..static = true
..name = 'fromJson'
..lambda = true
..returns = refer('${state.prefix}$identifier')
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'json'
..type = refer('Map<String, dynamic>'),
),
)
..body = const Code('serializers.deserializeWith(serializer, json)!'),
),
Method(
(final b) => b
..static = true
..name = 'fromJsonString'
..lambda = true
..returns = refer('${state.prefix}$identifier')
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'data'
..type = refer('String'),
),
)
..body = const Code('serializers.fromJson(serializer, data)!'),
),
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