mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
In Javascript, generate bidirectional mappings for enums, between Name: Value and between Value: Name. (#4960)
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
925c1d77fc
commit
d840856093
@@ -355,9 +355,22 @@ class JsGenerator : public BaseGenerator {
|
||||
if (it != enum_def.vals.vec.begin()) { code += '\n'; }
|
||||
GenDocComment(ev.doc_comment, code_ptr, "", " ");
|
||||
}
|
||||
|
||||
// Generate mapping between EnumName: EnumValue(int)
|
||||
code += " " + ev.name;
|
||||
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
|
||||
code += NumToString(ev.value);
|
||||
|
||||
if (lang_.language == IDLOptions::kJs) {
|
||||
// In pure Javascript, generate mapping between EnumValue(int):
|
||||
// 'EnumName' so enums can be looked up by their ID.
|
||||
code += ", ";
|
||||
|
||||
code += NumToString(ev.value);
|
||||
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
|
||||
code += "'" + ev.name + "'";
|
||||
}
|
||||
|
||||
code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n";
|
||||
|
||||
if (ev.union_type.struct_def) {
|
||||
|
||||
@@ -28,19 +28,19 @@ MyGame.OtherNameSpace = MyGame.OtherNameSpace || {};
|
||||
* @enum
|
||||
*/
|
||||
MyGame.Example.Color = {
|
||||
Red: 1,
|
||||
Green: 2,
|
||||
Blue: 8
|
||||
Red: 1, 1: 'Red',
|
||||
Green: 2, 2: 'Green',
|
||||
Blue: 8, 8: 'Blue'
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
*/
|
||||
MyGame.Example.Any = {
|
||||
NONE: 0,
|
||||
Monster: 1,
|
||||
TestSimpleTableWithEnum: 2,
|
||||
MyGame_Example2_Monster: 3
|
||||
NONE: 0, 0: 'NONE',
|
||||
Monster: 1, 1: 'Monster',
|
||||
TestSimpleTableWithEnum: 2, 2: 'TestSimpleTableWithEnum',
|
||||
MyGame_Example2_Monster: 3, 3: 'MyGame_Example2_Monster'
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,9 +16,9 @@ NamespaceA.NamespaceB = NamespaceA.NamespaceB || {};
|
||||
* @enum
|
||||
*/
|
||||
NamespaceA.NamespaceB.EnumInNestedNS = {
|
||||
A: 0,
|
||||
B: 1,
|
||||
C: 2
|
||||
A: 0, 0: 'A',
|
||||
B: 1, 1: 'B',
|
||||
C: 2, 2: 'C'
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
* @enum
|
||||
*/
|
||||
var Character = {
|
||||
NONE: 0,
|
||||
MuLan: 1,
|
||||
Rapunzel: 2,
|
||||
Belle: 3,
|
||||
BookFan: 4,
|
||||
Other: 5,
|
||||
Unused: 6
|
||||
NONE: 0, 0: 'NONE',
|
||||
MuLan: 1, 1: 'MuLan',
|
||||
Rapunzel: 2, 2: 'Rapunzel',
|
||||
Belle: 3, 3: 'Belle',
|
||||
BookFan: 4, 4: 'BookFan',
|
||||
Other: 5, 5: 'Other',
|
||||
Unused: 6, 6: 'Unused'
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user