Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/idl_gen_dart.cpp
Original file line number Diff line number Diff line change
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
Contributor 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
Contributor 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