diff options
author | Will Hughes <insertjokehere@users.noreply.github.com> | 2022-06-29 03:16:47 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-28 11:16:47 -0400 |
commit | b9eea76a86081d5aa8d0ad83921f1f97fa16cf33 (patch) | |
tree | 37818c6ddd5adbf4d8ad3a77d320633b1f0c8874 /dart | |
parent | 1b90300150c79f2d64e494430156388cacd81b6f (diff) | |
download | flatbuffers-b9eea76a86081d5aa8d0ad83921f1f97fa16cf33.tar.gz flatbuffers-b9eea76a86081d5aa8d0ad83921f1f97fa16cf33.tar.bz2 flatbuffers-b9eea76a86081d5aa8d0ad83921f1f97fa16cf33.zip |
[Dart] Implement putBool to fix errors when serializing structs with bools (#7359)
* [Dart] Implement putBool to fix errors when serializing structs with bools
* Add tests
Diffstat (limited to 'dart')
-rw-r--r-- | dart/lib/flat_buffers.dart | 9 | ||||
-rw-r--r-- | dart/test/bool_structs.fbs | 10 | ||||
-rw-r--r-- | dart/test/flat_buffers_test.dart | 20 |
3 files changed, 37 insertions, 2 deletions
diff --git a/dart/lib/flat_buffers.dart b/dart/lib/flat_buffers.dart index d27d4bf3..c69e01f1 100644 --- a/dart/lib/flat_buffers.dart +++ b/dart/lib/flat_buffers.dart @@ -401,6 +401,15 @@ class Builder { _setFloat32AtTail(_tail, value); } + /// Writes a bool to the tail of the buffer after preparing space for it. + /// Bools are represented as a Uint8, with the value set to '1' for true, and '0' for false + /// + /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. + void putBool(bool value) { + _prepare(_sizeofUint8, 1); + _buf.setInt8(_buf.lengthInBytes - _tail, value ? 1 : 0); + } + /// Writes a Int64 to the tail of the buffer after preparing space for it. /// /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. diff --git a/dart/test/bool_structs.fbs b/dart/test/bool_structs.fbs new file mode 100644 index 00000000..47b26b5b --- /dev/null +++ b/dart/test/bool_structs.fbs @@ -0,0 +1,10 @@ +// Test for #7355 +table Foo { + my_foo : foo_properties; +} + +struct foo_properties +{ + a : bool; + b : bool; +} diff --git a/dart/test/flat_buffers_test.dart b/dart/test/flat_buffers_test.dart index 79207366..99a0e7c3 100644 --- a/dart/test/flat_buffers_test.dart +++ b/dart/test/flat_buffers_test.dart @@ -14,6 +14,7 @@ import 'package:test_reflective_loader/test_reflective_loader.dart'; import './monster_test_my_game.example_generated.dart' as example; import './monster_test_my_game.example2_generated.dart' as example2; import './list_of_enums_generated.dart' as example3; +import './bool_structs_generated.dart' as example4; import './keyword_test_keyword_test_generated.dart' as keyword_test; main() { @@ -910,8 +911,11 @@ class GeneratorTest { @reflectiveTest class ListOfEnumsTest { void test_listOfEnums() async { - var mytable = example3.MyTableObjectBuilder( - options: [example3.OptionsEnum.A, example3.OptionsEnum.B, example3.OptionsEnum.C]); + var mytable = example3.MyTableObjectBuilder(options: [ + example3.OptionsEnum.A, + example3.OptionsEnum.B, + example3.OptionsEnum.C + ]); var bytes = mytable.toBytes(); var mytable_read = example3.MyTable(bytes); expect(mytable_read.options![0].value, example3.OptionsEnum.A.value); @@ -919,3 +923,15 @@ class ListOfEnumsTest { expect(mytable_read.options![2].value, example3.OptionsEnum.C.value); } } + +@reflectiveTest +class BoolInStructTest { + void test_boolInStruct() async { + var mystruct = example4.FooObjectBuilder( + myFoo: example4.FooPropertiesObjectBuilder(a: true, b: false)); + var bytes = mystruct.toBytes(); + var mystruct_read = example4.Foo(bytes); + expect(mystruct_read.myFoo!.a, true); + expect(mystruct_read.myFoo!.b, false); + } +} |