Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"toJson" for unions should include "unionJsonKey" in generated code #380

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ class GetPostsResponseError extends GetPostsResponse {

@override
Map<String, dynamic> toJson() {
return <String, dynamic>{};
return <String, dynamic>{
'code': 'error',
};
}

@override
Expand Down Expand Up @@ -209,6 +211,7 @@ class GetPostsResponseOk extends GetPostsResponse {
@override
Map<String, dynamic> toJson() {
return <String, dynamic>{
'code': 'ok',
'posts': <dynamic>[
for (final Post i0 in posts) i0.toJson(),
],
Expand Down
28 changes: 0 additions & 28 deletions package/lib/src/annotations/union.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Union {
this.unionJsonKey,
this.unionFallbackJsonValue,
this.unmodifiableCollections,
this.toJsonNamedKeyToFactoryName,
this.when,
});

Expand Down Expand Up @@ -67,33 +66,6 @@ class Union {

final bool? unmodifiableCollections;

/// Wraps the union fields into a new object with a key named after the factory
///
/// ```
/// @Union()
/// class Result{
/// factory Result.data(int value) = ResultData;
///
/// Map<String, dynamic> toJson();
/// }
/// ...
///
/// // toJson call this produce
///
/// {
/// "data": {
/// "value": value
/// }
/// }
///
/// instead of
///
/// {
/// "value": value
/// }
/// ```
final String? toJsonNamedKeyToFactoryName;

final bool? when;
}

Expand Down
8 changes: 3 additions & 5 deletions package/lib/src/backend/code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class DataClassPluginGenerator extends TachyonPluginCodeGenerator {
pluginOptions.dataClass.effectiveToJson(targetFileRelativePath)) {
await ToJsonGenerator(
codeWriter: codeWriter,
constructorName: '',
fields: fields,
jsonKeyNameConventionGetter: jsonKeyNameConventionGetter,
classDeclarationFinder: declarationFinder.findClassOrEnum,
Expand Down Expand Up @@ -389,15 +390,12 @@ class DataClassPluginGenerator extends TachyonPluginCodeGenerator {

if (unionAnnotationValueExtractor.getBool('toJson') ??
pluginOptions.union.effectiveToJson(targetFileRelativePath)) {
final String? toJsonNamedKeyToFactoryName =
unionAnnotationValueExtractor.getString('toJsonNamedKeyToFactoryName');
await ToJsonGenerator(
codeWriter: codeWriter,
fields: fields,
constructorName: ctor.name!.lexeme,
jsonKeyNameConventionGetter: jsonKeyNameConventionGetter,
namedKeyToFactoryEntry: toJsonNamedKeyToFactoryName != null
? "'$toJsonNamedKeyToFactoryName': '${ctor.name!.lexeme}'"
: null,
toJsonUnionKey: unionAnnotationValueExtractor.getString('unionJsonKey'),
classDeclarationFinder: declarationFinder.findClassOrEnum,
logger: logger,
).execute();
Expand Down
16 changes: 11 additions & 5 deletions package/lib/src/backend/core/generators/to_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ import 'package:tachyon/tachyon.dart';
class ToJsonGenerator implements Generator {
ToJsonGenerator({
required final CodeWriter codeWriter,
required final String constructorName,
required final List<DeclarationInfo> fields,
required final JsonKeyNameConventionGetter jsonKeyNameConventionGetter,
required final ClassOrEnumDeclarationFinder classDeclarationFinder,
final String? namedKeyToFactoryEntry,
final String? toJsonUnionKey,
required final Logger logger,
}) : _codeWriter = codeWriter,
_constructorName = constructorName,
_fields = fields,
_jsonKeyNameConventionGetter = jsonKeyNameConventionGetter,
_classDeclarationFinder = classDeclarationFinder,
_namedKeyToFactoryEntry = namedKeyToFactoryEntry,
_toJsonUnionKey = toJsonUnionKey,
_logger = logger;

final CodeWriter _codeWriter;
final String _constructorName;
final List<DeclarationInfo> _fields;
final JsonKeyNameConventionGetter _jsonKeyNameConventionGetter;
final ClassOrEnumDeclarationFinder _classDeclarationFinder;
final String? _namedKeyToFactoryEntry;
final String? _toJsonUnionKey;
final Logger _logger;

@override
Expand All @@ -34,8 +37,11 @@ class ToJsonGenerator implements Generator {
..writeln('Map<String, dynamic> toJson() {')
..writeln('return <String, dynamic>{');

if (_namedKeyToFactoryEntry != null) {
_codeWriter.write('$_namedKeyToFactoryEntry,');
if (_toJsonUnionKey != null &&
!_fields.any((DeclarationInfo field) => field.name == _toJsonUnionKey)) {
_codeWriter
..write("'$_toJsonUnionKey': ")
..write("'${_jsonKeyNameConventionGetter(null).transform(_constructorName)}',");
}

for (final DeclarationInfo field in _fields) {
Expand Down
Loading