Skip to content
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

[dart] fix toString() method generated for enum value #8260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/idl_gen_dart.cpp
Expand Up @@ -281,7 +281,15 @@ class DartGenerator : public BaseGenerator {
enum_type + "Reader();\n\n";
code += " @override\n";
code += " String toString() {\n";
code += " return '" + enum_type + "{value: $value}';\n";
code += " switch (value) {\n";
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
const auto enum_var = namer_.Variant(ev);
code += " case " + enum_def.ToString(ev) + ": return \"" + enum_var +
"\";\n";
}
code += " default: return \"\";\n";
code += " }\n";
Comment on lines +284 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed format loses the information about the containing enum. What's the reasoning behind that proposal?

Also, if we should change this, i'd suggest aligning with dart Enum toString() instead:

enum Color { red, green, blue }

main() {
  test('test', () {
    expect(Color.red.toString(), 'Color.red');
  });
}

Additionally, that would seem like a good time to switch to actual enums. Do you think we'd be able to do all that's needed to construct in the scope of flatbuffers if we use enhanced enum syntax?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I've looked into enhanced enums and it works fine, even with the default toString. I'll prep a PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaind It seems that there will also be issues with #5467

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current code already handles that and throws in such a case: throw StateError('Invalid value $value for bit flag enum Abc');. Whether that's the best thing to do is a different topic.

the behavior remains unchanged after the change to enhanced enums (#8313)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See what others think. If the new PR is merged, I will close this PR

code += " }\n";
code += "}\n\n";

Expand Down