Skip to content

Commit

Permalink
Fix code generation for "unionJsonKey" field
Browse files Browse the repository at this point in the history
- When "unionJsonKey" is declared as field
- The value assigned to generated field
  • Loading branch information
spideythewebhead committed Jun 29, 2024
1 parent 9bb1c4c commit a5a7484
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions package/lib/src/backend/code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,22 @@ class DataClassPluginGenerator extends TachyonPluginCodeGenerator {

final String? unionJsonKey = unionAnnotationValueExtractor.getString('unionJsonKey');
if (unionJsonKey != null) {
codeWriter
..write("final String code = '")
..write(jsonKeyNameConventionGetter(null).transform(ctor.name!.lexeme))
..writeln("';")
..writeln();
final bool isFieldAlreadyDeclared =
fields.any((DeclarationInfo field) => field.name == unionJsonKey);
if (!isFieldAlreadyDeclared) {
final String? unionJsonKeyValue = AnnotationValueExtractor(ctor.metadata
.getAllAnnotationsByType(AnnotationType.unionJsonKeyValue)
.firstOrNull)
.getPositionedArgument(0)
?.toSource();

codeWriter
..write('final String $unionJsonKey = ')
..write(unionJsonKeyValue ??
"'${jsonKeyNameConventionGetter(null).transform(ctor.name!.lexeme)}'")
..writeln(';')
..writeln();
}
}

if (unionAnnotationValueExtractor.getBool('fromJson') ??
Expand Down

0 comments on commit a5a7484

Please sign in to comment.