-
Notifications
You must be signed in to change notification settings - Fork 401
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
Add JsonKey.includeFromJson/includeToJson - allow explicit control of serialization #1256
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,12 @@ import 'package:analyzer/dart/element/element.dart'; | |
import 'package:build/build.dart'; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
import '../type_helper.dart'; | ||
import 'decode_helper.dart'; | ||
import 'encoder_helper.dart'; | ||
import 'field_helpers.dart'; | ||
import 'helper_core.dart'; | ||
import 'settings.dart'; | ||
import 'type_helper.dart'; | ||
import 'utils.dart'; | ||
|
||
class GeneratorHelper extends HelperCore with EncodeHelper, DecodeHelper { | ||
|
@@ -61,16 +61,17 @@ class GeneratorHelper extends HelperCore with EncodeHelper, DecodeHelper { | |
final accessibleFields = sortedFields.fold<Map<String, FieldElement>>( | ||
<String, FieldElement>{}, | ||
(map, field) { | ||
if (!field.isPublic) { | ||
final jsonKey = jsonKeyFor(field); | ||
if (!field.isPublic && !jsonKey.explicitYesFromJson) { | ||
unavailableReasons[field.name] = 'It is assigned to a private field.'; | ||
} else if (field.getter == null) { | ||
assert(field.setter != null); | ||
unavailableReasons[field.name] = | ||
'Setter-only properties are not supported.'; | ||
log.warning('Setters are ignored: ${element.name}.${field.name}'); | ||
} else if (jsonKeyFor(field).ignore) { | ||
} else if (jsonKey.explicitNoFromJson) { | ||
unavailableReasons[field.name] = | ||
'It is assigned to an ignored field.'; | ||
'It is assigned to a field not meant to be used in fromJson.'; | ||
} else { | ||
assert(!map.containsKey(field.name)); | ||
map[field.name] = field; | ||
|
@@ -85,28 +86,47 @@ class GeneratorHelper extends HelperCore with EncodeHelper, DecodeHelper { | |
final createResult = createFactory(accessibleFields, unavailableReasons); | ||
yield createResult.output; | ||
|
||
accessibleFieldSet = accessibleFields.entries | ||
final fieldsToUse = accessibleFields.entries | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs to be something sortable... |
||
.where((e) => createResult.usedFields.contains(e.key)) | ||
.map((e) => e.value) | ||
.toSet(); | ||
.toList(); | ||
|
||
// Need to add candidates BACK even if they are not used in the factory if | ||
// they are forced to be used for toJSON | ||
for (var candidate in sortedFields.where((element) => | ||
jsonKeyFor(element).explicitYesToJson && | ||
!fieldsToUse.contains(element))) { | ||
fieldsToUse.add(candidate); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add candidates BACK even if they are not used in the factory if they are forced to be used for toJSON There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make these comments actual comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! |
||
} | ||
|
||
// Need the fields to maintain the original source ordering | ||
fieldsToUse.sort( | ||
(a, b) => sortedFields.indexOf(a).compareTo(sortedFields.indexOf(b))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need the fields to maintain the original source ordering |
||
|
||
accessibleFieldSet = fieldsToUse.toSet(); | ||
} | ||
|
||
// Check for duplicate JSON keys due to colliding annotations. | ||
// We do this now, since we have a final field list after any pruning done | ||
// by `_writeCtor`. | ||
accessibleFieldSet.fold( | ||
<String>{}, | ||
(Set<String> set, fe) { | ||
final jsonKey = nameAccess(fe); | ||
if (!set.add(jsonKey)) { | ||
throw InvalidGenerationSourceError( | ||
'More than one field has the JSON key for name "$jsonKey".', | ||
element: fe, | ||
); | ||
} | ||
return set; | ||
}, | ||
); | ||
accessibleFieldSet | ||
..removeWhere( | ||
(element) => jsonKeyFor(element).explicitNoToJson, | ||
) | ||
|
||
// Check for duplicate JSON keys due to colliding annotations. | ||
// We do this now, since we have a final field list after any pruning done | ||
// by `_writeCtor`. | ||
..fold( | ||
<String>{}, | ||
(Set<String> set, fe) { | ||
final jsonKey = nameAccess(fe); | ||
if (!set.add(jsonKey)) { | ||
throw InvalidGenerationSourceError( | ||
'More than one field has the JSON key for name "$jsonKey".', | ||
element: fe, | ||
); | ||
} | ||
return set; | ||
}, | ||
); | ||
|
||
if (config.createFieldMap) { | ||
yield createFieldMap(accessibleFieldSet); | ||
|
@@ -123,3 +143,13 @@ class GeneratorHelper extends HelperCore with EncodeHelper, DecodeHelper { | |
yield* _addedMembers; | ||
} | ||
} | ||
|
||
extension on KeyConfig { | ||
bool get explicitYesFromJson => includeFromJson == true; | ||
|
||
bool get explicitNoFromJson => includeFromJson == false; | ||
|
||
bool get explicitYesToJson => includeToJson == true; | ||
|
||
bool get explicitNoToJson => includeFromJson == false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! Great find! #1281 |
||
} |
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.
Just a small implementation tweak...