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

Adjust the code generated for enum types to avoid conflicting with names not reserved by the Flatbuffers IDL #8285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 12 additions & 12 deletions dart/test/flat_buffers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class CheckOtherLangaugesData {
expect(mon.pos!.y, 2.0);
expect(mon.pos!.z, 3.0);
expect(mon.pos!.test1, 3.0);
expect(mon.pos!.test2.value, 2.0);
expect(mon.pos!.test2.$value, 2.0);
expect(mon.pos!.test3.a, 5);
expect(mon.pos!.test3.b, 6);
expect(mon.testType!.value, example.AnyTypeId.Monster.value);
expect(mon.testType!.$value, example.AnyTypeId.Monster.$value);
expect(mon.test is example.Monster, true);
final monster2 = mon.test as example.Monster;
expect(monster2.name, "Fred");
Expand Down Expand Up @@ -899,13 +899,13 @@ class TestPointReader extends TableReader<TestPointImpl> {
@reflectiveTest
class GeneratorTest {
void test_constantEnumValues() async {
expect(example.Color.values, same(example.Color.values));
expect(example.Race.values, same(example.Race.values));
expect(example.AnyTypeId.values, same(example.AnyTypeId.values));
expect(example.AnyUniqueAliasesTypeId.values,
same(example.AnyUniqueAliasesTypeId.values));
expect(example.AnyAmbiguousAliasesTypeId.values,
same(example.AnyAmbiguousAliasesTypeId.values));
expect(example.Color.$values, same(example.Color.$values));
expect(example.Race.$values, same(example.Race.$values));
expect(example.AnyTypeId.$values, same(example.AnyTypeId.$values));
expect(example.AnyUniqueAliasesTypeId.$values,
same(example.AnyUniqueAliasesTypeId.$values));
expect(example.AnyAmbiguousAliasesTypeId.$values,
same(example.AnyAmbiguousAliasesTypeId.$values));
}
}

Expand All @@ -920,9 +920,9 @@ class ListOfEnumsTest {
]);
var bytes = mytable.toBytes();
var mytable_read = example3.MyTable(bytes);
expect(mytable_read.options![0].value, example3.OptionsEnum.A.value);
expect(mytable_read.options![1].value, example3.OptionsEnum.B.value);
expect(mytable_read.options![2].value, example3.OptionsEnum.C.value);
expect(mytable_read.options![0].$value, example3.OptionsEnum.A.$value);
expect(mytable_read.options![1].$value, example3.OptionsEnum.B.$value);
expect(mytable_read.options![2].$value, example3.OptionsEnum.C.$value);
}
}

Expand Down
92 changes: 46 additions & 46 deletions dart/test/keyword_test_keyword_test_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import 'package:flat_buffers/flat_buffers.dart' as fb;


class Abc {
final int value;
const Abc._(this.value);
final int $value;
const Abc._(this.$value);

factory Abc.fromValue(int value) {
final result = values[value];
factory Abc.fromValue(int $value) {
final result = $values[$value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Abc');
throw StateError('Invalid value ${$value} for bit flag enum Abc');
}
return result;
}

static Abc? _createOrNull(int? value) =>
value == null ? null : Abc.fromValue(value);
static Abc? _createOrNull(int? $value) =>
$value == null ? null : Abc.fromValue($value);

static const int minValue = 0;
static const int maxValue = 2;
static bool containsValue(int value) => values.containsKey(value);
static const int $minValue = 0;
static const int $maxValue = 2;
static bool containsValue(int $value) => $values.containsKey($value);

static const Abc $void = Abc._(0);
static const Abc where = Abc._(1);
static const Abc stackalloc = Abc._(2);
static const Map<int, Abc> values = {
static const Map<int, Abc> $values = {
0: $void,
1: where,
2: stackalloc};
Expand All @@ -38,7 +38,7 @@ class Abc {

@override
String toString() {
return 'Abc{value: $value}';
return 'Abc{value: ${$value}}';
}
}

Expand All @@ -54,33 +54,33 @@ class _AbcReader extends fb.Reader<Abc> {
}

class Public {
final int value;
const Public._(this.value);
final int $value;
const Public._(this.$value);

factory Public.fromValue(int value) {
final result = values[value];
factory Public.fromValue(int $value) {
final result = $values[$value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Public');
throw StateError('Invalid value ${$value} for bit flag enum Public');
}
return result;
}

static Public? _createOrNull(int? value) =>
value == null ? null : Public.fromValue(value);
static Public? _createOrNull(int? $value) =>
$value == null ? null : Public.fromValue($value);

static const int minValue = 0;
static const int maxValue = 0;
static bool containsValue(int value) => values.containsKey(value);
static const int $minValue = 0;
static const int $maxValue = 0;
static bool containsValue(int $value) => $values.containsKey($value);

static const Public NONE = Public._(0);
static const Map<int, Public> values = {
static const Map<int, Public> $values = {
0: NONE};

static const fb.Reader<Public> reader = _PublicReader();

@override
String toString() {
return 'Public{value: $value}';
return 'Public{value: ${$value}}';
}
}

Expand All @@ -96,28 +96,28 @@ class _PublicReader extends fb.Reader<Public> {
}

class KeywordsInUnionTypeId {
final int value;
const KeywordsInUnionTypeId._(this.value);
final int $value;
const KeywordsInUnionTypeId._(this.$value);

factory KeywordsInUnionTypeId.fromValue(int value) {
final result = values[value];
factory KeywordsInUnionTypeId.fromValue(int $value) {
final result = $values[$value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum KeywordsInUnionTypeId');
throw StateError('Invalid value ${$value} for bit flag enum KeywordsInUnionTypeId');
}
return result;
}

static KeywordsInUnionTypeId? _createOrNull(int? value) =>
value == null ? null : KeywordsInUnionTypeId.fromValue(value);
static KeywordsInUnionTypeId? _createOrNull(int? $value) =>
$value == null ? null : KeywordsInUnionTypeId.fromValue($value);

static const int minValue = 0;
static const int maxValue = 2;
static bool containsValue(int value) => values.containsKey(value);
static const int $minValue = 0;
static const int $maxValue = 2;
static bool containsValue(int $value) => $values.containsKey($value);

static const KeywordsInUnionTypeId NONE = KeywordsInUnionTypeId._(0);
static const KeywordsInUnionTypeId $static = KeywordsInUnionTypeId._(1);
static const KeywordsInUnionTypeId internal = KeywordsInUnionTypeId._(2);
static const Map<int, KeywordsInUnionTypeId> values = {
static const Map<int, KeywordsInUnionTypeId> $values = {
0: NONE,
1: $static,
2: internal};
Expand All @@ -126,7 +126,7 @@ class KeywordsInUnionTypeId {

@override
String toString() {
return 'KeywordsInUnionTypeId{value: $value}';
return 'KeywordsInUnionTypeId{value: ${$value}}';
}
}

Expand Down Expand Up @@ -190,8 +190,8 @@ class KeywordsInTableT implements fb.Packable {
@override
int pack(fb.Builder fbBuilder) {
fbBuilder.startTable(4);
fbBuilder.addInt32(0, $is.value);
fbBuilder.addInt32(1, private.value);
fbBuilder.addInt32(0, $is.$value);
fbBuilder.addInt32(1, private.$value);
fbBuilder.addInt32(2, type);
fbBuilder.addBool(3, $default);
return fbBuilder.endTable();
Expand Down Expand Up @@ -221,11 +221,11 @@ class KeywordsInTableBuilder {
}

int addIs(Abc? $is) {
fbBuilder.addInt32(0, $is?.value);
fbBuilder.addInt32(0, $is?.$value);
return fbBuilder.offset;
}
int addPrivate(Public? private) {
fbBuilder.addInt32(1, private?.value);
fbBuilder.addInt32(1, private?.$value);
return fbBuilder.offset;
}
int addType(int? type) {
Expand Down Expand Up @@ -263,8 +263,8 @@ class KeywordsInTableObjectBuilder extends fb.ObjectBuilder {
@override
int finish(fb.Builder fbBuilder) {
fbBuilder.startTable(4);
fbBuilder.addInt32(0, _$is?.value);
fbBuilder.addInt32(1, _private?.value);
fbBuilder.addInt32(0, _$is?.$value);
fbBuilder.addInt32(1, _private?.$value);
fbBuilder.addInt32(2, _type);
fbBuilder.addBool(3, _$default);
return fbBuilder.endTable();
Expand Down Expand Up @@ -292,7 +292,7 @@ class Table2 {

KeywordsInUnionTypeId? get typeType => KeywordsInUnionTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 4));
dynamic get type {
switch (typeType?.value) {
switch (typeType?.$value) {
case 1: return KeywordsInTable.reader.vTableGetNullable(_bc, _bcOffset, 6);
case 2: return KeywordsInTable.reader.vTableGetNullable(_bc, _bcOffset, 6);
default: return null;
Expand Down Expand Up @@ -326,7 +326,7 @@ class Table2T implements fb.Packable {
int pack(fb.Builder fbBuilder) {
final int? typeOffset = type?.pack(fbBuilder);
fbBuilder.startTable(2);
fbBuilder.addUint8(0, typeType?.value);
fbBuilder.addUint8(0, typeType?.$value);
fbBuilder.addOffset(1, typeOffset);
return fbBuilder.endTable();
}
Expand Down Expand Up @@ -355,7 +355,7 @@ class Table2Builder {
}

int addTypeType(KeywordsInUnionTypeId? typeType) {
fbBuilder.addUint8(0, typeType?.value);
fbBuilder.addUint8(0, typeType?.$value);
return fbBuilder.offset;
}
int addTypeOffset(int? offset) {
Expand Down Expand Up @@ -384,7 +384,7 @@ class Table2ObjectBuilder extends fb.ObjectBuilder {
int finish(fb.Builder fbBuilder) {
final int? typeOffset = _type?.getOrCreateOffset(fbBuilder);
fbBuilder.startTable(2);
fbBuilder.addUint8(0, _typeType?.value);
fbBuilder.addUint8(0, _typeType?.$value);
fbBuilder.addOffset(1, typeOffset);
return fbBuilder.endTable();
}
Expand Down
40 changes: 20 additions & 20 deletions src/idl_gen_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,41 +220,41 @@ class DartGenerator : public BaseGenerator {
const bool permit_zero = is_bit_flags;

code += "class " + enum_type + " {\n";
code += " final int value;\n";
code += " const " + enum_type + "._(this.value);\n\n";
code += " factory " + enum_type + ".fromValue(int value) {\n";
code += " final result = values[value];\n";
code += " final int $value;\n";
code += " const " + enum_type + "._(this.$value);\n\n";
code += " factory " + enum_type + ".fromValue(int $value) {\n";
code += " final result = $values[$value];\n";
code += " if (result == null) {\n";
if (permit_zero) {
code += " if (value == 0) {\n";
code += " if ($value == 0) {\n";
code += " return " + enum_type + "._(0);\n";
code += " } else {\n";
}
code += " throw StateError('Invalid value $value for bit flag enum ";
code += " throw StateError('Invalid value ${$value} for bit flag enum ";
code += enum_type + "');\n";
if (permit_zero) { code += " }\n"; }
code += " }\n";

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

code += " static " + enum_type + "? _createOrNull(int? value) => \n";
code += " static " + enum_type + "? _createOrNull(int? $value) => \n";
code +=
" value == null ? null : " + enum_type + ".fromValue(value);\n\n";
" $value == null ? null : " + enum_type + ".fromValue($value);\n\n";

// this is meaningless for bit_flags
// however, note that unlike "regular" dart enums this enum can still have
// holes.
if (!is_bit_flags) {
code += " static const int minValue = " +
code += " static const int $minValue = " +
enum_def.ToString(*enum_def.MinValue()) + ";\n";
code += " static const int maxValue = " +
code += " static const int $maxValue = " +
enum_def.ToString(*enum_def.MaxValue()) + ";\n";
}

code +=
" static bool containsValue(int value) =>"
" values.containsKey(value);\n\n";
" static bool containsValue(int $value) =>"
" $values.containsKey($value);\n\n";

for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
Expand All @@ -268,7 +268,7 @@ class DartGenerator : public BaseGenerator {
enum_type + "._(" + enum_def.ToString(ev) + ");\n";
}

code += " static const Map<int, " + enum_type + "> values = {\n";
code += " static const Map<int, " + enum_type + "> $values = {\n";
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
const auto enum_var = namer_.Variant(ev);
Expand All @@ -281,7 +281,7 @@ class DartGenerator : public BaseGenerator {
enum_type + "Reader();\n\n";
code += " @override\n";
code += " String toString() {\n";
code += " return '" + enum_type + "{value: $value}';\n";
code += " return '" + enum_type + "{value: ${$value}}';\n";
code += " }\n";
code += "}\n\n";

Expand Down Expand Up @@ -684,7 +684,7 @@ class DartGenerator : public BaseGenerator {
code += " " + type_name + " get " + field_name;
if (field.value.type.base_type == BASE_TYPE_UNION) {
code += " {\n";
code += " switch (" + field_name + "Type?.value) {\n";
code += " switch (" + field_name + "Type?.$value) {\n";
const auto &enum_def = *field.value.type.enum_def;
for (auto en_it = enum_def.Vals().begin() + 1;
en_it != enum_def.Vals().end(); ++en_it) {
Expand Down Expand Up @@ -858,7 +858,7 @@ class DartGenerator : public BaseGenerator {
} else {
code += " fbBuilder.put" + GenType(field.value.type) + "(";
code += field_name;
if (field.value.type.enum_def) { code += ".value"; }
if (field.value.type.enum_def) { code += ".$value"; }
code += ");\n";
}
}
Expand Down Expand Up @@ -890,7 +890,7 @@ class DartGenerator : public BaseGenerator {
code += " fbBuilder.add" + GenType(field.value.type) + "(" +
NumToString(offset) + ", ";
code += field_var;
if (field.value.type.enum_def) { code += "?.value"; }
if (field.value.type.enum_def) { code += "?.$value"; }
code += ");\n";
} else if (IsStruct(field.value.type)) {
code += " int " + add_field + "(int offset) {\n";
Expand Down Expand Up @@ -1026,7 +1026,7 @@ class DartGenerator : public BaseGenerator {
code +=
GenType(field.value.type.VectorType()) + "(" + field_name + "!";
if (field.value.type.enum_def) {
code += ".map((f) => f.value).toList()";
code += ".map((f) => f.$value).toList()";
}
code += ");\n";
}
Expand Down Expand Up @@ -1071,7 +1071,7 @@ class DartGenerator : public BaseGenerator {
code += " fbBuilder.put" + GenType(field.value.type) + "(";
if (prependUnderscore) { code += "_"; }
code += field_name;
if (field.value.type.enum_def) { code += ".value"; }
if (field.value.type.enum_def) { code += ".$value"; }
code += ");\n";
}
}
Expand Down Expand Up @@ -1101,7 +1101,7 @@ class DartGenerator : public BaseGenerator {
NumToString(offset) + ", " + field_var;
if (field.value.type.enum_def) {
bool isNullable = getDefaultValue(field.value).empty();
code += (isNullable || !pack) ? "?.value" : ".value";
code += (isNullable || !pack) ? "?.$value" : ".$value";
}
code += ");\n";
} else if (IsStruct(field.value.type)) {
Expand Down