-
Notifications
You must be signed in to change notification settings - Fork 127
Fix issue with generation of 'null' value enum fields #1470
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
Conversation
…yzer to compute the values instead
AppVeyor builds are pub errors and I don't have access to retry them. @devoncarew? |
lib/src/model.dart
Outdated
newModelElement = new EnumField(e, library, getter, setter); | ||
if (e.enclosingElement.isEnum) { | ||
int index = | ||
e.computeConstantValue()?.getField('index')?.toIntValue(); |
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.
This definitely works but it took a bit of thinking before I understood why. Essentially what's happening here is that the fields in an enum class E
are all static fields having type E
, except for the two "special" fields index
(a non-static field having type int
) and values
(a static field having type List<E>
). You only want your new behavior for the non-special fields, and it works because only the type E
has a non-static member index
whose value is an int
; the types int
and List<E>
don't.
If I'm understanding the code correctly there's a getter FieldElement.isEnumConstant
that does what you want. So I think you could replace lines 2140-2161 with something like:
if (e.isEnumConstant) {
int index = e.computeConstantValue().getField('index').toIntValue();
newModelElement = new EnumField.forConstant(index, e, library, getter);
} else if (e.enclosingElement.isEnum) {
newModelElement = new EnumField(e, library, getter, setter);
} else {
newModelElement = new Field(e, library, getter, setter);
}
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.
Oh, that's so much better. This logic is what I intended: if I had seen/thought to look for e.isEnumConstant I would have written it exactly like this. Thanks!
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 made a comment about how enum values are computed. I am not too familiar with DartDoc so I'll leave the full review to Keerti.
@@ -458,7 +458,6 @@ class Class extends ModelElement implements EnclosedElement { | |||
..addAll(constructors) | |||
..addAll(staticMethods) | |||
..addAll(staticProperties) | |||
..addAll(allInstanceMethods) |
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 was this removed?
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.
It was a duplicate, it was already addAll on line 454.
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.
that line is not shown in the review :)
Fixes #1445. Adding Paul to the reviewers as a comment in ConstFieldElementImpl suggested he might have context as to whether the change to how we compute enum indexes is actually an improvement over some variant of the original hack.