diff options
author | Ivan Dlugos <6349682+vaind@users.noreply.github.com> | 2021-06-22 17:54:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-22 08:54:57 -0700 |
commit | a6ee3355740ff8e62c482fdd7475551e737feec8 (patch) | |
tree | 614400619bec4e75d544684710078c6f7bb32c9d /dart | |
parent | 71d43f3be94d6f448ff8776cb88dafa025c7b80d (diff) | |
download | flatbuffers-a6ee3355740ff8e62c482fdd7475551e737feec8.tar.gz flatbuffers-a6ee3355740ff8e62c482fdd7475551e737feec8.tar.bz2 flatbuffers-a6ee3355740ff8e62c482fdd7475551e737feec8.zip |
Dart null safety (#6696)
* Dart null-safety - prepare migration annotations in library files
* Dart null-safety - apply migration
* Dart null-safety - update flatc to generate valid code
* Dart null-safety - fix flatc generated code and adjust tests
* Dart null-safety - update example and the generated code in the tests folder
* Dart null safety - minor review changes
* Dart - apply `dartfmt -w .`
Diffstat (limited to 'dart')
-rw-r--r-- | dart/example/example.dart | 16 | ||||
-rw-r--r-- | dart/example/monster_my_game.sample_generated.dart | 202 | ||||
-rw-r--r-- | dart/lib/flat_buffers.dart | 159 | ||||
-rw-r--r-- | dart/lib/src/builder.dart | 202 | ||||
-rw-r--r-- | dart/lib/src/reference.dart | 228 | ||||
-rw-r--r-- | dart/lib/src/types.dart | 110 | ||||
-rw-r--r-- | dart/pubspec.yaml | 8 | ||||
-rw-r--r-- | dart/test/flat_buffers_test.dart | 122 | ||||
-rw-r--r-- | dart/test/flex_builder_test.dart | 478 | ||||
-rw-r--r-- | dart/test/flex_reader_test.dart | 932 | ||||
-rw-r--r-- | dart/test/flex_types_test.dart | 141 | ||||
-rw-r--r-- | dart/test/monster_test_my_game.example2_generated.dart | 13 | ||||
-rw-r--r-- | dart/test/monster_test_my_game.example_generated.dart | 1383 | ||||
-rw-r--r-- | dart/test/monster_test_my_game_generated.dart | 13 |
14 files changed, 2448 insertions, 1559 deletions
diff --git a/dart/example/example.dart b/dart/example/example.dart index d95bb31f..c84ffa60 100644 --- a/dart/example/example.dart +++ b/dart/example/example.dart @@ -26,10 +26,10 @@ void main() { void builderTest() { final builder = new fb.Builder(initialSize: 1024); - final int weaponOneName = builder.writeString("Sword"); + final int? weaponOneName = builder.writeString("Sword"); final int weaponOneDamage = 3; - final int weaponTwoName = builder.writeString("Axe"); + final int? weaponTwoName = builder.writeString("Axe"); final int weaponTwoDamage = 5; final swordBuilder = new myGame.WeaponBuilder(builder) @@ -45,7 +45,7 @@ void builderTest() { final int axe = axeBuilder.finish(); // Serialize a name for our monster, called "Orc". - final int name = builder.writeString('Orc'); + final int? name = builder.writeString('Orc'); // Create a list representing the inventory of the Orc. Each number // could correspond to an item that can be claimed after he is slain. @@ -122,27 +122,25 @@ bool verify(List<int> buffer) { assert(monster.name == "MyMonster"); // Get and test a field of the FlatBuffer's `struct`. - var pos = monster.pos; - assert(pos != null); + var pos = monster.pos!; assert(pos.z == 3.0); // Get a test an element from the `inventory` FlatBuffer's `vector`. - var inv = monster.inventory; - assert(inv != null); + var inv = monster.inventory!; assert(inv.length == 10); assert(inv[9] == 9); // Get and test the `weapons` FlatBuffers's `vector`. var expected_weapon_names = ["Sword", "Axe"]; var expected_weapon_damages = [3, 5]; - var weps = monster.weapons; + var weps = monster.weapons!; for (int i = 0; i < weps.length; i++) { assert(weps[i].name == expected_weapon_names[i]); assert(weps[i].damage == expected_weapon_damages[i]); } // Get and test the `Equipment` union (`equipped` field). - assert(monster.equippedType.value == myGame.EquipmentTypeId.Weapon.value); + assert(monster.equippedType!.value == myGame.EquipmentTypeId.Weapon.value); assert(monster.equippedType == myGame.EquipmentTypeId.Weapon); assert(monster.equipped is myGame.Weapon); diff --git a/dart/example/monster_my_game.sample_generated.dart b/dart/example/monster_my_game.sample_generated.dart index ced9b31b..4f08860f 100644 --- a/dart/example/monster_my_game.sample_generated.dart +++ b/dart/example/monster_my_game.sample_generated.dart @@ -1,5 +1,5 @@ // automatically generated by the FlatBuffers compiler, do not modify -// ignore_for_file: unused_import, unused_field, unused_local_variable +// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable library my_game.sample; @@ -12,13 +12,16 @@ class Color { const Color._(this.value); factory Color.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum Color'); } - return values[value]; + return result; } + static Color? _createOrNull(int? value) => + value == null ? null : Color.fromValue(value); + static const int minValue = 0; static const int maxValue = 2; static bool containsValue(int value) => values.containsKey(value); @@ -26,7 +29,10 @@ class Color { static const Color Red = const Color._(0); static const Color Green = const Color._(1); static const Color Blue = const Color._(2); - static const Map<int,Color> values = {0: Red,1: Green,2: Blue,}; + static const Map<int, Color> values = { + 0: Red, + 1: Green, + 2: Blue}; static const fb.Reader<Color> reader = const _ColorReader(); @@ -52,20 +58,25 @@ class EquipmentTypeId { const EquipmentTypeId._(this.value); factory EquipmentTypeId.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum EquipmentTypeId'); } - return values[value]; + return result; } + static EquipmentTypeId? _createOrNull(int? value) => + value == null ? null : EquipmentTypeId.fromValue(value); + static const int minValue = 0; static const int maxValue = 1; static bool containsValue(int value) => values.containsKey(value); static const EquipmentTypeId NONE = const EquipmentTypeId._(0); static const EquipmentTypeId Weapon = const EquipmentTypeId._(1); - static const Map<int,EquipmentTypeId> values = {0: NONE,1: Weapon,}; + static const Map<int, EquipmentTypeId> values = { + 0: NONE, + 1: Weapon}; static const fb.Reader<EquipmentTypeId> reader = const _EquipmentTypeIdReader(); @@ -116,9 +127,7 @@ class _Vec3Reader extends fb.StructReader<Vec3> { } class Vec3Builder { - Vec3Builder(this.fbBuilder) { - assert(fbBuilder != null); - } + Vec3Builder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -137,9 +146,9 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { final double _z; Vec3ObjectBuilder({ - double x, - double y, - double z, + required double x, + required double y, + required double z, }) : _x = x, _y = y, @@ -147,10 +156,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.putFloat32(_z); fbBuilder.putFloat32(_y); fbBuilder.putFloat32(_x); @@ -159,7 +165,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -177,21 +183,21 @@ class Monster { final fb.BufferContext _bc; final int _bcOffset; - Vec3 get pos => Vec3.reader.vTableGet(_bc, _bcOffset, 4, null); + Vec3? get pos => Vec3.reader.vTableGetNullable(_bc, _bcOffset, 4); int get mana => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 150); int get hp => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 100); - String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 10, null); - List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null); - Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 2)); - List<Weapon> get weapons => const fb.ListReader<Weapon>(Weapon.reader).vTableGet(_bc, _bcOffset, 18, null); - EquipmentTypeId get equippedType => new EquipmentTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 20, 0)); - dynamic get equipped { + String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 10); + List<int>? get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGetNullable(_bc, _bcOffset, 14); + Color get color => Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 2)); + List<Weapon>? get weapons => const fb.ListReader<Weapon>(Weapon.reader).vTableGetNullable(_bc, _bcOffset, 18); + EquipmentTypeId? get equippedType => EquipmentTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 20)); + dynamic? get equipped { switch (equippedType?.value) { - case 1: return Weapon.reader.vTableGet(_bc, _bcOffset, 22, null); + case 1: return Weapon.reader.vTableGetNullable(_bc, _bcOffset, 22); default: return null; } } - List<Vec3> get path => const fb.ListReader<Vec3>(Vec3.reader).vTableGet(_bc, _bcOffset, 24, null); + List<Vec3>? get path => const fb.ListReader<Vec3>(Vec3.reader).vTableGetNullable(_bc, _bcOffset, 24); @override String toString() { @@ -208,9 +214,7 @@ class _MonsterReader extends fb.TableReader<Monster> { } class MonsterBuilder { - MonsterBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + MonsterBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -222,39 +226,39 @@ class MonsterBuilder { fbBuilder.addStruct(0, offset); return fbBuilder.offset; } - int addMana(int mana) { + int addMana(int? mana) { fbBuilder.addInt16(1, mana); return fbBuilder.offset; } - int addHp(int hp) { + int addHp(int? hp) { fbBuilder.addInt16(2, hp); return fbBuilder.offset; } - int addNameOffset(int offset) { + int addNameOffset(int? offset) { fbBuilder.addOffset(3, offset); return fbBuilder.offset; } - int addInventoryOffset(int offset) { + int addInventoryOffset(int? offset) { fbBuilder.addOffset(5, offset); return fbBuilder.offset; } - int addColor(Color color) { + int addColor(Color? color) { fbBuilder.addInt8(6, color?.value); return fbBuilder.offset; } - int addWeaponsOffset(int offset) { + int addWeaponsOffset(int? offset) { fbBuilder.addOffset(7, offset); return fbBuilder.offset; } - int addEquippedType(EquipmentTypeId equippedType) { + int addEquippedType(EquipmentTypeId? equippedType) { fbBuilder.addUint8(8, equippedType?.value); return fbBuilder.offset; } - int addEquippedOffset(int offset) { + int addEquippedOffset(int? offset) { fbBuilder.addOffset(9, offset); return fbBuilder.offset; } - int addPathOffset(int offset) { + int addPathOffset(int? offset) { fbBuilder.addOffset(10, offset); return fbBuilder.offset; } @@ -265,28 +269,28 @@ class MonsterBuilder { } class MonsterObjectBuilder extends fb.ObjectBuilder { - final Vec3ObjectBuilder _pos; - final int _mana; - final int _hp; - final String _name; - final List<int> _inventory; - final Color _color; - final List<WeaponObjectBuilder> _weapons; - final EquipmentTypeId _equippedType; - final dynamic _equipped; - final List<Vec3ObjectBuilder> _path; + final Vec3ObjectBuilder? _pos; + final int? _mana; + final int? _hp; + final String? _name; + final List<int>? _inventory; + final Color? _color; + final List<WeaponObjectBuilder>? _weapons; + final EquipmentTypeId? _equippedType; + final dynamic? _equipped; + final List<Vec3ObjectBuilder>? _path; MonsterObjectBuilder({ - Vec3ObjectBuilder pos, - int mana, - int hp, - String name, - List<int> inventory, - Color color, - List<WeaponObjectBuilder> weapons, - EquipmentTypeId equippedType, - dynamic equipped, - List<Vec3ObjectBuilder> path, + Vec3ObjectBuilder? pos, + int? mana, + int? hp, + String? name, + List<int>? inventory, + Color? color, + List<WeaponObjectBuilder>? weapons, + EquipmentTypeId? equippedType, + dynamic? equipped, + List<Vec3ObjectBuilder>? path, }) : _pos = pos, _mana = mana, @@ -301,50 +305,37 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int nameOffset = fbBuilder.writeString(_name); - final int inventoryOffset = _inventory?.isNotEmpty == true - ? fbBuilder.writeListUint8(_inventory) + int finish(fb.Builder fbBuilder) { + final int? nameOffset = fbBuilder.writeString(_name); + final int? inventoryOffset = _inventory?.isNotEmpty == true + ? fbBuilder.writeListUint8(_inventory!) : null; - final int weaponsOffset = _weapons?.isNotEmpty == true - ? fbBuilder.writeList(_weapons.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) + final int? weaponsOffset = _weapons?.isNotEmpty == true + ? fbBuilder.writeList(_weapons!.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) : null; - final int equippedOffset = _equipped?.getOrCreateOffset(fbBuilder); - final int pathOffset = _path?.isNotEmpty == true - ? fbBuilder.writeListOfStructs(_path) + final int? equippedOffset = _equipped?.getOrCreateOffset(fbBuilder); + final int? pathOffset = _path?.isNotEmpty == true + ? fbBuilder.writeListOfStructs(_path!) : null; - fbBuilder.startTable(); if (_pos != null) { - fbBuilder.addStruct(0, _pos.finish(fbBuilder)); + fbBuilder.addStruct(0, _pos!.finish(fbBuilder)); } fbBuilder.addInt16(1, _mana); fbBuilder.addInt16(2, _hp); - if (nameOffset != null) { - fbBuilder.addOffset(3, nameOffset); - } - if (inventoryOffset != null) { - fbBuilder.addOffset(5, inventoryOffset); - } + fbBuilder.addOffset(3, nameOffset); + fbBuilder.addOffset(5, inventoryOffset); fbBuilder.addInt8(6, _color?.value); - if (weaponsOffset != null) { - fbBuilder.addOffset(7, weaponsOffset); - } + fbBuilder.addOffset(7, weaponsOffset); fbBuilder.addUint8(8, _equippedType?.value); - if (equippedOffset != null) { - fbBuilder.addOffset(9, equippedOffset); - } - if (pathOffset != null) { - fbBuilder.addOffset(10, pathOffset); - } + fbBuilder.addOffset(9, equippedOffset); + fbBuilder.addOffset(10, pathOffset); return fbBuilder.endTable(); } /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -362,7 +353,7 @@ class Weapon { final fb.BufferContext _bc; final int _bcOffset; - String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 4, null); + String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 4); int get damage => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 0); @override @@ -380,9 +371,7 @@ class _WeaponReader extends fb.TableReader<Weapon> { } class WeaponBuilder { - WeaponBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + WeaponBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -390,11 +379,11 @@ class WeaponBuilder { fbBuilder.startTable(); } - int addNameOffset(int offset) { + int addNameOffset(int? offset) { fbBuilder.addOffset(0, offset); return fbBuilder.offset; } - int addDamage(int damage) { + int addDamage(int? damage) { fbBuilder.addInt16(1, damage); return fbBuilder.offset; } @@ -405,34 +394,29 @@ class WeaponBuilder { } class WeaponObjectBuilder extends fb.ObjectBuilder { - final String _name; - final int _damage; + final String? _name; + final int? _damage; WeaponObjectBuilder({ - String name, - int damage, + String? name, + int? damage, }) : _name = name, _damage = damage; /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int nameOffset = fbBuilder.writeString(_name); - + int finish(fb.Builder fbBuilder) { + final int? nameOffset = fbBuilder.writeString(_name); fbBuilder.startTable(); - if (nameOffset != null) { - fbBuilder.addOffset(0, nameOffset); - } + fbBuilder.addOffset(0, nameOffset); fbBuilder.addInt16(1, _damage); return fbBuilder.endTable(); } /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); diff --git a/dart/lib/flat_buffers.dart b/dart/lib/flat_buffers.dart index 27db49be..74c25943 100644 --- a/dart/lib/flat_buffers.dart +++ b/dart/lib/flat_buffers.dart @@ -28,13 +28,17 @@ typedef void StructBuilder(); class BufferContext { final ByteData _buffer; + ByteData get buffer => _buffer; + + /// Create from a FlatBuffer represented by a list of bytes (uint8). factory BufferContext.fromBytes(List<int> byteList) { Uint8List uint8List = _asUint8List(byteList); ByteData buf = new ByteData.view(uint8List.buffer, uint8List.offsetInBytes); - return new BufferContext._(buf); + return BufferContext(buf); } - BufferContext._(this._buffer); + /// Create from a FlatBuffer represented by ByteData. + BufferContext(this._buffer); int derefObject(int offset) { return offset + _getUint32(offset); @@ -43,31 +47,23 @@ class BufferContext { Uint8List _asUint8LIst(int offset, int length) => _buffer.buffer.asUint8List(_buffer.offsetInBytes + offset, length); - double _getFloat64(int offset) => - _buffer.getFloat64(offset, Endian.little); + double _getFloat64(int offset) => _buffer.getFloat64(offset, Endian.little); - double _getFloat32(int offset) => - _buffer.getFloat32(offset, Endian.little); + double _getFloat32(int offset) => _buffer.getFloat32(offset, Endian.little); - int _getInt64(int offset) => - _buffer.getInt64(offset, Endian.little); + int _getInt64(int offset) => _buffer.getInt64(offset, Endian.little); - int _getInt32(int offset) => - _buffer.getInt32(offset, Endian.little); + int _getInt32(int offset) => _buffer.getInt32(offset, Endian.little); - int _getInt16(int offset) => - _buffer.getInt16(offset, Endian.little); + int _getInt16(int offset) => _buffer.getInt16(offset, Endian.little); int _getInt8(int offset) => _buffer.getInt8(offset); - int _getUint64(int offset) => - _buffer.getUint64(offset, Endian.little); + int _getUint64(int offset) => _buffer.getUint64(offset, Endian.little); - int _getUint32(int offset) => - _buffer.getUint32(offset, Endian.little); + int _getUint32(int offset) => _buffer.getUint32(offset, Endian.little); - int _getUint16(int offset) => - _buffer.getUint16(offset, Endian.little); + int _getUint16(int offset) => _buffer.getUint16(offset, Endian.little); int _getUint8(int offset) => _buffer.getUint8(offset); @@ -84,7 +80,7 @@ class BufferContext { /// Class implemented by typed builders generated by flatc. abstract class ObjectBuilder { - int _firstOffset; + int? _firstOffset; /// Can be used to write the data represented by this builder to the [Builder] /// and reuse the offset created in multiple tables. @@ -94,7 +90,7 @@ abstract class ObjectBuilder { /// first call to this method. int getOrCreateOffset(Builder fbBuilder) { _firstOffset ??= finish(fbBuilder); - return _firstOffset; + return _firstOffset!; } /// Writes the data in this helper to the [Builder]. @@ -110,8 +106,8 @@ class Builder { final int initialSize; /// The list of existing VTable(s). - //final List<_VTable> _vTables = <_VTable>[]; - final List<int> _vTables = <int>[]; + final List<int> _vTables = List<int>.filled(16, 0, growable: true) + ..length = 0; ByteData _buf; @@ -125,16 +121,16 @@ class Builder { int _tail = 0; /// The location of the end of the current table, measured in bytes from the - /// end of [_buf], or `null` if a table is not currently being built. - int _currentTableEndTail; + /// end of [_buf]. + int _currentTableEndTail = 0; - _VTable _currentVTable; + _VTable? _currentVTable; /// Map containing all strings that have been written so far. This allows us /// to avoid duplicating strings. /// /// Allocated only if `internStrings` is set to true on the constructor. - Map<String, int> _strings; + Map<String, int>? _strings; /// Creates a new FlatBuffers Builder. /// @@ -155,7 +151,7 @@ class Builder { /// Add the [field] with the given boolean [value]. The field is not added if /// the [value] is equal to [def]. Booleans are stored as 8-bit fields with /// `0` for `false` and `1` for `true`. - void addBool(int field, bool value, [bool def]) { + void addBool(int field, bool? value, [bool? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofUint8, 1); @@ -166,7 +162,7 @@ class Builder { /// Add the [field] with the given 32-bit signed integer [value]. The field is /// not added if the [value] is equal to [def]. - void addInt32(int field, int value, [int def]) { + void addInt32(int field, int? value, [int? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofInt32, 1); @@ -177,7 +173,7 @@ class Builder { /// Add the [field] with the given 32-bit signed integer [value]. The field is /// not added if the [value] is equal to [def]. - void addInt16(int field, int value, [int def]) { + void addInt16(int field, int? value, [int? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofInt16, 1); @@ -188,7 +184,7 @@ class Builder { /// Add the [field] with the given 8-bit signed integer [value]. The field is /// not added if the [value] is equal to [def]. - void addInt8(int field, int value, [int def]) { + void addInt8(int field, int? value, [int? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofInt8, 1); @@ -200,11 +196,11 @@ class Builder { void addStruct(int field, int offset) { _ensureCurrentVTable(); _trackField(field); - _currentVTable.addField(field, offset); + _currentVTable!.addField(field, offset); } /// Add the [field] referencing an object with the given [offset]. - void addOffset(int field, int offset) { + void addOffset(int field, int? offset) { _ensureCurrentVTable(); if (offset != null) { _prepare(_sizeofUint32, 1); @@ -215,7 +211,7 @@ class Builder { /// Add the [field] with the given 32-bit unsigned integer [value]. The field /// is not added if the [value] is equal to [def]. - void addUint32(int field, int value, [int def]) { + void addUint32(int field, int? value, [int? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofUint32, 1); @@ -226,7 +222,7 @@ class Builder { /// Add the [field] with the given 32-bit unsigned integer [value]. The field /// is not added if the [value] is equal to [def]. - void addUint16(int field, int value, [int def]) { + void addUint16(int field, int? value, [int? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofUint16, 1); @@ -237,7 +233,7 @@ class Builder { /// Add the [field] with the given 8-bit unsigned integer [value]. The field /// is not added if the [value] is equal to [def]. - void addUint8(int field, int value, [int def]) { + void addUint8(int field, int? value, [int? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofUint8, 1); @@ -248,7 +244,7 @@ class Builder { /// Add the [field] with the given 32-bit float [value]. The field /// is not added if the [value] is equal to [def]. - void addFloat32(int field, double value, [double def]) { + void addFloat32(int field, double? value, [double? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofFloat32, 1); @@ -259,7 +255,7 @@ class Builder { /// Add the [field] with the given 64-bit double [value]. The field /// is not added if the [value] is equal to [def]. - void addFloat64(int field, double value, [double def]) { + void addFloat64(int field, double? value, [double? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofFloat64, 1); @@ -270,7 +266,7 @@ class Builder { /// Add the [field] with the given 64-bit unsigned integer [value]. The field /// is not added if the [value] is equal to [def]. - void addUint64(int field, int value, [double def]) { + void addUint64(int field, int? value, [double? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofUint64, 1); @@ -281,7 +277,7 @@ class Builder { /// Add the [field] with the given 64-bit unsigned integer [value]. The field /// is not added if the [value] is equal to [def]. - void addInt64(int field, int value, [double def]) { + void addInt64(int field, int? value, [double? def]) { _ensureCurrentVTable(); if (value != null && value != def) { _prepare(_sizeofInt64, 1); @@ -299,11 +295,12 @@ class Builder { _prepare(_sizeofInt32, 1); int tableTail = _tail; // Prepare the size of the current table. - _currentVTable.tableSize = tableTail - _currentTableEndTail; + final currentVTable = _currentVTable!; + currentVTable.tableSize = tableTail - _currentTableEndTail; // Prepare the VTable to use for the current table. - int vTableTail; + int? vTableTail; { - _currentVTable.computeFieldOffsets(tableTail); + currentVTable.computeFieldOffsets(tableTail); // Try to find an existing compatible VTable. // Search backward - more likely to have recently used one for (int i = _vTables.length - 1; i >= 0; i--) { @@ -311,19 +308,19 @@ class Builder { final int vt2Start = _buf.lengthInBytes - vt2Offset; final int vt2Size = _buf.getUint16(vt2Start, Endian.little); - if (_currentVTable._vTableSize == vt2Size && - _currentVTable._offsetsMatch(vt2Start, _buf)) { + if (currentVTable._vTableSize == vt2Size && + currentVTable._offsetsMatch(vt2Start, _buf)) { vTableTail = vt2Offset; break; } } // Write a new VTable. if (vTableTail == null) { - _prepare(_sizeofUint16, _currentVTable.numOfUint16); + _prepare(_sizeofUint16, _currentVTable!.numOfUint16); vTableTail = _tail; - _currentVTable.tail = vTableTail; - _currentVTable.output(_buf, _buf.lengthInBytes - _tail); - _vTables.add(_currentVTable.tail); + currentVTable.tail = vTableTail; + currentVTable.output(_buf, _buf.lengthInBytes - _tail); + _vTables.add(currentVTable.tail); } } // Set the VTable offset. @@ -346,7 +343,7 @@ class Builder { /// written object. If [fileIdentifier] is specified (and not `null`), it is /// interpreted as a 4-byte Latin-1 encoded string that should be placed at /// bytes 4-7 of the file. - Uint8List finish(int offset, [String fileIdentifier]) { + Uint8List finish(int offset, [String? fileIdentifier]) { _prepare(max(_sizeofUint32, _maxAlign), fileIdentifier == null ? 1 : 2); final finishedSize = size(); _setUint32AtTail(_buf, finishedSize, finishedSize - offset); @@ -444,7 +441,7 @@ class Builder { _maxAlign = 1; _tail = 0; _currentVTable = null; - _vTables.clear(); + _vTables.length = 0; if (_strings != null) { _strings = new Map<String, int>(); } @@ -613,7 +610,7 @@ class Builder { /// Write the given list of bools as unsigend 8-bit integer [values]. int writeListBool(List<bool> values) { - return writeListUint8(values?.map((b) => b ? 1 : 0)?.toList()); + return writeListUint8(values.map((b) => b ? 1 : 0).toList()); } /// Write the given list of signed 8-bit integer [values]. @@ -648,11 +645,11 @@ class Builder { /// Write the given string [value] and return its offset, or `null` if /// the [value] is `null`. - int writeString(String value) { + int? writeString(String? value) { _ensureNoVTable(); if (value != null) { if (_strings != null) { - return _strings.putIfAbsent(value, () => _writeString(value)); + return _strings!.putIfAbsent(value, () => _writeString(value)); } else { return _writeString(value); } @@ -732,7 +729,7 @@ class Builder { /// Record the offset of the given [field]. void _trackField(int field) { - _currentVTable.addField(field, _tail); + _currentVTable!.addField(field, _tail); } static void _setFloat64AtTail(ByteData _buf, int tail, double x) { @@ -848,6 +845,7 @@ class Float32Reader extends Reader<double> { class Int64Reader extends Reader<int> { const Int64Reader() : super(); + @override int get size => _sizeofInt64; @@ -915,19 +913,23 @@ abstract class Reader<T> { T read(BufferContext bc, int offset); /// Read the value of the given [field] in the given [object]. - T vTableGet(BufferContext object, int offset, int field, [T defaultValue]) { + T vTableGet(BufferContext object, int offset, int field, T defaultValue) { + int fieldOffset = _vTableFieldOffset(object, offset, field); + return fieldOffset == 0 ? defaultValue : read(object, offset + fieldOffset); + } + + /// Read the value of the given [field] in the given [object]. + T? vTableGetNullable(BufferContext object, int offset, int field) { + int fieldOffset = _vTableFieldOffset(object, offset, field); + return fieldOffset == 0 ? null : read(object, offset + fieldOffset); + } + + int _vTableFieldOffset(BufferContext object, int offset, int field) { int vTableSOffset = object._getInt32(offset); int vTableOffset = offset - vTableSOffset; int vTableSize = object._getUint16(vTableOffset); - int vTableFieldOffset = field; - if (vTableFieldOffset < vTableSize) { - int fieldOffsetInObject = - object._getUint16(vTableOffset + vTableFieldOffset); - if (fieldOffsetInObject != 0) { - return read(object, offset + fieldOffsetInObject); - } - } - return defaultValue; + if (field >= vTableSize) return 0; + return object._getUint16(vTableOffset + field); } } @@ -1101,20 +1103,20 @@ class _FbFloat32List extends _FbList<double> { class _FbGenericList<E> extends _FbList<E> { final Reader<E> elementReader; - List<E> _items; + List<E?>? _items; _FbGenericList(this.elementReader, BufferContext bp, int offset) : super(bp, offset); @override E operator [](int i) { - _items ??= new List<E>(length); - E item = _items[i]; + _items ??= List<E?>.filled(length, null); + E? item = _items![i]; if (item == null) { item = elementReader.read(bc, offset + 4 + elementReader.size * i); - _items[i] = item; + _items![i] = item; } - return item; + return item!; } } @@ -1122,14 +1124,14 @@ class _FbGenericList<E> extends _FbList<E> { abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> { final BufferContext bc; final int offset; - int _length; + int? _length; _FbList(this.bc, this.offset); @override int get length { _length ??= bc._getUint32(offset); - return _length; + return _length!; } @override @@ -1185,15 +1187,15 @@ class _FbBoolList extends _FbList<bool> { class _VTable { static const int _metadataLength = 4; - final List<int> fieldTails = <int>[]; - final List<int> fieldOffsets = <int>[]; + final fieldTails = <int?>[]; + final fieldOffsets = <int>[]; /// The size of the table that uses this VTable. - int tableSize; + int tableSize = 0; - /// The tail of this VTable. It is used to share the same VTable between + /// The tail of this VTable. It is used to share the same VTable between /// multiple tables of identical structure. - int tail; + int tail = 0; int get _vTableSize => numOfUint16 * _sizeofUint16; @@ -1209,8 +1211,7 @@ class _VTable { bool _offsetsMatch(int vt2Start, ByteData buf) { for (int i = 0; i < fieldOffsets.length; i++) { if (fieldOffsets[i] != - buf.getUint16( - vt2Start + _metadataLength + (2 * i), Endian.little)) { + buf.getUint16(vt2Start + _metadataLength + (2 * i), Endian.little)) { return false; } } @@ -1220,7 +1221,7 @@ class _VTable { /// Fill the [fieldOffsets] field. void computeFieldOffsets(int tableTail) { assert(fieldOffsets.isEmpty); - for (int fieldTail in fieldTails) { + for (int? fieldTail in fieldTails) { int fieldOffset = fieldTail == null ? 0 : tableTail - fieldTail; fieldOffsets.add(fieldOffset); } diff --git a/dart/lib/src/builder.dart b/dart/lib/src/builder.dart index 5ce46dcb..27c0184a 100644 --- a/dart/lib/src/builder.dart +++ b/dart/lib/src/builder.dart @@ -6,37 +6,26 @@ import 'types.dart'; /// The main builder class for creation of a FlexBuffer. class Builder { ByteData _buffer; - List<_StackValue> _stack; - List<_StackPointer> _stackPointers; - int _offset; - bool _finished; - Map<String, _StackValue> _stringCache; - Map<String, _StackValue> _keyCache; - Map<_KeysHash, _StackValue> _keyVectorCache; - Map<int, _StackValue> _indirectIntCache; - Map<double, _StackValue> _indirectDoubleCache; + List<_StackValue> _stack = []; + List<_StackPointer> _stackPointers = []; + int _offset = 0; + bool _finished = false; + Map<String, _StackValue> _stringCache = {}; + Map<String, _StackValue> _keyCache = {}; + Map<_KeysHash, _StackValue> _keyVectorCache = {}; + Map<int, _StackValue> _indirectIntCache = {}; + Map<double, _StackValue> _indirectDoubleCache = {}; /// Instantiate the builder if you intent to gradually build up the buffer by calling /// add... methods and calling [finish] to receive the the resulting byte array. /// /// The default size of internal buffer is set to 2048. Provide a different value in order to avoid buffer copies. - Builder({int size = 2048}) { - _buffer = ByteData(size); - _stack = []; - _stackPointers = []; - _offset = 0; - _finished = false; - _stringCache = {}; - _keyCache = {}; - _keyVectorCache = {}; - _indirectIntCache = {}; - _indirectDoubleCache = {}; - } + Builder({int size = 2048}) : _buffer = ByteData(size) {} /// Use this method in order to turn an object into a FlexBuffer directly. /// /// Use the manual instantiation of the [Builder] and gradual addition of values, if performance is more important than convenience. - static ByteBuffer buildFromObject(Object value) { + static ByteBuffer buildFromObject(Object? value) { final builder = Builder(); builder._add(value); final buffer = builder.finish(); @@ -45,7 +34,7 @@ class Builder { return byteData.buffer; } - void _add(Object value) { + void _add(Object? value) { if (value == null) { addNull(); } else if (value is bool) { @@ -106,7 +95,7 @@ class Builder { void addString(String value) { _integrityCheckOnValueAddition(); if (_stringCache.containsKey(value)) { - _stack.add(_stringCache[value]); + _stack.add(_stringCache[value]!); return; } final utf8String = utf8.encode(value); @@ -118,7 +107,8 @@ class Builder { final newOffset = _newOffset(length + 1); _pushBuffer(utf8String); _offset = newOffset; - final stackValue = _StackValue.WithOffset(stringOffset, ValueType.String, bitWidth); + final stackValue = + _StackValue.WithOffset(stringOffset, ValueType.String, bitWidth); _stack.add(stackValue); _stringCache[value] = stackValue; } @@ -129,7 +119,7 @@ class Builder { void addKey(String value) { _integrityCheckOnKeyAddition(); if (_keyCache.containsKey(value)) { - _stack.add(_keyCache[value]); + _stack.add(_keyCache[value]!); return; } final utf8String = utf8.encode(value); @@ -138,7 +128,8 @@ class Builder { final newOffset = _newOffset(length + 1); _pushBuffer(utf8String); _offset = newOffset; - final stackValue = _StackValue.WithOffset(keyOffset, ValueType.Key, BitWidth.width8); + final stackValue = + _StackValue.WithOffset(keyOffset, ValueType.Key, BitWidth.width8); _stack.add(stackValue); _keyCache[value] = stackValue; } @@ -156,7 +147,8 @@ class Builder { final newOffset = _newOffset(length); _pushBuffer(value.asUint8List()); _offset = newOffset; - final stackValue = _StackValue.WithOffset(blobOffset, ValueType.Blob, bitWidth); + final stackValue = + _StackValue.WithOffset(blobOffset, ValueType.Blob, bitWidth); _stack.add(stackValue); } @@ -169,7 +161,7 @@ class Builder { void addIntIndirectly(int value, {bool cache = false}) { _integrityCheckOnValueAddition(); if (_indirectIntCache.containsKey(value)) { - _stack.add(_indirectIntCache[value]); + _stack.add(_indirectIntCache[value]!); return; } final stackValue = _StackValue.WithInt(value); @@ -177,7 +169,8 @@ class Builder { final newOffset = _newOffset(byteWidth); final valueOffset = _offset; _pushBuffer(stackValue.asU8List(stackValue.width)); - final stackOffset = _StackValue.WithOffset(valueOffset, ValueType.IndirectInt, stackValue.width); + final stackOffset = _StackValue.WithOffset( + valueOffset, ValueType.IndirectInt, stackValue.width); _stack.add(stackOffset); _offset = newOffset; if (cache) { @@ -193,7 +186,7 @@ class Builder { void addDoubleIndirectly(double value, {bool cache = false}) { _integrityCheckOnValueAddition(); if (cache && _indirectDoubleCache.containsKey(value)) { - _stack.add(_indirectDoubleCache[value]); + _stack.add(_indirectDoubleCache[value]!); return; } final stackValue = _StackValue.WithDouble(value); @@ -201,7 +194,8 @@ class Builder { final newOffset = _newOffset(byteWidth); final valueOffset = _offset; _pushBuffer(stackValue.asU8List(stackValue.width)); - final stackOffset = _StackValue.WithOffset(valueOffset, ValueType.IndirectFloat, stackValue.width); + final stackOffset = _StackValue.WithOffset( + valueOffset, ValueType.IndirectFloat, stackValue.width); _stack.add(stackOffset); _offset = newOffset; if (cache) { @@ -258,8 +252,10 @@ class Builder { tmp._offset = _offset; tmp._stack = List.from(_stack); tmp._stackPointers = List.from(_stackPointers); - tmp._buffer.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List(0, _offset)); - for (var i = 0; i < tmp._stackPointers.length; i++){ + tmp._buffer.buffer + .asUint8List() + .setAll(0, _buffer.buffer.asUint8List(0, _offset)); + for (var i = 0; i < tmp._stackPointers.length; i++) { tmp.end(); } final buffer = tmp.finish(); @@ -267,14 +263,15 @@ class Builder { bd.buffer.asUint8List().setAll(0, buffer); return bd.buffer; } - + void _integrityCheckOnValueAddition() { if (_finished) { throw StateError('Adding values after finish is prohibited'); } if (_stackPointers.isNotEmpty && _stackPointers.last.isVector == false) { if (_stack.last.type != ValueType.Key) { - throw StateError('Adding value to a map before adding a key is prohibited'); + throw StateError( + 'Adding value to a map before adding a key is prohibited'); } } } @@ -290,7 +287,8 @@ class Builder { void _finish() { if (_stack.length != 1) { - throw StateError('Stack has to be exactly 1, but is ${_stack.length}. You have to end all started vectors and maps, before calling [finish]'); + throw StateError( + 'Stack has to be exactly 1, but is ${_stack.length}. You have to end all started vectors and maps, before calling [finish]'); } final value = _stack[0]; final byteWidth = _align(value.elementWidth(_offset, 0)); @@ -299,8 +297,9 @@ class Builder { _writeUInt(byteWidth, 1); _finished = true; } - - _StackValue _createVector(int start, int vecLength, int step, [_StackValue keys]) { + + _StackValue _createVector(int start, int vecLength, int step, + [_StackValue? keys]) { var bitWidth = BitWidthUtil.uwidth(vecLength); var prefixElements = 1; if (keys != null) { @@ -327,7 +326,9 @@ class Builder { } } final byteWidth = _align(bitWidth); - final fix = typed & ValueTypeUtils.isNumber(vectorType) && vecLength >= 2 && vecLength <= 4; + final fix = typed & ValueTypeUtils.isNumber(vectorType) && + vecLength >= 2 && + vecLength <= 4; if (keys != null) { _writeStackValue(keys, byteWidth); _writeUInt(1 << keys.width.index, byteWidth); @@ -348,7 +349,8 @@ class Builder { return _StackValue.WithOffset(vecOffset, ValueType.Map, bitWidth); } if (typed) { - final vType = ValueTypeUtils.toTypedVector(vectorType, fix ? vecLength : 0); + final vType = + ValueTypeUtils.toTypedVector(vectorType, fix ? vecLength : 0); return _StackValue.WithOffset(vecOffset, vType, bitWidth); } return _StackValue.WithOffset(vecOffset, ValueType.Vector, bitWidth); @@ -363,12 +365,13 @@ class Builder { void _sortKeysAndEndMap(_StackPointer pointer) { if (((_stack.length - pointer.stackPosition) & 1) == 1) { - throw StateError('The stack needs to hold key value pairs (even number of elements). Check if you combined [addKey] with add... method calls properly.'); + throw StateError( + 'The stack needs to hold key value pairs (even number of elements). Check if you combined [addKey] with add... method calls properly.'); } var sorted = true; for (var i = pointer.stackPosition; i < _stack.length - 2; i += 2) { - if (_shouldFlip(_stack[i], _stack[i+2])) { + if (_shouldFlip(_stack[i], _stack[i + 2])) { sorted = false; break; } @@ -394,12 +397,12 @@ class Builder { } _endMap(pointer); } - + void _endMap(_StackPointer pointer) { final vecLength = (_stack.length - pointer.stackPosition) >> 1; final offsets = <int>[]; for (var i = pointer.stackPosition; i < _stack.length; i += 2) { - offsets.add(_stack[i].offset); + offsets.add(_stack[i].offset!); } final keysHash = _KeysHash(offsets); var keysStackValue; @@ -409,21 +412,23 @@ class Builder { keysStackValue = _createVector(pointer.stackPosition, vecLength, 2); _keyVectorCache[keysHash] = keysStackValue; } - final vec = _createVector(pointer.stackPosition + 1, vecLength, 2, keysStackValue); + final vec = + _createVector(pointer.stackPosition + 1, vecLength, 2, keysStackValue); _stack.removeRange(pointer.stackPosition, _stack.length); _stack.add(vec); } bool _shouldFlip(_StackValue v1, _StackValue v2) { if (v1.type != ValueType.Key || v2.type != ValueType.Key) { - throw StateError('Stack values are not keys $v1 | $v2. Check if you combined [addKey] with add... method calls properly.'); + throw StateError( + 'Stack values are not keys $v1 | $v2. Check if you combined [addKey] with add... method calls properly.'); } var c1, c2; var index = 0; do { - c1 = _buffer.getUint8(v1.offset + index); - c2 = _buffer.getUint8(v2.offset + index); + c1 = _buffer.getUint8(v1.offset! + index); + c2 = _buffer.getUint8(v2.offset! + index); if (c2 < c1) return true; if (c1 < c2) return false; index += 1; @@ -440,11 +445,12 @@ class Builder { void _writeStackValue(_StackValue value, int byteWidth) { final newOffset = _newOffset(byteWidth); if (value.isOffset) { - final relativeOffset = _offset - value.offset; + final relativeOffset = _offset - value.offset!; if (byteWidth == 8 || relativeOffset < (1 << (byteWidth * 8))) { _writeUInt(relativeOffset, byteWidth); } else { - throw StateError('Unexpected size $byteWidth. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new'); + throw StateError( + 'Unexpected size $byteWidth. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new'); } } else { _pushBuffer(value.asU8List(BitWidthUtil.fromByteWidth(byteWidth))); @@ -467,16 +473,13 @@ class Builder { } if (prevSize < size) { final newBuf = ByteData(size); - newBuf.buffer - .asUint8List() - .setAll(0, _buffer.buffer.asUint8List()); + newBuf.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List()); } return newOffset; } void _pushInt(int value, BitWidth width) { switch (width) { - case BitWidth.width8: _buffer.setInt8(_offset, value); break; @@ -494,7 +497,6 @@ class Builder { void _pushUInt(int value, BitWidth width) { switch (width) { - case BitWidth.width8: _buffer.setUint8(_offset, value); break; @@ -516,37 +518,39 @@ class Builder { } class _StackValue { - Object _value; - int _offset; + late Object _value; + int? _offset; ValueType _type; BitWidth _width; - _StackValue.WithNull() { - _type = ValueType.Null; - _width = BitWidth.width8; - } - _StackValue.WithInt(int value) { - _type = value != null ? ValueType.Int : ValueType.Null; - _width = BitWidthUtil.width(value); - _value = value; - } - _StackValue.WithBool(bool value) { - _type = value != null ? ValueType.Bool : ValueType.Null; - _width = BitWidth.width8; - _value = value; - } - _StackValue.WithDouble(double value) { - _type = value != null ? ValueType.Float : ValueType.Null; - _width = BitWidthUtil.width(value); - _value = value; - } - _StackValue.WithOffset(int value, ValueType type, BitWidth width) { - _offset = value; - _type = type; - _width = width; - } + + _StackValue.WithNull() + : _type = ValueType.Null, + _width = BitWidth.width8 {} + + _StackValue.WithInt(int value) + : _type = ValueType.Int, + _width = BitWidthUtil.width(value), + _value = value {} + + _StackValue.WithBool(bool value) + : _type = ValueType.Bool, + _width = BitWidth.width8, + _value = value {} + + _StackValue.WithDouble(double value) + : _type = ValueType.Float, + _width = BitWidthUtil.width(value), + _value = value {} + + _StackValue.WithOffset(int value, ValueType type, BitWidth width) + : _offset = value, + _type = type, + _width = width {} BitWidth storedWidth({BitWidth width = BitWidth.width8}) { - return ValueTypeUtils.isInline(_type) ? BitWidthUtil.max(_width, width) : _width; + return ValueTypeUtils.isInline(_type) + ? BitWidthUtil.max(_width, width) + : _width; } int storedPackedType({BitWidth width = BitWidth.width8}) { @@ -555,16 +559,18 @@ class _StackValue { BitWidth elementWidth(int size, int index) { if (ValueTypeUtils.isInline(_type)) return _width; - for(var i = 0; i < 4; i++) { + final offset = offsetLoc - _offset!; + for (var i = 0; i < 4; i++) { final width = 1 << i; - final offsetLoc = size + BitWidthUtil.paddingSize(size, width) + index * width; - final offset = offsetLoc - _offset; + final offsetLoc = + size + BitWidthUtil.paddingSize(size, width) + index * width; final bitWidth = BitWidthUtil.uwidth(offset); if (1 << bitWidth.index == width) { return bitWidth; } } - throw StateError('Element is of unknown. Size: $size at index: $index. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new'); + throw StateError( + 'Element is of unknown. Size: $size at index: $index. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new'); } List<int> asU8List(BitWidth width) { @@ -572,30 +578,30 @@ class _StackValue { if (_type == ValueType.Float) { if (width == BitWidth.width32) { final result = ByteData(4); - result.setFloat32(0, _value, Endian.little); + result.setFloat32(0, _value as double, Endian.little); return result.buffer.asUint8List(); } else { final result = ByteData(8); - result.setFloat64(0, _value, Endian.little); + result.setFloat64(0, _value as double, Endian.little); return result.buffer.asUint8List(); } } else { - switch(width) { + switch (width) { case BitWidth.width8: final result = ByteData(1); - result.setInt8(0, _value); + result.setInt8(0, _value as int); return result.buffer.asUint8List(); case BitWidth.width16: final result = ByteData(2); - result.setInt16(0, _value, Endian.little); + result.setInt16(0, _value as int, Endian.little); return result.buffer.asUint8List(); case BitWidth.width32: final result = ByteData(4); - result.setInt32(0, _value, Endian.little); + result.setInt32(0, _value as int, Endian.little); return result.buffer.asUint8List(); case BitWidth.width64: final result = ByteData(8); - result.setInt64(0, _value, Endian.little); + result.setInt64(0, _value as int, Endian.little); return result.buffer.asUint8List(); } } @@ -607,11 +613,12 @@ class _StackValue { } if (_type == ValueType.Bool) { final result = ByteData(1); - result.setInt8(0, _value ? 1 : 0); + result.setInt8(0, _value as bool ? 1 : 0); return result.buffer.asUint8List(); } - throw StateError('Unexpected type: $_type. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new'); + throw StateError( + 'Unexpected type: $_type. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new'); } ValueType get type { @@ -625,7 +632,8 @@ class _StackValue { bool get isOffset { return !ValueTypeUtils.isInline(_type); } - int get offset => _offset; + + int? get offset => _offset; bool get isFloat32 { return _type == ValueType.Float && _width == BitWidth.width32; diff --git a/dart/lib/src/reference.dart b/dart/lib/src/reference.dart index 3954f068..c2956e30 100644 --- a/dart/lib/src/reference.dart +++ b/dart/lib/src/reference.dart @@ -11,14 +11,15 @@ class Reference { final int _offset; final BitWidth _parentWidth; final String _path; - int _byteWidth; - ValueType _valueType; - int _length; + final int _byteWidth; + final ValueType _valueType; + int? _length; - Reference._(this._buffer, this._offset, this._parentWidth, int packedType, this._path) { - _byteWidth = 1 << (packedType & 3); - _valueType = ValueTypeUtils.fromInt(packedType >> 2); - } + Reference._( + this._buffer, this._offset, this._parentWidth, int packedType, this._path, + [int? byteWidth, ValueType? valueType]) + : _byteWidth = byteWidth ?? 1 << (packedType & 3), + _valueType = valueType ?? ValueTypeUtils.fromInt(packedType >> 2) {} /// Use this method to access the root value of a FlexBuffer. static Reference fromBuffer(ByteBuffer buffer) { @@ -30,31 +31,44 @@ class Reference { final byteWidth = byteData.getUint8(len - 1); final packedType = byteData.getUint8(len - 2); final offset = len - byteWidth - 2; - return Reference._(ByteData.view(buffer), offset, BitWidthUtil.fromByteWidth(byteWidth), packedType, "/"); + return Reference._(ByteData.view(buffer), offset, + BitWidthUtil.fromByteWidth(byteWidth), packedType, "/"); } /// Returns true if the underlying value is null. bool get isNull => _valueType == ValueType.Null; + /// Returns true if the underlying value can be represented as [num]. - bool get isNum => ValueTypeUtils.isNumber(_valueType) || ValueTypeUtils.isIndirectNumber(_valueType); + bool get isNum => + ValueTypeUtils.isNumber(_valueType) || + ValueTypeUtils.isIndirectNumber(_valueType); + /// Returns true if the underlying value was encoded as a float (direct or indirect). - bool get isDouble => _valueType == ValueType.Float || _valueType == ValueType.IndirectFloat; + bool get isDouble => + _valueType == ValueType.Float || _valueType == ValueType.IndirectFloat; + /// Returns true if the underlying value was encoded as an int or uint (direct or indirect). bool get isInt => isNum && !isDouble; + /// Returns true if the underlying value was encoded as a string or a key. - bool get isString => _valueType == ValueType.String || _valueType == ValueType.Key; + bool get isString => + _valueType == ValueType.String || _valueType == ValueType.Key; + /// Returns true if the underlying value was encoded as a bool. bool get isBool => _valueType == ValueType.Bool; + /// Returns true if the underlying value was encoded as a blob. bool get isBlob => _valueType == ValueType.Blob; + /// Returns true if the underlying value points to a vector. bool get isVector => ValueTypeUtils.isAVector(_valueType); + /// Returns true if the underlying value points to a map. bool get isMap => _valueType == ValueType.Map; /// If this [isBool], returns the bool value. Otherwise, returns null. - bool get boolValue { - if(_valueType == ValueType.Bool) { + bool? get boolValue { + if (_valueType == ValueType.Bool) { return _readInt(_offset, _parentWidth) != 0; } return null; @@ -63,7 +77,7 @@ class Reference { /// Returns an [int], if the underlying value can be represented as an int. /// /// Otherwise returns [null]. - int get intValue { + int? get intValue { if (_valueType == ValueType.Int) { return _readInt(_offset, _parentWidth); } @@ -82,7 +96,7 @@ class Reference { /// Returns [double], if the underlying value [isDouble]. /// /// Otherwise returns [null]. - double get doubleValue { + double? get doubleValue { if (_valueType == ValueType.Float) { return _readFloat(_offset, _parentWidth); } @@ -95,12 +109,12 @@ class Reference { /// Returns [num], if the underlying value is numeric, be it int uint, or float (direct or indirect). /// /// Otherwise returns [null]. - num get numValue => doubleValue ?? intValue; + num? get numValue => doubleValue ?? intValue; /// Returns [String] value or null otherwise. - /// - /// This method performers a utf8 decoding, as FlexBuffers format stores strings in utf8 encoding. - String get stringValue { + /// + /// This method performers a utf8 decoding, as FlexBuffers format stores strings in utf8 encoding. + String? get stringValue { if (_valueType == ValueType.String || _valueType == ValueType.Key) { return utf8.decode(_buffer.buffer.asUint8List(_indirect, length)); } @@ -108,7 +122,7 @@ class Reference { } /// Returns [Uint8List] value or null otherwise. - Uint8List get blobValue { + Uint8List? get blobValue { if (_valueType == ValueType.Blob) { return _buffer.buffer.asUint8List(_indirect, length); } @@ -122,22 +136,31 @@ class Reference { Reference operator [](Object key) { if (key is int && ValueTypeUtils.isAVector(_valueType)) { final index = key; - if(index >= length || index < 0) { - throw ArgumentError('Key: [$key] is not applicable on: $_path of: $_valueType length: $length'); + if (index >= length || index < 0) { + throw ArgumentError( + 'Key: [$key] is not applicable on: $_path of: $_valueType length: $length'); } final elementOffset = _indirect + index * _byteWidth; - final reference = Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), 0, "$_path[$index]"); - reference._byteWidth = 1; + int packedType = 0; + int? byteWidth; + ValueType? valueType; if (ValueTypeUtils.isTypedVector(_valueType)) { - reference._valueType = ValueTypeUtils.typedVectorElementType(_valueType); - return reference; - } - if(ValueTypeUtils.isFixedTypedVector(_valueType)) { - reference._valueType = ValueTypeUtils.fixedTypedVectorElementType(_valueType); - return reference; + byteWidth = 1; + valueType = ValueTypeUtils.typedVectorElementType(_valueType); + } else if (ValueTypeUtils.isFixedTypedVector(_valueType)) { + byteWidth = 1; + valueType = ValueTypeUtils.fixedTypedVectorElementType(_valueType); + } else { + packedType = _buffer.getUint8(_indirect + length * _byteWidth + index); } - final packedType = _buffer.getUint8(_indirect + length * _byteWidth + index); - return Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path[$index]"); + return Reference._( + _buffer, + elementOffset, + BitWidthUtil.fromByteWidth(_byteWidth), + packedType, + "$_path[$index]", + byteWidth, + valueType); } if (key is String && _valueType == ValueType.Map) { final index = _keyIndex(key); @@ -145,13 +168,14 @@ class Reference { return _valueForIndexWithKey(index, key); } } - throw ArgumentError('Key: [$key] is not applicable on: $_path of: $_valueType'); + throw ArgumentError( + 'Key: [$key] is not applicable on: $_path of: $_valueType'); } /// Get an iterable if the underlying flexBuffer value is a vector. /// Otherwise throws an exception. Iterable<Reference> get vectorIterable { - if(isVector == false) { + if (isVector == false) { throw UnsupportedError('Value is not a vector. It is: $_valueType'); } return _VectorIterator(this); @@ -160,7 +184,7 @@ class Reference { /// Get an iterable for keys if the underlying flexBuffer value is a map. /// Otherwise throws an exception. Iterable<String> get mapKeyIterable { - if(isMap == false) { + if (isMap == false) { throw UnsupportedError('Value is not a map. It is: $_valueType'); } return _MapKeyIterator(this); @@ -169,7 +193,7 @@ class Reference { /// Get an iterable for values if the underlying flexBuffer value is a map. /// Otherwise throws an exception. Iterable<Reference> get mapValueIterable { - if(isMap == false) { + if (isMap == false) { throw UnsupportedError('Value is not a map. It is: $_valueType'); } return _MapValueIterator(this); @@ -181,59 +205,62 @@ class Reference { /// If the underlying value is a vector, or map, the length reflects number of elements / element pairs. /// If the values is a string or a blob, the length reflects a number of bytes the value occupies (strings are encoded in utf8 format). int get length { - if (_length != null) { - return _length; - } - // needs to be checked before more generic isAVector - if(ValueTypeUtils.isFixedTypedVector(_valueType)) { - _length = ValueTypeUtils.fixedTypedVectorElementSize(_valueType); - } else if(_valueType == ValueType.Blob || ValueTypeUtils.isAVector(_valueType) || _valueType == ValueType.Map){ - _length = _readUInt(_indirect - _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth)); - } else if (_valueType == ValueType.Null) { - _length = 0; - } else if (_valueType == ValueType.String) { - final indirect = _indirect; - var size_byte_width = _byteWidth; - var size = _readUInt(indirect - size_byte_width, BitWidthUtil.fromByteWidth(size_byte_width)); - while (_buffer.getInt8(indirect + size) != 0) { - size_byte_width <<= 1; - size = _readUInt(indirect - size_byte_width, BitWidthUtil.fromByteWidth(size_byte_width)); - } - _length = size; - } else if (_valueType == ValueType.Key) { - final indirect = _indirect; - var size = 1; - while (_buffer.getInt8(indirect + size) != 0) { - size += 1; + if (_length == null) { + // needs to be checked before more generic isAVector + if (ValueTypeUtils.isFixedTypedVector(_valueType)) { + _length = ValueTypeUtils.fixedTypedVectorElementSize(_valueType); + } else if (_valueType == ValueType.Blob || + ValueTypeUtils.isAVector(_valueType) || + _valueType == ValueType.Map) { + _length = _readUInt( + _indirect - _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth)); + } else if (_valueType == ValueType.Null) { + _length = 0; + } else if (_valueType == ValueType.String) { + final indirect = _indirect; + var size_byte_width = _byteWidth; + var size = _readUInt(indirect - size_byte_width, + BitWidthUtil.fromByteWidth(size_byte_width)); + while (_buffer.getInt8(indirect + size) != 0) { + size_byte_width <<= 1; + size = _readUInt(indirect - size_byte_width, + BitWidthUtil.fromByteWidth(size_byte_width)); + } + _length = size; + } else if (_valueType == ValueType.Key) { + final indirect = _indirect; + var size = 1; + while (_buffer.getInt8(indirect + size) != 0) { + size += 1; + } + _length = size; + } else { + _length = 1; } - _length = size; - } else { - _length = 1; } - return _length; + return _length!; } - /// Returns a minified JSON representation of the underlying FlexBuffer value. /// /// This method involves materializing the entire object tree, which may be /// expensive. It is more efficient to work with [Reference] and access only the needed data. /// Blob values are represented as base64 encoded string. String get json { - if(_valueType == ValueType.Bool) { - return boolValue ? 'true' : 'false'; + if (_valueType == ValueType.Bool) { + return boolValue! ? 'true' : 'false'; } if (_valueType == ValueType.Null) { return 'null'; } - if(ValueTypeUtils.isNumber(_valueType)) { + if (ValueTypeUtils.isNumber(_valueType)) { return jsonEncode(numValue); } if (_valueType == ValueType.String) { return jsonEncode(stringValue); } if (_valueType == ValueType.Blob) { - return jsonEncode(base64Encode(blobValue)); + return jsonEncode(base64Encode(blobValue!)); } if (ValueTypeUtils.isAVector(_valueType)) { final result = StringBuffer(); @@ -261,7 +288,8 @@ class Reference { result.write('}'); return result.toString(); } - throw UnsupportedError('Type: $_valueType is not supported for JSON conversion'); + throw UnsupportedError( + 'Type: $_valueType is not supported for JSON conversion'); } /// Computes the indirect offset of the value. @@ -316,16 +344,20 @@ class Reference { } void _validateOffset(int offset, BitWidth width) { - if (_offset < 0 || _buffer.lengthInBytes <= offset + width.index || offset & (BitWidthUtil.toByteWidth(width) - 1) != 0) { + if (_offset < 0 || + _buffer.lengthInBytes <= offset + width.index || + offset & (BitWidthUtil.toByteWidth(width) - 1) != 0) { throw StateError('Bad offset: $offset, width: $width'); } } - int _keyIndex(String key) { + int? _keyIndex(String key) { final input = utf8.encode(key); final keysVectorOffset = _indirect - _byteWidth * 3; - final indirectOffset = keysVectorOffset - _readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth)); - final byteWidth = _readUInt(keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth)); + final indirectOffset = keysVectorOffset - + _readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth)); + final byteWidth = _readUInt( + keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth)); var low = 0; var high = length - 1; while (low <= high) { @@ -341,9 +373,11 @@ class Reference { return null; } - int _diffKeys(List<int> input, int index, int indirect_offset, int byteWidth) { + int _diffKeys( + List<int> input, int index, int indirect_offset, int byteWidth) { final keyOffset = indirect_offset + index * byteWidth; - final keyIndirectOffset = keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth)); + final keyIndirectOffset = + keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth)); for (var i = 0; i < input.length; i++) { final dif = input[i] - _buffer.getUint8(keyIndirectOffset + i); if (dif != 0) { @@ -357,38 +391,42 @@ class Reference { final indirect = _indirect; final elementOffset = indirect + index * _byteWidth; final packedType = _buffer.getUint8(indirect + length * _byteWidth + index); - return Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/$key"); + return Reference._(_buffer, elementOffset, + BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/$key"); } Reference _valueForIndex(int index) { final indirect = _indirect; final elementOffset = indirect + index * _byteWidth; final packedType = _buffer.getUint8(indirect + length * _byteWidth + index); - return Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/[$index]"); + return Reference._(_buffer, elementOffset, + BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/[$index]"); } String _keyForIndex(int index) { final keysVectorOffset = _indirect - _byteWidth * 3; - final indirectOffset = keysVectorOffset - _readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth)); - final byteWidth = _readUInt(keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth)); + final indirectOffset = keysVectorOffset - + _readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth)); + final byteWidth = _readUInt( + keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth)); final keyOffset = indirectOffset + index * byteWidth; - final keyIndirectOffset = keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth)); + final keyIndirectOffset = + keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth)); var length = 0; while (_buffer.getUint8(keyIndirectOffset + length) != 0) { length += 1; } return utf8.decode(_buffer.buffer.asUint8List(keyIndirectOffset, length)); } - } -class _VectorIterator with IterableMixin<Reference> implements Iterator<Reference> { +class _VectorIterator + with IterableMixin<Reference> + implements Iterator<Reference> { final Reference _vector; - int index; + int index = -1; - _VectorIterator(this._vector) { - index = -1; - } + _VectorIterator(this._vector); @override Reference get current => _vector[index]; @@ -405,11 +443,9 @@ class _VectorIterator with IterableMixin<Reference> implements Iterator<Referenc class _MapKeyIterator with IterableMixin<String> implements Iterator<String> { final Reference _map; - int index; + int index = -1; - _MapKeyIterator(this._map) { - index = -1; - } + _MapKeyIterator(this._map); @override String get current => _map._keyForIndex(index); @@ -424,13 +460,13 @@ class _MapKeyIterator with IterableMixin<String> implements Iterator<String> { Iterator<String> get iterator => this; } -class _MapValueIterator with IterableMixin<Reference> implements Iterator<Reference> { +class _MapValueIterator + with IterableMixin<Reference> + implements Iterator<Reference> { final Reference _map; - int index; + int index = -1; - _MapValueIterator(this._map) { - index = -1; - } + _MapValueIterator(this._map); @override Reference get current => _map._valueForIndex(index); diff --git a/dart/lib/src/types.dart b/dart/lib/src/types.dart index 8aed2725..bc9bb3ac 100644 --- a/dart/lib/src/types.dart +++ b/dart/lib/src/types.dart @@ -1,17 +1,13 @@ import 'dart:typed_data'; /// Represents the number of bits a value occupies. -enum BitWidth { - width8, - width16, - width32, - width64 -} +enum BitWidth { width8, width16, width32, width64 } class BitWidthUtil { static int toByteWidth(BitWidth self) { return 1 << self.index; } + static BitWidth width(num value) { if (value.toInt() == value) { var v = value.toInt().abs(); @@ -20,8 +16,11 @@ class BitWidthUtil { if (v >> 31 == 0) return BitWidth.width32; return BitWidth.width64; } - return value == _toF32(value) ? BitWidth.width32 : BitWidth.width64; + return value == _toF32(value as double) + ? BitWidth.width32 + : BitWidth.width64; } + static BitWidth uwidth(num value) { if (value.toInt() == value) { var v = value.toInt().abs(); @@ -30,8 +29,11 @@ class BitWidthUtil { if (v >> 32 == 0) return BitWidth.width32; return BitWidth.width64; } - return value == _toF32(value) ? BitWidth.width32 : BitWidth.width64; + return value == _toF32(value as double) + ? BitWidth.width32 + : BitWidth.width64; } + static BitWidth fromByteWidth(int value) { if (value == 1) { return BitWidth.width8; @@ -47,9 +49,11 @@ class BitWidthUtil { } throw Exception('Unexpected value ${value}'); } + static int paddingSize(int bufSize, int scalarSize) { return (~bufSize + 1) & (scalarSize - 1); } + static double _toF32(double value) { var bdata = ByteData(4); bdata.setFloat32(0, value); @@ -66,15 +70,36 @@ class BitWidthUtil { /// Represents all internal FlexBuffer types. enum ValueType { - Null, Int, UInt, Float, - Key, String, IndirectInt, IndirectUInt, IndirectFloat, - Map, Vector, VectorInt, VectorUInt, VectorFloat, VectorKey, - @Deprecated('VectorString is deprecated due to a flaw in the binary format (https://github.com/google/flatbuffers/issues/5627)') + Null, + Int, + UInt, + Float, + Key, + String, + IndirectInt, + IndirectUInt, + IndirectFloat, + Map, + Vector, + VectorInt, + VectorUInt, + VectorFloat, + VectorKey, + @Deprecated( + 'VectorString is deprecated due to a flaw in the binary format (https://github.com/google/flatbuffers/issues/5627)') VectorString, - VectorInt2, VectorUInt2, VectorFloat2, - VectorInt3, VectorUInt3, VectorFloat3, - VectorInt4, VectorUInt4, VectorFloat4, - Blob, Bool, VectorBool + VectorInt2, + VectorUInt2, + VectorFloat2, + VectorInt3, + VectorUInt3, + VectorFloat3, + VectorInt4, + VectorUInt4, + VectorFloat4, + Blob, + Bool, + VectorBool } class ValueTypeUtils { @@ -89,71 +114,70 @@ class ValueTypeUtils { } static bool isInline(ValueType self) { - return self == ValueType.Bool - || toInt(self) <= toInt(ValueType.Float); + return self == ValueType.Bool || toInt(self) <= toInt(ValueType.Float); } static bool isNumber(ValueType self) { - return toInt(self) >= toInt(ValueType.Int) - && toInt(self) <= toInt(ValueType.Float); + return toInt(self) >= toInt(ValueType.Int) && + toInt(self) <= toInt(ValueType.Float); } static bool isIndirectNumber(ValueType self) { - return toInt(self) >= toInt(ValueType.IndirectInt) - && toInt(self) <= toInt(ValueType.IndirectFloat); + return toInt(self) >= toInt(ValueType.IndirectInt) && + toInt(self) <= toInt(ValueType.IndirectFloat); } static bool isTypedVectorElement(ValueType self) { return self == ValueType.Bool || - ( - toInt(self) >= toInt(ValueType.Int) - && toInt(self) <= toInt(ValueType.String) - ); + (toInt(self) >= toInt(ValueType.Int) && + toInt(self) <= toInt(ValueType.String)); } static bool isTypedVector(ValueType self) { return self == ValueType.VectorBool || - ( - toInt(self) >= toInt(ValueType.VectorInt) - && toInt(self) <= toInt(ValueType.VectorString) - ); + (toInt(self) >= toInt(ValueType.VectorInt) && + toInt(self) <= toInt(ValueType.VectorString)); } static bool isFixedTypedVector(ValueType self) { - return ( - toInt(self) >= toInt(ValueType.VectorInt2) - && toInt(self) <= toInt(ValueType.VectorFloat4) - ); + return (toInt(self) >= toInt(ValueType.VectorInt2) && + toInt(self) <= toInt(ValueType.VectorFloat4)); } static bool isAVector(ValueType self) { - return ( - isTypedVector(self) || isFixedTypedVector(self) || self == ValueType.Vector - ); + return (isTypedVector(self) || + isFixedTypedVector(self) || + self == ValueType.Vector); } static ValueType toTypedVector(ValueType self, int length) { if (length == 0) { - return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt)); + return ValueTypeUtils.fromInt( + toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt)); } if (length == 2) { - return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt2)); + return ValueTypeUtils.fromInt( + toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt2)); } if (length == 3) { - return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt3)); + return ValueTypeUtils.fromInt( + toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt3)); } if (length == 4) { - return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt4)); + return ValueTypeUtils.fromInt( + toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt4)); } throw Exception('unexpected length ' + length.toString()); } static ValueType typedVectorElementType(ValueType self) { - return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.VectorInt) + toInt(ValueType.Int)); + return ValueTypeUtils.fromInt( + toInt(self) - toInt(ValueType.VectorInt) + toInt(ValueType.Int)); } static ValueType fixedTypedVectorElementType(ValueType self) { - return ValueTypeUtils.fromInt((toInt(self) - toInt(ValueType.VectorInt2)) % 3 + toInt(ValueType.Int)); + return ValueTypeUtils.fromInt( + (toInt(self) - toInt(ValueType.VectorInt2)) % 3 + toInt(ValueType.Int)); } static int fixedTypedVectorElementSize(ValueType self) { diff --git a/dart/pubspec.yaml b/dart/pubspec.yaml index c13d3ce9..38738f55 100644 --- a/dart/pubspec.yaml +++ b/dart/pubspec.yaml @@ -13,8 +13,8 @@ authors: homepage: https://github.com/google/flatbuffers documentation: https://google.github.io/flatbuffers/index.html dev_dependencies: - test: ^1.3.0 - test_reflective_loader: ^0.1.4 - path: ^1.5.1 + test: ^1.17.7 + test_reflective_loader: ^0.2.0 + path: ^1.8.0 environment: - sdk: '>=2.0.0-dev.28.0 <3.0.0'
\ No newline at end of file + sdk: '>=2.12.0 <3.0.0' diff --git a/dart/test/flat_buffers_test.dart b/dart/test/flat_buffers_test.dart index 0e8c7572..c2121a0f 100644 --- a/dart/test/flat_buffers_test.dart +++ b/dart/test/flat_buffers_test.dart @@ -39,26 +39,26 @@ class CheckOtherLangaugesData { expect(mon.hp, 80); expect(mon.mana, 150); expect(mon.name, 'MyMonster'); - expect(mon.pos.x, 1.0); - 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.test3.a, 5); - expect(mon.pos.test3.b, 6); - expect(mon.testType.value, example.AnyTypeId.Monster.value); + expect(mon.pos!.x, 1.0); + 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!.test3.a, 5); + expect(mon.pos!.test3.b, 6); + 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"); - expect(mon.inventory.length, 5); - expect(mon.inventory.reduce((cur, next) => cur + next), 10); - expect(mon.test4.length, 2); - expect( - mon.test4[0].a + mon.test4[0].b + mon.test4[1].a + mon.test4[1].b, 100); - expect(mon.testarrayofstring.length, 2); - expect(mon.testarrayofstring[0], "test1"); - expect(mon.testarrayofstring[1], "test2"); + expect(mon.inventory!.length, 5); + expect(mon.inventory!.reduce((cur, next) => cur + next), 10); + final test4 = mon.test4!; + expect(test4.length, 2); + expect(test4[0].a + test4[0].b + test4[1].a + test4[1].b, 100); + expect(mon.testarrayofstring!.length, 2); + expect(mon.testarrayofstring![0], "test1"); + expect(mon.testarrayofstring![1], "test2"); // this will fail if accessing any field fails. expect( @@ -68,7 +68,7 @@ class CheckOtherLangaugesData { 'mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], ' 'color: Color{value: 8}, testType: AnyTypeId{value: 1}, ' 'test: Monster{pos: null, mana: 150, hp: 100, name: Fred, ' - 'inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, ' + 'inventory: null, color: Color{value: 8}, testType: null, ' 'test: null, test4: null, testarrayofstring: null, ' 'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, ' 'testempty: null, testbool: false, testhashs32Fnv1: 0, ' @@ -82,14 +82,13 @@ class CheckOtherLangaugesData { 'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, ' 'coOwningReference: 0, vectorOfCoOwningReferences: null, ' 'nonOwningReference: 0, vectorOfNonOwningReferences: null, ' - 'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, ' - 'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, ' + 'anyUniqueType: null, anyUnique: null, anyAmbiguousType: null, ' 'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, ' 'testrequirednestedflatbuffer: null, scalarKeySortedTables: null}, ' '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: AnyTypeId{value: 0}, ' + 'inventory: null, color: Color{value: 8}, testType: null, ' 'test: null, test4: null, testarrayofstring: null, ' 'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, ' 'testempty: null, testbool: false, testhashs32Fnv1: 0, ' @@ -103,8 +102,7 @@ class CheckOtherLangaugesData { 'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, ' 'coOwningReference: 0, vectorOfCoOwningReferences: null, ' 'nonOwningReference: 0, vectorOfNonOwningReferences: null, ' - 'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, ' - 'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, ' + 'anyUniqueType: null, anyUnique: null, anyAmbiguousType: null, ' 'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, ' 'testrequirednestedflatbuffer: null, scalarKeySortedTables: null}, ' 'testnestedflatbuffer: null, testempty: null, testbool: true, ' @@ -126,8 +124,8 @@ class CheckOtherLangaugesData { 'vectorOfStrongReferrables: null, coOwningReference: 0, ' 'vectorOfCoOwningReferences: null, nonOwningReference: 0, ' 'vectorOfNonOwningReferences: null, ' - 'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, ' - 'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, ' + 'anyUniqueType: null, anyUnique: null, ' + 'anyAmbiguousType: null, ' 'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, ' 'testrequirednestedflatbuffer: null, scalarKeySortedTables: [Stat{id: ' 'miss, val: 0, count: 0}, Stat{id: hit, val: 10, count: 1}]}', @@ -137,7 +135,7 @@ class CheckOtherLangaugesData { @reflectiveTest class BuilderTest { - void test_monsterBuilder([Builder builder]) { + void test_monsterBuilder([Builder? builder]) { final fbBuilder = builder ?? new Builder(); final str = fbBuilder.writeString('MyMonster'); @@ -183,10 +181,10 @@ class BuilderTest { fbBuilder.finish(mon); } - void test_error_addInt32_withoutStartTable([Builder builder]) { + void test_error_addInt32_withoutStartTable([Builder? builder]) { builder ??= new Builder(); expect(() { - builder.addInt32(0, 0); + builder!.addInt32(0, 0); }, throwsStateError); } @@ -288,7 +286,7 @@ class BuilderTest { 20); } - void test_table_format([Builder builder]) { + void test_table_format([Builder? builder]) { Uint8List byteList; { builder ??= new Builder(initialSize: 0); @@ -326,8 +324,8 @@ class BuilderTest { List<int> byteList; { Builder builder = new Builder(initialSize: 0); - int latinStringOffset = builder.writeString(latinString); - int unicodeStringOffset = builder.writeString(unicodeString); + int? latinStringOffset = builder.writeString(latinString); + int? unicodeStringOffset = builder.writeString(unicodeString); builder.startTable(); builder.addOffset(0, latinStringOffset); builder.addOffset(1, unicodeStringOffset); @@ -337,17 +335,21 @@ class BuilderTest { // read and verify BufferContext buf = new BufferContext.fromBytes(byteList); int objectOffset = buf.derefObject(0); - expect(const StringReader().vTableGet(buf, objectOffset, indexToField(0)), + expect( + const StringReader() + .vTableGetNullable(buf, objectOffset, indexToField(0)), latinString); - expect(const StringReader().vTableGet(buf, objectOffset, indexToField(1)), + expect( + const StringReader() + .vTableGetNullable(buf, objectOffset, indexToField(1)), unicodeString); } - void test_table_types([Builder builder]) { + void test_table_types([Builder? builder]) { List<int> byteList; { builder ??= new Builder(initialSize: 0); - int stringOffset = builder.writeString('12345'); + int? stringOffset = builder.writeString('12345'); builder.startTable(); builder.addBool(0, true); builder.addInt8(1, 10); @@ -363,18 +365,32 @@ class BuilderTest { BufferContext buf = new BufferContext.fromBytes(byteList); int objectOffset = buf.derefObject(0); expect( - const BoolReader().vTableGet(buf, objectOffset, indexToField(0)), true); + const BoolReader() + .vTableGetNullable(buf, objectOffset, indexToField(0)), + true); expect( - const Int8Reader().vTableGet(buf, objectOffset, indexToField(1)), 10); + const Int8Reader() + .vTableGetNullable(buf, objectOffset, indexToField(1)), + 10); expect( - const Int32Reader().vTableGet(buf, objectOffset, indexToField(2)), 20); - expect(const StringReader().vTableGet(buf, objectOffset, indexToField(3)), + const Int32Reader() + .vTableGetNullable(buf, objectOffset, indexToField(2)), + 20); + expect( + const StringReader() + .vTableGetNullable(buf, objectOffset, indexToField(3)), '12345'); expect( - const Int32Reader().vTableGet(buf, objectOffset, indexToField(4)), 40); - expect(const Uint32Reader().vTableGet(buf, objectOffset, indexToField(5)), + const Int32Reader() + .vTableGetNullable(buf, objectOffset, indexToField(4)), + 40); + expect( + const Uint32Reader() + .vTableGetNullable(buf, objectOffset, indexToField(5)), 0x9ABCDEF0); - expect(const Uint8Reader().vTableGet(buf, objectOffset, indexToField(6)), + expect( + const Uint8Reader() + .vTableGetNullable(buf, objectOffset, indexToField(6)), 0x9A); } @@ -488,7 +504,7 @@ class BuilderTest { } } - void test_writeList_ofObjects([Builder builder]) { + void test_writeList_ofObjects([Builder? builder]) { List<int> byteList; { builder ??= new Builder(initialSize: 0); @@ -527,9 +543,9 @@ class BuilderTest { List<int> byteList; { Builder builder = new Builder(initialSize: 0); - int str1 = builder.writeString('12345'); - int str2 = builder.writeString('ABC'); - int offset = builder.writeList([str1, str2]); + int? str1 = builder.writeString('12345'); + int? str2 = builder.writeString('ABC'); + int offset = builder.writeList([str1!, str2!]); byteList = builder.finish(offset); } // read and verify @@ -541,12 +557,12 @@ class BuilderTest { expect(items, contains('ABC')); } - void test_writeList_ofStrings_inObject([Builder builder]) { + void test_writeList_ofStrings_inObject([Builder? builder]) { List<int> byteList; { builder ??= new Builder(initialSize: 0); int listOffset = builder.writeList( - [builder.writeString('12345'), builder.writeString('ABC')]); + [builder.writeString('12345')!, builder.writeString('ABC')!]); builder.startTable(); builder.addOffset(0, listOffset); int offset = builder.endTable(); @@ -555,7 +571,7 @@ class BuilderTest { // read and verify BufferContext buf = new BufferContext.fromBytes(byteList); StringListWrapperImpl reader = new StringListWrapperReader().read(buf, 0); - List<String> items = reader.items; + List<String>? items = reader.items; expect(items, hasLength(2)); expect(items, contains('12345')); expect(items, contains('ABC')); @@ -605,7 +621,7 @@ class BuilderTest { void test_reset() { // We'll run a selection of tests , reusing the builder between them. - final testCases = <void Function(Builder)>[ + final testCases = <void Function(Builder?)>[ test_monsterBuilder, test_error_addInt32_withoutStartTable, test_table_format, @@ -624,14 +640,14 @@ class BuilderTest { // print the order so failures are reproducible printOnFailure('Running reset() test cases in order: $indexes'); - Builder builder; + Builder? builder; indexes.forEach((index) { if (builder == null) { // Initial size small enough so at least one test case increases it. // On the other hand, it's large enough so that some test cases don't. builder = Builder(initialSize: 32); } else { - builder.reset(); + builder!.reset(); } testCases[index](builder); }); @@ -739,8 +755,8 @@ class StringListWrapperImpl { StringListWrapperImpl(this.bp, this.offset); - List<String> get items => const ListReader<String>(const StringReader()) - .vTableGet(bp, offset, indexToField(0)); + List<String>? get items => const ListReader<String>(const StringReader()) + .vTableGetNullable(bp, offset, indexToField(0)); } class StringListWrapperReader extends TableReader<StringListWrapperImpl> { diff --git a/dart/test/flex_builder_test.dart b/dart/test/flex_builder_test.dart index 5a1c1a8f..66e66384 100644 --- a/dart/test/flex_builder_test.dart +++ b/dart/test/flex_builder_test.dart @@ -58,18 +58,18 @@ void main() { { var flx = Builder(); flx.addString('hello 😱'); - expect(flx.finish(), [10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1]); + expect(flx.finish(), + [10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1]); } }); - test('build vector', (){ + test('build vector', () { { var flx = Builder() ..startVector() ..addInt(1) ..addInt(2) - ..end() - ; + ..end(); expect(flx.finish(), [1, 2, 2, 64, 1]); } { @@ -77,8 +77,7 @@ void main() { ..startVector() ..addInt(-1) ..addInt(256) - ..end() - ; + ..end(); expect(flx.finish(), [255, 255, 0, 1, 4, 65, 1]); } { @@ -86,8 +85,7 @@ void main() { ..startVector() ..addInt(-45) ..addInt(256000) - ..end() - ; + ..end(); expect(flx.finish(), [211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1]); } { @@ -95,9 +93,28 @@ void main() { ..startVector() ..addDouble(1.1) ..addDouble(-256) - ..end() - ; - expect(flx.finish(), [154, 153, 153, 153, 153, 153, 241, 63, 0, 0, 0, 0, 0, 0, 112, 192, 16, 75, 1]); + ..end(); + expect(flx.finish(), [ + 154, + 153, + 153, + 153, + 153, + 153, + 241, + 63, + 0, + 0, + 0, + 0, + 0, + 0, + 112, + 192, + 16, + 75, + 1 + ]); } { var flx = Builder() @@ -105,8 +122,7 @@ void main() { ..addInt(1) ..addInt(2) ..addInt(4) - ..end() - ; + ..end(); expect(flx.finish(), [1, 2, 4, 3, 76, 1]); } { @@ -115,19 +131,17 @@ void main() { ..addInt(-1) ..addInt(256) ..addInt(4) - ..end() - ; + ..end(); expect(flx.finish(), [255, 255, 0, 1, 4, 0, 6, 77, 1]); } { var flx = Builder() ..startVector() - ..startVector() - ..addInt(61) - ..end() - ..addInt(64) + ..startVector() + ..addInt(61) ..end() - ; + ..addInt(64) + ..end(); expect(flx.finish(), [1, 61, 2, 2, 64, 44, 4, 4, 40, 1]); } { @@ -136,9 +150,31 @@ void main() { ..addString('foo') ..addString('bar') ..addString('baz') - ..end() - ; - expect(flx.finish(), [3, 102, 111, 111, 0, 3, 98, 97, 114, 0, 3, 98, 97, 122, 0, 3, 15, 11, 7, 3, 60, 1]); + ..end(); + expect(flx.finish(), [ + 3, + 102, + 111, + 111, + 0, + 3, + 98, + 97, + 114, + 0, + 3, + 98, + 97, + 122, + 0, + 3, + 15, + 11, + 7, + 3, + 60, + 1 + ]); } { var flx = Builder() @@ -149,9 +185,34 @@ void main() { ..addString('foo') ..addString('bar') ..addString('baz') - ..end() - ; - expect(flx.finish(), [3, 102, 111, 111, 0, 3, 98, 97, 114, 0, 3, 98, 97, 122, 0, 6, 15, 11, 7, 18, 14, 10, 6, 60, 1]); + ..end(); + expect(flx.finish(), [ + 3, + 102, + 111, + 111, + 0, + 3, + 98, + 97, + 114, + 0, + 3, + 98, + 97, + 122, + 0, + 6, + 15, + 11, + 7, + 18, + 14, + 10, + 6, + 60, + 1 + ]); } { var flx = Builder() @@ -159,8 +220,7 @@ void main() { ..addBool(true) ..addBool(false) ..addBool(true) - ..end() - ; + ..end(); expect(flx.finish(), [3, 1, 0, 1, 3, 144, 1]); } { @@ -171,29 +231,83 @@ void main() { ..addInt(-5) ..addDouble(1.3) ..addBool(true) - ..end() - ; + ..end(); expect(flx.finish(), [ - 3, 102, 111, 111, 0, 0, 0, 0, - 5, 0, 0, 0, 0, 0, 0, 0, - 15, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 251, 255, 255, 255, 255, 255, 255, 255, - 205, 204, 204, 204, 204, 204, 244, 63, - 1, 0, 0, 0, 0, 0, 0, 0, - 20, 4, 4, 15, 104, 45, 43, 1]); + 3, + 102, + 111, + 111, + 0, + 0, + 0, + 0, + 5, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 15, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 251, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 205, + 204, + 204, + 204, + 204, + 204, + 244, + 63, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 20, + 4, + 4, + 15, + 104, + 45, + 43, + 1 + ]); } }); - test('build map', () - { + test('build map', () { { var flx = Builder() ..startMap() ..addKey('a') ..addInt(12) - ..end() - ; + ..end(); expect(flx.finish(), [97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]); } { @@ -203,105 +317,270 @@ void main() { ..addInt(12) ..addKey('') ..addInt(45) - ..end() - ; - expect(flx.finish(), [97, 0, 0, 2, 2, 5, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]); + ..end(); + expect( + flx.finish(), [97, 0, 0, 2, 2, 5, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]); } { var flx = Builder() ..startVector() - ..startMap() - ..addKey('something') - ..addInt(12) - ..end() - ..startMap() - ..addKey('something') - ..addInt(45) - ..end() + ..startMap() + ..addKey('something') + ..addInt(12) ..end() - ; - expect(flx.finish(), [115, 111, 109, 101, 116, 104, 105, 110, 103, 0, - 1, 11, 1, 1, 1, 12, 4, 6, 1, 1, 45, 4, 2, 8, 4, 36, 36, 4, 40, 1]); + ..startMap() + ..addKey('something') + ..addInt(45) + ..end() + ..end(); + expect(flx.finish(), [ + 115, + 111, + 109, + 101, + 116, + 104, + 105, + 110, + 103, + 0, + 1, + 11, + 1, + 1, + 1, + 12, + 4, + 6, + 1, + 1, + 45, + 4, + 2, + 8, + 4, + 36, + 36, + 4, + 40, + 1 + ]); } }); - test('build blob', () - { + test('build blob', () { { - var flx = Builder() - ..addBlob(Uint8List.fromList([1, 2, 3]).buffer) - ; + var flx = Builder()..addBlob(Uint8List.fromList([1, 2, 3]).buffer); expect(flx.finish(), [3, 1, 2, 3, 3, 100, 1]); } }); - test('build from object', (){ - expect(Builder.buildFromObject(Uint8List.fromList([1, 2, 3]).buffer).asUint8List(), [3, 1, 2, 3, 3, 100, 1]); + test('build from object', () { + expect( + Builder.buildFromObject(Uint8List.fromList([1, 2, 3]).buffer) + .asUint8List(), + [3, 1, 2, 3, 3, 100, 1]); expect(Builder.buildFromObject(null).asUint8List(), [0, 0, 1]); expect(Builder.buildFromObject(true).asUint8List(), [1, 104, 1]); expect(Builder.buildFromObject(false).asUint8List(), [0, 104, 1]); expect(Builder.buildFromObject(25).asUint8List(), [25, 4, 1]); expect(Builder.buildFromObject(-250).asUint8List(), [6, 255, 5, 2]); - expect(Builder.buildFromObject(-2.50).asUint8List(), [0, 0, 32, 192, 14, 4]); - expect(Builder.buildFromObject('Maxim').asUint8List(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]); - expect(Builder.buildFromObject([1, 3.3, 'max', true, null, false]).asUint8List(), [ - 3, 109, 97, 120, 0, 0, 0, 0, - 6, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 102, 102, 102, 102, 102, 102, 10, 64, - 31, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 4, 15, 20, 104, 0, 104, 54, 43, 1 - ]); - expect(Builder.buildFromObject([{'something':12}, {'something': 45}]).asUint8List(), [ - 115, 111, 109, 101, 116, 104, 105, 110, 103, 0, - 1, 11, 1, 1, 1, 12, 4, 6, 1, 1, 45, 4, 2, 8, 4, 36, 36, 4, 40, 1 - ]); + expect( + Builder.buildFromObject(-2.50).asUint8List(), [0, 0, 32, 192, 14, 4]); + expect(Builder.buildFromObject('Maxim').asUint8List(), + [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]); + expect( + Builder.buildFromObject([1, 3.3, 'max', true, null, false]) + .asUint8List(), + [ + 3, + 109, + 97, + 120, + 0, + 0, + 0, + 0, + 6, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 102, + 102, + 102, + 102, + 102, + 102, + 10, + 64, + 31, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4, + 15, + 20, + 104, + 0, + 104, + 54, + 43, + 1 + ]); + expect( + Builder.buildFromObject([ + {'something': 12}, + {'something': 45} + ]).asUint8List(), + [ + 115, + 111, + 109, + 101, + 116, + 104, + 105, + 110, + 103, + 0, + 1, + 11, + 1, + 1, + 1, + 12, + 4, + 6, + 1, + 1, + 45, + 4, + 2, + 8, + 4, + 36, + 36, + 4, + 40, + 1 + ]); }); - test('add double indirectly', (){ - var flx = Builder() - ..addDoubleIndirectly(0.1) - ; + test('add double indirectly', () { + var flx = Builder()..addDoubleIndirectly(0.1); expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 8, 35, 1]); }); - test('add double indirectly to vector with cache', (){ + test('add double indirectly to vector with cache', () { var flx = Builder() ..startVector() ..addDoubleIndirectly(0.1, cache: true) ..addDoubleIndirectly(0.1, cache: true) ..addDoubleIndirectly(0.1, cache: true) ..addDoubleIndirectly(0.1, cache: true) - ..end() - ; - expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, - 4, 9, 10, 11, 12, 35, 35, 35, 35, 8, 40, 1]); + ..end(); + expect(flx.finish(), [ + 154, + 153, + 153, + 153, + 153, + 153, + 185, + 63, + 4, + 9, + 10, + 11, + 12, + 35, + 35, + 35, + 35, + 8, + 40, + 1 + ]); }); - test('add int indirectly', (){ - var flx = Builder() - ..addIntIndirectly(2345234523452345) - ; + test('add int indirectly', () { + var flx = Builder()..addIntIndirectly(2345234523452345); expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0, 8, 27, 1]); }); - test('add int indirectly to vector with cache', (){ + test('add int indirectly to vector with cache', () { var flx = Builder() ..startVector() ..addIntIndirectly(2345234523452345, cache: true) ..addIntIndirectly(2345234523452345, cache: true) ..addIntIndirectly(2345234523452345, cache: true) ..addIntIndirectly(2345234523452345, cache: true) - ..end() - ; - expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0, - 4, 9, 10, 11, 12, 27, 27, 27, 27, 8, 40, 1]); + ..end(); + expect(flx.finish(), [ + 185, + 115, + 175, + 118, + 250, + 84, + 8, + 0, + 4, + 9, + 10, + 11, + 12, + 27, + 27, + 27, + 27, + 8, + 40, + 1 + ]); }); - test('snapshot', (){ + test('snapshot', () { var flx = Builder(); flx.startVector(); flx.addInt(12); @@ -312,4 +591,3 @@ void main() { expect(flx.snapshot().asUint8List(), [12, 24, 45, 3, 76, 1]); }); } - diff --git a/dart/test/flex_reader_test.dart b/dart/test/flex_reader_test.dart index ec30367b..875b1c16 100644 --- a/dart/test/flex_reader_test.dart +++ b/dart/test/flex_reader_test.dart @@ -21,55 +21,68 @@ void main() { expect(Reference.fromBuffer(b([255, 251, 5, 2])).intValue, -1025); expect(Reference.fromBuffer(b([1, 4, 9, 2])).intValue, 1025); expect(Reference.fromBuffer(b([255, 255, 255, 127, 6, 4])).intValue, - 2147483647); + 2147483647); + expect(Reference.fromBuffer(b([0, 0, 0, 128, 6, 4])).intValue, -2147483648); expect( - Reference.fromBuffer(b([0, 0, 0, 128, 6, 4])).intValue, -2147483648); + Reference.fromBuffer(b([255, 255, 255, 255, 0, 0, 0, 0, 7, 8])) + .intValue, + 4294967295); expect( - Reference.fromBuffer(b([255, 255, 255, 255, 0, 0, 0, 0, 7, 8])) - .intValue, - 4294967295); - expect( - Reference.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 127, 7, 8])) - .intValue, - 9223372036854775807); + Reference.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 127, 7, 8])) + .intValue, + 9223372036854775807); expect(Reference.fromBuffer(b([0, 0, 0, 0, 0, 0, 0, 128, 7, 8])).intValue, - -9223372036854775808); + -9223372036854775808); // Dart does not really support UInt64 // expect(FlxValue.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 255, 11, 8])).intValue, 18446744073709551615); }); test('double value', () { expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).doubleValue, 4.5); expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).doubleValue, - closeTo(.1, .001)); + closeTo(.1, .001)); expect( - Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8])) - .doubleValue, - .1); + Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8])) + .doubleValue, + .1); }); test('num value', () { expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).numValue, 4.5); expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).numValue, - closeTo(.1, .001)); + closeTo(.1, .001)); expect( - Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8])) - .numValue, - .1); + Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8])) + .numValue, + .1); expect(Reference.fromBuffer(b([255, 251, 5, 2])).numValue, -1025); }); test('string value', () { expect( - Reference.fromBuffer(b([5, 77, 97, 120, 105, 109, 0, 6, 20, 1])) - .stringValue, - 'Maxim'); + Reference.fromBuffer(b([5, 77, 97, 120, 105, 109, 0, 6, 20, 1])) + .stringValue, + 'Maxim'); expect( - Reference.fromBuffer(b([ - 10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1 - ])).stringValue, - 'hello 😱'); + Reference.fromBuffer(b([ + 10, + 104, + 101, + 108, + 108, + 111, + 32, + 240, + 159, + 152, + 177, + 0, + 11, + 20, + 1 + ])).stringValue, + 'hello 😱'); }); test('blob value', () { expect( - Reference.fromBuffer(b([3, 1, 2, 3, 3, 100, 1])).blobValue, [1, 2, 3]); + Reference.fromBuffer(b([3, 1, 2, 3, 3, 100, 1])).blobValue, [1, 2, 3]); }); test('bool vector', () { var flx = Reference.fromBuffer(b([3, 1, 0, 1, 3, 144, 1])); @@ -81,27 +94,92 @@ void main() { testNumbers([3, 1, 2, 3, 3, 44, 1], [1, 2, 3]); testNumbers([3, 255, 2, 3, 3, 44, 1], [-1, 2, 3]); testNumbers([3, 0, 1, 0, 43, 2, 3, 0, 6, 45, 1], [1, 555, 3]); - testNumbers( - [3, 0, 0, 0, 1, 0, 0, 0, 204, 216, 0, 0, 3, 0, 0, 0, 12, 46, 1], - [1, 55500, 3]); + testNumbers([3, 0, 0, 0, 1, 0, 0, 0, 204, 216, 0, 0, 3, 0, 0, 0, 12, 46, 1], + [1, 55500, 3]); testNumbers([ - 3, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 172, 128, 94, 239, 12, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, - 24, 47, 1 - ], [1, 55555555500, 3 + 3, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 172, + 128, + 94, + 239, + 12, + 0, + 0, + 0, + 3, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 24, + 47, + 1 + ], [ + 1, + 55555555500, + 3 ]); testNumbers( - [3, 0, 0, 0, 0, 0, 192, 63, 0, 0, 32, 64, 0, 0, 96, 64, 12, 54, 1], - [1.5, 2.5, 3.5]); + [3, 0, 0, 0, 0, 0, 192, 63, 0, 0, 32, 64, 0, 0, 96, 64, 12, 54, 1], + [1.5, 2.5, 3.5]); testNumbers([ - 3, 0, 0, 0, 0, 0, 0, 0, - 154, 153, 153, 153, 153, 153, 241, 63, - 154, 153, 153, 153, 153, 153, 1, 64, - 102, 102, 102, 102, 102, 102, 10, 64, - 24, 55, 1 - ], [1.1, 2.2, 3.3 + 3, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 154, + 153, + 153, + 153, + 153, + 153, + 241, + 63, + 154, + 153, + 153, + 153, + 153, + 153, + 1, + 64, + 102, + 102, + 102, + 102, + 102, + 102, + 10, + 64, + 24, + 55, + 1 + ], [ + 1.1, + 2.2, + 3.3 ]); }); test('number vector, fixed type', () { @@ -109,11 +187,28 @@ void main() { testNumbers([255, 255, 0, 1, 4, 65, 1], [-1, 256]); testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1], [-45, 256000]); testNumbers([ - 211, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 127, - 16, 67, 1 + 211, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 127, + 16, + 67, + 1 ], [ - -45, 9223372036854775807 + -45, + 9223372036854775807 ]); testNumbers([1, 2, 2, 68, 1], [1, 2]); @@ -122,85 +217,326 @@ void main() { testNumbers([205, 204, 140, 63, 0, 0, 0, 192, 8, 74, 1], [1.1, -2]); testNumbers([ - 154, 153, 153, 153, 153, 153, 241, 63, - 0, 0, 0, 0, 0, 0, 112, 192, - 16, 75, 1 + 154, + 153, + 153, + 153, + 153, + 153, + 241, + 63, + 0, + 0, + 0, + 0, + 0, + 0, + 112, + 192, + 16, + 75, + 1 ], [ - 1.1, -256 + 1.1, + -256 ]); testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 4, 0, 0, 0, 12, 78, 1], - [-45, 256000, 4]); + [-45, 256000, 4]); testNumbers([ - 211, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 127, - 4, 0, 0, 0, 0, 0, 0, 0, - 9, 0, 0, 0, 0, 0, 0, 0, - 32, 91, 1 + 211, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 127, + 4, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 9, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 32, + 91, + 1 ], [ - -45, 9223372036854775807, 4, 9 + -45, + 9223372036854775807, + 4, + 9 ]); testNumbers([ - 45, 0, 0, 0, 0, 0, 0, 0, - 255, 255, 255, 255, 255, 255, 255, 127, - 4, 0, 0, 0, 0, 0, 0, 0, - 9, 0, 0, 0, 0, 0, 0, 0, - 32, 95, 1 + 45, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 127, + 4, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 9, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 32, + 95, + 1 ], [ - 45, 9223372036854775807, 4, 9 + 45, + 9223372036854775807, + 4, + 9 ]); testNumbers([ - 154, 153, 153, 153, 153, 153, 241, 63, - 0, 0, 0, 0, 0, 0, 112, 64, - 0, 0, 0, 0, 0, 0, 16, 64, - 24, 87, 1 + 154, + 153, + 153, + 153, + 153, + 153, + 241, + 63, + 0, + 0, + 0, + 0, + 0, + 0, + 112, + 64, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 64, + 24, + 87, + 1 ], [ - 1.1, 256, 4 + 1.1, + 256, + 4 ]); testNumbers([ - 154, 153, 153, 153, 153, 153, 241, 63, - 0, 0, 0, 0, 0, 0, 112, 64, - 0, 0, 0, 0, 0, 0, 16, 64, - 0, 0, 0, 0, 0, 0, 34, 64, - 32, 99, 1 + 154, + 153, + 153, + 153, + 153, + 153, + 241, + 63, + 0, + 0, + 0, + 0, + 0, + 0, + 112, + 64, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 64, + 0, + 0, + 0, + 0, + 0, + 0, + 34, + 64, + 32, + 99, + 1 ], [ - 1.1, 256, 4, 9 + 1.1, + 256, + 4, + 9 ]); }); test('string vector', () { testStrings([ - 3, 102, 111, 111, 0, - 3, 98, 97, 114, 0, - 3, 98, 97, 122, 0, - 3, 15, 11, 7, - 3, 60, 1 + 3, + 102, + 111, + 111, + 0, + 3, + 98, + 97, + 114, + 0, + 3, + 98, + 97, + 122, + 0, + 3, + 15, + 11, + 7, + 3, + 60, + 1 ], [ - 'foo', 'bar', 'baz' + 'foo', + 'bar', + 'baz' ]); testStrings([ - 3, 102, 111, 111, 0, - 3, 98, 97, 114, 0, - 3, 98, 97, 122, 0, - 6, 15, 11, 7, 18, 14, 10, - 6, 60, 1 + 3, + 102, + 111, + 111, + 0, + 3, + 98, + 97, + 114, + 0, + 3, + 98, + 97, + 122, + 0, + 6, + 15, + 11, + 7, + 18, + 14, + 10, + 6, + 60, + 1 ], [ - 'foo', 'bar', 'baz', 'foo', 'bar', 'baz' + 'foo', + 'bar', + 'baz', + 'foo', + 'bar', + 'baz' ]); }); test('mixed vector', () { var flx = Reference.fromBuffer(b([ - 3, 102, 111, 111, 0, 0, 0, 0, - 5, 0, 0, 0, 0, 0, 0, 0, - 15, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 251, 255, 255, 255, 255, 255, 255, 255, - 205, 204, 204, 204, 204, 204, 244, 63, - 1, 0, 0, 0, 0, 0, 0, 0, - 20, 4, 4, 15, 104, 45, 43, 1 + 3, + 102, + 111, + 111, + 0, + 0, + 0, + 0, + 5, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 15, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 251, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 205, + 204, + 204, + 204, + 204, + 204, + 244, + 63, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 20, + 4, + 4, + 15, + 104, + 45, + 43, + 1 ])); expect(flx.length, 5); expect(flx[0].stringValue, 'foo'); @@ -216,7 +552,8 @@ void main() { expect(flx['a'].numValue, 12); }); test('two value map', () { - var flx = Reference.fromBuffer(b([0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1])); + var flx = Reference.fromBuffer( + b([0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1])); expect(flx.length, 2); expect(flx['a'].numValue, 12); expect(flx[''].numValue, 45); @@ -239,45 +576,239 @@ void main() { expect(flx['address']['zip'].stringValue, '12345'); expect(flx['address']['countryCode'].stringValue, 'XX'); - expect(() => flx['address']['country'].stringValue, - throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [country] is not applicable on: //address of: ValueType.Map'))); - expect(() => flx['address']['countryCode'][0], - throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [0] is not applicable on: //address/countryCode of: ValueType.String'))); - expect(() => flx[1], - throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [1] is not applicable on: / of: ValueType.Map'))); - expect(() => flx['flags'][4], - throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4'))); - expect(() => flx['flags'][-1], - throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [-1] is not applicable on: //flags of: ValueType.VectorBool length: 4'))); + expect( + () => flx['address']['country'].stringValue, + throwsA(predicate((dynamic e) => + e is ArgumentError && + e.message == + 'Key: [country] is not applicable on: //address of: ValueType.Map'))); + expect( + () => flx['address']['countryCode'][0], + throwsA(predicate((dynamic e) => + e is ArgumentError && + e.message == + 'Key: [0] is not applicable on: //address/countryCode of: ValueType.String'))); + expect( + () => flx[1], + throwsA(predicate((dynamic e) => + e is ArgumentError && + e.message == + 'Key: [1] is not applicable on: / of: ValueType.Map'))); + expect( + () => flx['flags'][4], + throwsA(predicate((dynamic e) => + e is ArgumentError && + e.message == + 'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4'))); + expect( + () => flx['flags'][-1], + throwsA(predicate((dynamic e) => + e is ArgumentError && + e.message == + 'Key: [-1] is not applicable on: //flags of: ValueType.VectorBool length: 4'))); }); test('complex map to json', () { var flx = complexMap(); - expect(flx.json, '{"address":{"city":"Bla","countryCode":"XX","zip":"12345"},"age":35,"flags":[true,false,true,true],"name":"Maxim","weight":72.5}'); + expect(flx.json, + '{"address":{"city":"Bla","countryCode":"XX","zip":"12345"},"age":35,"flags":[true,false,true,true],"name":"Maxim","weight":72.5}'); }); test('complex map iterators', () { var flx = complexMap(); - expect(flx.mapKeyIterable.map((e) => e).toList(), ['address', 'age', 'flags', 'name', 'weight']); - expect(flx.mapValueIterable.map((e) => e.json).toList(), [flx['address'].json, flx['age'].json, flx['flags'].json, flx['name'].json, flx['weight'].json]); - expect(flx['flags'].vectorIterable.map((e) => e.boolValue).toList(), [true, false, true, true]); + expect(flx.mapKeyIterable.map((e) => e).toList(), + ['address', 'age', 'flags', 'name', 'weight']); + expect(flx.mapValueIterable.map((e) => e.json).toList(), [ + flx['address'].json, + flx['age'].json, + flx['flags'].json, + flx['name'].json, + flx['weight'].json + ]); + expect(flx['flags'].vectorIterable.map((e) => e.boolValue).toList(), + [true, false, true, true]); }); - test('bug where offest were stored as int instead of uint', (){ - const data = [99, 104, 97, 110, 110, 101, 108, 115, 95, 105, 110, 0, - 100, 105, 108, 97, 116, 105, 111, 110, 95, 104, 101, 105, 103, 104, 116, 95, 102, 97, 99, 116, 111, 114, 0, - 100, 105, 108, 97, 116, 105, 111, 110, 95, 119, 105, 100, 116, 104, 95, 102, 97, 99, 116, 111, 114, 0, - 102, 117, 115, 101, 100, 95, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 95, 102, 117, 110, 99, 116, 105, 111, 110, 0, - 112, 97, 100, 95, 118, 97, 108, 117, 101, 115, 0, 112, 97, 100, 100, 105, 110, 103, 0, - 115, 116, 114, 105, 100, 101, 95, 104, 101, 105, 103, 104, 116, 0, - 115, 116, 114, 105, 100, 101, 95, 119, 105, 100, 116, 104, 0, - 8, 130, 119, 97, 76, 51, 41, 34, 21, 8, 1, 8, 64, 1, 1, 1, 1, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 16, 36, 1]; + test('bug where offest were stored as int instead of uint', () { + const data = [ + 99, + 104, + 97, + 110, + 110, + 101, + 108, + 115, + 95, + 105, + 110, + 0, + 100, + 105, + 108, + 97, + 116, + 105, + 111, + 110, + 95, + 104, + 101, + 105, + 103, + 104, + 116, + 95, + 102, + 97, + 99, + 116, + 111, + 114, + 0, + 100, + 105, + 108, + 97, + 116, + 105, + 111, + 110, + 95, + 119, + 105, + 100, + 116, + 104, + 95, + 102, + 97, + 99, + 116, + 111, + 114, + 0, + 102, + 117, + 115, + 101, + 100, + 95, + 97, + 99, + 116, + 105, + 118, + 97, + 116, + 105, + 111, + 110, + 95, + 102, + 117, + 110, + 99, + 116, + 105, + 111, + 110, + 0, + 112, + 97, + 100, + 95, + 118, + 97, + 108, + 117, + 101, + 115, + 0, + 112, + 97, + 100, + 100, + 105, + 110, + 103, + 0, + 115, + 116, + 114, + 105, + 100, + 101, + 95, + 104, + 101, + 105, + 103, + 104, + 116, + 0, + 115, + 116, + 114, + 105, + 100, + 101, + 95, + 119, + 105, + 100, + 116, + 104, + 0, + 8, + 130, + 119, + 97, + 76, + 51, + 41, + 34, + 21, + 8, + 1, + 8, + 64, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 16, + 36, + 1 + ]; var flx = Reference.fromBuffer(b(data)); - expect(flx.json, '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}'); - const object = {"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}; + expect(flx.json, + '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}'); + const object = { + "channels_in": 64, + "dilation_height_factor": 1, + "dilation_width_factor": 1, + "fused_activation_function": 1, + "pad_values": 1, + "padding": 0, + "stride_height": 1, + "stride_width": 1 + }; var data1 = Builder.buildFromObject(object).asUint8List(); expect(data1.length, data.length); var flx1 = Reference.fromBuffer(b(data1)); - expect(flx1.json, '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}'); + expect(flx1.json, + '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}'); }); } @@ -302,7 +833,7 @@ void testStrings(List<int> buffer, List<String> numbers) { } } -Reference complexMap(){ +Reference complexMap() { // { // "age": 35, // "flags": [True, False, True, True], @@ -315,20 +846,145 @@ Reference complexMap(){ // } // } return Reference.fromBuffer(b([ - 97, 100, 100, 114, 101, 115, 115, 0, - 99, 105, 116, 121, 0, 3, 66, 108, 97, 0, - 99, 111, 117, 110, 116, 114, 121, 67, 111, 100, 101, 0, - 2, 88, 88, 0, - 122, 105, 112, 0, - 5, 49, 50, 51, 52, 53, 0, - 3, 38, 29, 14, 3, 1, 3, 38, 22, 15, 20, 20, 20, - 97, 103, 101, 0, - 102, 108, 97, 103, 115, 0, - 4, 1, 0, 1, 1, - 110, 97, 109, 101, 0, - 5, 77, 97, 120, 105, 109, 0, - 119, 101, 105, 103, 104, 116, 0, - 5, 93, 36, 33, 23, 12, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 60, 0, 0, 0, 35, 0, 0, 0, 51, 0, 0, 0, 45, - 0, 0, 0, 0, 0, 145, 66, 36, 4, 144, 20, 14, 25, 38, 1 + 97, + 100, + 100, + 114, + 101, + 115, + 115, + 0, + 99, + 105, + 116, + 121, + 0, + 3, + 66, + 108, + 97, + 0, + 99, + 111, + 117, + 110, + 116, + 114, + 121, + 67, + 111, + 100, + 101, + 0, + 2, + 88, + 88, + 0, + 122, + 105, + 112, + 0, + 5, + 49, + 50, + 51, + 52, + 53, + 0, + 3, + 38, + 29, + 14, + 3, + 1, + 3, + 38, + 22, + 15, + 20, + 20, + 20, + 97, + 103, + 101, + 0, + 102, + 108, + 97, + 103, + 115, + 0, + 4, + 1, + 0, + 1, + 1, + 110, + 97, + 109, + 101, + 0, + 5, + 77, + 97, + 120, + 105, + 109, + 0, + 119, + 101, + 105, + 103, + 104, + 116, + 0, + 5, + 93, + 36, + 33, + 23, + 12, + 0, + 0, + 7, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 5, + 0, + 0, + 0, + 60, + 0, + 0, + 0, + 35, + 0, + 0, + 0, + 51, + 0, + 0, + 0, + 45, + 0, + 0, + 0, + 0, + 0, + 145, + 66, + 36, + 4, + 144, + 20, + 14, + 25, + 38, + 1 ])); } diff --git a/dart/test/flex_types_test.dart b/dart/test/flex_types_test.dart index 16235f62..76ce0704 100644 --- a/dart/test/flex_types_test.dart +++ b/dart/test/flex_types_test.dart @@ -48,69 +48,116 @@ void main() { expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt), isFalse); }); test('to typed vector', () { - expect(ValueTypeUtils.toTypedVector(ValueType.Int,0), equals(ValueType.VectorInt)); - expect(ValueTypeUtils.toTypedVector(ValueType.UInt,0), equals(ValueType.VectorUInt)); - expect(ValueTypeUtils.toTypedVector(ValueType.Bool,0), equals(ValueType.VectorBool)); - expect(ValueTypeUtils.toTypedVector(ValueType.Float,0), equals(ValueType.VectorFloat)); - expect(ValueTypeUtils.toTypedVector(ValueType.Key,0), equals(ValueType.VectorKey)); - expect(ValueTypeUtils.toTypedVector(ValueType.String,0), equals(ValueType.VectorString)); + expect(ValueTypeUtils.toTypedVector(ValueType.Int, 0), + equals(ValueType.VectorInt)); + expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 0), + equals(ValueType.VectorUInt)); + expect(ValueTypeUtils.toTypedVector(ValueType.Bool, 0), + equals(ValueType.VectorBool)); + expect(ValueTypeUtils.toTypedVector(ValueType.Float, 0), + equals(ValueType.VectorFloat)); + expect(ValueTypeUtils.toTypedVector(ValueType.Key, 0), + equals(ValueType.VectorKey)); + expect(ValueTypeUtils.toTypedVector(ValueType.String, 0), + equals(ValueType.VectorString)); - expect(ValueTypeUtils.toTypedVector(ValueType.Int,2), equals(ValueType.VectorInt2)); - expect(ValueTypeUtils.toTypedVector(ValueType.UInt,2), equals(ValueType.VectorUInt2)); - expect(ValueTypeUtils.toTypedVector(ValueType.Float,2), equals(ValueType.VectorFloat2)); + expect(ValueTypeUtils.toTypedVector(ValueType.Int, 2), + equals(ValueType.VectorInt2)); + expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 2), + equals(ValueType.VectorUInt2)); + expect(ValueTypeUtils.toTypedVector(ValueType.Float, 2), + equals(ValueType.VectorFloat2)); - expect(ValueTypeUtils.toTypedVector(ValueType.Int,3), equals(ValueType.VectorInt3)); - expect(ValueTypeUtils.toTypedVector(ValueType.UInt,3), equals(ValueType.VectorUInt3)); - expect(ValueTypeUtils.toTypedVector(ValueType.Float,3), equals(ValueType.VectorFloat3)); + expect(ValueTypeUtils.toTypedVector(ValueType.Int, 3), + equals(ValueType.VectorInt3)); + expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 3), + equals(ValueType.VectorUInt3)); + expect(ValueTypeUtils.toTypedVector(ValueType.Float, 3), + equals(ValueType.VectorFloat3)); - expect(ValueTypeUtils.toTypedVector(ValueType.Int,4), equals(ValueType.VectorInt4)); - expect(ValueTypeUtils.toTypedVector(ValueType.UInt,4), equals(ValueType.VectorUInt4)); - expect(ValueTypeUtils.toTypedVector(ValueType.Float,4), equals(ValueType.VectorFloat4)); + expect(ValueTypeUtils.toTypedVector(ValueType.Int, 4), + equals(ValueType.VectorInt4)); + expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 4), + equals(ValueType.VectorUInt4)); + expect(ValueTypeUtils.toTypedVector(ValueType.Float, 4), + equals(ValueType.VectorFloat4)); }); test('typed vector element type', () { - expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorInt), equals(ValueType.Int)); - expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorUInt), equals(ValueType.UInt)); - expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorFloat), equals(ValueType.Float)); - expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorString), equals(ValueType.String)); - expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorKey), equals(ValueType.Key)); - expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorBool), equals(ValueType.Bool)); + expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorInt), + equals(ValueType.Int)); + expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorUInt), + equals(ValueType.UInt)); + expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorFloat), + equals(ValueType.Float)); + expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorString), + equals(ValueType.String)); + expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorKey), + equals(ValueType.Key)); + expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorBool), + equals(ValueType.Bool)); }); test('fixed typed vector element type', () { - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt2), equals(ValueType.Int)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt3), equals(ValueType.Int)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt4), equals(ValueType.Int)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt2), + equals(ValueType.Int)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt3), + equals(ValueType.Int)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt4), + equals(ValueType.Int)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt2), equals(ValueType.UInt)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt3), equals(ValueType.UInt)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt4), equals(ValueType.UInt)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt2), + equals(ValueType.UInt)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt3), + equals(ValueType.UInt)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt4), + equals(ValueType.UInt)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat2), equals(ValueType.Float)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat3), equals(ValueType.Float)); - expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat4), equals(ValueType.Float)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat2), + equals(ValueType.Float)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat3), + equals(ValueType.Float)); + expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat4), + equals(ValueType.Float)); }); test('fixed typed vector element size', () { - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt2), equals(2)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt3), equals(3)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt4), equals(4)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt2), + equals(2)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt3), + equals(3)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt4), + equals(4)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt2), equals(2)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt3), equals(3)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt4), equals(4)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt2), + equals(2)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt3), + equals(3)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt4), + equals(4)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat2), equals(2)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat3), equals(3)); - expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat4), equals(4)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat2), + equals(2)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat3), + equals(3)); + expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat4), + equals(4)); }); test('packed type', () { - expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width8), equals(0)); - expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width16), equals(1)); - expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width32), equals(2)); - expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width64), equals(3)); + expect( + ValueTypeUtils.packedType(ValueType.Null, BitWidth.width8), equals(0)); + expect( + ValueTypeUtils.packedType(ValueType.Null, BitWidth.width16), equals(1)); + expect( + ValueTypeUtils.packedType(ValueType.Null, BitWidth.width32), equals(2)); + expect( + ValueTypeUtils.packedType(ValueType.Null, BitWidth.width64), equals(3)); - expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width8), equals(4)); - expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width16), equals(5)); - expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width32), equals(6)); - expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width64), equals(7)); + expect( + ValueTypeUtils.packedType(ValueType.Int, BitWidth.width8), equals(4)); + expect( + ValueTypeUtils.packedType(ValueType.Int, BitWidth.width16), equals(5)); + expect( + ValueTypeUtils.packedType(ValueType.Int, BitWidth.width32), equals(6)); + expect( + ValueTypeUtils.packedType(ValueType.Int, BitWidth.width64), equals(7)); }); test('bit width', () { expect(BitWidthUtil.width(0), BitWidth.width8); diff --git a/dart/test/monster_test_my_game.example2_generated.dart b/dart/test/monster_test_my_game.example2_generated.dart index d1c6a03a..92d7c911 100644 --- a/dart/test/monster_test_my_game.example2_generated.dart +++ b/dart/test/monster_test_my_game.example2_generated.dart @@ -1,5 +1,5 @@ // automatically generated by the FlatBuffers compiler, do not modify -// ignore_for_file: unused_import, unused_field, unused_local_variable +// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable library my_game.example2; @@ -29,7 +29,7 @@ class Monster { MonsterT unpack() => MonsterT(); - static int pack(fb.Builder fbBuilder, MonsterT object) { + static int pack(fb.Builder fbBuilder, MonsterT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -37,8 +37,6 @@ class Monster { class MonsterT { int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.startTable(); return fbBuilder.endTable(); } @@ -63,17 +61,14 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.startTable(); return fbBuilder.endTable(); } /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); diff --git a/dart/test/monster_test_my_game.example_generated.dart b/dart/test/monster_test_my_game.example_generated.dart index 75302f9f..36ada7d5 100644 --- a/dart/test/monster_test_my_game.example_generated.dart +++ b/dart/test/monster_test_my_game.example_generated.dart @@ -1,5 +1,5 @@ // automatically generated by the FlatBuffers compiler, do not modify -// ignore_for_file: unused_import, unused_field, unused_local_variable +// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable library my_game.example; @@ -15,13 +15,16 @@ class Color { const Color._(this.value); factory Color.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum Color'); } - return values[value]; + return result; } + static Color? _createOrNull(int? value) => + value == null ? null : Color.fromValue(value); + static bool containsValue(int value) => values.containsKey(value); static const Color Red = const Color._(1); @@ -32,7 +35,10 @@ class Color { /// \brief color Blue (1u << 3) static const Color Blue = const Color._(8); - static const Map<int,Color> values = {1: Red,2: Green,8: Blue,}; + static const Map<int, Color> values = { + 1: Red, + 2: Green, + 8: Blue}; static const fb.Reader<Color> reader = const _ColorReader(); @@ -58,13 +64,16 @@ class Race { const Race._(this.value); factory Race.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum Race'); } - return values[value]; + return result; } + 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); @@ -73,7 +82,11 @@ class Race { static const Race Human = const Race._(0); static const Race Dwarf = const Race._(1); static const Race Elf = const Race._(2); - static const Map<int,Race> values = {-1: None,0: Human,1: Dwarf,2: Elf,}; + static const Map<int, Race> values = { + -1: None, + 0: Human, + 1: Dwarf, + 2: Elf}; static const fb.Reader<Race> reader = const _RaceReader(); @@ -99,13 +112,16 @@ class AnyTypeId { const AnyTypeId._(this.value); factory AnyTypeId.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum AnyTypeId'); } - return values[value]; + return result; } + 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); @@ -114,7 +130,11 @@ class AnyTypeId { static const AnyTypeId Monster = const AnyTypeId._(1); static const AnyTypeId TestSimpleTableWithEnum = const AnyTypeId._(2); static const AnyTypeId MyGame_Example2_Monster = const AnyTypeId._(3); - static const Map<int,AnyTypeId> values = {0: NONE,1: Monster,2: TestSimpleTableWithEnum,3: MyGame_Example2_Monster,}; + static const Map<int, AnyTypeId> values = { + 0: NONE, + 1: Monster, + 2: TestSimpleTableWithEnum, + 3: MyGame_Example2_Monster}; static const fb.Reader<AnyTypeId> reader = const _AnyTypeIdReader(); @@ -140,13 +160,16 @@ class AnyUniqueAliasesTypeId { const AnyUniqueAliasesTypeId._(this.value); factory AnyUniqueAliasesTypeId.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum AnyUniqueAliasesTypeId'); } - return values[value]; + return result; } + 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); @@ -155,7 +178,11 @@ class AnyUniqueAliasesTypeId { static const AnyUniqueAliasesTypeId M = const AnyUniqueAliasesTypeId._(1); static const AnyUniqueAliasesTypeId TS = const AnyUniqueAliasesTypeId._(2); static const AnyUniqueAliasesTypeId M2 = const AnyUniqueAliasesTypeId._(3); - static const Map<int,AnyUniqueAliasesTypeId> values = {0: NONE,1: M,2: TS,3: M2,}; + static const Map<int, AnyUniqueAliasesTypeId> values = { + 0: NONE, + 1: M, + 2: TS, + 3: M2}; static const fb.Reader<AnyUniqueAliasesTypeId> reader = const _AnyUniqueAliasesTypeIdReader(); @@ -181,13 +208,16 @@ class AnyAmbiguousAliasesTypeId { const AnyAmbiguousAliasesTypeId._(this.value); factory AnyAmbiguousAliasesTypeId.fromValue(int value) { - if (value == null) value = 0; - if (!values.containsKey(value)) { + final result = values[value]; + if (result == null) { throw new StateError('Invalid value $value for bit flag enum AnyAmbiguousAliasesTypeId'); } - return values[value]; + return result; } + 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); @@ -196,7 +226,11 @@ class AnyAmbiguousAliasesTypeId { static const AnyAmbiguousAliasesTypeId M1 = const AnyAmbiguousAliasesTypeId._(1); static const AnyAmbiguousAliasesTypeId M2 = const AnyAmbiguousAliasesTypeId._(2); static const AnyAmbiguousAliasesTypeId M3 = const AnyAmbiguousAliasesTypeId._(3); - static const Map<int,AnyAmbiguousAliasesTypeId> values = {0: NONE,1: M1,2: M2,3: M3,}; + static const Map<int, AnyAmbiguousAliasesTypeId> values = { + 0: NONE, + 1: M1, + 2: M2, + 3: M3}; static const fb.Reader<AnyAmbiguousAliasesTypeId> reader = const _AnyAmbiguousAliasesTypeIdReader(); @@ -237,7 +271,7 @@ class Test { a: a, b: b); - static int pack(fb.Builder fbBuilder, TestT object) { + static int pack(fb.Builder fbBuilder, TestT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -248,12 +282,10 @@ class TestT { int b; TestT({ - this.a, - this.b}); + required this.a, + required this.b}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.pad(1); fbBuilder.putInt8(b); fbBuilder.putInt16(a); @@ -278,9 +310,7 @@ class _TestReader extends fb.StructReader<Test> { } class TestBuilder { - TestBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + TestBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -298,18 +328,15 @@ class TestObjectBuilder extends fb.ObjectBuilder { final int _b; TestObjectBuilder({ - int a, - int b, + required int a, + required int b, }) : _a = a, _b = b; /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.pad(1); fbBuilder.putInt8(_b); fbBuilder.putInt16(_a); @@ -318,7 +345,7 @@ class TestObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -336,7 +363,7 @@ class TestSimpleTableWithEnum { final fb.BufferContext _bc; final int _bcOffset; - Color get color => new Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 4, 2)); + Color get color => Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 4, 2)); @override String toString() { @@ -346,7 +373,7 @@ class TestSimpleTableWithEnum { TestSimpleTableWithEnumT unpack() => TestSimpleTableWithEnumT( color: color); - static int pack(fb.Builder fbBuilder, TestSimpleTableWithEnumT object) { + static int pack(fb.Builder fbBuilder, TestSimpleTableWithEnumT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -356,13 +383,11 @@ class TestSimpleTableWithEnumT { Color color; TestSimpleTableWithEnumT({ - this.color}); + this.color = Color.Green}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.startTable(); - fbBuilder.addUint8(0, color?.value); + fbBuilder.addUint8(0, color.value); return fbBuilder.endTable(); } @@ -381,9 +406,7 @@ class _TestSimpleTableWithEnumReader extends fb.TableReader<TestSimpleTableWithE } class TestSimpleTableWithEnumBuilder { - TestSimpleTableWithEnumBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + TestSimpleTableWithEnumBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -391,7 +414,7 @@ class TestSimpleTableWithEnumBuilder { fbBuilder.startTable(); } - int addColor(Color color) { + int addColor(Color? color) { fbBuilder.addUint8(0, color?.value); return fbBuilder.offset; } @@ -402,19 +425,16 @@ class TestSimpleTableWithEnumBuilder { } class TestSimpleTableWithEnumObjectBuilder extends fb.ObjectBuilder { - final Color _color; + final Color? _color; TestSimpleTableWithEnumObjectBuilder({ - Color color, + Color? color, }) : _color = color; /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.startTable(); fbBuilder.addUint8(0, _color?.value); return fbBuilder.endTable(); @@ -422,7 +442,7 @@ class TestSimpleTableWithEnumObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -440,7 +460,7 @@ class Vec3 { double get y => const fb.Float32Reader().read(_bc, _bcOffset + 4); double get z => const fb.Float32Reader().read(_bc, _bcOffset + 8); double get test1 => const fb.Float64Reader().read(_bc, _bcOffset + 16); - Color get test2 => new Color.fromValue(const fb.Uint8Reader().read(_bc, _bcOffset + 24)); + Color get test2 => Color.fromValue(const fb.Uint8Reader().read(_bc, _bcOffset + 24)); Test get test3 => Test.reader.read(_bc, _bcOffset + 26); @override @@ -454,9 +474,9 @@ class Vec3 { z: z, test1: test1, test2: test2, - test3: test3?.unpack()); + test3: test3.unpack()); - static int pack(fb.Builder fbBuilder, Vec3T object) { + static int pack(fb.Builder fbBuilder, Vec3T? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -471,20 +491,18 @@ class Vec3T { TestT test3; Vec3T({ - this.x, - this.y, - this.z, - this.test1, - this.test2, - this.test3}); + required this.x, + required this.y, + required this.z, + required this.test1, + required this.test2, + required this.test3}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.pad(2); test3.pack(fbBuilder); fbBuilder.pad(1); - fbBuilder.putUint8(test2?.value); + fbBuilder.putUint8(test2.value); fbBuilder.putFloat64(test1); fbBuilder.pad(4); fbBuilder.putFloat32(z); @@ -511,9 +529,7 @@ class _Vec3Reader extends fb.StructReader<Vec3> { } class Vec3Builder { - Vec3Builder(this.fbBuilder) { - assert(fbBuilder != null); - } + Vec3Builder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -521,7 +537,7 @@ class Vec3Builder { fbBuilder.pad(2); test3(); fbBuilder.pad(1); - fbBuilder.putUint8(test2?.value); + fbBuilder.putUint8(test2.value); fbBuilder.putFloat64(test1); fbBuilder.pad(4); fbBuilder.putFloat32(z); @@ -541,12 +557,12 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { final TestObjectBuilder _test3; Vec3ObjectBuilder({ - double x, - double y, - double z, - double test1, - Color test2, - TestObjectBuilder test3, + required double x, + required double y, + required double z, + required double test1, + required Color test2, + required TestObjectBuilder test3, }) : _x = x, _y = y, @@ -557,14 +573,11 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.pad(2); _test3.finish(fbBuilder); fbBuilder.pad(1); - fbBuilder.putUint8(_test2?.value); + fbBuilder.putUint8(_test2.value); fbBuilder.putFloat64(_test1); fbBuilder.pad(4); fbBuilder.putFloat32(_z); @@ -575,7 +588,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -601,7 +614,7 @@ class Ability { id: id, distance: distance); - static int pack(fb.Builder fbBuilder, AbilityT object) { + static int pack(fb.Builder fbBuilder, AbilityT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -612,12 +625,10 @@ class AbilityT { int distance; AbilityT({ - this.id, - this.distance}); + required this.id, + required this.distance}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.putUint32(distance); fbBuilder.putUint32(id); return fbBuilder.offset; @@ -641,9 +652,7 @@ class _AbilityReader extends fb.StructReader<Ability> { } class AbilityBuilder { - AbilityBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + AbilityBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -660,18 +669,15 @@ class AbilityObjectBuilder extends fb.ObjectBuilder { final int _distance; AbilityObjectBuilder({ - int id, - int distance, + required int id, + required int distance, }) : _id = id, _distance = distance; /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.putUint32(_distance); fbBuilder.putUint32(_id); return fbBuilder.offset; @@ -679,7 +685,7 @@ class AbilityObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -703,11 +709,11 @@ class StructOfStructs { } StructOfStructsT unpack() => StructOfStructsT( - a: a?.unpack(), - b: b?.unpack(), - c: c?.unpack()); + a: a.unpack(), + b: b.unpack(), + c: c.unpack()); - static int pack(fb.Builder fbBuilder, StructOfStructsT object) { + static int pack(fb.Builder fbBuilder, StructOfStructsT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -719,13 +725,11 @@ class StructOfStructsT { AbilityT c; StructOfStructsT({ - this.a, - this.b, - this.c}); + required this.a, + required this.b, + required this.c}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - c.pack(fbBuilder); b.pack(fbBuilder); a.pack(fbBuilder); @@ -750,9 +754,7 @@ class _StructOfStructsReader extends fb.StructReader<StructOfStructs> { } class StructOfStructsBuilder { - StructOfStructsBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + StructOfStructsBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -771,9 +773,9 @@ class StructOfStructsObjectBuilder extends fb.ObjectBuilder { final AbilityObjectBuilder _c; StructOfStructsObjectBuilder({ - AbilityObjectBuilder a, - TestObjectBuilder b, - AbilityObjectBuilder c, + required AbilityObjectBuilder a, + required TestObjectBuilder b, + required AbilityObjectBuilder c, }) : _a = a, _b = b, @@ -781,10 +783,7 @@ class StructOfStructsObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { _c.finish(fbBuilder); _b.finish(fbBuilder); _a.finish(fbBuilder); @@ -793,7 +792,7 @@ class StructOfStructsObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -811,7 +810,7 @@ class Stat { final fb.BufferContext _bc; final int _bcOffset; - String get id => const fb.StringReader().vTableGet(_bc, _bcOffset, 4, null); + String? get id => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 4); int get val => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 6, 0); int get count => const fb.Uint16Reader().vTableGet(_bc, _bcOffset, 8, 0); @@ -825,30 +824,26 @@ class Stat { val: val, count: count); - static int pack(fb.Builder fbBuilder, StatT object) { + static int pack(fb.Builder fbBuilder, StatT? object) { if (object == null) return 0; return object.pack(fbBuilder); } } class StatT { - String id; + String? id; int val; int count; StatT({ this.id, - this.val, - this.count}); + this.val = 0, + this.count = 0}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int idOffset = fbBuilder.writeString(id); - + final int? idOffset = fbBuilder.writeString(id); fbBuilder.startTable(); - if (idOffset != null) { - fbBuilder.addOffset(0, idOffset); - } + fbBuilder.addOffset(0, idOffset); fbBuilder.addInt64(1, val); fbBuilder.addUint16(2, count); return fbBuilder.endTable(); @@ -869,9 +864,7 @@ class _StatReader extends fb.TableReader<Stat> { } class StatBuilder { - StatBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + StatBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -879,15 +872,15 @@ class StatBuilder { fbBuilder.startTable(); } - int addIdOffset(int offset) { + int addIdOffset(int? offset) { fbBuilder.addOffset(0, offset); return fbBuilder.offset; } - int addVal(int val) { + int addVal(int? val) { fbBuilder.addInt64(1, val); return fbBuilder.offset; } - int addCount(int count) { + int addCount(int? count) { fbBuilder.addUint16(2, count); return fbBuilder.offset; } @@ -898,14 +891,14 @@ class StatBuilder { } class StatObjectBuilder extends fb.ObjectBuilder { - final String _id; - final int _val; - final int _count; + final String? _id; + final int? _val; + final int? _count; StatObjectBuilder({ - String id, - int val, - int count, + String? id, + int? val, + int? count, }) : _id = id, _val = val, @@ -913,15 +906,10 @@ class StatObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int idOffset = fbBuilder.writeString(_id); - + int finish(fb.Builder fbBuilder) { + final int? idOffset = fbBuilder.writeString(_id); fbBuilder.startTable(); - if (idOffset != null) { - fbBuilder.addOffset(0, idOffset); - } + fbBuilder.addOffset(0, idOffset); fbBuilder.addInt64(1, _val); fbBuilder.addUint16(2, _count); return fbBuilder.endTable(); @@ -929,7 +917,7 @@ class StatObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -957,7 +945,7 @@ class Referrable { ReferrableT unpack() => ReferrableT( id: id); - static int pack(fb.Builder fbBuilder, ReferrableT object) { + static int pack(fb.Builder fbBuilder, ReferrableT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -967,11 +955,9 @@ class ReferrableT { int id; ReferrableT({ - this.id}); + this.id = 0}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.startTable(); fbBuilder.addUint64(0, id); return fbBuilder.endTable(); @@ -992,9 +978,7 @@ class _ReferrableReader extends fb.TableReader<Referrable> { } class ReferrableBuilder { - ReferrableBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + ReferrableBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -1002,7 +986,7 @@ class ReferrableBuilder { fbBuilder.startTable(); } - int addId(int id) { + int addId(int? id) { fbBuilder.addUint64(0, id); return fbBuilder.offset; } @@ -1013,19 +997,16 @@ class ReferrableBuilder { } class ReferrableObjectBuilder extends fb.ObjectBuilder { - final int _id; + final int? _id; ReferrableObjectBuilder({ - int id, + int? id, }) : _id = id; /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.startTable(); fbBuilder.addUint64(0, _id); return fbBuilder.endTable(); @@ -1033,7 +1014,7 @@ class ReferrableObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -1052,29 +1033,29 @@ class Monster { final fb.BufferContext _bc; final int _bcOffset; - Vec3 get pos => Vec3.reader.vTableGet(_bc, _bcOffset, 4, null); + Vec3? get pos => Vec3.reader.vTableGetNullable(_bc, _bcOffset, 4); int get mana => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 150); int get hp => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 100); - String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 10, null); - List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null); - Color get color => new Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 16, 8)); - AnyTypeId get testType => new AnyTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 18, 0)); - dynamic get test { + String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 10); + List<int>? get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGetNullable(_bc, _bcOffset, 14); + Color get color => Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 16, 8)); + AnyTypeId? get testType => AnyTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 18)); + dynamic? get test { switch (testType?.value) { - case 1: return Monster.reader.vTableGet(_bc, _bcOffset, 20, null); - case 2: return TestSimpleTableWithEnum.reader.vTableGet(_bc, _bcOffset, 20, null); - case 3: return my_game_example2.Monster.reader.vTableGet(_bc, _bcOffset, 20, null); + case 1: return Monster.reader.vTableGetNullable(_bc, _bcOffset, 20); + case 2: return TestSimpleTableWithEnum.reader.vTableGetNullable(_bc, _bcOffset, 20); + case 3: return my_game_example2.Monster.reader.vTableGetNullable(_bc, _bcOffset, 20); default: return null; } } - List<Test> get test4 => const fb.ListReader<Test>(Test.reader).vTableGet(_bc, _bcOffset, 22, null); - List<String> get testarrayofstring => const fb.ListReader<String>(const fb.StringReader()).vTableGet(_bc, _bcOffset, 24, null); + List<Test>? get test4 => const fb.ListReader<Test>(Test.reader).vTableGetNullable(_bc, _bcOffset, 22); + List<String>? get testarrayofstring => const fb.ListReader<String>(const fb.StringReader()).vTableGetNullable(_bc, _bcOffset, 24); /// an example documentation comment: this will end up in the generated code /// multiline too - List<Monster> get testarrayoftables => const fb.ListReader<Monster>(Monster.reader).vTableGet(_bc, _bcOffset, 26, null); - Monster get enemy => Monster.reader.vTableGet(_bc, _bcOffset, 28, null); - List<int> get testnestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 30, null); - Stat get testempty => Stat.reader.vTableGet(_bc, _bcOffset, 32, null); + List<Monster>? get testarrayoftables => const fb.ListReader<Monster>(Monster.reader).vTableGetNullable(_bc, _bcOffset, 26); + Monster? get enemy => Monster.reader.vTableGetNullable(_bc, _bcOffset, 28); + List<int>? get testnestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGetNullable(_bc, _bcOffset, 30); + Stat? get testempty => Stat.reader.vTableGetNullable(_bc, _bcOffset, 32); bool get testbool => const fb.BoolReader().vTableGet(_bc, _bcOffset, 34, false); int get testhashs32Fnv1 => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 36, 0); int get testhashu32Fnv1 => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 38, 0); @@ -1084,47 +1065,47 @@ class Monster { int get testhashu32Fnv1a => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 46, 0); int get testhashs64Fnv1a => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 48, 0); int get testhashu64Fnv1a => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 50, 0); - List<bool> get testarrayofbools => const fb.ListReader<bool>(const fb.BoolReader()).vTableGet(_bc, _bcOffset, 52, null); + List<bool>? get testarrayofbools => const fb.ListReader<bool>(const fb.BoolReader()).vTableGetNullable(_bc, _bcOffset, 52); double get testf => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 54, 3.14159); double get testf2 => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 56, 3.0); double get testf3 => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 58, 0.0); - List<String> get testarrayofstring2 => const fb.ListReader<String>(const fb.StringReader()).vTableGet(_bc, _bcOffset, 60, null); - List<Ability> get testarrayofsortedstruct => const fb.ListReader<Ability>(Ability.reader).vTableGet(_bc, _bcOffset, 62, null); - List<int> get flex => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 64, null); - List<Test> get test5 => const fb.ListReader<Test>(Test.reader).vTableGet(_bc, _bcOffset, 66, null); - List<int> get vectorOfLongs => const fb.ListReader<int>(const fb.Int64Reader()).vTableGet(_bc, _bcOffset, 68, null); - List<double> get vectorOfDoubles => const fb.ListReader<double>(const fb.Float64Reader()).vTableGet(_bc, _bcOffset, 70, null); - my_game.InParentNamespace get parentNamespaceTest => my_game.InParentNamespace.reader.vTableGet(_bc, _bcOffset, 72, null); - List<Referrable> get vectorOfReferrables => const fb.ListReader<Referrable>(Referrable.reader).vTableGet(_bc, _bcOffset, 74, null); + List<String>? get testarrayofstring2 => const fb.ListReader<String>(const fb.StringReader()).vTableGetNullable(_bc, _bcOffset, 60); + List<Ability>? get testarrayofsortedstruct => const fb.ListReader<Ability>(Ability.reader).vTableGetNullable(_bc, _bcOffset, 62); + List<int>? get flex => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGetNullable(_bc, _bcOffset, 64); + List<Test>? get test5 => const fb.ListReader<Test>(Test.reader).vTableGetNullable(_bc, _bcOffset, 66); + List<int>? get vectorOfLongs => const fb.ListReader<int>(const fb.Int64Reader()).vTableGetNullable(_bc, _bcOffset, 68); + List<double>? get vectorOfDoubles => const fb.ListReader<double>(const fb.Float64Reader()).vTableGetNullable(_bc, _bcOffset, 70); + my_game.InParentNamespace? get parentNamespaceTest => my_game.InParentNamespace.reader.vTableGetNullable(_bc, _bcOffset, 72); + List<Referrable>? get vectorOfReferrables => const fb.ListReader<Referrable>(Referrable.reader).vTableGetNullable(_bc, _bcOffset, 74); int get singleWeakReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 76, 0); - List<int> get vectorOfWeakReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 78, null); - List<Referrable> get vectorOfStrongReferrables => const fb.ListReader<Referrable>(Referrable.reader).vTableGet(_bc, _bcOffset, 80, null); + List<int>? get vectorOfWeakReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGetNullable(_bc, _bcOffset, 78); + List<Referrable>? get vectorOfStrongReferrables => const fb.ListReader<Referrable>(Referrable.reader).vTableGetNullable(_bc, _bcOffset, 80); int get coOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 82, 0); - List<int> get vectorOfCoOwningReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 84, null); + List<int>? get vectorOfCoOwningReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGetNullable(_bc, _bcOffset, 84); int get nonOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 86, 0); - List<int> get vectorOfNonOwningReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 88, null); - AnyUniqueAliasesTypeId get anyUniqueType => new AnyUniqueAliasesTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 90, 0)); - dynamic get anyUnique { + List<int>? get vectorOfNonOwningReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGetNullable(_bc, _bcOffset, 88); + AnyUniqueAliasesTypeId? get anyUniqueType => AnyUniqueAliasesTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 90)); + dynamic? get anyUnique { switch (anyUniqueType?.value) { - case 1: return Monster.reader.vTableGet(_bc, _bcOffset, 92, null); - case 2: return TestSimpleTableWithEnum.reader.vTableGet(_bc, _bcOffset, 92, null); - case 3: return my_game_example2.Monster.reader.vTableGet(_bc, _bcOffset, 92, null); + case 1: return Monster.reader.vTableGetNullable(_bc, _bcOffset, 92); + case 2: return TestSimpleTableWithEnum.reader.vTableGetNullable(_bc, _bcOffset, 92); + case 3: return my_game_example2.Monster.reader.vTableGetNullable(_bc, _bcOffset, 92); default: return null; } } - AnyAmbiguousAliasesTypeId get anyAmbiguousType => new AnyAmbiguousAliasesTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 94, 0)); - dynamic get anyAmbiguous { + AnyAmbiguousAliasesTypeId? get anyAmbiguousType => AnyAmbiguousAliasesTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 94)); + dynamic? get anyAmbiguous { switch (anyAmbiguousType?.value) { - case 1: return Monster.reader.vTableGet(_bc, _bcOffset, 96, null); - case 2: return Monster.reader.vTableGet(_bc, _bcOffset, 96, null); - case 3: return Monster.reader.vTableGet(_bc, _bcOffset, 96, null); + case 1: return Monster.reader.vTableGetNullable(_bc, _bcOffset, 96); + case 2: return Monster.reader.vTableGetNullable(_bc, _bcOffset, 96); + case 3: return Monster.reader.vTableGetNullable(_bc, _bcOffset, 96); default: return null; } } - List<Color> get vectorOfEnums => const fb.ListReader<Color>(Color.reader).vTableGet(_bc, _bcOffset, 98, null); - Race get signedEnum => new Race.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 100, -1)); - List<int> get testrequirednestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 102, null); - List<Stat> get scalarKeySortedTables => const fb.ListReader<Stat>(Stat.reader).vTableGet(_bc, _bcOffset, 104, null); + List<Color>? get vectorOfEnums => const fb.ListReader<Color>(Color.reader).vTableGetNullable(_bc, _bcOffset, 98); + Race get signedEnum => Race.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 100, -1)); + List<int>? get testrequirednestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGetNullable(_bc, _bcOffset, 102); + List<Stat>? get scalarKeySortedTables => const fb.ListReader<Stat>(Stat.reader).vTableGetNullable(_bc, _bcOffset, 104); @override String toString() { @@ -1140,9 +1121,9 @@ class Monster { color: color, testType: testType, test: test, - test4: test4?.map((e) => e.unpack())?.toList(), + test4: test4?.map((e) => e.unpack()).toList(), testarrayofstring: testarrayofstring, - testarrayoftables: testarrayoftables?.map((e) => e.unpack())?.toList(), + testarrayoftables: testarrayoftables?.map((e) => e.unpack()).toList(), enemy: enemy?.unpack(), testnestedflatbuffer: testnestedflatbuffer, testempty: testempty?.unpack(), @@ -1160,16 +1141,16 @@ class Monster { testf2: testf2, testf3: testf3, testarrayofstring2: testarrayofstring2, - testarrayofsortedstruct: testarrayofsortedstruct?.map((e) => e.unpack())?.toList(), + testarrayofsortedstruct: testarrayofsortedstruct?.map((e) => e.unpack()).toList(), flex: flex, - test5: test5?.map((e) => e.unpack())?.toList(), + test5: test5?.map((e) => e.unpack()).toList(), vectorOfLongs: vectorOfLongs, vectorOfDoubles: vectorOfDoubles, parentNamespaceTest: parentNamespaceTest?.unpack(), - vectorOfReferrables: vectorOfReferrables?.map((e) => e.unpack())?.toList(), + vectorOfReferrables: vectorOfReferrables?.map((e) => e.unpack()).toList(), singleWeakReference: singleWeakReference, vectorOfWeakReferences: vectorOfWeakReferences, - vectorOfStrongReferrables: vectorOfStrongReferrables?.map((e) => e.unpack())?.toList(), + vectorOfStrongReferrables: vectorOfStrongReferrables?.map((e) => e.unpack()).toList(), coOwningReference: coOwningReference, vectorOfCoOwningReferences: vectorOfCoOwningReferences, nonOwningReference: nonOwningReference, @@ -1181,9 +1162,9 @@ class Monster { vectorOfEnums: vectorOfEnums, signedEnum: signedEnum, testrequirednestedflatbuffer: testrequirednestedflatbuffer, - scalarKeySortedTables: scalarKeySortedTables?.map((e) => e.unpack())?.toList()); + scalarKeySortedTables: scalarKeySortedTables?.map((e) => e.unpack()).toList()); - static int pack(fb.Builder fbBuilder, MonsterT object) { + static int pack(fb.Builder fbBuilder, MonsterT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -1191,22 +1172,22 @@ class Monster { /// an example documentation comment: "monster object" class MonsterT { - Vec3T pos; + Vec3T? pos; int mana; int hp; - String name; - List<int> inventory; + String? name; + List<int>? inventory; Color color; - AnyTypeId testType; - dynamic test; - List<TestT> test4; - List<String> testarrayofstring; + AnyTypeId? testType; + dynamic? test; + List<TestT>? test4; + List<String>? testarrayofstring; /// an example documentation comment: this will end up in the generated code /// multiline too - List<MonsterT> testarrayoftables; - MonsterT enemy; - List<int> testnestedflatbuffer; - StatT testempty; + List<MonsterT>? testarrayoftables; + MonsterT? enemy; + List<int>? testnestedflatbuffer; + StatT? testempty; bool testbool; int testhashs32Fnv1; int testhashu32Fnv1; @@ -1216,41 +1197,41 @@ class MonsterT { int testhashu32Fnv1a; int testhashs64Fnv1a; int testhashu64Fnv1a; - List<bool> testarrayofbools; + List<bool>? testarrayofbools; double testf; double testf2; double testf3; - List<String> testarrayofstring2; - List<AbilityT> testarrayofsortedstruct; - List<int> flex; - List<TestT> test5; - List<int> vectorOfLongs; - List<double> vectorOfDoubles; - my_game.InParentNamespaceT parentNamespaceTest; - List<ReferrableT> vectorOfReferrables; + List<String>? testarrayofstring2; + List<AbilityT>? testarrayofsortedstruct; + List<int>? flex; + List<TestT>? test5; + List<int>? vectorOfLongs; + List<double>? vectorOfDoubles; + my_game.InParentNamespaceT? parentNamespaceTest; + List<ReferrableT>? vectorOfReferrables; int singleWeakReference; - List<int> vectorOfWeakReferences; - List<ReferrableT> vectorOfStrongReferrables; + List<int>? vectorOfWeakReferences; + List<ReferrableT>? vectorOfStrongReferrables; int coOwningReference; - List<int> vectorOfCoOwningReferences; + List<int>? vectorOfCoOwningReferences; int nonOwningReference; - List<int> vectorOfNonOwningReferences; - AnyUniqueAliasesTypeId anyUniqueType; - dynamic anyUnique; - AnyAmbiguousAliasesTypeId anyAmbiguousType; - dynamic anyAmbiguous; - List<Color> vectorOfEnums; + List<int>? vectorOfNonOwningReferences; + AnyUniqueAliasesTypeId? anyUniqueType; + dynamic? anyUnique; + AnyAmbiguousAliasesTypeId? anyAmbiguousType; + dynamic? anyAmbiguous; + List<Color>? vectorOfEnums; Race signedEnum; - List<int> testrequirednestedflatbuffer; - List<StatT> scalarKeySortedTables; + List<int>? testrequirednestedflatbuffer; + List<StatT>? scalarKeySortedTables; MonsterT({ this.pos, - this.mana, - this.hp, + this.mana = 150, + this.hp = 100, this.name, this.inventory, - this.color, + this.color = Color.Blue, this.testType, this.test, this.test4, @@ -1259,19 +1240,19 @@ class MonsterT { this.enemy, this.testnestedflatbuffer, this.testempty, - this.testbool, - this.testhashs32Fnv1, - this.testhashu32Fnv1, - this.testhashs64Fnv1, - this.testhashu64Fnv1, - this.testhashs32Fnv1a, - this.testhashu32Fnv1a, - this.testhashs64Fnv1a, - this.testhashu64Fnv1a, + this.testbool = false, + this.testhashs32Fnv1 = 0, + this.testhashu32Fnv1 = 0, + this.testhashs64Fnv1 = 0, + this.testhashu64Fnv1 = 0, + this.testhashs32Fnv1a = 0, + this.testhashu32Fnv1a = 0, + this.testhashs64Fnv1a = 0, + this.testhashu64Fnv1a = 0, this.testarrayofbools, - this.testf, - this.testf2, - this.testf3, + this.testf = 3.14159, + this.testf2 = 3.0, + this.testf3 = 0.0, this.testarrayofstring2, this.testarrayofsortedstruct, this.flex, @@ -1280,133 +1261,113 @@ class MonsterT { this.vectorOfDoubles, this.parentNamespaceTest, this.vectorOfReferrables, - this.singleWeakReference, + this.singleWeakReference = 0, this.vectorOfWeakReferences, this.vectorOfStrongReferrables, - this.coOwningReference, + this.coOwningReference = 0, this.vectorOfCoOwningReferences, - this.nonOwningReference, + this.nonOwningReference = 0, this.vectorOfNonOwningReferences, this.anyUniqueType, this.anyUnique, this.anyAmbiguousType, this.anyAmbiguous, this.vectorOfEnums, - this.signedEnum, + this.signedEnum = Race.None, this.testrequirednestedflatbuffer, this.scalarKeySortedTables}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int nameOffset = fbBuilder.writeString(name); - final int inventoryOffset = inventory?.isNotEmpty == true - ? fbBuilder.writeListUint8(inventory) + final int? nameOffset = fbBuilder.writeString(name); + final int? inventoryOffset = inventory?.isNotEmpty == true + ? fbBuilder.writeListUint8(inventory!) : null; - final int testOffset = test?.pack(fbBuilder); - int test4Offset = null; + final int? testOffset = test?.pack(fbBuilder); + int? test4Offset = null; if (test4?.isNotEmpty == true) { - test4.forEach((e) => e.pack(fbBuilder)); - test4Offset = fbBuilder.endStructVector(test4.length); + test4!.forEach((e) => e.pack(fbBuilder)); + test4Offset = fbBuilder.endStructVector(test4!.length); } - final int testarrayofstringOffset = testarrayofstring?.isNotEmpty == true - ? fbBuilder.writeList(testarrayofstring.map((b) => fbBuilder.writeString(b)).toList()) + final int? testarrayofstringOffset = testarrayofstring?.isNotEmpty == true + ? fbBuilder.writeList(testarrayofstring!.map((b) => fbBuilder.writeString(b)!).toList()) : null; - final int testarrayoftablesOffset = testarrayoftables?.isNotEmpty == true - ? fbBuilder.writeList(testarrayoftables.map((b) => b.pack(fbBuilder)).toList()) + final int? testarrayoftablesOffset = testarrayoftables?.isNotEmpty == true + ? fbBuilder.writeList(testarrayoftables!.map((b) => b.pack(fbBuilder)).toList()) : null; - final int enemyOffset = enemy?.pack(fbBuilder); - final int testnestedflatbufferOffset = testnestedflatbuffer?.isNotEmpty == true - ? fbBuilder.writeListUint8(testnestedflatbuffer) + final int? enemyOffset = enemy?.pack(fbBuilder); + final int? testnestedflatbufferOffset = testnestedflatbuffer?.isNotEmpty == true + ? fbBuilder.writeListUint8(testnestedflatbuffer!) : null; - final int testemptyOffset = testempty?.pack(fbBuilder); - final int testarrayofboolsOffset = testarrayofbools?.isNotEmpty == true - ? fbBuilder.writeListBool(testarrayofbools) + final int? testemptyOffset = testempty?.pack(fbBuilder); + final int? testarrayofboolsOffset = testarrayofbools?.isNotEmpty == true + ? fbBuilder.writeListBool(testarrayofbools!) : null; - final int testarrayofstring2Offset = testarrayofstring2?.isNotEmpty == true - ? fbBuilder.writeList(testarrayofstring2.map((b) => fbBuilder.writeString(b)).toList()) + final int? testarrayofstring2Offset = testarrayofstring2?.isNotEmpty == true + ? fbBuilder.writeList(testarrayofstring2!.map((b) => fbBuilder.writeString(b)!).toList()) : null; - int testarrayofsortedstructOffset = null; + int? testarrayofsortedstructOffset = null; if (testarrayofsortedstruct?.isNotEmpty == true) { - testarrayofsortedstruct.forEach((e) => e.pack(fbBuilder)); - testarrayofsortedstructOffset = fbBuilder.endStructVector(testarrayofsortedstruct.length); + testarrayofsortedstruct!.forEach((e) => e.pack(fbBuilder)); + testarrayofsortedstructOffset = fbBuilder.endStructVector(testarrayofsortedstruct!.length); } - final int flexOffset = flex?.isNotEmpty == true - ? fbBuilder.writeListUint8(flex) + final int? flexOffset = flex?.isNotEmpty == true + ? fbBuilder.writeListUint8(flex!) : null; - int test5Offset = null; + int? test5Offset = null; if (test5?.isNotEmpty == true) { - test5.forEach((e) => e.pack(fbBuilder)); - test5Offset = fbBuilder.endStructVector(test5.length); + test5!.forEach((e) => e.pack(fbBuilder)); + test5Offset = fbBuilder.endStructVector(test5!.length); } - final int vectorOfLongsOffset = vectorOfLongs?.isNotEmpty == true - ? fbBuilder.writeListInt64(vectorOfLongs) + final int? vectorOfLongsOffset = vectorOfLongs?.isNotEmpty == true + ? fbBuilder.writeListInt64(vectorOfLongs!) : null; - final int vectorOfDoublesOffset = vectorOfDoubles?.isNotEmpty == true - ? fbBuilder.writeListFloat64(vectorOfDoubles) + final int? vectorOfDoublesOffset = vectorOfDoubles?.isNotEmpty == true + ? fbBuilder.writeListFloat64(vectorOfDoubles!) : null; - final int parentNamespaceTestOffset = parentNamespaceTest?.pack(fbBuilder); - final int vectorOfReferrablesOffset = vectorOfReferrables?.isNotEmpty == true - ? fbBuilder.writeList(vectorOfReferrables.map((b) => b.pack(fbBuilder)).toList()) + final int? parentNamespaceTestOffset = parentNamespaceTest?.pack(fbBuilder); + final int? vectorOfReferrablesOffset = vectorOfReferrables?.isNotEmpty == true + ? fbBuilder.writeList(vectorOfReferrables!.map((b) => b.pack(fbBuilder)).toList()) : null; - final int vectorOfWeakReferencesOffset = vectorOfWeakReferences?.isNotEmpty == true - ? fbBuilder.writeListUint64(vectorOfWeakReferences) + final int? vectorOfWeakReferencesOffset = vectorOfWeakReferences?.isNotEmpty == true + ? fbBuilder.writeListUint64(vectorOfWeakReferences!) : null; - final int vectorOfStrongReferrablesOffset = vectorOfStrongReferrables?.isNotEmpty == true - ? fbBuilder.writeList(vectorOfStrongReferrables.map((b) => b.pack(fbBuilder)).toList()) + final int? vectorOfStrongReferrablesOffset = vectorOfStrongReferrables?.isNotEmpty == true + ? fbBuilder.writeList(vectorOfStrongReferrables!.map((b) => b.pack(fbBuilder)).toList()) : null; - final int vectorOfCoOwningReferencesOffset = vectorOfCoOwningReferences?.isNotEmpty == true - ? fbBuilder.writeListUint64(vectorOfCoOwningReferences) + final int? vectorOfCoOwningReferencesOffset = vectorOfCoOwningReferences?.isNotEmpty == true + ? fbBuilder.writeListUint64(vectorOfCoOwningReferences!) : null; - final int vectorOfNonOwningReferencesOffset = vectorOfNonOwningReferences?.isNotEmpty == true - ? fbBuilder.writeListUint64(vectorOfNonOwningReferences) + final int? vectorOfNonOwningReferencesOffset = vectorOfNonOwningReferences?.isNotEmpty == true + ? fbBuilder.writeListUint64(vectorOfNonOwningReferences!) : null; - final int anyUniqueOffset = anyUnique?.pack(fbBuilder); - final int anyAmbiguousOffset = anyAmbiguous?.pack(fbBuilder); - final int vectorOfEnumsOffset = vectorOfEnums?.isNotEmpty == true - ? fbBuilder.writeListUint8(vectorOfEnums.map((f) => f.value).toList()) + final int? anyUniqueOffset = anyUnique?.pack(fbBuilder); + final int? anyAmbiguousOffset = anyAmbiguous?.pack(fbBuilder); + final int? vectorOfEnumsOffset = vectorOfEnums?.isNotEmpty == true + ? fbBuilder.writeListUint8(vectorOfEnums!.map((f) => f.value).toList()) : null; - final int testrequirednestedflatbufferOffset = testrequirednestedflatbuffer?.isNotEmpty == true - ? fbBuilder.writeListUint8(testrequirednestedflatbuffer) + final int? testrequirednestedflatbufferOffset = testrequirednestedflatbuffer?.isNotEmpty == true + ? fbBuilder.writeListUint8(testrequirednestedflatbuffer!) : null; - final int scalarKeySortedTablesOffset = scalarKeySortedTables?.isNotEmpty == true - ? fbBuilder.writeList(scalarKeySortedTables.map((b) => b.pack(fbBuilder)).toList()) + final int? scalarKeySortedTablesOffset = scalarKeySortedTables?.isNotEmpty == true + ? fbBuilder.writeList(scalarKeySortedTables!.map((b) => b.pack(fbBuilder)).toList()) : null; - fbBuilder.startTable(); if (pos != null) { - fbBuilder.addStruct(0, pos.pack(fbBuilder)); + fbBuilder.addStruct(0, pos!.pack(fbBuilder)); } fbBuilder.addInt16(1, mana); fbBuilder.addInt16(2, hp); - if (nameOffset != null) { - fbBuilder.addOffset(3, nameOffset); - } - if (inventoryOffset != null) { - fbBuilder.addOffset(5, inventoryOffset); - } - fbBuilder.addUint8(6, color?.value); + fbBuilder.addOffset(3, nameOffset); + fbBuilder.addOffset(5, inventoryOffset); + fbBuilder.addUint8(6, color.value); fbBuilder.addUint8(7, testType?.value); - if (testOffset != null) { - fbBuilder.addOffset(8, testOffset); - } - if (test4Offset != null) { - fbBuilder.addOffset(9, test4Offset); - } - if (testarrayofstringOffset != null) { - fbBuilder.addOffset(10, testarrayofstringOffset); - } - if (testarrayoftablesOffset != null) { - fbBuilder.addOffset(11, testarrayoftablesOffset); - } - if (enemyOffset != null) { - fbBuilder.addOffset(12, enemyOffset); - } - if (testnestedflatbufferOffset != null) { - fbBuilder.addOffset(13, testnestedflatbufferOffset); - } - if (testemptyOffset != null) { - fbBuilder.addOffset(14, testemptyOffset); - } + fbBuilder.addOffset(8, testOffset); + fbBuilder.addOffset(9, test4Offset); + fbBuilder.addOffset(10, testarrayofstringOffset); + fbBuilder.addOffset(11, testarrayoftablesOffset); + fbBuilder.addOffset(12, enemyOffset); + fbBuilder.addOffset(13, testnestedflatbufferOffset); + fbBuilder.addOffset(14, testemptyOffset); fbBuilder.addBool(15, testbool); fbBuilder.addInt32(16, testhashs32Fnv1); fbBuilder.addUint32(17, testhashu32Fnv1); @@ -1416,69 +1377,33 @@ class MonsterT { fbBuilder.addUint32(21, testhashu32Fnv1a); fbBuilder.addInt64(22, testhashs64Fnv1a); fbBuilder.addUint64(23, testhashu64Fnv1a); - if (testarrayofboolsOffset != null) { - fbBuilder.addOffset(24, testarrayofboolsOffset); - } + fbBuilder.addOffset(24, testarrayofboolsOffset); fbBuilder.addFloat32(25, testf); fbBuilder.addFloat32(26, testf2); fbBuilder.addFloat32(27, testf3); - if (testarrayofstring2Offset != null) { - fbBuilder.addOffset(28, testarrayofstring2Offset); - } - if (testarrayofsortedstructOffset != null) { - fbBuilder.addOffset(29, testarrayofsortedstructOffset); - } - if (flexOffset != null) { - fbBuilder.addOffset(30, flexOffset); - } - if (test5Offset != null) { - fbBuilder.addOffset(31, test5Offset); - } - if (vectorOfLongsOffset != null) { - fbBuilder.addOffset(32, vectorOfLongsOffset); - } - if (vectorOfDoublesOffset != null) { - fbBuilder.addOffset(33, vectorOfDoublesOffset); - } - if (parentNamespaceTestOffset != null) { - fbBuilder.addOffset(34, parentNamespaceTestOffset); - } - if (vectorOfReferrablesOffset != null) { - fbBuilder.addOffset(35, vectorOfReferrablesOffset); - } + fbBuilder.addOffset(28, testarrayofstring2Offset); + fbBuilder.addOffset(29, testarrayofsortedstructOffset); + fbBuilder.addOffset(30, flexOffset); + fbBuilder.addOffset(31, test5Offset); + fbBuilder.addOffset(32, vectorOfLongsOffset); + fbBuilder.addOffset(33, vectorOfDoublesOffset); + fbBuilder.addOffset(34, parentNamespaceTestOffset); + fbBuilder.addOffset(35, vectorOfReferrablesOffset); fbBuilder.addUint64(36, singleWeakReference); - if (vectorOfWeakReferencesOffset != null) { - fbBuilder.addOffset(37, vectorOfWeakReferencesOffset); - } - if (vectorOfStrongReferrablesOffset != null) { - fbBuilder.addOffset(38, vectorOfStrongReferrablesOffset); - } + fbBuilder.addOffset(37, vectorOfWeakReferencesOffset); + fbBuilder.addOffset(38, vectorOfStrongReferrablesOffset); fbBuilder.addUint64(39, coOwningReference); - if (vectorOfCoOwningReferencesOffset != null) { - fbBuilder.addOffset(40, vectorOfCoOwningReferencesOffset); - } + fbBuilder.addOffset(40, vectorOfCoOwningReferencesOffset); fbBuilder.addUint64(41, nonOwningReference); - if (vectorOfNonOwningReferencesOffset != null) { - fbBuilder.addOffset(42, vectorOfNonOwningReferencesOffset); - } + fbBuilder.addOffset(42, vectorOfNonOwningReferencesOffset); fbBuilder.addUint8(43, anyUniqueType?.value); - if (anyUniqueOffset != null) { - fbBuilder.addOffset(44, anyUniqueOffset); - } + fbBuilder.addOffset(44, anyUniqueOffset); fbBuilder.addUint8(45, anyAmbiguousType?.value); - if (anyAmbiguousOffset != null) { - fbBuilder.addOffset(46, anyAmbiguousOffset); - } - if (vectorOfEnumsOffset != null) { - fbBuilder.addOffset(47, vectorOfEnumsOffset); - } - fbBuilder.addInt8(48, signedEnum?.value); - if (testrequirednestedflatbufferOffset != null) { - fbBuilder.addOffset(49, testrequirednestedflatbufferOffset); - } - if (scalarKeySortedTablesOffset != null) { - fbBuilder.addOffset(50, scalarKeySortedTablesOffset); - } + fbBuilder.addOffset(46, anyAmbiguousOffset); + fbBuilder.addOffset(47, vectorOfEnumsOffset); + fbBuilder.addInt8(48, signedEnum.value); + fbBuilder.addOffset(49, testrequirednestedflatbufferOffset); + fbBuilder.addOffset(50, scalarKeySortedTablesOffset); return fbBuilder.endTable(); } @@ -1497,9 +1422,7 @@ class _MonsterReader extends fb.TableReader<Monster> { } class MonsterBuilder { - MonsterBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + MonsterBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -1511,199 +1434,199 @@ class MonsterBuilder { fbBuilder.addStruct(0, offset); return fbBuilder.offset; } - int addMana(int mana) { + int addMana(int? mana) { fbBuilder.addInt16(1, mana); return fbBuilder.offset; } - int addHp(int hp) { + int addHp(int? hp) { fbBuilder.addInt16(2, hp); return fbBuilder.offset; } - int addNameOffset(int offset) { + int addNameOffset(int? offset) { fbBuilder.addOffset(3, offset); return fbBuilder.offset; } - int addInventoryOffset(int offset) { + int addInventoryOffset(int? offset) { fbBuilder.addOffset(5, offset); return fbBuilder.offset; } - int addColor(Color color) { + int addColor(Color? color) { fbBuilder.addUint8(6, color?.value); return fbBuilder.offset; } - int addTestType(AnyTypeId testType) { + int addTestType(AnyTypeId? testType) { fbBuilder.addUint8(7, testType?.value); return fbBuilder.offset; } - int addTestOffset(int offset) { + int addTestOffset(int? offset) { fbBuilder.addOffset(8, offset); return fbBuilder.offset; } - int addTest4Offset(int offset) { + int addTest4Offset(int? offset) { fbBuilder.addOffset(9, offset); return fbBuilder.offset; } - int addTestarrayofstringOffset(int offset) { + int addTestarrayofstringOffset(int? offset) { fbBuilder.addOffset(10, offset); return fbBuilder.offset; } - int addTestarrayoftablesOffset(int offset) { + int addTestarrayoftablesOffset(int? offset) { fbBuilder.addOffset(11, offset); return fbBuilder.offset; } - int addEnemyOffset(int offset) { + int addEnemyOffset(int? offset) { fbBuilder.addOffset(12, offset); return fbBuilder.offset; } - int addTestnestedflatbufferOffset(int offset) { + int addTestnestedflatbufferOffset(int? offset) { fbBuilder.addOffset(13, offset); return fbBuilder.offset; } - int addTestemptyOffset(int offset) { + int addTestemptyOffset(int? offset) { fbBuilder.addOffset(14, offset); return fbBuilder.offset; } - int addTestbool(bool testbool) { + int addTestbool(bool? testbool) { fbBuilder.addBool(15, testbool); return fbBuilder.offset; } - int addTesthashs32Fnv1(int testhashs32Fnv1) { + int addTesthashs32Fnv1(int? testhashs32Fnv1) { fbBuilder.addInt32(16, testhashs32Fnv1); return fbBuilder.offset; } - int addTesthashu32Fnv1(int testhashu32Fnv1) { + int addTesthashu32Fnv1(int? testhashu32Fnv1) { fbBuilder.addUint32(17, testhashu32Fnv1); return fbBuilder.offset; } - int addTesthashs64Fnv1(int testhashs64Fnv1) { + int addTesthashs64Fnv1(int? testhashs64Fnv1) { fbBuilder.addInt64(18, testhashs64Fnv1); return fbBuilder.offset; } - int addTesthashu64Fnv1(int testhashu64Fnv1) { + int addTesthashu64Fnv1(int? testhashu64Fnv1) { fbBuilder.addUint64(19, testhashu64Fnv1); return fbBuilder.offset; } - int addTesthashs32Fnv1a(int testhashs32Fnv1a) { + int addTesthashs32Fnv1a(int? testhashs32Fnv1a) { fbBuilder.addInt32(20, testhashs32Fnv1a); return fbBuilder.offset; } - int addTesthashu32Fnv1a(int testhashu32Fnv1a) { + int addTesthashu32Fnv1a(int? testhashu32Fnv1a) { fbBuilder.addUint32(21, testhashu32Fnv1a); return fbBuilder.offset; } - int addTesthashs64Fnv1a(int testhashs64Fnv1a) { + int addTesthashs64Fnv1a(int? testhashs64Fnv1a) { fbBuilder.addInt64(22, testhashs64Fnv1a); return fbBuilder.offset; } - int addTesthashu64Fnv1a(int testhashu64Fnv1a) { + int addTesthashu64Fnv1a(int? testhashu64Fnv1a) { fbBuilder.addUint64(23, testhashu64Fnv1a); return fbBuilder.offset; } - int addTestarrayofboolsOffset(int offset) { + int addTestarrayofboolsOffset(int? offset) { fbBuilder.addOffset(24, offset); return fbBuilder.offset; } - int addTestf(double testf) { + int addTestf(double? testf) { fbBuilder.addFloat32(25, testf); return fbBuilder.offset; } - int addTestf2(double testf2) { + int addTestf2(double? testf2) { fbBuilder.addFloat32(26, testf2); return fbBuilder.offset; } - int addTestf3(double testf3) { + int addTestf3(double? testf3) { fbBuilder.addFloat32(27, testf3); return fbBuilder.offset; } - int addTestarrayofstring2Offset(int offset) { + int addTestarrayofstring2Offset(int? offset) { fbBuilder.addOffset(28, offset); return fbBuilder.offset; } - int addTestarrayofsortedstructOffset(int offset) { + int addTestarrayofsortedstructOffset(int? offset) { fbBuilder.addOffset(29, offset); return fbBuilder.offset; } - int addFlexOffset(int offset) { + int addFlexOffset(int? offset) { fbBuilder.addOffset(30, offset); return fbBuilder.offset; } - int addTest5Offset(int offset) { + int addTest5Offset(int? offset) { fbBuilder.addOffset(31, offset); return fbBuilder.offset; } - int addVectorOfLongsOffset(int offset) { + int addVectorOfLongsOffset(int? offset) { fbBuilder.addOffset(32, offset); return fbBuilder.offset; } - int addVectorOfDoublesOffset(int offset) { + int addVectorOfDoublesOffset(int? offset) { fbBuilder.addOffset(33, offset); return fbBuilder.offset; } - int addParentNamespaceTestOffset(int offset) { + int addParentNamespaceTestOffset(int? offset) { fbBuilder.addOffset(34, offset); return fbBuilder.offset; } - int addVectorOfReferrablesOffset(int offset) { + int addVectorOfReferrablesOffset(int? offset) { fbBuilder.addOffset(35, offset); return fbBuilder.offset; } - int addSingleWeakReference(int singleWeakReference) { + int addSingleWeakReference(int? singleWeakReference) { fbBuilder.addUint64(36, singleWeakReference); return fbBuilder.offset; } - int addVectorOfWeakReferencesOffset(int offset) { + int addVectorOfWeakReferencesOffset(int? offset) { fbBuilder.addOffset(37, offset); return fbBuilder.offset; } - int addVectorOfStrongReferrablesOffset(int offset) { + int addVectorOfStrongReferrablesOffset(int? offset) { fbBuilder.addOffset(38, offset); return fbBuilder.offset; } - int addCoOwningReference(int coOwningReference) { + int addCoOwningReference(int? coOwningReference) { fbBuilder.addUint64(39, coOwningReference); return fbBuilder.offset; } - int addVectorOfCoOwningReferencesOffset(int offset) { + int addVectorOfCoOwningReferencesOffset(int? offset) { fbBuilder.addOffset(40, offset); return fbBuilder.offset; } - int addNonOwningReference(int nonOwningReference) { + int addNonOwningReference(int? nonOwningReference) { fbBuilder.addUint64(41, nonOwningReference); return fbBuilder.offset; } - int addVectorOfNonOwningReferencesOffset(int offset) { + int addVectorOfNonOwningReferencesOffset(int? offset) { fbBuilder.addOffset(42, offset); return fbBuilder.offset; } - int addAnyUniqueType(AnyUniqueAliasesTypeId anyUniqueType) { + int addAnyUniqueType(AnyUniqueAliasesTypeId? anyUniqueType) { fbBuilder.addUint8(43, anyUniqueType?.value); return fbBuilder.offset; } - int addAnyUniqueOffset(int offset) { + int addAnyUniqueOffset(int? offset) { fbBuilder.addOffset(44, offset); return fbBuilder.offset; } - int addAnyAmbiguousType(AnyAmbiguousAliasesTypeId anyAmbiguousType) { + int addAnyAmbiguousType(AnyAmbiguousAliasesTypeId? anyAmbiguousType) { fbBuilder.addUint8(45, anyAmbiguousType?.value); return fbBuilder.offset; } - int addAnyAmbiguousOffset(int offset) { + int addAnyAmbiguousOffset(int? offset) { fbBuilder.addOffset(46, offset); return fbBuilder.offset; } - int addVectorOfEnumsOffset(int offset) { + int addVectorOfEnumsOffset(int? offset) { fbBuilder.addOffset(47, offset); return fbBuilder.offset; } - int addSignedEnum(Race signedEnum) { + int addSignedEnum(Race? signedEnum) { fbBuilder.addInt8(48, signedEnum?.value); return fbBuilder.offset; } - int addTestrequirednestedflatbufferOffset(int offset) { + int addTestrequirednestedflatbufferOffset(int? offset) { fbBuilder.addOffset(49, offset); return fbBuilder.offset; } - int addScalarKeySortedTablesOffset(int offset) { + int addScalarKeySortedTablesOffset(int? offset) { fbBuilder.addOffset(50, offset); return fbBuilder.offset; } @@ -1714,108 +1637,108 @@ class MonsterBuilder { } class MonsterObjectBuilder extends fb.ObjectBuilder { - final Vec3ObjectBuilder _pos; - final int _mana; - final int _hp; - final String _name; - final List<int> _inventory; - final Color _color; - final AnyTypeId _testType; - final dynamic _test; - final List<TestObjectBuilder> _test4; - final List<String> _testarrayofstring; - final List<MonsterObjectBuilder> _testarrayoftables; - final MonsterObjectBuilder _enemy; - final List<int> _testnestedflatbuffer; - final StatObjectBuilder _testempty; - final bool _testbool; - final int _testhashs32Fnv1; - final int _testhashu32Fnv1; - final int _testhashs64Fnv1; - final int _testhashu64Fnv1; - final int _testhashs32Fnv1a; - final int _testhashu32Fnv1a; - final int _testhashs64Fnv1a; - final int _testhashu64Fnv1a; - final List<bool> _testarrayofbools; - final double _testf; - final double _testf2; - final double _testf3; - final List<String> _testarrayofstring2; - final List<AbilityObjectBuilder> _testarrayofsortedstruct; - final List<int> _flex; - final List<TestObjectBuilder> _test5; - final List<int> _vectorOfLongs; - final List<double> _vectorOfDoubles; - final my_game.InParentNamespaceObjectBuilder _parentNamespaceTest; - final List<ReferrableObjectBuilder> _vectorOfReferrables; - final int _singleWeakReference; - final List<int> _vectorOfWeakReferences; - final List<ReferrableObjectBuilder> _vectorOfStrongReferrables; - final int _coOwningReference; - final List<int> _vectorOfCoOwningReferences; - final int _nonOwningReference; - final List<int> _vectorOfNonOwningReferences; - final AnyUniqueAliasesTypeId _anyUniqueType; - final dynamic _anyUnique; - final AnyAmbiguousAliasesTypeId _anyAmbiguousType; - final dynamic _anyAmbiguous; - final List<Color> _vectorOfEnums; - final Race _signedEnum; - final List<int> _testrequirednestedflatbuffer; - final List<StatObjectBuilder> _scalarKeySortedTables; + final Vec3ObjectBuilder? _pos; + final int? _mana; + final int? _hp; + final String? _name; + final List<int>? _inventory; + final Color? _color; + final AnyTypeId? _testType; + final dynamic? _test; + final List<TestObjectBuilder>? _test4; + final List<String>? _testarrayofstring; + final List<MonsterObjectBuilder>? _testarrayoftables; + final MonsterObjectBuilder? _enemy; + final List<int>? _testnestedflatbuffer; + final StatObjectBuilder? _testempty; + final bool? _testbool; + final int? _testhashs32Fnv1; + final int? _testhashu32Fnv1; + final int? _testhashs64Fnv1; + final int? _testhashu64Fnv1; + final int? _testhashs32Fnv1a; + final int? _testhashu32Fnv1a; + final int? _testhashs64Fnv1a; + final int? _testhashu64Fnv1a; + final List<bool>? _testarrayofbools; + final double? _testf; + final double? _testf2; + final double? _testf3; + final List<String>? _testarrayofstring2; + final List<AbilityObjectBuilder>? _testarrayofsortedstruct; + final List<int>? _flex; + final List<TestObjectBuilder>? _test5; + final List<int>? _vectorOfLongs; + final List<double>? _vectorOfDoubles; + final my_game.InParentNamespaceObjectBuilder? _parentNamespaceTest; + final List<ReferrableObjectBuilder>? _vectorOfReferrables; + final int? _singleWeakReference; + final List<int>? _vectorOfWeakReferences; + final List<ReferrableObjectBuilder>? _vectorOfStrongReferrables; + final int? _coOwningReference; + final List<int>? _vectorOfCoOwningReferences; + final int? _nonOwningReference; + final List<int>? _vectorOfNonOwningReferences; + final AnyUniqueAliasesTypeId? _anyUniqueType; + final dynamic? _anyUnique; + final AnyAmbiguousAliasesTypeId? _anyAmbiguousType; + final dynamic? _anyAmbiguous; + final List<Color>? _vectorOfEnums; + final Race? _signedEnum; + final List<int>? _testrequirednestedflatbuffer; + final List<StatObjectBuilder>? _scalarKeySortedTables; MonsterObjectBuilder({ - Vec3ObjectBuilder pos, - int mana, - int hp, - String name, - List<int> inventory, - Color color, - AnyTypeId testType, - dynamic test, - List<TestObjectBuilder> test4, - List<String> testarrayofstring, - List<MonsterObjectBuilder> testarrayoftables, - MonsterObjectBuilder enemy, - List<int> testnestedflatbuffer, - StatObjectBuilder testempty, - bool testbool, - int testhashs32Fnv1, - int testhashu32Fnv1, - int testhashs64Fnv1, - int testhashu64Fnv1, - int testhashs32Fnv1a, - int testhashu32Fnv1a, - int testhashs64Fnv1a, - int testhashu64Fnv1a, - List<bool> testarrayofbools, - double testf, - double testf2, - double testf3, - List<String> testarrayofstring2, - List<AbilityObjectBuilder> testarrayofsortedstruct, - List<int> flex, - List<TestObjectBuilder> test5, - List<int> vectorOfLongs, - List<double> vectorOfDoubles, - my_game.InParentNamespaceObjectBuilder parentNamespaceTest, - List<ReferrableObjectBuilder> vectorOfReferrables, - int singleWeakReference, - List<int> vectorOfWeakReferences, - List<ReferrableObjectBuilder> vectorOfStrongReferrables, - int coOwningReference, - List<int> vectorOfCoOwningReferences, - int nonOwningReference, - List<int> vectorOfNonOwningReferences, - AnyUniqueAliasesTypeId anyUniqueType, - dynamic anyUnique, - AnyAmbiguousAliasesTypeId anyAmbiguousType, - dynamic anyAmbiguous, - List<Color> vectorOfEnums, - Race signedEnum, - List<int> testrequirednestedflatbuffer, - List<StatObjectBuilder> scalarKeySortedTables, + Vec3ObjectBuilder? pos, + int? mana, + int? hp, + String? name, + List<int>? inventory, + Color? color, + AnyTypeId? testType, + dynamic? test, + List<TestObjectBuilder>? test4, + List<String>? testarrayofstring, + List<MonsterObjectBuilder>? testarrayoftables, + MonsterObjectBuilder? enemy, + List<int>? testnestedflatbuffer, + StatObjectBuilder? testempty, + bool? testbool, + int? testhashs32Fnv1, + int? testhashu32Fnv1, + int? testhashs64Fnv1, + int? testhashu64Fnv1, + int? testhashs32Fnv1a, + int? testhashu32Fnv1a, + int? testhashs64Fnv1a, + int? testhashu64Fnv1a, + List<bool>? testarrayofbools, + double? testf, + double? testf2, + double? testf3, + List<String>? testarrayofstring2, + List<AbilityObjectBuilder>? testarrayofsortedstruct, + List<int>? flex, + List<TestObjectBuilder>? test5, + List<int>? vectorOfLongs, + List<double>? vectorOfDoubles, + my_game.InParentNamespaceObjectBuilder? parentNamespaceTest, + List<ReferrableObjectBuilder>? vectorOfReferrables, + int? singleWeakReference, + List<int>? vectorOfWeakReferences, + List<ReferrableObjectBuilder>? vectorOfStrongReferrables, + int? coOwningReference, + List<int>? vectorOfCoOwningReferences, + int? nonOwningReference, + List<int>? vectorOfNonOwningReferences, + AnyUniqueAliasesTypeId? anyUniqueType, + dynamic? anyUnique, + AnyAmbiguousAliasesTypeId? anyAmbiguousType, + dynamic? anyAmbiguous, + List<Color>? vectorOfEnums, + Race? signedEnum, + List<int>? testrequirednestedflatbuffer, + List<StatObjectBuilder>? scalarKeySortedTables, }) : _pos = pos, _mana = mana, @@ -1870,112 +1793,91 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int nameOffset = fbBuilder.writeString(_name); - final int inventoryOffset = _inventory?.isNotEmpty == true - ? fbBuilder.writeListUint8(_inventory) + int finish(fb.Builder fbBuilder) { + final int? nameOffset = fbBuilder.writeString(_name); + final int? inventoryOffset = _inventory?.isNotEmpty == true + ? fbBuilder.writeListUint8(_inventory!) : null; - final int testOffset = _test?.getOrCreateOffset(fbBuilder); - final int test4Offset = _test4?.isNotEmpty == true - ? fbBuilder.writeListOfStructs(_test4) + final int? testOffset = _test?.getOrCreateOffset(fbBuilder); + final int? test4Offset = _test4?.isNotEmpty == true + ? fbBuilder.writeListOfStructs(_test4!) : null; - final int testarrayofstringOffset = _testarrayofstring?.isNotEmpty == true - ? fbBuilder.writeList(_testarrayofstring.map((b) => fbBuilder.writeString(b)).toList()) + final int? testarrayofstringOffset = _testarrayofstring?.isNotEmpty == true + ? fbBuilder.writeList(_testarrayofstring!.map((b) => fbBuilder.writeString(b)!).toList()) : null; - final int testarrayoftablesOffset = _testarrayoftables?.isNotEmpty == true - ? fbBuilder.writeList(_testarrayoftables.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) + final int? testarrayoftablesOffset = _testarrayoftables?.isNotEmpty == true + ? fbBuilder.writeList(_testarrayoftables!.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) : null; - final int enemyOffset = _enemy?.getOrCreateOffset(fbBuilder); - final int testnestedflatbufferOffset = _testnestedflatbuffer?.isNotEmpty == true - ? fbBuilder.writeListUint8(_testnestedflatbuffer) + final int? enemyOffset = _enemy?.getOrCreateOffset(fbBuilder); + final int? testnestedflatbufferOffset = _testnestedflatbuffer?.isNotEmpty == true + ? fbBuilder.writeListUint8(_testnestedflatbuffer!) : null; - final int testemptyOffset = _testempty?.getOrCreateOffset(fbBuilder); - final int testarrayofboolsOffset = _testarrayofbools?.isNotEmpty == true - ? fbBuilder.writeListBool(_testarrayofbools) + final int? testemptyOffset = _testempty?.getOrCreateOffset(fbBuilder); + final int? testarrayofboolsOffset = _testarrayofbools?.isNotEmpty == true + ? fbBuilder.writeListBool(_testarrayofbools!) : null; - final int testarrayofstring2Offset = _testarrayofstring2?.isNotEmpty == true - ? fbBuilder.writeList(_testarrayofstring2.map((b) => fbBuilder.writeString(b)).toList()) + final int? testarrayofstring2Offset = _testarrayofstring2?.isNotEmpty == true + ? fbBuilder.writeList(_testarrayofstring2!.map((b) => fbBuilder.writeString(b)!).toList()) : null; - final int testarrayofsortedstructOffset = _testarrayofsortedstruct?.isNotEmpty == true - ? fbBuilder.writeListOfStructs(_testarrayofsortedstruct) + final int? testarrayofsortedstructOffset = _testarrayofsortedstruct?.isNotEmpty == true + ? fbBuilder.writeListOfStructs(_testarrayofsortedstruct!) : null; - final int flexOffset = _flex?.isNotEmpty == true - ? fbBuilder.writeListUint8(_flex) + final int? flexOffset = _flex?.isNotEmpty == true + ? fbBuilder.writeListUint8(_flex!) : null; - final int test5Offset = _test5?.isNotEmpty == true - ? fbBuilder.writeListOfStructs(_test5) + final int? test5Offset = _test5?.isNotEmpty == true + ? fbBuilder.writeListOfStructs(_test5!) : null; - final int vectorOfLongsOffset = _vectorOfLongs?.isNotEmpty == true - ? fbBuilder.writeListInt64(_vectorOfLongs) + final int? vectorOfLongsOffset = _vectorOfLongs?.isNotEmpty == true + ? fbBuilder.writeListInt64(_vectorOfLongs!) : null; - final int vectorOfDoublesOffset = _vectorOfDoubles?.isNotEmpty == true - ? fbBuilder.writeListFloat64(_vectorOfDoubles) + final int? vectorOfDoublesOffset = _vectorOfDoubles?.isNotEmpty == true + ? fbBuilder.writeListFloat64(_vectorOfDoubles!) : null; - final int parentNamespaceTestOffset = _parentNamespaceTest?.getOrCreateOffset(fbBuilder); - final int vectorOfReferrablesOffset = _vectorOfReferrables?.isNotEmpty == true - ? fbBuilder.writeList(_vectorOfReferrables.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) + final int? parentNamespaceTestOffset = _parentNamespaceTest?.getOrCreateOffset(fbBuilder); + final int? vectorOfReferrablesOffset = _vectorOfReferrables?.isNotEmpty == true + ? fbBuilder.writeList(_vectorOfReferrables!.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) : null; - final int vectorOfWeakReferencesOffset = _vectorOfWeakReferences?.isNotEmpty == true - ? fbBuilder.writeListUint64(_vectorOfWeakReferences) + final int? vectorOfWeakReferencesOffset = _vectorOfWeakReferences?.isNotEmpty == true + ? fbBuilder.writeListUint64(_vectorOfWeakReferences!) : null; - final int vectorOfStrongReferrablesOffset = _vectorOfStrongReferrables?.isNotEmpty == true - ? fbBuilder.writeList(_vectorOfStrongReferrables.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) + final int? vectorOfStrongReferrablesOffset = _vectorOfStrongReferrables?.isNotEmpty == true + ? fbBuilder.writeList(_vectorOfStrongReferrables!.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) : null; - final int vectorOfCoOwningReferencesOffset = _vectorOfCoOwningReferences?.isNotEmpty == true - ? fbBuilder.writeListUint64(_vectorOfCoOwningReferences) + final int? vectorOfCoOwningReferencesOffset = _vectorOfCoOwningReferences?.isNotEmpty == true + ? fbBuilder.writeListUint64(_vectorOfCoOwningReferences!) : null; - final int vectorOfNonOwningReferencesOffset = _vectorOfNonOwningReferences?.isNotEmpty == true - ? fbBuilder.writeListUint64(_vectorOfNonOwningReferences) + final int? vectorOfNonOwningReferencesOffset = _vectorOfNonOwningReferences?.isNotEmpty == true + ? fbBuilder.writeListUint64(_vectorOfNonOwningReferences!) : null; - final int anyUniqueOffset = _anyUnique?.getOrCreateOffset(fbBuilder); - final int anyAmbiguousOffset = _anyAmbiguous?.getOrCreateOffset(fbBuilder); - final int vectorOfEnumsOffset = _vectorOfEnums?.isNotEmpty == true - ? fbBuilder.writeListUint8(_vectorOfEnums.map((f) => f.value).toList()) + final int? anyUniqueOffset = _anyUnique?.getOrCreateOffset(fbBuilder); + final int? anyAmbiguousOffset = _anyAmbiguous?.getOrCreateOffset(fbBuilder); + final int? vectorOfEnumsOffset = _vectorOfEnums?.isNotEmpty == true + ? fbBuilder.writeListUint8(_vectorOfEnums!.map((f) => f.value).toList()) : null; - final int testrequirednestedflatbufferOffset = _testrequirednestedflatbuffer?.isNotEmpty == true - ? fbBuilder.writeListUint8(_testrequirednestedflatbuffer) + final int? testrequirednestedflatbufferOffset = _testrequirednestedflatbuffer?.isNotEmpty == true + ? fbBuilder.writeListUint8(_testrequirednestedflatbuffer!) : null; - final int scalarKeySortedTablesOffset = _scalarKeySortedTables?.isNotEmpty == true - ? fbBuilder.writeList(_scalarKeySortedTables.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) + final int? scalarKeySortedTablesOffset = _scalarKeySortedTables?.isNotEmpty == true + ? fbBuilder.writeList(_scalarKeySortedTables!.map((b) => b.getOrCreateOffset(fbBuilder)).toList()) : null; - fbBuilder.startTable(); if (_pos != null) { - fbBuilder.addStruct(0, _pos.finish(fbBuilder)); + fbBuilder.addStruct(0, _pos!.finish(fbBuilder)); } fbBuilder.addInt16(1, _mana); fbBuilder.addInt16(2, _hp); - if (nameOffset != null) { - fbBuilder.addOffset(3, nameOffset); - } - if (inventoryOffset != null) { - fbBuilder.addOffset(5, inventoryOffset); - } + fbBuilder.addOffset(3, nameOffset); + fbBuilder.addOffset(5, inventoryOffset); fbBuilder.addUint8(6, _color?.value); fbBuilder.addUint8(7, _testType?.value); - if (testOffset != null) { - fbBuilder.addOffset(8, testOffset); - } - if (test4Offset != null) { - fbBuilder.addOffset(9, test4Offset); - } - if (testarrayofstringOffset != null) { - fbBuilder.addOffset(10, testarrayofstringOffset); - } - if (testarrayoftablesOffset != null) { - fbBuilder.addOffset(11, testarrayoftablesOffset); - } - if (enemyOffset != null) { - fbBuilder.addOffset(12, enemyOffset); - } - if (testnestedflatbufferOffset != null) { - fbBuilder.addOffset(13, testnestedflatbufferOffset); - } - if (testemptyOffset != null) { - fbBuilder.addOffset(14, testemptyOffset); - } + fbBuilder.addOffset(8, testOffset); + fbBuilder.addOffset(9, test4Offset); + fbBuilder.addOffset(10, testarrayofstringOffset); + fbBuilder.addOffset(11, testarrayoftablesOffset); + fbBuilder.addOffset(12, enemyOffset); + fbBuilder.addOffset(13, testnestedflatbufferOffset); + fbBuilder.addOffset(14, testemptyOffset); fbBuilder.addBool(15, _testbool); fbBuilder.addInt32(16, _testhashs32Fnv1); fbBuilder.addUint32(17, _testhashu32Fnv1); @@ -1985,75 +1887,39 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { fbBuilder.addUint32(21, _testhashu32Fnv1a); fbBuilder.addInt64(22, _testhashs64Fnv1a); fbBuilder.addUint64(23, _testhashu64Fnv1a); - if (testarrayofboolsOffset != null) { - fbBuilder.addOffset(24, testarrayofboolsOffset); - } + fbBuilder.addOffset(24, testarrayofboolsOffset); fbBuilder.addFloat32(25, _testf); fbBuilder.addFloat32(26, _testf2); fbBuilder.addFloat32(27, _testf3); - if (testarrayofstring2Offset != null) { - fbBuilder.addOffset(28, testarrayofstring2Offset); - } - if (testarrayofsortedstructOffset != null) { - fbBuilder.addOffset(29, testarrayofsortedstructOffset); - } - if (flexOffset != null) { - fbBuilder.addOffset(30, flexOffset); - } - if (test5Offset != null) { - fbBuilder.addOffset(31, test5Offset); - } - if (vectorOfLongsOffset != null) { - fbBuilder.addOffset(32, vectorOfLongsOffset); - } - if (vectorOfDoublesOffset != null) { - fbBuilder.addOffset(33, vectorOfDoublesOffset); - } - if (parentNamespaceTestOffset != null) { - fbBuilder.addOffset(34, parentNamespaceTestOffset); - } - if (vectorOfReferrablesOffset != null) { - fbBuilder.addOffset(35, vectorOfReferrablesOffset); - } + fbBuilder.addOffset(28, testarrayofstring2Offset); + fbBuilder.addOffset(29, testarrayofsortedstructOffset); + fbBuilder.addOffset(30, flexOffset); + fbBuilder.addOffset(31, test5Offset); + fbBuilder.addOffset(32, vectorOfLongsOffset); + fbBuilder.addOffset(33, vectorOfDoublesOffset); + fbBuilder.addOffset(34, parentNamespaceTestOffset); + fbBuilder.addOffset(35, vectorOfReferrablesOffset); fbBuilder.addUint64(36, _singleWeakReference); - if (vectorOfWeakReferencesOffset != null) { - fbBuilder.addOffset(37, vectorOfWeakReferencesOffset); - } - if (vectorOfStrongReferrablesOffset != null) { - fbBuilder.addOffset(38, vectorOfStrongReferrablesOffset); - } + fbBuilder.addOffset(37, vectorOfWeakReferencesOffset); + fbBuilder.addOffset(38, vectorOfStrongReferrablesOffset); fbBuilder.addUint64(39, _coOwningReference); - if (vectorOfCoOwningReferencesOffset != null) { - fbBuilder.addOffset(40, vectorOfCoOwningReferencesOffset); - } + fbBuilder.addOffset(40, vectorOfCoOwningReferencesOffset); fbBuilder.addUint64(41, _nonOwningReference); - if (vectorOfNonOwningReferencesOffset != null) { - fbBuilder.addOffset(42, vectorOfNonOwningReferencesOffset); - } + fbBuilder.addOffset(42, vectorOfNonOwningReferencesOffset); fbBuilder.addUint8(43, _anyUniqueType?.value); - if (anyUniqueOffset != null) { - fbBuilder.addOffset(44, anyUniqueOffset); - } + fbBuilder.addOffset(44, anyUniqueOffset); fbBuilder.addUint8(45, _anyAmbiguousType?.value); - if (anyAmbiguousOffset != null) { - fbBuilder.addOffset(46, anyAmbiguousOffset); - } - if (vectorOfEnumsOffset != null) { - fbBuilder.addOffset(47, vectorOfEnumsOffset); - } + fbBuilder.addOffset(46, anyAmbiguousOffset); + fbBuilder.addOffset(47, vectorOfEnumsOffset); fbBuilder.addInt8(48, _signedEnum?.value); - if (testrequirednestedflatbufferOffset != null) { - fbBuilder.addOffset(49, testrequirednestedflatbufferOffset); - } - if (scalarKeySortedTablesOffset != null) { - fbBuilder.addOffset(50, scalarKeySortedTablesOffset); - } + fbBuilder.addOffset(49, testrequirednestedflatbufferOffset); + fbBuilder.addOffset(50, scalarKeySortedTablesOffset); return fbBuilder.endTable(); } /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); @@ -2081,8 +1947,8 @@ class TypeAliases { int get u64 => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 18, 0); double get f32 => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 20, 0.0); double get f64 => const fb.Float64Reader().vTableGet(_bc, _bcOffset, 22, 0.0); - List<int> get v8 => const fb.ListReader<int>(const fb.Int8Reader()).vTableGet(_bc, _bcOffset, 24, null); - List<double> get vf64 => const fb.ListReader<double>(const fb.Float64Reader()).vTableGet(_bc, _bcOffset, 26, null); + List<int>? get v8 => const fb.ListReader<int>(const fb.Int8Reader()).vTableGetNullable(_bc, _bcOffset, 24); + List<double>? get vf64 => const fb.ListReader<double>(const fb.Float64Reader()).vTableGetNullable(_bc, _bcOffset, 26); @override String toString() { @@ -2103,7 +1969,7 @@ class TypeAliases { v8: v8, vf64: vf64); - static int pack(fb.Builder fbBuilder, TypeAliasesT object) { + static int pack(fb.Builder fbBuilder, TypeAliasesT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -2120,32 +1986,30 @@ class TypeAliasesT { int u64; double f32; double f64; - List<int> v8; - List<double> vf64; + List<int>? v8; + List<double>? vf64; TypeAliasesT({ - this.i8, - this.u8, - this.i16, - this.u16, - this.i32, - this.u32, - this.i64, - this.u64, - this.f32, - this.f64, + this.i8 = 0, + this.u8 = 0, + this.i16 = 0, + this.u16 = 0, + this.i32 = 0, + this.u32 = 0, + this.i64 = 0, + this.u64 = 0, + this.f32 = 0.0, + this.f64 = 0.0, this.v8, this.vf64}); int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int v8Offset = v8?.isNotEmpty == true - ? fbBuilder.writeListInt8(v8) + final int? v8Offset = v8?.isNotEmpty == true + ? fbBuilder.writeListInt8(v8!) : null; - final int vf64Offset = vf64?.isNotEmpty == true - ? fbBuilder.writeListFloat64(vf64) + final int? vf64Offset = vf64?.isNotEmpty == true + ? fbBuilder.writeListFloat64(vf64!) : null; - fbBuilder.startTable(); fbBuilder.addInt8(0, i8); fbBuilder.addUint8(1, u8); @@ -2157,12 +2021,8 @@ class TypeAliasesT { fbBuilder.addUint64(7, u64); fbBuilder.addFloat32(8, f32); fbBuilder.addFloat64(9, f64); - if (v8Offset != null) { - fbBuilder.addOffset(10, v8Offset); - } - if (vf64Offset != null) { - fbBuilder.addOffset(11, vf64Offset); - } + fbBuilder.addOffset(10, v8Offset); + fbBuilder.addOffset(11, vf64Offset); return fbBuilder.endTable(); } @@ -2181,9 +2041,7 @@ class _TypeAliasesReader extends fb.TableReader<TypeAliases> { } class TypeAliasesBuilder { - TypeAliasesBuilder(this.fbBuilder) { - assert(fbBuilder != null); - } + TypeAliasesBuilder(this.fbBuilder) {} final fb.Builder fbBuilder; @@ -2191,51 +2049,51 @@ class TypeAliasesBuilder { fbBuilder.startTable(); } - int addI8(int i8) { + int addI8(int? i8) { fbBuilder.addInt8(0, i8); return fbBuilder.offset; } - int addU8(int u8) { + int addU8(int? u8) { fbBuilder.addUint8(1, u8); return fbBuilder.offset; } - int addI16(int i16) { + int addI16(int? i16) { fbBuilder.addInt16(2, i16); return fbBuilder.offset; } - int addU16(int u16) { + int addU16(int? u16) { fbBuilder.addUint16(3, u16); return fbBuilder.offset; } - int addI32(int i32) { + int addI32(int? i32) { fbBuilder.addInt32(4, i32); return fbBuilder.offset; } - int addU32(int u32) { + int addU32(int? u32) { fbBuilder.addUint32(5, u32); return fbBuilder.offset; } - int addI64(int i64) { + int addI64(int? i64) { fbBuilder.addInt64(6, i64); return fbBuilder.offset; } - int addU64(int u64) { + int addU64(int? u64) { fbBuilder.addUint64(7, u64); return fbBuilder.offset; } - int addF32(double f32) { + int addF32(double? f32) { fbBuilder.addFloat32(8, f32); return fbBuilder.offset; } - int addF64(double f64) { + int addF64(double? f64) { fbBuilder.addFloat64(9, f64); return fbBuilder.offset; } - int addV8Offset(int offset) { + int addV8Offset(int? offset) { fbBuilder.addOffset(10, offset); return fbBuilder.offset; } - int addVf64Offset(int offset) { + int addVf64Offset(int? offset) { fbBuilder.addOffset(11, offset); return fbBuilder.offset; } @@ -2246,32 +2104,32 @@ class TypeAliasesBuilder { } class TypeAliasesObjectBuilder extends fb.ObjectBuilder { - final int _i8; - final int _u8; - final int _i16; - final int _u16; - final int _i32; - final int _u32; - final int _i64; - final int _u64; - final double _f32; - final double _f64; - final List<int> _v8; - final List<double> _vf64; + final int? _i8; + final int? _u8; + final int? _i16; + final int? _u16; + final int? _i32; + final int? _u32; + final int? _i64; + final int? _u64; + final double? _f32; + final double? _f64; + final List<int>? _v8; + final List<double>? _vf64; TypeAliasesObjectBuilder({ - int i8, - int u8, - int i16, - int u16, - int i32, - int u32, - int i64, - int u64, - double f32, - double f64, - List<int> v8, - List<double> vf64, + int? i8, + int? u8, + int? i16, + int? u16, + int? i32, + int? u32, + int? i64, + int? u64, + double? f32, + double? f64, + List<int>? v8, + List<double>? vf64, }) : _i8 = i8, _u8 = u8, @@ -2288,16 +2146,13 @@ class TypeAliasesObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - final int v8Offset = _v8?.isNotEmpty == true - ? fbBuilder.writeListInt8(_v8) + int finish(fb.Builder fbBuilder) { + final int? v8Offset = _v8?.isNotEmpty == true + ? fbBuilder.writeListInt8(_v8!) : null; - final int vf64Offset = _vf64?.isNotEmpty == true - ? fbBuilder.writeListFloat64(_vf64) + final int? vf64Offset = _vf64?.isNotEmpty == true + ? fbBuilder.writeListFloat64(_vf64!) : null; - fbBuilder.startTable(); fbBuilder.addInt8(0, _i8); fbBuilder.addUint8(1, _u8); @@ -2309,18 +2164,14 @@ class TypeAliasesObjectBuilder extends fb.ObjectBuilder { fbBuilder.addUint64(7, _u64); fbBuilder.addFloat32(8, _f32); fbBuilder.addFloat64(9, _f64); - if (v8Offset != null) { - fbBuilder.addOffset(10, v8Offset); - } - if (vf64Offset != null) { - fbBuilder.addOffset(11, vf64Offset); - } + fbBuilder.addOffset(10, v8Offset); + fbBuilder.addOffset(11, vf64Offset); return fbBuilder.endTable(); } /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); diff --git a/dart/test/monster_test_my_game_generated.dart b/dart/test/monster_test_my_game_generated.dart index d53550d1..146c4017 100644 --- a/dart/test/monster_test_my_game_generated.dart +++ b/dart/test/monster_test_my_game_generated.dart @@ -1,5 +1,5 @@ // automatically generated by the FlatBuffers compiler, do not modify -// ignore_for_file: unused_import, unused_field, unused_local_variable +// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable library my_game; @@ -29,7 +29,7 @@ class InParentNamespace { InParentNamespaceT unpack() => InParentNamespaceT(); - static int pack(fb.Builder fbBuilder, InParentNamespaceT object) { + static int pack(fb.Builder fbBuilder, InParentNamespaceT? object) { if (object == null) return 0; return object.pack(fbBuilder); } @@ -37,8 +37,6 @@ class InParentNamespace { class InParentNamespaceT { int pack(fb.Builder fbBuilder) { - assert(fbBuilder != null); - fbBuilder.startTable(); return fbBuilder.endTable(); } @@ -63,17 +61,14 @@ class InParentNamespaceObjectBuilder extends fb.ObjectBuilder { /// Finish building, and store into the [fbBuilder]. @override - int finish( - fb.Builder fbBuilder) { - assert(fbBuilder != null); - + int finish(fb.Builder fbBuilder) { fbBuilder.startTable(); return fbBuilder.endTable(); } /// Convenience method to serialize to byte list. @override - Uint8List toBytes([String fileIdentifier]) { + Uint8List toBytes([String? fileIdentifier]) { fb.Builder fbBuilder = new fb.Builder(); int offset = finish(fbBuilder); return fbBuilder.finish(offset, fileIdentifier); |