-
Notifications
You must be signed in to change notification settings - Fork 92
[native_assets_cli] Split up syntax generator #2106
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
pkgs/json_syntax_generator/lib/src/generator/code_generation_helpers.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// Wraps the [input] in curly braces if it's not empty. | ||
| String wrapBracesIfNotEmpty(String input) => input.isEmpty ? input : '{$input}'; | ||
|
|
||
| /// Wraps the [input] in curly braces or adds a semicolon if it's empty. | ||
| String wrapInBracesOrSemicolon(String input) => | ||
| input.isEmpty ? ';' : '{ $input }'; | ||
60 changes: 60 additions & 0 deletions
60
pkgs/json_syntax_generator/lib/src/generator/enum_class_generator.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import '../model/class_info.dart'; | ||
|
|
||
| class EnumGenerator { | ||
| final EnumClassInfo classInfo; | ||
|
|
||
| EnumGenerator(this.classInfo); | ||
|
|
||
| String generate() { | ||
| final buffer = StringBuffer(); | ||
| final className = classInfo.name; | ||
| final enumValues = classInfo.enumValues; | ||
|
|
||
| final staticFinals = <String>[]; | ||
| for (final value in enumValues) { | ||
| final valueName = value.name; | ||
| final jsonValue = value.jsonValue; | ||
| staticFinals.add( | ||
| 'static const $valueName = $className._(\'$jsonValue\');', | ||
| ); | ||
| } | ||
|
|
||
| buffer.writeln(''' | ||
| class $className { | ||
| final String name; | ||
|
|
||
| const $className._(this.name); | ||
|
|
||
| ${staticFinals.join('\n\n')} | ||
|
|
||
| static const List<$className> values = [ | ||
| ${enumValues.map((e) => e.name).join(',')} | ||
| ]; | ||
|
|
||
| static final Map<String, $className> _byName = { | ||
| for (final value in values) value.name: value, | ||
| }; | ||
|
|
||
| $className.unknown(this.name) : assert(!_byName.keys.contains(name)); | ||
|
|
||
| factory $className.fromJson(String name) { | ||
| final knownValue = _byName[name]; | ||
| if(knownValue != null) { | ||
| return knownValue; | ||
| } | ||
| return $className.unknown(name); | ||
| } | ||
|
|
||
| bool get isKnown => _byName[name] != null; | ||
|
|
||
| @override | ||
| String toString() => name; | ||
| } | ||
| '''); | ||
| return buffer.toString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
pkgs/json_syntax_generator/lib/src/generator/normal_class_generator.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import '../model/class_info.dart'; | ||
| import 'code_generation_helpers.dart'; | ||
| import 'property_generator.dart'; | ||
|
|
||
| class ClassGenerator { | ||
| final NormalClassInfo classInfo; | ||
|
|
||
| ClassGenerator(this.classInfo); | ||
|
|
||
| String generate() { | ||
| final buffer = StringBuffer(); | ||
| final className = classInfo.name; | ||
| final superclass = classInfo.superclass; | ||
| final superclassName = superclass?.name; | ||
| final properties = classInfo.properties; | ||
| final identifyingSubtype = classInfo.taggedUnionKey; | ||
|
|
||
| final constructorParams = <String>[]; | ||
| final setupParams = <String>[]; | ||
| final constructorSetterCalls = <String>[]; | ||
| final accessors = <String>[]; | ||
| final superParams = <String>[]; | ||
|
|
||
| final propertyNames = | ||
| { | ||
| for (final property in properties) property.name, | ||
| if (superclass != null) | ||
| for (final property in superclass.properties) property.name, | ||
| }.toList() | ||
| ..sort(); | ||
| for (final propertyName in propertyNames) { | ||
| final superClassProperty = superclass?.getProperty(propertyName); | ||
| final thisClassProperty = classInfo.getProperty(propertyName); | ||
| final property = superClassProperty ?? thisClassProperty!; | ||
| if (superClassProperty != null) { | ||
| if (identifyingSubtype == null) { | ||
| constructorParams.add( | ||
| '${property.isRequired ? 'required ' : ''}super.${property.name}', | ||
| ); | ||
| } else { | ||
| superParams.add("type: '$identifyingSubtype'"); | ||
| } | ||
| } else { | ||
| final dartType = property.type; | ||
| constructorParams.add( | ||
| '${property.isRequired ? 'required ' : ''}$dartType ${property.name}', | ||
| ); | ||
| setupParams.add( | ||
| '${property.isRequired ? 'required' : ''} $dartType ${property.name}', | ||
| ); | ||
| if (property.setterPrivate) { | ||
| constructorSetterCalls.add('_${property.name} = ${property.name};'); | ||
| } else { | ||
| constructorSetterCalls.add( | ||
| 'this.${property.name} = ${property.name};', | ||
| ); | ||
| } | ||
| } | ||
| if (thisClassProperty != null) { | ||
| accessors.add(PropertyGenerator(thisClassProperty).generate()); | ||
| } | ||
| } | ||
|
|
||
| if (constructorSetterCalls.isNotEmpty) { | ||
| constructorSetterCalls.add('json.sortOnKey();'); | ||
| } | ||
|
|
||
| if (superclass != null) { | ||
| buffer.writeln(''' | ||
| class $className extends $superclassName { | ||
| $className.fromJson(super.json) : super.fromJson(); | ||
|
|
||
| $className(${wrapBracesIfNotEmpty(constructorParams.join(', '))}) | ||
| : super(${superParams.join(',')}) | ||
| ${wrapInBracesOrSemicolon(constructorSetterCalls.join('\n '))} | ||
| '''); | ||
| if (setupParams.isNotEmpty) { | ||
| buffer.writeln(''' | ||
| /// Setup all fields for [$className] that are not in | ||
| /// [$superclassName]. | ||
| void setup ( | ||
| ${wrapBracesIfNotEmpty(setupParams.join(','))} | ||
| ) { | ||
| ${constructorSetterCalls.join('\n')} | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| buffer.writeln(''' | ||
| ${accessors.join('\n')} | ||
|
|
||
| @override | ||
| String toString() => '$className(\$json)'; | ||
| } | ||
| '''); | ||
| } else { | ||
| buffer.writeln(''' | ||
| class $className { | ||
| final Map<String, Object?> json; | ||
|
|
||
| $className.fromJson(this.json); | ||
|
|
||
| $className(${wrapBracesIfNotEmpty(constructorParams.join(', '))}) : json = {} { | ||
| ${constructorSetterCalls.join('\n ')} | ||
| } | ||
|
|
||
| ${accessors.join('\n')} | ||
|
|
||
| @override | ||
| String toString() => '$className(\$json)'; | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| if (identifyingSubtype != null) { | ||
| buffer.writeln(''' | ||
| extension ${className}Extension on $superclassName { | ||
| bool get is$className => type == '$identifyingSubtype'; | ||
|
|
||
| $className get as$className => $className.fromJson(json); | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| return buffer.toString(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great minds think alike: https://github.com/dart-lang/native/blob/main/pkgs/jnigen/lib/src/bindings/dart_generator.dart#L61-L66
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha nice! 😄