-
Notifications
You must be signed in to change notification settings - Fork 418
Better error unknown #16
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
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 |
---|---|---|
|
@@ -58,6 +58,17 @@ class JsonSerializableGenerator | |
// TODO: support overriding the field set with an annotation option | ||
var fieldsList = classElement.fields.where((e) => !e.isStatic).toList(); | ||
|
||
var undefinedFields = | ||
fieldsList.where((fe) => fe.type.isUndefined).toList(); | ||
if (undefinedFields.isNotEmpty) { | ||
var description = | ||
undefinedFields.map((fe) => "`${fe.displayName}`").join(', '); | ||
|
||
throw new InvalidGenerationSourceError( | ||
'At least one field has an invalid type: $description.', | ||
todo: 'Check names and imports.'); | ||
} | ||
|
||
// Sort these in the order in which they appear in the class | ||
// Sadly, `classElement.fields` puts properties after fields | ||
fieldsList.sort((a, b) => a.nameOffset.compareTo(b.nameOffset)); | ||
|
@@ -151,6 +162,19 @@ class JsonSerializableGenerator | |
fieldsToSet.remove(arg.name); | ||
} | ||
|
||
var undefinedArgs = [ctorArguments, ctorNamedArguments] | ||
.expand((l) => l) | ||
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. we need Iterable.followedBy :) 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. Indeed! |
||
.where((pe) => pe.type.isUndefined) | ||
.toList(); | ||
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. why toList()? 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. ditto |
||
if (undefinedArgs.isNotEmpty) { | ||
var description = | ||
undefinedArgs.map((fe) => "`${fe.displayName}`").join(', '); | ||
|
||
throw new InvalidGenerationSourceError( | ||
'At least one constructor argument has an invalid type: $description.', | ||
todo: 'Check names and imports.'); | ||
} | ||
|
||
// these are fields to skip – now to find them | ||
var finalFields = | ||
fieldsToSet.values.where((field) => field.isFinal).toSet(); | ||
|
@@ -296,15 +320,13 @@ class JsonSerializableGenerator | |
{ParameterElement ctorParam}) { | ||
name = _fieldToJsonMapKey(name, field); | ||
var result = "json['$name']"; | ||
return _writeAccessToJsonValue(result, field.type, ctorParam: ctorParam); | ||
|
||
var targetType = ctorParam?.type ?? field.type; | ||
return _writeAccessToJsonValue(result, targetType); | ||
} | ||
|
||
String _writeAccessToJsonValue(String varExpression, DartType searchType, | ||
{ParameterElement ctorParam, int depth: 0}) { | ||
if (ctorParam != null) { | ||
searchType = ctorParam.type as InterfaceType; | ||
} | ||
|
||
{int depth: 0}) { | ||
for (var helper in _typeHelpers) { | ||
if (helper.canDeserialize(searchType)) { | ||
return "$varExpression == null ? null : " | ||
|
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.
why do you need toList?
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.
I hate reiterating iterables – even trivially. Can have unintended perf and behavior affects.
So I just never do it.