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: use enhanced enums #8313

Open
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dart/test/bool_structs_generated.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down
28 changes: 14 additions & 14 deletions dart/test/flat_buffers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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, '
Expand All @@ -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, '
Expand All @@ -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}, '
Expand All @@ -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}');
Expand Down
2 changes: 1 addition & 1 deletion dart/test/include_test1_generated.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
28 changes: 9 additions & 19 deletions dart/test/include_test2_my_game.other_name_space_generated.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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> {
Expand Down
96 changes: 33 additions & 63 deletions dart/test/keyword_test_keyword_test_generated.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
// 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;

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> {
Expand All @@ -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> {
Expand All @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion dart/test/monster_test_my_game.example2_generated.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Loading
Loading