dart: use enhanced enums (#8313)

* dart: rename enums.fbs

* feat: use dart enhanced enums

* generate code

---------

Co-authored-by: Wouter van Oortmerssen <aardappel@gmail.com>
This commit is contained in:
Ivan Dlugos 2024-11-18 18:31:19 +01:00 committed by GitHub
parent 5f453ef738
commit a9df44828d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 297 additions and 512 deletions

View File

@ -5,7 +5,7 @@ homepage: https://github.com/google/flatbuffers
documentation: https://google.github.io/flatbuffers/index.html
environment:
sdk: '>=2.12.0 <4.0.0'
sdk: '>=2.17.0 <4.0.0'
dev_dependencies:
test: ^1.17.7

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;

View File

@ -1,43 +1,33 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;
class OptionsEnum {
enum OptionsEnum {
A(1),
B(2),
C(3);
final int value;
const OptionsEnum._(this.value);
const OptionsEnum(this.value);
factory OptionsEnum.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum OptionsEnum');
switch (value) {
case 1: return OptionsEnum.A;
case 2: return OptionsEnum.B;
case 3: return OptionsEnum.C;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static OptionsEnum? _createOrNull(int? value) =>
static OptionsEnum? _createOrNull(int? value) =>
value == null ? null : OptionsEnum.fromValue(value);
static const int minValue = 1;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const OptionsEnum A = OptionsEnum._(1);
static const OptionsEnum B = OptionsEnum._(2);
static const OptionsEnum C = OptionsEnum._(3);
static const Map<int, OptionsEnum> values = {
1: A,
2: B,
3: C};
static const fb.Reader<OptionsEnum> reader = _OptionsEnumReader();
@override
String toString() {
return 'OptionsEnum{value: $value}';
}
}
class _OptionsEnumReader extends fb.Reader<OptionsEnum> {

View File

@ -9,7 +9,7 @@ import 'package:test_reflective_loader/test_reflective_loader.dart';
import './monster_test_my_game.example_generated.dart' as example;
import './monster_test_my_game.example2_generated.dart' as example2;
import './list_of_enums_generated.dart' as example3;
import 'enums_generated.dart' as example3;
import './bool_structs_generated.dart' as example4;
main() {
@ -63,11 +63,11 @@ class CheckOtherLangaugesData {
expect(
mon.toString(),
'Monster{'
'pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color{value: 2}, test3: Test{a: 5, b: 6}}, '
'pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color.Green, test3: Test{a: 5, b: 6}}, '
'mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], '
'color: Color{value: 8}, testType: AnyTypeId{value: 1}, '
'color: Color.Blue, testType: AnyTypeId.Monster, '
'test: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
'inventory: null, color: Color{value: 8}, testType: null, '
'inventory: null, color: Color.Blue, testType: null, '
'test: null, test4: null, testarrayofstring: null, '
'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, '
'testempty: null, testbool: false, testhashs32Fnv1: 0, '
@ -82,18 +82,18 @@ class CheckOtherLangaugesData {
'coOwningReference: 0, vectorOfCoOwningReferences: null, '
'nonOwningReference: 0, vectorOfNonOwningReferences: null, '
'anyUniqueType: null, anyUnique: null, anyAmbiguousType: null, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race.None, '
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null, '
'nativeInline: null, '
'longEnumNonEnumDefault: LongEnum{value: 0}, '
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
'longEnumNonEnumDefault: LongEnum._default, '
'longEnumNormalDefault: LongEnum.LongOne, nanDefault: NaN, '
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}, '
'test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], '
'testarrayofstring: [test1, test2], testarrayoftables: null, '
'enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
'inventory: null, color: Color{value: 8}, testType: null, '
'inventory: null, color: Color.Blue, testType: null, '
'test: null, test4: null, testarrayofstring: null, '
'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, '
'testempty: null, testbool: false, testhashs32Fnv1: 0, '
@ -108,11 +108,11 @@ class CheckOtherLangaugesData {
'coOwningReference: 0, vectorOfCoOwningReferences: null, '
'nonOwningReference: 0, vectorOfNonOwningReferences: null, '
'anyUniqueType: null, anyUnique: null, anyAmbiguousType: null, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race.None, '
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null, '
'nativeInline: null, '
'longEnumNonEnumDefault: LongEnum{value: 0}, '
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
'longEnumNonEnumDefault: LongEnum._default, '
'longEnumNormalDefault: LongEnum.LongOne, nanDefault: NaN, '
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}, '
@ -137,12 +137,12 @@ class CheckOtherLangaugesData {
'vectorOfNonOwningReferences: null, '
'anyUniqueType: null, anyUnique: null, '
'anyAmbiguousType: null, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race.None, '
'testrequirednestedflatbuffer: null, scalarKeySortedTables: [Stat{id: '
'miss, val: 0, count: 0}, Stat{id: hit, val: 10, count: 1}], '
'nativeInline: Test{a: 1, b: 2}, '
'longEnumNonEnumDefault: LongEnum{value: 0}, '
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
'longEnumNonEnumDefault: LongEnum._default, '
'longEnumNormalDefault: LongEnum.LongOne, nanDefault: NaN, '
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}');

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game.other_name_space;
@ -9,35 +9,25 @@ import 'package:flat_buffers/flat_buffers.dart' as fb;
import './include_test1_generated.dart';
class FromInclude {
enum FromInclude {
IncludeVal(0);
final int value;
const FromInclude._(this.value);
const FromInclude(this.value);
factory FromInclude.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum FromInclude');
switch (value) {
case 0: return FromInclude.IncludeVal;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static FromInclude? _createOrNull(int? value) =>
static FromInclude? _createOrNull(int? value) =>
value == null ? null : FromInclude.fromValue(value);
static const int minValue = 0;
static const int maxValue = 0;
static bool containsValue(int value) => values.containsKey(value);
static const FromInclude IncludeVal = FromInclude._(0);
static const Map<int, FromInclude> values = {
0: IncludeVal};
static const fb.Reader<FromInclude> reader = _FromIncludeReader();
@override
String toString() {
return 'FromInclude{value: $value}';
}
}
class _FromIncludeReader extends fb.Reader<FromInclude> {

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library keyword_test;
@ -7,39 +7,29 @@ import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;
class Abc {
enum Abc {
$void(0),
where(1),
stackalloc(2);
final int value;
const Abc._(this.value);
const Abc(this.value);
factory Abc.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Abc');
switch (value) {
case 0: return Abc.$void;
case 1: return Abc.where;
case 2: return Abc.stackalloc;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static Abc? _createOrNull(int? 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 Abc $void = Abc._(0);
static const Abc where = Abc._(1);
static const Abc stackalloc = Abc._(2);
static const Map<int, Abc> values = {
0: $void,
1: where,
2: stackalloc};
static const fb.Reader<Abc> reader = _AbcReader();
@override
String toString() {
return 'Abc{value: $value}';
}
}
class _AbcReader extends fb.Reader<Abc> {
@ -53,35 +43,25 @@ class _AbcReader extends fb.Reader<Abc> {
Abc.fromValue(const fb.Int32Reader().read(bc, offset));
}
class Public {
enum Public {
NONE(0);
final int value;
const Public._(this.value);
const Public(this.value);
factory Public.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Public');
switch (value) {
case 0: return Public.NONE;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static Public? _createOrNull(int? 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 Public NONE = Public._(0);
static const Map<int, Public> values = {
0: NONE};
static const fb.Reader<Public> reader = _PublicReader();
@override
String toString() {
return 'Public{value: $value}';
}
}
class _PublicReader extends fb.Reader<Public> {
@ -95,39 +75,29 @@ class _PublicReader extends fb.Reader<Public> {
Public.fromValue(const fb.Int32Reader().read(bc, offset));
}
class KeywordsInUnionTypeId {
enum KeywordsInUnionTypeId {
NONE(0),
$static(1),
internal(2);
final int value;
const KeywordsInUnionTypeId._(this.value);
const KeywordsInUnionTypeId(this.value);
factory KeywordsInUnionTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum KeywordsInUnionTypeId');
switch (value) {
case 0: return KeywordsInUnionTypeId.NONE;
case 1: return KeywordsInUnionTypeId.$static;
case 2: return KeywordsInUnionTypeId.internal;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static KeywordsInUnionTypeId? _createOrNull(int? 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 KeywordsInUnionTypeId NONE = KeywordsInUnionTypeId._(0);
static const KeywordsInUnionTypeId $static = KeywordsInUnionTypeId._(1);
static const KeywordsInUnionTypeId internal = KeywordsInUnionTypeId._(2);
static const Map<int, KeywordsInUnionTypeId> values = {
0: NONE,
1: $static,
2: internal};
static const fb.Reader<KeywordsInUnionTypeId> reader = _KeywordsInUnionTypeIdReader();
@override
String toString() {
return 'KeywordsInUnionTypeId{value: $value}';
}
}
class _KeywordsInUnionTypeIdReader extends fb.Reader<KeywordsInUnionTypeId> {

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game.example2;

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game.example;
@ -12,46 +12,29 @@ import './monster_test_my_game.example2_generated.dart' as my_game_example2;
import './include_test1_generated.dart';
/// Composite components of Monster color.
class Color {
enum Color {
Red(1),
Green(2),
Blue(8),
_default(0);
final int value;
const Color._(this.value);
const Color(this.value);
factory Color.fromValue(int value) {
final result = values[value];
if (result == null) {
if (value == 0) {
return Color._(0);
} else {
throw StateError('Invalid value $value for bit flag enum Color');
}
switch (value) {
case 1: return Color.Red;
case 2: return Color.Green;
case 8: return Color.Blue;
case 0: return Color._default;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static Color? _createOrNull(int? value) =>
static Color? _createOrNull(int? value) =>
value == null ? null : Color.fromValue(value);
static bool containsValue(int value) => values.containsKey(value);
static const Color Red = Color._(1);
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
static const Color Green = Color._(2);
/// \brief color Blue (1u << 3)
static const Color Blue = Color._(8);
static const Map<int, Color> values = {
1: Red,
2: Green,
8: Blue};
static const fb.Reader<Color> reader = _ColorReader();
@override
String toString() {
return 'Color{value: $value}';
}
}
class _ColorReader extends fb.Reader<Color> {
@ -65,41 +48,31 @@ class _ColorReader extends fb.Reader<Color> {
Color.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class Race {
enum Race {
None(-1),
Human(0),
Dwarf(1),
Elf(2);
final int value;
const Race._(this.value);
const Race(this.value);
factory Race.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Race');
switch (value) {
case -1: return Race.None;
case 0: return Race.Human;
case 1: return Race.Dwarf;
case 2: return Race.Elf;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static Race? _createOrNull(int? value) =>
static Race? _createOrNull(int? value) =>
value == null ? null : Race.fromValue(value);
static const int minValue = -1;
static const int maxValue = 2;
static bool containsValue(int value) => values.containsKey(value);
static const Race None = Race._(-1);
static const Race Human = Race._(0);
static const Race Dwarf = Race._(1);
static const Race Elf = Race._(2);
static const Map<int, Race> values = {
-1: None,
0: Human,
1: Dwarf,
2: Elf};
static const fb.Reader<Race> reader = _RaceReader();
@override
String toString() {
return 'Race{value: $value}';
}
}
class _RaceReader extends fb.Reader<Race> {
@ -113,41 +86,29 @@ class _RaceReader extends fb.Reader<Race> {
Race.fromValue(const fb.Int8Reader().read(bc, offset));
}
class LongEnum {
enum LongEnum {
LongOne(2),
LongTwo(4),
LongBig(1099511627776),
_default(0);
final int value;
const LongEnum._(this.value);
const LongEnum(this.value);
factory LongEnum.fromValue(int value) {
final result = values[value];
if (result == null) {
if (value == 0) {
return LongEnum._(0);
} else {
throw StateError('Invalid value $value for bit flag enum LongEnum');
}
switch (value) {
case 2: return LongEnum.LongOne;
case 4: return LongEnum.LongTwo;
case 1099511627776: return LongEnum.LongBig;
case 0: return LongEnum._default;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static LongEnum? _createOrNull(int? value) =>
static LongEnum? _createOrNull(int? value) =>
value == null ? null : LongEnum.fromValue(value);
static bool containsValue(int value) => values.containsKey(value);
static const LongEnum LongOne = LongEnum._(2);
static const LongEnum LongTwo = LongEnum._(4);
static const LongEnum LongBig = LongEnum._(1099511627776);
static const Map<int, LongEnum> values = {
2: LongOne,
4: LongTwo,
1099511627776: LongBig};
static const fb.Reader<LongEnum> reader = _LongEnumReader();
@override
String toString() {
return 'LongEnum{value: $value}';
}
}
class _LongEnumReader extends fb.Reader<LongEnum> {
@ -161,41 +122,31 @@ class _LongEnumReader extends fb.Reader<LongEnum> {
LongEnum.fromValue(const fb.Uint64Reader().read(bc, offset));
}
class AnyTypeId {
enum AnyTypeId {
NONE(0),
Monster(1),
TestSimpleTableWithEnum(2),
MyGame_Example2_Monster(3);
final int value;
const AnyTypeId._(this.value);
const AnyTypeId(this.value);
factory AnyTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum AnyTypeId');
switch (value) {
case 0: return AnyTypeId.NONE;
case 1: return AnyTypeId.Monster;
case 2: return AnyTypeId.TestSimpleTableWithEnum;
case 3: return AnyTypeId.MyGame_Example2_Monster;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static AnyTypeId? _createOrNull(int? value) =>
static AnyTypeId? _createOrNull(int? value) =>
value == null ? null : AnyTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const AnyTypeId NONE = AnyTypeId._(0);
static const AnyTypeId Monster = AnyTypeId._(1);
static const AnyTypeId TestSimpleTableWithEnum = AnyTypeId._(2);
static const AnyTypeId MyGame_Example2_Monster = AnyTypeId._(3);
static const Map<int, AnyTypeId> values = {
0: NONE,
1: Monster,
2: TestSimpleTableWithEnum,
3: MyGame_Example2_Monster};
static const fb.Reader<AnyTypeId> reader = _AnyTypeIdReader();
@override
String toString() {
return 'AnyTypeId{value: $value}';
}
}
class _AnyTypeIdReader extends fb.Reader<AnyTypeId> {
@ -209,41 +160,31 @@ class _AnyTypeIdReader extends fb.Reader<AnyTypeId> {
AnyTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class AnyUniqueAliasesTypeId {
enum AnyUniqueAliasesTypeId {
NONE(0),
M(1),
TS(2),
M2(3);
final int value;
const AnyUniqueAliasesTypeId._(this.value);
const AnyUniqueAliasesTypeId(this.value);
factory AnyUniqueAliasesTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum AnyUniqueAliasesTypeId');
switch (value) {
case 0: return AnyUniqueAliasesTypeId.NONE;
case 1: return AnyUniqueAliasesTypeId.M;
case 2: return AnyUniqueAliasesTypeId.TS;
case 3: return AnyUniqueAliasesTypeId.M2;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static AnyUniqueAliasesTypeId? _createOrNull(int? value) =>
static AnyUniqueAliasesTypeId? _createOrNull(int? value) =>
value == null ? null : AnyUniqueAliasesTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const AnyUniqueAliasesTypeId NONE = AnyUniqueAliasesTypeId._(0);
static const AnyUniqueAliasesTypeId M = AnyUniqueAliasesTypeId._(1);
static const AnyUniqueAliasesTypeId TS = AnyUniqueAliasesTypeId._(2);
static const AnyUniqueAliasesTypeId M2 = AnyUniqueAliasesTypeId._(3);
static const Map<int, AnyUniqueAliasesTypeId> values = {
0: NONE,
1: M,
2: TS,
3: M2};
static const fb.Reader<AnyUniqueAliasesTypeId> reader = _AnyUniqueAliasesTypeIdReader();
@override
String toString() {
return 'AnyUniqueAliasesTypeId{value: $value}';
}
}
class _AnyUniqueAliasesTypeIdReader extends fb.Reader<AnyUniqueAliasesTypeId> {
@ -257,41 +198,31 @@ class _AnyUniqueAliasesTypeIdReader extends fb.Reader<AnyUniqueAliasesTypeId> {
AnyUniqueAliasesTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class AnyAmbiguousAliasesTypeId {
enum AnyAmbiguousAliasesTypeId {
NONE(0),
M1(1),
M2(2),
M3(3);
final int value;
const AnyAmbiguousAliasesTypeId._(this.value);
const AnyAmbiguousAliasesTypeId(this.value);
factory AnyAmbiguousAliasesTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum AnyAmbiguousAliasesTypeId');
switch (value) {
case 0: return AnyAmbiguousAliasesTypeId.NONE;
case 1: return AnyAmbiguousAliasesTypeId.M1;
case 2: return AnyAmbiguousAliasesTypeId.M2;
case 3: return AnyAmbiguousAliasesTypeId.M3;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static AnyAmbiguousAliasesTypeId? _createOrNull(int? value) =>
static AnyAmbiguousAliasesTypeId? _createOrNull(int? value) =>
value == null ? null : AnyAmbiguousAliasesTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const AnyAmbiguousAliasesTypeId NONE = AnyAmbiguousAliasesTypeId._(0);
static const AnyAmbiguousAliasesTypeId M1 = AnyAmbiguousAliasesTypeId._(1);
static const AnyAmbiguousAliasesTypeId M2 = AnyAmbiguousAliasesTypeId._(2);
static const AnyAmbiguousAliasesTypeId M3 = AnyAmbiguousAliasesTypeId._(3);
static const Map<int, AnyAmbiguousAliasesTypeId> values = {
0: NONE,
1: M1,
2: M2,
3: M3};
static const fb.Reader<AnyAmbiguousAliasesTypeId> reader = _AnyAmbiguousAliasesTypeIdReader();
@override
String toString() {
return 'AnyAmbiguousAliasesTypeId{value: $value}';
}
}
class _AnyAmbiguousAliasesTypeIdReader extends fb.Reader<AnyAmbiguousAliasesTypeId> {
@ -1461,7 +1392,7 @@ class MonsterT implements fb.Packable {
this.testrequirednestedflatbuffer,
this.scalarKeySortedTables,
this.nativeInline,
this.longEnumNonEnumDefault = const LongEnum._(0),
this.longEnumNonEnumDefault = LongEnum._default,
this.longEnumNormalDefault = LongEnum.LongOne,
this.nanDefault = double.nan,
this.infDefault = double.infinity,

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game;

View File

@ -135,7 +135,7 @@ class DartGenerator : public BaseGenerator {
code = code + "// " + FlatBuffersGeneratedWarning() + "\n";
code = code +
"// ignore_for_file: unused_import, unused_field, unused_element, "
"unused_local_variable\n\n";
"unused_local_variable, constant_identifier_names\n\n";
if (!kv->first.empty()) { code += "library " + kv->first + ";\n\n"; }
@ -215,36 +215,45 @@ class DartGenerator : public BaseGenerator {
namer_.Type(enum_def) + (enum_def.is_union ? "TypeId" : "");
const bool is_bit_flags =
enum_def.attributes.Lookup("bit_flags") != nullptr;
// The flatbuffer schema language allows bit flag enums to potentially have
// a default value of zero, even if it's not a valid enum value...
const bool permit_zero = is_bit_flags;
const bool auto_default = is_bit_flags && !enum_def.FindByValue("0");
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 += " if (result == null) {\n";
if (permit_zero) {
code += " if (value == 0) {\n";
code += " return " + enum_type + "._(0);\n";
code += " } else {\n";
code += "enum " + enum_type + " {\n";
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
const auto enum_var = namer_.Variant(ev);
if (it != enum_def.Vals().begin()) code += ",\n";
code += " " + enum_var + "(" + enum_def.ToString(ev) + ")";
}
code += " throw StateError('Invalid value $value for bit flag enum ";
code += enum_type + "');\n";
if (permit_zero) { code += " }\n"; }
code += " }\n";
if (auto_default) { code += ",\n _default(0)"; }
code += ";\n\n";
code += " return result;\n";
code += " final int value;\n";
code += " const " + enum_type + "(this.value);\n\n";
code += " factory " + enum_type + ".fromValue(int 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) + ":";
code += " return " + enum_type + "." + enum_var + ";\n";
}
if (auto_default) {
code += " case 0: return " + enum_type + "._default;\n";
}
code += " default: throw StateError(";
code += "'Invalid value $value for bit flag enum');\n";
code += " }\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";
// this is meaningless for bit_flags
// however, note that unlike "regular" dart enums this enum can still have
// holes.
// 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 = " +
enum_def.ToString(*enum_def.MinValue()) + ";\n";
@ -252,37 +261,8 @@ class DartGenerator : public BaseGenerator {
enum_def.ToString(*enum_def.MaxValue()) + ";\n";
}
code +=
" 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;
const auto enum_var = namer_.Variant(ev);
if (!ev.doc_comment.empty()) {
if (it != enum_def.Vals().begin()) { code += '\n'; }
GenDocComment(ev.doc_comment, " ", code);
}
code += " static const " + enum_type + " " + enum_var + " = " +
enum_type + "._(" + enum_def.ToString(ev) + ");\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);
if (it != enum_def.Vals().begin()) code += ",\n";
code += " " + enum_def.ToString(ev) + ": " + enum_var;
}
code += "};\n\n";
code += " static const " + _kFb + ".Reader<" + enum_type + "> reader = _" +
enum_type + "Reader();\n\n";
code += " @override\n";
code += " String toString() {\n";
code += " return '" + enum_type + "{value: $value}';\n";
code += " }\n";
enum_type + "Reader();\n";
code += "}\n\n";
GenEnumReader(enum_def, enum_type, code);
@ -552,8 +532,7 @@ class DartGenerator : public BaseGenerator {
if (auto val = enum_def.FindByValue(defaultValue)) {
constructor_args += " = " + namer_.EnumVariant(enum_def, *val);
} else {
constructor_args += " = const " + namer_.Type(enum_def) + "._(" +
defaultValue + ")";
constructor_args += " = " + namer_.Type(enum_def) + "._default";
}
} else {
constructor_args += " = " + defaultValue;

View File

@ -1,4 +1,5 @@
#!/bin/sh
set -euo pipefail
#
# Copyright 2016 Google Inc. All rights reserved.
#
@ -16,7 +17,10 @@
pushd "$(dirname $0)" >/dev/null
command -v dart >/dev/null 2>&1 || { echo >&2 "Dart tests require dart to be in path but it's not installed. Aborting."; exit 1; }
command -v dart >/dev/null 2>&1 || {
echo >&2 "Dart tests require dart to be in path but it's not installed. Aborting."
exit 1
}
# output required files to the dart folder so that pub will be able to
# distribute them and more people can more easily run the dart tests
../flatc --dart --gen-object-api -I include_test -o ../dart/test monster_test.fbs
@ -28,7 +32,7 @@ cp monster_test.fbs ../dart/test
cd ../dart
../flatc --dart --gen-object-api -o ./test ./test/list_of_enums.fbs
../flatc --dart --gen-object-api -o ./test ./test/enums.fbs
../flatc --dart --gen-object-api -o ./test ./test/bool_structs.fbs
# update packages

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game.other_name_space;
@ -9,35 +9,25 @@ import 'package:flat_buffers/flat_buffers.dart' as fb;
import './include_test1_generated.dart';
class FromInclude {
enum FromInclude {
IncludeVal(0);
final int value;
const FromInclude._(this.value);
const FromInclude(this.value);
factory FromInclude.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum FromInclude');
switch (value) {
case 0: return FromInclude.IncludeVal;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static FromInclude? _createOrNull(int? value) =>
static FromInclude? _createOrNull(int? value) =>
value == null ? null : FromInclude.fromValue(value);
static const int minValue = 0;
static const int maxValue = 0;
static bool containsValue(int value) => values.containsKey(value);
static const FromInclude IncludeVal = FromInclude._(0);
static const Map<int, FromInclude> values = {
0: IncludeVal};
static const fb.Reader<FromInclude> reader = _FromIncludeReader();
@override
String toString() {
return 'FromInclude{value: $value}';
}
}
class _FromIncludeReader extends fb.Reader<FromInclude> {

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game;

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game.example2;

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game.example;
@ -12,46 +12,29 @@ import './monster_test_my_game.example2_generated.dart' as my_game_example2;
import './include_test1_generated.dart';
/// Composite components of Monster color.
class Color {
enum Color {
Red(1),
Green(2),
Blue(8),
_default(0);
final int value;
const Color._(this.value);
const Color(this.value);
factory Color.fromValue(int value) {
final result = values[value];
if (result == null) {
if (value == 0) {
return Color._(0);
} else {
throw StateError('Invalid value $value for bit flag enum Color');
}
switch (value) {
case 1: return Color.Red;
case 2: return Color.Green;
case 8: return Color.Blue;
case 0: return Color._default;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static Color? _createOrNull(int? value) =>
static Color? _createOrNull(int? value) =>
value == null ? null : Color.fromValue(value);
static bool containsValue(int value) => values.containsKey(value);
static const Color Red = Color._(1);
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
static const Color Green = Color._(2);
/// \brief color Blue (1u << 3)
static const Color Blue = Color._(8);
static const Map<int, Color> values = {
1: Red,
2: Green,
8: Blue};
static const fb.Reader<Color> reader = _ColorReader();
@override
String toString() {
return 'Color{value: $value}';
}
}
class _ColorReader extends fb.Reader<Color> {
@ -65,41 +48,31 @@ class _ColorReader extends fb.Reader<Color> {
Color.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class Race {
enum Race {
None(-1),
Human(0),
Dwarf(1),
Elf(2);
final int value;
const Race._(this.value);
const Race(this.value);
factory Race.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Race');
switch (value) {
case -1: return Race.None;
case 0: return Race.Human;
case 1: return Race.Dwarf;
case 2: return Race.Elf;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static Race? _createOrNull(int? value) =>
static Race? _createOrNull(int? value) =>
value == null ? null : Race.fromValue(value);
static const int minValue = -1;
static const int maxValue = 2;
static bool containsValue(int value) => values.containsKey(value);
static const Race None = Race._(-1);
static const Race Human = Race._(0);
static const Race Dwarf = Race._(1);
static const Race Elf = Race._(2);
static const Map<int, Race> values = {
-1: None,
0: Human,
1: Dwarf,
2: Elf};
static const fb.Reader<Race> reader = _RaceReader();
@override
String toString() {
return 'Race{value: $value}';
}
}
class _RaceReader extends fb.Reader<Race> {
@ -113,41 +86,29 @@ class _RaceReader extends fb.Reader<Race> {
Race.fromValue(const fb.Int8Reader().read(bc, offset));
}
class LongEnum {
enum LongEnum {
LongOne(2),
LongTwo(4),
LongBig(1099511627776),
_default(0);
final int value;
const LongEnum._(this.value);
const LongEnum(this.value);
factory LongEnum.fromValue(int value) {
final result = values[value];
if (result == null) {
if (value == 0) {
return LongEnum._(0);
} else {
throw StateError('Invalid value $value for bit flag enum LongEnum');
}
switch (value) {
case 2: return LongEnum.LongOne;
case 4: return LongEnum.LongTwo;
case 1099511627776: return LongEnum.LongBig;
case 0: return LongEnum._default;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static LongEnum? _createOrNull(int? value) =>
static LongEnum? _createOrNull(int? value) =>
value == null ? null : LongEnum.fromValue(value);
static bool containsValue(int value) => values.containsKey(value);
static const LongEnum LongOne = LongEnum._(2);
static const LongEnum LongTwo = LongEnum._(4);
static const LongEnum LongBig = LongEnum._(1099511627776);
static const Map<int, LongEnum> values = {
2: LongOne,
4: LongTwo,
1099511627776: LongBig};
static const fb.Reader<LongEnum> reader = _LongEnumReader();
@override
String toString() {
return 'LongEnum{value: $value}';
}
}
class _LongEnumReader extends fb.Reader<LongEnum> {
@ -161,41 +122,31 @@ class _LongEnumReader extends fb.Reader<LongEnum> {
LongEnum.fromValue(const fb.Uint64Reader().read(bc, offset));
}
class AnyTypeId {
enum AnyTypeId {
NONE(0),
Monster(1),
TestSimpleTableWithEnum(2),
MyGame_Example2_Monster(3);
final int value;
const AnyTypeId._(this.value);
const AnyTypeId(this.value);
factory AnyTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum AnyTypeId');
switch (value) {
case 0: return AnyTypeId.NONE;
case 1: return AnyTypeId.Monster;
case 2: return AnyTypeId.TestSimpleTableWithEnum;
case 3: return AnyTypeId.MyGame_Example2_Monster;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static AnyTypeId? _createOrNull(int? value) =>
static AnyTypeId? _createOrNull(int? value) =>
value == null ? null : AnyTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const AnyTypeId NONE = AnyTypeId._(0);
static const AnyTypeId Monster = AnyTypeId._(1);
static const AnyTypeId TestSimpleTableWithEnum = AnyTypeId._(2);
static const AnyTypeId MyGame_Example2_Monster = AnyTypeId._(3);
static const Map<int, AnyTypeId> values = {
0: NONE,
1: Monster,
2: TestSimpleTableWithEnum,
3: MyGame_Example2_Monster};
static const fb.Reader<AnyTypeId> reader = _AnyTypeIdReader();
@override
String toString() {
return 'AnyTypeId{value: $value}';
}
}
class _AnyTypeIdReader extends fb.Reader<AnyTypeId> {
@ -209,41 +160,31 @@ class _AnyTypeIdReader extends fb.Reader<AnyTypeId> {
AnyTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class AnyUniqueAliasesTypeId {
enum AnyUniqueAliasesTypeId {
NONE(0),
M(1),
TS(2),
M2(3);
final int value;
const AnyUniqueAliasesTypeId._(this.value);
const AnyUniqueAliasesTypeId(this.value);
factory AnyUniqueAliasesTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum AnyUniqueAliasesTypeId');
switch (value) {
case 0: return AnyUniqueAliasesTypeId.NONE;
case 1: return AnyUniqueAliasesTypeId.M;
case 2: return AnyUniqueAliasesTypeId.TS;
case 3: return AnyUniqueAliasesTypeId.M2;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static AnyUniqueAliasesTypeId? _createOrNull(int? value) =>
static AnyUniqueAliasesTypeId? _createOrNull(int? value) =>
value == null ? null : AnyUniqueAliasesTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const AnyUniqueAliasesTypeId NONE = AnyUniqueAliasesTypeId._(0);
static const AnyUniqueAliasesTypeId M = AnyUniqueAliasesTypeId._(1);
static const AnyUniqueAliasesTypeId TS = AnyUniqueAliasesTypeId._(2);
static const AnyUniqueAliasesTypeId M2 = AnyUniqueAliasesTypeId._(3);
static const Map<int, AnyUniqueAliasesTypeId> values = {
0: NONE,
1: M,
2: TS,
3: M2};
static const fb.Reader<AnyUniqueAliasesTypeId> reader = _AnyUniqueAliasesTypeIdReader();
@override
String toString() {
return 'AnyUniqueAliasesTypeId{value: $value}';
}
}
class _AnyUniqueAliasesTypeIdReader extends fb.Reader<AnyUniqueAliasesTypeId> {
@ -257,41 +198,31 @@ class _AnyUniqueAliasesTypeIdReader extends fb.Reader<AnyUniqueAliasesTypeId> {
AnyUniqueAliasesTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class AnyAmbiguousAliasesTypeId {
enum AnyAmbiguousAliasesTypeId {
NONE(0),
M1(1),
M2(2),
M3(3);
final int value;
const AnyAmbiguousAliasesTypeId._(this.value);
const AnyAmbiguousAliasesTypeId(this.value);
factory AnyAmbiguousAliasesTypeId.fromValue(int value) {
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum AnyAmbiguousAliasesTypeId');
switch (value) {
case 0: return AnyAmbiguousAliasesTypeId.NONE;
case 1: return AnyAmbiguousAliasesTypeId.M1;
case 2: return AnyAmbiguousAliasesTypeId.M2;
case 3: return AnyAmbiguousAliasesTypeId.M3;
default: throw StateError('Invalid value $value for bit flag enum');
}
return result;
}
static AnyAmbiguousAliasesTypeId? _createOrNull(int? value) =>
static AnyAmbiguousAliasesTypeId? _createOrNull(int? value) =>
value == null ? null : AnyAmbiguousAliasesTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 3;
static bool containsValue(int value) => values.containsKey(value);
static const AnyAmbiguousAliasesTypeId NONE = AnyAmbiguousAliasesTypeId._(0);
static const AnyAmbiguousAliasesTypeId M1 = AnyAmbiguousAliasesTypeId._(1);
static const AnyAmbiguousAliasesTypeId M2 = AnyAmbiguousAliasesTypeId._(2);
static const AnyAmbiguousAliasesTypeId M3 = AnyAmbiguousAliasesTypeId._(3);
static const Map<int, AnyAmbiguousAliasesTypeId> values = {
0: NONE,
1: M1,
2: M2,
3: M3};
static const fb.Reader<AnyAmbiguousAliasesTypeId> reader = _AnyAmbiguousAliasesTypeIdReader();
@override
String toString() {
return 'AnyAmbiguousAliasesTypeId{value: $value}';
}
}
class _AnyAmbiguousAliasesTypeIdReader extends fb.Reader<AnyAmbiguousAliasesTypeId> {
@ -1461,7 +1392,7 @@ class MonsterT implements fb.Packable {
this.testrequirednestedflatbuffer,
this.scalarKeySortedTables,
this.nativeInline,
this.longEnumNonEnumDefault = const LongEnum._(0),
this.longEnumNonEnumDefault = LongEnum._default,
this.longEnumNormalDefault = LongEnum.LongOne,
this.nanDefault = double.nan,
this.infDefault = double.infinity,

View File

@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable, constant_identifier_names
library my_game;