Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
Explain why generated code treats Map fields differently. (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan authored Oct 11, 2024
1 parent 6de2869 commit 23d034c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tool/dart_model_generator/lib/generate_dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,21 @@ class ClassTypeDefinition implements Definition {
}
};

// `Map` fields are handled differently, see `generateCode` comment.
List<Property> get propertiesExceptMap =>
properties.where((p) => !p.type.isMap).toList();

/// Generates a "class".
///
/// It's an extension type over `Map<String, Object?>`, which is the
/// JSON representation. The extension type constructor is called `fromJson`.
///
/// An unnamed constructor is generated that accepts optional named
/// parameters for most fields.
///
/// `Map` fields are handled differently: they are instantiated as empty
/// mutable maps that must be populated afterwards. This is to prevent the
/// creation of temporary maps that would have to be copied.
@override
String generateCode(GenerationContext context) {
final result = StringBuffer();
Expand All @@ -481,7 +493,6 @@ class ClassTypeDefinition implements Definition {
result.writeln(' $name() : ');
} else {
result.writeln(' $name({');
// TODO: Why are we excluding Map properties?
for (final property in propertiesExceptMap) {
result.writeln(property.parameterCode);
}
Expand Down

0 comments on commit 23d034c

Please sign in to comment.