From efcbdc7698987643ed713a9285f81863b4000f05 Mon Sep 17 00:00:00 2001 From: William Bain Date: Mon, 25 Jan 2021 12:33:47 -0500 Subject: [PATCH] [Rust] Ensure unions are referenced with the correct path (#6422) * Add codegen test for namespaced unions * [Rust] Handle cross-namespace union use * [Rust] Test namespace handling * [Rust] Drop trailing whitespace in codegen * [Rust] Set flags in generate_code.bat to match .sh * [C#] Add additional namespace test file --- samples/monster_generated.rs | 4 +- src/idl_gen_rust.cpp | 12 +- .../FlatBuffers.Core.Test.csproj | 3 + .../FlatBuffers.Test/FlatBuffers.Test.csproj | 3 + tests/generate_code.bat | 10 +- tests/generate_code.sh | 6 +- tests/monster_test_generated.rs | 12 +- .../NamespaceA/NamespaceB/UnionInNestedNS.cs | 77 +++ .../NamespaceA/NamespaceB/UnionInNestedNS.go | 36 ++ .../NamespaceB/UnionInNestedNS.java | 14 + .../NamespaceA/NamespaceB/UnionInNestedNS.kt | 14 + .../NamespaceA/NamespaceB/UnionInNestedNS.lua | 10 + .../NamespaceA/NamespaceB/UnionInNestedNS.php | 23 + .../NamespaceA/NamespaceB/UnionInNestedNS.py | 12 + .../NamespaceA/TableInFirstNS.cs | 43 +- .../NamespaceA/TableInFirstNS.go | 44 +- .../NamespaceA/TableInFirstNS.java | 10 +- .../NamespaceA/TableInFirstNS.kt | 25 +- .../NamespaceA/TableInFirstNS.lua | 23 +- .../NamespaceA/TableInFirstNS.php | 45 +- .../NamespaceA/TableInFirstNS.py | 36 +- tests/namespace_test/namespace_test1.fbs | 4 + .../namespace_test1_generated.h | 191 ++++++ .../namespace_test1_generated.lobster | 4 + .../namespace_test1_generated.rs | 132 ++++ ...st1_namespace_a.namespace_b_generated.dart | 39 ++ tests/namespace_test/namespace_test2.fbs | 1 + .../namespace_test2_generated.h | 51 +- .../namespace_test2_generated.lobster | 16 +- .../namespace_test2_generated.rs | 565 +++++++++++++++++- ...namespace_test2_namespace_a_generated.dart | 34 +- .../rust_usage_test/tests/integration_test.rs | 6 + 32 files changed, 1448 insertions(+), 57 deletions(-) create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.cs create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.go create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.java create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.kt create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.lua create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.php create mode 100644 tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.py diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs index 62a9c2d54..13b2cf14d 100644 --- a/samples/monster_generated.rs +++ b/samples/monster_generated.rs @@ -212,7 +212,7 @@ impl Default for EquipmentT { } } impl EquipmentT { - fn equipment_type(&self) -> Equipment { + pub fn equipment_type(&self) -> Equipment { match self { Self::NONE => Equipment::NONE, Self::Weapon(_) => Equipment::Weapon, @@ -689,7 +689,7 @@ impl std::fmt::Debug for Monster<'_> { ds.field("equipped", &"InvalidFlatbuffer: Union discriminant does not match value.") } }, - _ => { + _ => { let x: Option<()> = None; ds.field("equipped", &x) }, diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 0bd279968..1677ce825 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -759,7 +759,7 @@ class RustGenerator : public BaseGenerator { code_ += "impl flatbuffers::SimpleToVerifyInSlice for {{ENUM_NAME}} {}"; if (enum_def.is_union) { - // Generate tyoesafe offset(s) for unions + // Generate typesafe offset(s) for unions code_.SetValue("NAME", Name(enum_def)); code_.SetValue("UNION_OFFSET_NAME", Name(enum_def) + "UnionTableOffset"); code_ += "pub struct {{UNION_OFFSET_NAME}} {}"; @@ -808,7 +808,7 @@ class RustGenerator : public BaseGenerator { // Get flatbuffers union key. // CASPER: add docstrings? - code_ += " fn {{ENUM_NAME_SNAKE}}_type(&self) -> {{ENUM_NAME}} {"; + code_ += " pub fn {{ENUM_NAME_SNAKE}}_type(&self) -> {{ENUM_NAME}} {"; code_ += " match self {"; code_ += " Self::NONE => {{ENUM_NAME}}::NONE,"; ForAllUnionObjectVariantsBesidesNone(enum_def, [&] { @@ -1501,8 +1501,8 @@ class RustGenerator : public BaseGenerator { case ftUnionKey: return; case ftUnionValue: { const auto &enum_def = *type.enum_def; - code_.SetValue("ENUM_NAME", Name(enum_def)); - code_.SetValue("NATIVE_ENUM_NAME", NativeName(enum_def)); + code_.SetValue("ENUM_NAME", WrapInNameSpace(enum_def)); + code_.SetValue("NATIVE_ENUM_NAME", NamespacedNativeName(enum_def)); code_ += " let {{FIELD_NAME}} = match " "self.{{FIELD_NAME}}_type() {"; @@ -1730,7 +1730,7 @@ class RustGenerator : public BaseGenerator { } // Unions. EnumDef &union_def = *field.value.type.enum_def; - code_.SetValue("UNION_TYPE", Name(union_def)); + code_.SetValue("UNION_TYPE", WrapInNameSpace(union_def)); code_ += "\n .visit_union::<{{UNION_TYPE}}, _>(" "&\"{{FIELD_NAME}}_type\", Self::{{OFFSET_NAME}}_TYPE, " @@ -1878,7 +1878,7 @@ class RustGenerator : public BaseGenerator { code_ += " }"; code_ += " },"; }); - code_ += " _ => { "; + code_ += " _ => {"; code_ += " let x: Option<()> = None;"; code_ += " ds.field(\"{{FIELD_NAME}}\", &x)"; code_ += " },"; diff --git a/tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj b/tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj index bbbd7d812..201a17cad 100644 --- a/tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj +++ b/tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj @@ -110,6 +110,9 @@ NamespaceA\NamespaceB\TableInNestedNS.cs + + NamespaceA\NamespaceB\UnionInNestedNS.cs + NamespaceA\TableInFirstNS.cs diff --git a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj index 616ab1963..c59a8c2cf 100644 --- a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj +++ b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj @@ -141,6 +141,9 @@ NamespaceA\NamespaceB\TableInNestedNS.cs + + NamespaceA\NamespaceB\UnionInNestedNS.cs + NamespaceA\TableInFirstNS.cs diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 7972d1c78..f79abd635 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -30,19 +30,23 @@ if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" ( set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS% set TEST_CS_FLAGS=--cs-gen-json-serializer set TEST_JS_TS_FLAGS=--gen-name-strings -set TEST_RUST_FLAGS=--gen-name-strings set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api +set TEST_RUST_FLAGS=%TEST_BASE_FLAGS% --gen-name-strings set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes ..\%buildtype%\flatc.exe --binary --cpp --java --kotlin --csharp --dart --go --lobster --lua --ts --php --grpc ^ %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL -..\%buildtype%\flatc.exe --rust %TEST_NOINCL_FLAGS% %TEST_RUST_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL ..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL -..\%buildtype%\flatc.exe --binary --cpp --java --csharp --dart --go --lobster --lua --ts --php --python --rust ^ +..\%buildtype%\flatc.exe --binary --cpp --java --csharp --dart --go --lobster --lua --ts --php --python ^ %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% %TEST_JS_TS_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL +@rem For Rust we currently generate two independent schemas, with namespace_test2 +@rem duplicating the types in namespace_test1 +..\%buildtype%\flatc.exe --rust --gen-all %TEST_RUST_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL + ..\%buildtype%\flatc.exe --cpp --java --csharp --ts --php %TEST_BASE_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% %TEST_JS_TS_FLAGS% -o union_vector ./union_vector/union_vector.fbs || goto FAIL ..\%buildtype%\flatc.exe --rust -I include_test -o include_test include_test/include_test1.fbs || goto FAIL ..\%buildtype%\flatc.exe --rust -I include_test -o include_test/sub include_test/sub/include_test2.fbs || goto FAIL diff --git a/tests/generate_code.sh b/tests/generate_code.sh index f8229c497..1531281e3 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -37,9 +37,13 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS -I include_test monster_test.f ../flatc --python $TEST_BASE_FLAGS -I include_test monster_test.fbs monsterdata_test.json -../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --ts --php --python --rust \ +../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --ts --php --python \ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_JS_TS_FLAGS -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs +# For Rust we currently generate two independent schemas, with namespace_test2 +# duplicating the types in namespace_test1 +../flatc --rust --gen-all $TEST_RUST_FLAGS -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs + ../flatc --cpp --java --kotlin --csharp --ts --php $TEST_BASE_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_JS_TS_FLAGS -o union_vector ./union_vector/union_vector.fbs ../flatc --rust -I include_test -o include_test include_test/include_test1.fbs ../flatc --rust -I include_test -o include_test/sub include_test/sub/include_test2.fbs diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index 3f8714ba7..bae1066c4 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -498,7 +498,7 @@ impl Default for AnyT { } } impl AnyT { - fn any_type(&self) -> Any { + pub fn any_type(&self) -> Any { match self { Self::NONE => Any::NONE, Self::Monster(_) => Any::Monster, @@ -686,7 +686,7 @@ impl Default for AnyUniqueAliasesT { } } impl AnyUniqueAliasesT { - fn any_unique_aliases_type(&self) -> AnyUniqueAliases { + pub fn any_unique_aliases_type(&self) -> AnyUniqueAliases { match self { Self::NONE => AnyUniqueAliases::NONE, Self::M(_) => AnyUniqueAliases::M, @@ -874,7 +874,7 @@ impl Default for AnyAmbiguousAliasesT { } } impl AnyAmbiguousAliasesT { - fn any_ambiguous_aliases_type(&self) -> AnyAmbiguousAliases { + pub fn any_ambiguous_aliases_type(&self) -> AnyAmbiguousAliases { match self { Self::NONE => AnyAmbiguousAliases::NONE, Self::M1(_) => AnyAmbiguousAliases::M1, @@ -3004,7 +3004,7 @@ impl std::fmt::Debug for Monster<'_> { ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") } }, - _ => { + _ => { let x: Option<()> = None; ds.field("test", &x) }, @@ -3066,7 +3066,7 @@ impl std::fmt::Debug for Monster<'_> { ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") } }, - _ => { + _ => { let x: Option<()> = None; ds.field("any_unique", &x) }, @@ -3094,7 +3094,7 @@ impl std::fmt::Debug for Monster<'_> { ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") } }, - _ => { + _ => { let x: Option<()> = None; ds.field("any_ambiguous", &x) }, diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.cs b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.cs new file mode 100644 index 000000000..9924f32ba --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.cs @@ -0,0 +1,77 @@ +// +// automatically generated by the FlatBuffers compiler, do not modify +// + +namespace NamespaceA.NamespaceB +{ + +[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] +public enum UnionInNestedNS : byte +{ + NONE = 0, + TableInNestedNS = 1, +}; + +public class UnionInNestedNSUnion { + public UnionInNestedNS Type { get; set; } + public object Value { get; set; } + + public UnionInNestedNSUnion() { + this.Type = UnionInNestedNS.NONE; + this.Value = null; + } + + public T As() where T : class { return this.Value as T; } + public NamespaceA.NamespaceB.TableInNestedNST AsTableInNestedNS() { return this.As(); } + + public static int Pack(FlatBuffers.FlatBufferBuilder builder, UnionInNestedNSUnion _o) { + switch (_o.Type) { + default: return 0; + case UnionInNestedNS.TableInNestedNS: return NamespaceA.NamespaceB.TableInNestedNS.Pack(builder, _o.AsTableInNestedNS()).Value; + } + } +} + +public class UnionInNestedNSUnion_JsonConverter : Newtonsoft.Json.JsonConverter { + public override bool CanConvert(System.Type objectType) { + return objectType == typeof(UnionInNestedNSUnion) || objectType == typeof(System.Collections.Generic.List); + } + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { + var _olist = value as System.Collections.Generic.List; + if (_olist != null) { + writer.WriteStartArray(); + foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); } + writer.WriteEndArray(); + } else { + this.WriteJson(writer, value as UnionInNestedNSUnion, serializer); + } + } + public void WriteJson(Newtonsoft.Json.JsonWriter writer, UnionInNestedNSUnion _o, Newtonsoft.Json.JsonSerializer serializer) { + if (_o == null) return; + serializer.Serialize(writer, _o.Value); + } + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { + var _olist = existingValue as System.Collections.Generic.List; + if (_olist != null) { + for (var _j = 0; _j < _olist.Count; ++_j) { + reader.Read(); + _olist[_j] = this.ReadJson(reader, _olist[_j], serializer); + } + reader.Read(); + return _olist; + } else { + return this.ReadJson(reader, existingValue as UnionInNestedNSUnion, serializer); + } + } + public UnionInNestedNSUnion ReadJson(Newtonsoft.Json.JsonReader reader, UnionInNestedNSUnion _o, Newtonsoft.Json.JsonSerializer serializer) { + if (_o == null) return null; + switch (_o.Type) { + default: break; + case UnionInNestedNS.TableInNestedNS: _o.Value = serializer.Deserialize(reader); break; + } + return _o; + } +} + + +} diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.go b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.go new file mode 100644 index 000000000..f7b1d1c2d --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.go @@ -0,0 +1,36 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package NamespaceB + +import ( + "strconv" + + flatbuffers "github.com/google/flatbuffers/go" + + NamespaceA__NamespaceB "NamespaceA/NamespaceB" +) + +type UnionInNestedNST struct { + Type UnionInNestedNS + Value interface{} +} + +func (t *UnionInNestedNST) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + if t == nil { + return 0 + } + switch t.Type { + case UnionInNestedNSTableInNestedNS: + return t.Value.(*NamespaceA__NamespaceB.TableInNestedNST).Pack(builder) + } + return 0 +} + +func (rcv UnionInNestedNS) UnPack(table flatbuffers.Table) *UnionInNestedNST { + switch rcv { + case UnionInNestedNSTableInNestedNS: + x := TableInNestedNS{_tab: table} + return &NamespaceA__NamespaceB.UnionInNestedNST{ Type: UnionInNestedNSTableInNestedNS, Value: x.UnPack() } + } + return nil +} diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.java b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.java new file mode 100644 index 000000000..dc54b9eb1 --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.java @@ -0,0 +1,14 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package NamespaceA.NamespaceB; + +public final class UnionInNestedNS { + private UnionInNestedNS() { } + public static final byte NONE = 0; + public static final byte TableInNestedNS = 1; + + public static final String[] names = { "NONE", "TableInNestedNS", }; + + public static String name(int e) { return names[e]; } +} + diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.kt b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.kt new file mode 100644 index 000000000..1c52f95c2 --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.kt @@ -0,0 +1,14 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package NamespaceA.NamespaceB + +@Suppress("unused") +@ExperimentalUnsignedTypes +class UnionInNestedNS private constructor() { + companion object { + const val NONE: UByte = 0u + const val TableInNestedNS: UByte = 1u + val names : Array = arrayOf("NONE", "TableInNestedNS") + fun name(e: Int) : String = names[e] + } +} diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.lua b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.lua new file mode 100644 index 000000000..30bc93e51 --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.lua @@ -0,0 +1,10 @@ +-- automatically generated by the FlatBuffers compiler, do not modify + +-- namespace: NamespaceB + +local UnionInNestedNS = { + NONE = 0, + TableInNestedNS = 1, +} + +return UnionInNestedNS -- return the module \ No newline at end of file diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.php b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.php new file mode 100644 index 000000000..ff948d739 --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.php @@ -0,0 +1,23 @@ +"NONE", + UnionInNestedNS::TableInNestedNS=>"TableInNestedNS", + ); + + public static function Name($e) + { + if (!isset(self::$names[$e])) { + throw new \Exception(); + } + return self::$names[$e]; + } +} diff --git a/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.py b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.py new file mode 100644 index 000000000..66f03f245 --- /dev/null +++ b/tests/namespace_test/NamespaceA/NamespaceB/UnionInNestedNS.py @@ -0,0 +1,12 @@ +# automatically generated by the FlatBuffers compiler, do not modify + +# namespace: NamespaceB + + +def UnionInNestedNSCreator(unionType, table): + from flatbuffers.table import Table + if not isinstance(table, Table): + return None + if unionType == UnionInNestedNS().TableInNestedNS: + return TableInNestedNST.InitFromBuf(table.Bytes, table.Pos) + return None diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.cs b/tests/namespace_test/NamespaceA/TableInFirstNS.cs index 3d597d7f8..5426470e2 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.cs +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.cs @@ -22,23 +22,32 @@ public struct TableInFirstNS : IFlatbufferObject public NamespaceA.NamespaceB.TableInNestedNS? FooTable { get { int o = __p.__offset(4); return o != 0 ? (NamespaceA.NamespaceB.TableInNestedNS?)(new NamespaceA.NamespaceB.TableInNestedNS()).__assign(__p.__indirect(o + __p.bb_pos), __p.bb) : null; } } public NamespaceA.NamespaceB.EnumInNestedNS FooEnum { get { int o = __p.__offset(6); return o != 0 ? (NamespaceA.NamespaceB.EnumInNestedNS)__p.bb.GetSbyte(o + __p.bb_pos) : NamespaceA.NamespaceB.EnumInNestedNS.A; } } public bool MutateFooEnum(NamespaceA.NamespaceB.EnumInNestedNS foo_enum) { int o = __p.__offset(6); if (o != 0) { __p.bb.PutSbyte(o + __p.bb_pos, (sbyte)foo_enum); return true; } else { return false; } } - public NamespaceA.NamespaceB.StructInNestedNS? FooStruct { get { int o = __p.__offset(8); return o != 0 ? (NamespaceA.NamespaceB.StructInNestedNS?)(new NamespaceA.NamespaceB.StructInNestedNS()).__assign(o + __p.bb_pos, __p.bb) : null; } } + public NamespaceA.NamespaceB.UnionInNestedNS FooUnionType { get { int o = __p.__offset(8); return o != 0 ? (NamespaceA.NamespaceB.UnionInNestedNS)__p.bb.Get(o + __p.bb_pos) : NamespaceA.NamespaceB.UnionInNestedNS.NONE; } } + public TTable? FooUnion() where TTable : struct, IFlatbufferObject { int o = __p.__offset(10); return o != 0 ? (TTable?)__p.__union(o + __p.bb_pos) : null; } + public NamespaceA.NamespaceB.TableInNestedNS FooUnionAsTableInNestedNS() { return FooUnion().Value; } + public NamespaceA.NamespaceB.StructInNestedNS? FooStruct { get { int o = __p.__offset(12); return o != 0 ? (NamespaceA.NamespaceB.StructInNestedNS?)(new NamespaceA.NamespaceB.StructInNestedNS()).__assign(o + __p.bb_pos, __p.bb) : null; } } public static Offset CreateTableInFirstNS(FlatBufferBuilder builder, Offset foo_tableOffset = default(Offset), NamespaceA.NamespaceB.EnumInNestedNS foo_enum = NamespaceA.NamespaceB.EnumInNestedNS.A, + NamespaceA.NamespaceB.UnionInNestedNS foo_union_type = NamespaceA.NamespaceB.UnionInNestedNS.NONE, + int foo_unionOffset = 0, NamespaceA.NamespaceB.StructInNestedNST foo_struct = null) { - builder.StartTable(3); + builder.StartTable(5); TableInFirstNS.AddFooStruct(builder, NamespaceA.NamespaceB.StructInNestedNS.Pack(builder, foo_struct)); + TableInFirstNS.AddFooUnion(builder, foo_unionOffset); TableInFirstNS.AddFooTable(builder, foo_tableOffset); + TableInFirstNS.AddFooUnionType(builder, foo_union_type); TableInFirstNS.AddFooEnum(builder, foo_enum); return TableInFirstNS.EndTableInFirstNS(builder); } - public static void StartTableInFirstNS(FlatBufferBuilder builder) { builder.StartTable(3); } + public static void StartTableInFirstNS(FlatBufferBuilder builder) { builder.StartTable(5); } public static void AddFooTable(FlatBufferBuilder builder, Offset fooTableOffset) { builder.AddOffset(0, fooTableOffset.Value, 0); } public static void AddFooEnum(FlatBufferBuilder builder, NamespaceA.NamespaceB.EnumInNestedNS fooEnum) { builder.AddSbyte(1, (sbyte)fooEnum, 0); } - public static void AddFooStruct(FlatBufferBuilder builder, Offset fooStructOffset) { builder.AddStruct(2, fooStructOffset.Value, 0); } + public static void AddFooUnionType(FlatBufferBuilder builder, NamespaceA.NamespaceB.UnionInNestedNS fooUnionType) { builder.AddByte(2, (byte)fooUnionType, 0); } + public static void AddFooUnion(FlatBufferBuilder builder, int fooUnionOffset) { builder.AddOffset(3, fooUnionOffset, 0); } + public static void AddFooStruct(FlatBufferBuilder builder, Offset fooStructOffset) { builder.AddStruct(4, fooStructOffset.Value, 0); } public static Offset EndTableInFirstNS(FlatBufferBuilder builder) { int o = builder.EndTable(); return new Offset(o); @@ -51,15 +60,27 @@ public struct TableInFirstNS : IFlatbufferObject public void UnPackTo(TableInFirstNST _o) { _o.FooTable = this.FooTable.HasValue ? this.FooTable.Value.UnPack() : null; _o.FooEnum = this.FooEnum; + _o.FooUnion = new NamespaceA.NamespaceB.UnionInNestedNSUnion(); + _o.FooUnion.Type = this.FooUnionType; + switch (this.FooUnionType) { + default: break; + case NamespaceA.NamespaceB.UnionInNestedNS.TableInNestedNS: + _o.FooUnion.Value = this.FooUnion().HasValue ? this.FooUnion().Value.UnPack() : null; + break; + } _o.FooStruct = this.FooStruct.HasValue ? this.FooStruct.Value.UnPack() : null; } public static Offset Pack(FlatBufferBuilder builder, TableInFirstNST _o) { if (_o == null) return default(Offset); var _foo_table = _o.FooTable == null ? default(Offset) : NamespaceA.NamespaceB.TableInNestedNS.Pack(builder, _o.FooTable); + var _foo_union_type = _o.FooUnion == null ? NamespaceA.NamespaceB.UnionInNestedNS.NONE : _o.FooUnion.Type; + var _foo_union = _o.FooUnion == null ? 0 : NamespaceA.NamespaceB.UnionInNestedNSUnion.Pack(builder, _o.FooUnion); return CreateTableInFirstNS( builder, _foo_table, _o.FooEnum, + _foo_union_type, + _foo_union, _o.FooStruct); } }; @@ -70,12 +91,26 @@ public class TableInFirstNST public NamespaceA.NamespaceB.TableInNestedNST FooTable { get; set; } [Newtonsoft.Json.JsonProperty("foo_enum")] public NamespaceA.NamespaceB.EnumInNestedNS FooEnum { get; set; } + [Newtonsoft.Json.JsonProperty("foo_union_type")] + private NamespaceA.NamespaceB.UnionInNestedNS FooUnionType { + get { + return this.FooUnion != null ? this.FooUnion.Type : NamespaceA.NamespaceB.UnionInNestedNS.NONE; + } + set { + this.FooUnion = new NamespaceA.NamespaceB.UnionInNestedNSUnion(); + this.FooUnion.Type = value; + } + } + [Newtonsoft.Json.JsonProperty("foo_union")] + [Newtonsoft.Json.JsonConverter(typeof(NamespaceA.NamespaceB.UnionInNestedNSUnion_JsonConverter))] + public NamespaceA.NamespaceB.UnionInNestedNSUnion FooUnion { get; set; } [Newtonsoft.Json.JsonProperty("foo_struct")] public NamespaceA.NamespaceB.StructInNestedNST FooStruct { get; set; } public TableInFirstNST() { this.FooTable = null; this.FooEnum = NamespaceA.NamespaceB.EnumInNestedNS.A; + this.FooUnion = null; this.FooStruct = new NamespaceA.NamespaceB.StructInNestedNST(); } } diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.go b/tests/namespace_test/NamespaceA/TableInFirstNS.go index 7f419b8c1..3feaaaa1e 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.go +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.go @@ -11,15 +11,22 @@ import ( type TableInFirstNST struct { FooTable *NamespaceA__NamespaceB.TableInNestedNST FooEnum NamespaceA__NamespaceB.EnumInNestedNS + FooUnion *NamespaceA__NamespaceB.UnionInNestedNST FooStruct *NamespaceA__NamespaceB.StructInNestedNST } func (t *TableInFirstNST) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT { if t == nil { return 0 } fooTableOffset := t.FooTable.Pack(builder) + fooUnionOffset := t.FooUnion.Pack(builder) + TableInFirstNSStart(builder) TableInFirstNSAddFooTable(builder, fooTableOffset) TableInFirstNSAddFooEnum(builder, t.FooEnum) + if t.FooUnion != nil { + TableInFirstNSAddFooUnionType(builder, t.FooUnion.Type) + } + TableInFirstNSAddFooUnion(builder, fooUnionOffset) fooStructOffset := t.FooStruct.Pack(builder) TableInFirstNSAddFooStruct(builder, fooStructOffset) return TableInFirstNSEnd(builder) @@ -28,6 +35,10 @@ func (t *TableInFirstNST) Pack(builder *flatbuffers.Builder) flatbuffers.UOffset func (rcv *TableInFirstNS) UnPackTo(t *TableInFirstNST) { t.FooTable = rcv.FooTable(nil).UnPack() t.FooEnum = rcv.FooEnum() + fooUnionTable := flatbuffers.Table{} + if rcv.FooUnion(&fooUnionTable) { + t.FooUnion = rcv.FooUnionType().UnPack(fooUnionTable) + } t.FooStruct = rcv.FooStruct(nil).UnPack() } @@ -90,8 +101,29 @@ func (rcv *TableInFirstNS) MutateFooEnum(n NamespaceA__NamespaceB.EnumInNestedNS return rcv._tab.MutateInt8Slot(6, int8(n)) } -func (rcv *TableInFirstNS) FooStruct(obj *NamespaceA__NamespaceB.StructInNestedNS) *NamespaceA__NamespaceB.StructInNestedNS { +func (rcv *TableInFirstNS) FooUnionType() NamespaceA__NamespaceB.UnionInNestedNS { o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) + if o != 0 { + return NamespaceA__NamespaceB.UnionInNestedNS(rcv._tab.GetByte(o + rcv._tab.Pos)) + } + return 0 +} + +func (rcv *TableInFirstNS) MutateFooUnionType(n NamespaceA__NamespaceB.UnionInNestedNS) bool { + return rcv._tab.MutateByteSlot(8, byte(n)) +} + +func (rcv *TableInFirstNS) FooUnion(obj *flatbuffers.Table) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) + if o != 0 { + rcv._tab.Union(obj, o) + return true + } + return false +} + +func (rcv *TableInFirstNS) FooStruct(obj *NamespaceA__NamespaceB.StructInNestedNS) *NamespaceA__NamespaceB.StructInNestedNS { + o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) if o != 0 { x := o + rcv._tab.Pos if obj == nil { @@ -104,7 +136,7 @@ func (rcv *TableInFirstNS) FooStruct(obj *NamespaceA__NamespaceB.StructInNestedN } func TableInFirstNSStart(builder *flatbuffers.Builder) { - builder.StartObject(3) + builder.StartObject(5) } func TableInFirstNSAddFooTable(builder *flatbuffers.Builder, fooTable flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(fooTable), 0) @@ -112,8 +144,14 @@ func TableInFirstNSAddFooTable(builder *flatbuffers.Builder, fooTable flatbuffer func TableInFirstNSAddFooEnum(builder *flatbuffers.Builder, fooEnum NamespaceA__NamespaceB.EnumInNestedNS) { builder.PrependInt8Slot(1, int8(fooEnum), 0) } +func TableInFirstNSAddFooUnionType(builder *flatbuffers.Builder, fooUnionType NamespaceA__NamespaceB.UnionInNestedNS) { + builder.PrependByteSlot(2, byte(fooUnionType), 0) +} +func TableInFirstNSAddFooUnion(builder *flatbuffers.Builder, fooUnion flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(fooUnion), 0) +} func TableInFirstNSAddFooStruct(builder *flatbuffers.Builder, fooStruct flatbuffers.UOffsetT) { - builder.PrependStructSlot(2, flatbuffers.UOffsetT(fooStruct), 0) + builder.PrependStructSlot(4, flatbuffers.UOffsetT(fooStruct), 0) } func TableInFirstNSEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.java b/tests/namespace_test/NamespaceA/TableInFirstNS.java index f04d9e91a..f9ac66cea 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.java +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.java @@ -19,13 +19,17 @@ public final class TableInFirstNS extends Table { public NamespaceA.NamespaceB.TableInNestedNS fooTable(NamespaceA.NamespaceB.TableInNestedNS obj) { int o = __offset(4); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; } public byte fooEnum() { int o = __offset(6); return o != 0 ? bb.get(o + bb_pos) : 0; } public boolean mutateFooEnum(byte foo_enum) { int o = __offset(6); if (o != 0) { bb.put(o + bb_pos, foo_enum); return true; } else { return false; } } + public byte fooUnionType() { int o = __offset(8); return o != 0 ? bb.get(o + bb_pos) : 0; } + public Table fooUnion(Table obj) { int o = __offset(10); return o != 0 ? __union(obj, o + bb_pos) : null; } public NamespaceA.NamespaceB.StructInNestedNS fooStruct() { return fooStruct(new NamespaceA.NamespaceB.StructInNestedNS()); } - public NamespaceA.NamespaceB.StructInNestedNS fooStruct(NamespaceA.NamespaceB.StructInNestedNS obj) { int o = __offset(8); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; } + public NamespaceA.NamespaceB.StructInNestedNS fooStruct(NamespaceA.NamespaceB.StructInNestedNS obj) { int o = __offset(12); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; } - public static void startTableInFirstNS(FlatBufferBuilder builder) { builder.startTable(3); } + public static void startTableInFirstNS(FlatBufferBuilder builder) { builder.startTable(5); } public static void addFooTable(FlatBufferBuilder builder, int fooTableOffset) { builder.addOffset(0, fooTableOffset, 0); } public static void addFooEnum(FlatBufferBuilder builder, byte fooEnum) { builder.addByte(1, fooEnum, 0); } - public static void addFooStruct(FlatBufferBuilder builder, int fooStructOffset) { builder.addStruct(2, fooStructOffset, 0); } + public static void addFooUnionType(FlatBufferBuilder builder, byte fooUnionType) { builder.addByte(2, fooUnionType, 0); } + public static void addFooUnion(FlatBufferBuilder builder, int fooUnionOffset) { builder.addOffset(3, fooUnionOffset, 0); } + public static void addFooStruct(FlatBufferBuilder builder, int fooStructOffset) { builder.addStruct(4, fooStructOffset, 0); } public static int endTableInFirstNS(FlatBufferBuilder builder) { int o = builder.endTable(); return o; diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.kt b/tests/namespace_test/NamespaceA/TableInFirstNS.kt index febe050cf..308ff2b00 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.kt +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.kt @@ -40,9 +40,26 @@ class TableInFirstNS : Table() { false } } + val fooUnionType : UByte + get() { + val o = __offset(8) + return if(o != 0) bb.get(o + bb_pos).toUByte() else 0u + } + fun mutateFooUnionType(fooUnionType: UByte) : Boolean { + val o = __offset(8) + return if (o != 0) { + bb.put(o + bb_pos, fooUnionType.toByte()) + true + } else { + false + } + } + fun fooUnion(obj: Table) : Table? { + val o = __offset(10); return if (o != 0) __union(obj, o + bb_pos) else null + } val fooStruct : NamespaceA.NamespaceB.StructInNestedNS? get() = fooStruct(NamespaceA.NamespaceB.StructInNestedNS()) fun fooStruct(obj: NamespaceA.NamespaceB.StructInNestedNS) : NamespaceA.NamespaceB.StructInNestedNS? { - val o = __offset(8) + val o = __offset(12) return if (o != 0) { obj.__assign(o + bb_pos, bb) } else { @@ -56,10 +73,12 @@ class TableInFirstNS : Table() { _bb.order(ByteOrder.LITTLE_ENDIAN) return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)) } - fun startTableInFirstNS(builder: FlatBufferBuilder) = builder.startTable(3) + fun startTableInFirstNS(builder: FlatBufferBuilder) = builder.startTable(5) fun addFooTable(builder: FlatBufferBuilder, fooTable: Int) = builder.addOffset(0, fooTable, 0) fun addFooEnum(builder: FlatBufferBuilder, fooEnum: Byte) = builder.addByte(1, fooEnum, 0) - fun addFooStruct(builder: FlatBufferBuilder, fooStruct: Int) = builder.addStruct(2, fooStruct, 0) + fun addFooUnionType(builder: FlatBufferBuilder, fooUnionType: UByte) = builder.addByte(2, fooUnionType.toByte(), 0) + fun addFooUnion(builder: FlatBufferBuilder, fooUnion: Int) = builder.addOffset(3, fooUnion, 0) + fun addFooStruct(builder: FlatBufferBuilder, fooStruct: Int) = builder.addStruct(4, fooStruct, 0) fun endTableInFirstNS(builder: FlatBufferBuilder) : Int { val o = builder.endTable() return o diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.lua b/tests/namespace_test/NamespaceA/TableInFirstNS.lua index f70f2c7bc..172e16fd0 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.lua +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.lua @@ -37,8 +37,23 @@ function TableInFirstNS_mt:FooEnum() end return 0 end -function TableInFirstNS_mt:FooStruct() +function TableInFirstNS_mt:FooUnionType() local o = self.view:Offset(8) + if o ~= 0 then + return self.view:Get(flatbuffers.N.Uint8, o + self.view.pos) + end + return 0 +end +function TableInFirstNS_mt:FooUnion() + local o = self.view:Offset(10) + if o ~= 0 then + local obj = flatbuffers.view.New(require('flatbuffers.binaryarray').New(0), 0) + self.view:Union(obj, o) + return obj + end +end +function TableInFirstNS_mt:FooStruct() + local o = self.view:Offset(12) if o ~= 0 then local x = o + self.view.pos local obj = require('NamespaceA.NamespaceB.StructInNestedNS').New() @@ -46,10 +61,12 @@ function TableInFirstNS_mt:FooStruct() return obj end end -function TableInFirstNS.Start(builder) builder:StartObject(3) end +function TableInFirstNS.Start(builder) builder:StartObject(5) end function TableInFirstNS.AddFooTable(builder, fooTable) builder:PrependUOffsetTRelativeSlot(0, fooTable, 0) end function TableInFirstNS.AddFooEnum(builder, fooEnum) builder:PrependInt8Slot(1, fooEnum, 0) end -function TableInFirstNS.AddFooStruct(builder, fooStruct) builder:PrependStructSlot(2, fooStruct, 0) end +function TableInFirstNS.AddFooUnionType(builder, fooUnionType) builder:PrependUint8Slot(2, fooUnionType, 0) end +function TableInFirstNS.AddFooUnion(builder, fooUnion) builder:PrependUOffsetTRelativeSlot(3, fooUnion, 0) end +function TableInFirstNS.AddFooStruct(builder, fooStruct) builder:PrependStructSlot(4, fooStruct, 0) end function TableInFirstNS.End(builder) return builder:EndObject() end return TableInFirstNS -- return the module \ No newline at end of file diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.php b/tests/namespace_test/NamespaceA/TableInFirstNS.php index 9fb29c370..637ead543 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.php +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.php @@ -48,10 +48,28 @@ class TableInFirstNS extends Table return $o != 0 ? $this->bb->getSbyte($o + $this->bb_pos) : \NamespaceA\NamespaceB\EnumInNestedNS::A; } + /** + * @return byte + */ + public function getFooUnionType() + { + $o = $this->__offset(8); + return $o != 0 ? $this->bb->getByte($o + $this->bb_pos) : \NamespaceA\NamespaceB\UnionInNestedNS::NONE; + } + + /** + * @returnint + */ + public function getFooUnion($obj) + { + $o = $this->__offset(10); + return $o != 0 ? $this->__union($obj, $o) : null; + } + public function getFooStruct() { $obj = new StructInNestedNS(); - $o = $this->__offset(8); + $o = $this->__offset(12); return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0; } @@ -61,18 +79,20 @@ class TableInFirstNS extends Table */ public static function startTableInFirstNS(FlatBufferBuilder $builder) { - $builder->StartObject(3); + $builder->StartObject(5); } /** * @param FlatBufferBuilder $builder * @return TableInFirstNS */ - public static function createTableInFirstNS(FlatBufferBuilder $builder, $foo_table, $foo_enum, $foo_struct) + public static function createTableInFirstNS(FlatBufferBuilder $builder, $foo_table, $foo_enum, $foo_union_type, $foo_union, $foo_struct) { - $builder->startObject(3); + $builder->startObject(5); self::addFooTable($builder, $foo_table); self::addFooEnum($builder, $foo_enum); + self::addFooUnionType($builder, $foo_union_type); + self::addFooUnion($builder, $foo_union); self::addFooStruct($builder, $foo_struct); $o = $builder->endObject(); return $o; @@ -98,6 +118,21 @@ class TableInFirstNS extends Table $builder->addSbyteX(1, $fooEnum, 0); } + /** + * @param FlatBufferBuilder $builder + * @param byte + * @return void + */ + public static function addFooUnionType(FlatBufferBuilder $builder, $fooUnionType) + { + $builder->addByteX(2, $fooUnionType, 0); + } + + public static function addFooUnion(FlatBufferBuilder $builder, $offset) + { + $builder->addOffsetX(3, $offset, 0); + } + /** * @param FlatBufferBuilder $builder * @param int @@ -105,7 +140,7 @@ class TableInFirstNS extends Table */ public static function addFooStruct(FlatBufferBuilder $builder, $fooStruct) { - $builder->addStructX(2, $fooStruct, 0); + $builder->addStructX(4, $fooStruct, 0); } /** diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.py b/tests/namespace_test/NamespaceA/TableInFirstNS.py index 8d917ac4f..abaac47c1 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.py +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.py @@ -38,8 +38,25 @@ class TableInFirstNS(object): return 0 # TableInFirstNS - def FooStruct(self): + def FooUnionType(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8)) + if o != 0: + return self._tab.Get(flatbuffers.number_types.Uint8Flags, o + self._tab.Pos) + return 0 + + # TableInFirstNS + def FooUnion(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10)) + if o != 0: + from flatbuffers.table import Table + obj = Table(bytearray(), 0) + self._tab.Union(obj, o) + return obj + return None + + # TableInFirstNS + def FooStruct(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12)) if o != 0: x = o + self._tab.Pos obj = StructInNestedNS() @@ -47,14 +64,16 @@ class TableInFirstNS(object): return obj return None -def TableInFirstNSStart(builder): builder.StartObject(3) +def TableInFirstNSStart(builder): builder.StartObject(5) def TableInFirstNSAddFooTable(builder, fooTable): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(fooTable), 0) def TableInFirstNSAddFooEnum(builder, fooEnum): builder.PrependInt8Slot(1, fooEnum, 0) -def TableInFirstNSAddFooStruct(builder, fooStruct): builder.PrependStructSlot(2, flatbuffers.number_types.UOffsetTFlags.py_type(fooStruct), 0) +def TableInFirstNSAddFooUnionType(builder, fooUnionType): builder.PrependUint8Slot(2, fooUnionType, 0) +def TableInFirstNSAddFooUnion(builder, fooUnion): builder.PrependUOffsetTRelativeSlot(3, flatbuffers.number_types.UOffsetTFlags.py_type(fooUnion), 0) +def TableInFirstNSAddFooStruct(builder, fooStruct): builder.PrependStructSlot(4, flatbuffers.number_types.UOffsetTFlags.py_type(fooStruct), 0) def TableInFirstNSEnd(builder): return builder.EndObject() try: - from typing import Optional + from typing import Optional, Union except: pass @@ -64,6 +83,8 @@ class TableInFirstNST(object): def __init__(self): self.fooTable = None # type: Optional[TableInNestedNST] self.fooEnum = 0 # type: int + self.fooUnionType = 0 # type: int + self.fooUnion = None # type: Union[None, TableInNestedNST] self.fooStruct = None # type: Optional[StructInNestedNST] @classmethod @@ -85,6 +106,8 @@ class TableInFirstNST(object): if tableInFirstNS.FooTable() is not None: self.fooTable = TableInNestedNST.InitFromObj(tableInFirstNS.FooTable()) self.fooEnum = tableInFirstNS.FooEnum() + self.fooUnionType = tableInFirstNS.FooUnionType() + self.fooUnion = UnionInNestedNSCreator(self.fooUnionType, tableInFirstNS.FooUnion()) if tableInFirstNS.FooStruct() is not None: self.fooStruct = StructInNestedNST.InitFromObj(tableInFirstNS.FooStruct()) @@ -92,10 +115,15 @@ class TableInFirstNST(object): def Pack(self, builder): if self.fooTable is not None: fooTable = self.fooTable.Pack(builder) + if self.fooUnion is not None: + fooUnion = self.fooUnion.Pack(builder) TableInFirstNSStart(builder) if self.fooTable is not None: TableInFirstNSAddFooTable(builder, fooTable) TableInFirstNSAddFooEnum(builder, self.fooEnum) + TableInFirstNSAddFooUnionType(builder, self.fooUnionType) + if self.fooUnion is not None: + TableInFirstNSAddFooUnion(builder, fooUnion) if self.fooStruct is not None: fooStruct = self.fooStruct.Pack(builder) TableInFirstNSAddFooStruct(builder, fooStruct) diff --git a/tests/namespace_test/namespace_test1.fbs b/tests/namespace_test/namespace_test1.fbs index 49449bfbc..f0b9c1669 100644 --- a/tests/namespace_test/namespace_test1.fbs +++ b/tests/namespace_test/namespace_test1.fbs @@ -5,6 +5,10 @@ table TableInNestedNS foo:int; } +union UnionInNestedNS { + TableInNestedNS, +} + enum EnumInNestedNS:byte { A, B, C diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 9201f8ce5..540fa2517 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -24,6 +24,110 @@ inline const flatbuffers::TypeTable *TableInNestedNSTypeTable(); inline const flatbuffers::TypeTable *StructInNestedNSTypeTable(); +enum UnionInNestedNS : uint8_t { + UnionInNestedNS_NONE = 0, + UnionInNestedNS_TableInNestedNS = 1, + UnionInNestedNS_MIN = UnionInNestedNS_NONE, + UnionInNestedNS_MAX = UnionInNestedNS_TableInNestedNS +}; + +inline const UnionInNestedNS (&EnumValuesUnionInNestedNS())[2] { + static const UnionInNestedNS values[] = { + UnionInNestedNS_NONE, + UnionInNestedNS_TableInNestedNS + }; + return values; +} + +inline const char * const *EnumNamesUnionInNestedNS() { + static const char * const names[3] = { + "NONE", + "TableInNestedNS", + nullptr + }; + return names; +} + +inline const char *EnumNameUnionInNestedNS(UnionInNestedNS e) { + if (flatbuffers::IsOutRange(e, UnionInNestedNS_NONE, UnionInNestedNS_TableInNestedNS)) return ""; + const size_t index = static_cast(e); + return EnumNamesUnionInNestedNS()[index]; +} + +template struct UnionInNestedNSTraits { + static const UnionInNestedNS enum_value = UnionInNestedNS_NONE; +}; + +template<> struct UnionInNestedNSTraits { + static const UnionInNestedNS enum_value = UnionInNestedNS_TableInNestedNS; +}; + +struct UnionInNestedNSUnion { + UnionInNestedNS type; + void *value; + + UnionInNestedNSUnion() : type(UnionInNestedNS_NONE), value(nullptr) {} + UnionInNestedNSUnion(UnionInNestedNSUnion&& u) FLATBUFFERS_NOEXCEPT : + type(UnionInNestedNS_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + UnionInNestedNSUnion(const UnionInNestedNSUnion &); + UnionInNestedNSUnion &operator=(const UnionInNestedNSUnion &u) + { UnionInNestedNSUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + UnionInNestedNSUnion &operator=(UnionInNestedNSUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~UnionInNestedNSUnion() { Reset(); } + + void Reset(); + +#ifndef FLATBUFFERS_CPP98_STL + template + void Set(T&& val) { + using RT = typename std::remove_reference::type; + Reset(); + type = UnionInNestedNSTraits::enum_value; + if (type != UnionInNestedNS_NONE) { + value = new RT(std::forward(val)); + } + } +#endif // FLATBUFFERS_CPP98_STL + + static void *UnPack(const void *obj, UnionInNestedNS type, const flatbuffers::resolver_function_t *resolver); + flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + NamespaceA::NamespaceB::TableInNestedNST *AsTableInNestedNS() { + return type == UnionInNestedNS_TableInNestedNS ? + reinterpret_cast(value) : nullptr; + } + const NamespaceA::NamespaceB::TableInNestedNST *AsTableInNestedNS() const { + return type == UnionInNestedNS_TableInNestedNS ? + reinterpret_cast(value) : nullptr; + } +}; + + +inline bool operator==(const UnionInNestedNSUnion &lhs, const UnionInNestedNSUnion &rhs) { + if (lhs.type != rhs.type) return false; + switch (lhs.type) { + case UnionInNestedNS_NONE: { + return true; + } + case UnionInNestedNS_TableInNestedNS: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + default: { + return false; + } + } +} + +inline bool operator!=(const UnionInNestedNSUnion &lhs, const UnionInNestedNSUnion &rhs) { + return !(lhs == rhs); +} + +bool VerifyUnionInNestedNS(flatbuffers::Verifier &verifier, const void *obj, UnionInNestedNS type); +bool VerifyUnionInNestedNSVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); + enum EnumInNestedNS : int8_t { EnumInNestedNS_A = 0, EnumInNestedNS_B = 1, @@ -203,6 +307,93 @@ inline flatbuffers::Offset CreateTableInNestedNS(flatbuffers::F _foo); } +inline bool VerifyUnionInNestedNS(flatbuffers::Verifier &verifier, const void *obj, UnionInNestedNS type) { + switch (type) { + case UnionInNestedNS_NONE: { + return true; + } + case UnionInNestedNS_TableInNestedNS: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +inline bool VerifyUnionInNestedNSVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyUnionInNestedNS( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *UnionInNestedNSUnion::UnPack(const void *obj, UnionInNestedNS type, const flatbuffers::resolver_function_t *resolver) { + switch (type) { + case UnionInNestedNS_TableInNestedNS: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline flatbuffers::Offset UnionInNestedNSUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { + switch (type) { + case UnionInNestedNS_TableInNestedNS: { + auto ptr = reinterpret_cast(value); + return CreateTableInNestedNS(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline UnionInNestedNSUnion::UnionInNestedNSUnion(const UnionInNestedNSUnion &u) : type(u.type), value(nullptr) { + switch (type) { + case UnionInNestedNS_TableInNestedNS: { + value = new NamespaceA::NamespaceB::TableInNestedNST(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void UnionInNestedNSUnion::Reset() { + switch (type) { + case UnionInNestedNS_TableInNestedNS: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = UnionInNestedNS_NONE; +} + +inline const flatbuffers::TypeTable *UnionInNestedNSTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + NamespaceA::NamespaceB::TableInNestedNSTypeTable + }; + static const char * const names[] = { + "NONE", + "TableInNestedNS" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_UNION, 2, type_codes, type_refs, nullptr, nullptr, names + }; + return &tt; +} + inline const flatbuffers::TypeTable *EnumInNestedNSTypeTable() { static const flatbuffers::TypeCode type_codes[] = { { flatbuffers::ET_CHAR, 0, 0 }, diff --git a/tests/namespace_test/namespace_test1_generated.lobster b/tests/namespace_test/namespace_test1_generated.lobster index 65030059d..5721b8f42 100644 --- a/tests/namespace_test/namespace_test1_generated.lobster +++ b/tests/namespace_test/namespace_test1_generated.lobster @@ -3,6 +3,10 @@ import flatbuffers namespace NamespaceA_NamespaceB +enum UnionInNestedNS: + UnionInNestedNS_NONE = 0 + UnionInNestedNS_TableInNestedNS = 1 + enum EnumInNestedNS: EnumInNestedNS_A = 0 EnumInNestedNS_B = 1 diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs index 516c9b88b..88c7cb2eb 100644 --- a/tests/namespace_test/namespace_test1_generated.rs +++ b/tests/namespace_test/namespace_test1_generated.rs @@ -26,6 +26,138 @@ pub mod namespace_b { extern crate flatbuffers; use self::flatbuffers::EndianScalar; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_UNION_IN_NESTED_NS: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_UNION_IN_NESTED_NS: u8 = 1; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_UNION_IN_NESTED_NS: [UnionInNestedNS; 2] = [ + UnionInNestedNS::NONE, + UnionInNestedNS::TableInNestedNS, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct UnionInNestedNS(pub u8); +#[allow(non_upper_case_globals)] +impl UnionInNestedNS { + pub const NONE: Self = Self(0); + pub const TableInNestedNS: Self = Self(1); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 1; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::TableInNestedNS, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::TableInNestedNS => Some("TableInNestedNS"), + _ => None, + } + } +} +impl std::fmt::Debug for UnionInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = flatbuffers::read_scalar_at::(buf, loc); + Self(b) + } +} + +impl flatbuffers::Push for UnionInNestedNS { + type Output = UnionInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, self.0); + } +} + +impl flatbuffers::EndianScalar for UnionInNestedNS { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for UnionInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for UnionInNestedNS {} +pub struct UnionInNestedNSUnionTableOffset {} + +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum UnionInNestedNST { + NONE, + TableInNestedNS(Box), +} +impl Default for UnionInNestedNST { + fn default() -> Self { + Self::NONE + } +} +impl UnionInNestedNST { + pub fn union_in_nested_ns_type(&self) -> UnionInNestedNS { + match self { + Self::NONE => UnionInNestedNS::NONE, + Self::TableInNestedNS(_) => UnionInNestedNS::TableInNestedNS, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::TableInNestedNS(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned TableInNestedNST, setting the union to NONE. + pub fn take_table_in_nested_ns(&mut self) -> Option> { + if let Self::TableInNestedNS(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::TableInNestedNS(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the TableInNestedNST. + pub fn as_table_in_nested_ns(&self) -> Option<&TableInNestedNST> { + if let Self::TableInNestedNS(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the TableInNestedNST. + pub fn as_table_in_nested_ns_mut(&mut self) -> Option<&mut TableInNestedNST> { + if let Self::TableInNestedNS(v) = self { Some(v.as_mut()) } else { None } + } +} #[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] pub const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0; #[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] diff --git a/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart b/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart index 98c28bc9f..4941a3c26 100644 --- a/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart +++ b/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart @@ -7,6 +7,45 @@ import 'dart:typed_data' show Uint8List; import 'package:flat_buffers/flat_buffers.dart' as fb; +class UnionInNestedNSTypeId { + final int value; + const UnionInNestedNSTypeId._(this.value); + + factory UnionInNestedNSTypeId.fromValue(int value) { + if (value == null) value = 0; + if (!values.containsKey(value)) { + throw new StateError('Invalid value $value for bit flag enum UnionInNestedNSTypeId'); + } + return values[value]; + } + + static const int minValue = 0; + static const int maxValue = 1; + static bool containsValue(int value) => values.containsKey(value); + + static const UnionInNestedNSTypeId NONE = const UnionInNestedNSTypeId._(0); + static const UnionInNestedNSTypeId TableInNestedNS = const UnionInNestedNSTypeId._(1); + static const Map values = {0: NONE,1: TableInNestedNS,}; + + static const fb.Reader reader = const _UnionInNestedNSTypeIdReader(); + + @override + String toString() { + return 'UnionInNestedNSTypeId{value: $value}'; + } +} + +class _UnionInNestedNSTypeIdReader extends fb.Reader { + const _UnionInNestedNSTypeIdReader(); + + @override + int get size => 1; + + @override + UnionInNestedNSTypeId read(fb.BufferContext bc, int offset) => + new UnionInNestedNSTypeId.fromValue(const fb.Uint8Reader().read(bc, offset)); +} + class EnumInNestedNS { final int value; const EnumInNestedNS._(this.value); diff --git a/tests/namespace_test/namespace_test2.fbs b/tests/namespace_test/namespace_test2.fbs index 11d7deadc..e1440e7c1 100644 --- a/tests/namespace_test/namespace_test2.fbs +++ b/tests/namespace_test/namespace_test2.fbs @@ -6,6 +6,7 @@ table TableInFirstNS { foo_table:NamespaceB.TableInNestedNS; foo_enum:NamespaceB.EnumInNestedNS; + foo_union:NamespaceB.UnionInNestedNS; foo_struct:NamespaceB.StructInNestedNS; } diff --git a/tests/namespace_test/namespace_test2_generated.h b/tests/namespace_test/namespace_test2_generated.h index df8f959ce..968e240cc 100644 --- a/tests/namespace_test/namespace_test2_generated.h +++ b/tests/namespace_test/namespace_test2_generated.h @@ -64,6 +64,7 @@ struct TableInFirstNST : public flatbuffers::NativeTable { } flatbuffers::unique_ptr foo_table{}; NamespaceA::NamespaceB::EnumInNestedNS foo_enum = NamespaceA::NamespaceB::EnumInNestedNS_A; + NamespaceA::NamespaceB::UnionInNestedNSUnion foo_union{}; flatbuffers::unique_ptr foo_struct{}; }; @@ -71,6 +72,7 @@ inline bool operator==(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { return (lhs.foo_table == rhs.foo_table) && (lhs.foo_enum == rhs.foo_enum) && + (lhs.foo_union == rhs.foo_union) && (lhs.foo_struct == rhs.foo_struct); } @@ -91,7 +93,9 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { VT_FOO_TABLE = 4, VT_FOO_ENUM = 6, - VT_FOO_STRUCT = 8 + VT_FOO_UNION_TYPE = 8, + VT_FOO_UNION = 10, + VT_FOO_STRUCT = 12 }; const NamespaceA::NamespaceB::TableInNestedNS *foo_table() const { return GetPointer(VT_FOO_TABLE); @@ -105,6 +109,19 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS _foo_enum) { return SetField(VT_FOO_ENUM, static_cast(_foo_enum), 0); } + NamespaceA::NamespaceB::UnionInNestedNS foo_union_type() const { + return static_cast(GetField(VT_FOO_UNION_TYPE, 0)); + } + const void *foo_union() const { + return GetPointer(VT_FOO_UNION); + } + template const T *foo_union_as() const; + const NamespaceA::NamespaceB::TableInNestedNS *foo_union_as_TableInNestedNS() const { + return foo_union_type() == NamespaceA::NamespaceB::UnionInNestedNS_TableInNestedNS ? static_cast(foo_union()) : nullptr; + } + void *mutable_foo_union() { + return GetPointer(VT_FOO_UNION); + } const NamespaceA::NamespaceB::StructInNestedNS *foo_struct() const { return GetStruct(VT_FOO_STRUCT); } @@ -116,6 +133,9 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyOffset(verifier, VT_FOO_TABLE) && verifier.VerifyTable(foo_table()) && VerifyField(verifier, VT_FOO_ENUM) && + VerifyField(verifier, VT_FOO_UNION_TYPE) && + VerifyOffset(verifier, VT_FOO_UNION) && + VerifyUnionInNestedNS(verifier, foo_union(), foo_union_type()) && VerifyField(verifier, VT_FOO_STRUCT) && verifier.EndTable(); } @@ -124,6 +144,10 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInFirstNST* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; +template<> inline const NamespaceA::NamespaceB::TableInNestedNS *TableInFirstNS::foo_union_as() const { + return foo_union_as_TableInNestedNS(); +} + struct TableInFirstNSBuilder { typedef TableInFirstNS Table; flatbuffers::FlatBufferBuilder &fbb_; @@ -134,6 +158,12 @@ struct TableInFirstNSBuilder { void add_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS foo_enum) { fbb_.AddElement(TableInFirstNS::VT_FOO_ENUM, static_cast(foo_enum), 0); } + void add_foo_union_type(NamespaceA::NamespaceB::UnionInNestedNS foo_union_type) { + fbb_.AddElement(TableInFirstNS::VT_FOO_UNION_TYPE, static_cast(foo_union_type), 0); + } + void add_foo_union(flatbuffers::Offset foo_union) { + fbb_.AddOffset(TableInFirstNS::VT_FOO_UNION, foo_union); + } void add_foo_struct(const NamespaceA::NamespaceB::StructInNestedNS *foo_struct) { fbb_.AddStruct(TableInFirstNS::VT_FOO_STRUCT, foo_struct); } @@ -152,10 +182,14 @@ inline flatbuffers::Offset CreateTableInFirstNS( flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset foo_table = 0, NamespaceA::NamespaceB::EnumInNestedNS foo_enum = NamespaceA::NamespaceB::EnumInNestedNS_A, + NamespaceA::NamespaceB::UnionInNestedNS foo_union_type = NamespaceA::NamespaceB::UnionInNestedNS_NONE, + flatbuffers::Offset foo_union = 0, const NamespaceA::NamespaceB::StructInNestedNS *foo_struct = 0) { TableInFirstNSBuilder builder_(_fbb); builder_.add_foo_struct(foo_struct); + builder_.add_foo_union(foo_union); builder_.add_foo_table(foo_table); + builder_.add_foo_union_type(foo_union_type); builder_.add_foo_enum(foo_enum); return builder_.Finish(); } @@ -347,6 +381,8 @@ inline void TableInFirstNS::UnPackTo(TableInFirstNST *_o, const flatbuffers::res (void)_resolver; { auto _e = foo_table(); if (_e) _o->foo_table = flatbuffers::unique_ptr(_e->UnPack(_resolver)); } { auto _e = foo_enum(); _o->foo_enum = _e; } + { auto _e = foo_union_type(); _o->foo_union.type = _e; } + { auto _e = foo_union(); if (_e) _o->foo_union.value = NamespaceA::NamespaceB::UnionInNestedNSUnion::UnPack(_e, foo_union_type(), _resolver); } { auto _e = foo_struct(); if (_e) _o->foo_struct = flatbuffers::unique_ptr(new NamespaceA::NamespaceB::StructInNestedNS(*_e)); } } @@ -360,11 +396,15 @@ inline flatbuffers::Offset CreateTableInFirstNS(flatbuffers::Fla struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TableInFirstNST* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; auto _foo_table = _o->foo_table ? CreateTableInNestedNS(_fbb, _o->foo_table.get(), _rehasher) : 0; auto _foo_enum = _o->foo_enum; + auto _foo_union_type = _o->foo_union.type; + auto _foo_union = _o->foo_union.Pack(_fbb); auto _foo_struct = _o->foo_struct ? _o->foo_struct.get() : 0; return NamespaceA::CreateTableInFirstNS( _fbb, _foo_table, _foo_enum, + _foo_union_type, + _foo_union, _foo_struct); } @@ -435,20 +475,25 @@ inline const flatbuffers::TypeTable *TableInFirstNSTypeTable() { static const flatbuffers::TypeCode type_codes[] = { { flatbuffers::ET_SEQUENCE, 0, 0 }, { flatbuffers::ET_CHAR, 0, 1 }, - { flatbuffers::ET_SEQUENCE, 0, 2 } + { flatbuffers::ET_UTYPE, 0, 2 }, + { flatbuffers::ET_SEQUENCE, 0, 2 }, + { flatbuffers::ET_SEQUENCE, 0, 3 } }; static const flatbuffers::TypeFunction type_refs[] = { NamespaceA::NamespaceB::TableInNestedNSTypeTable, NamespaceA::NamespaceB::EnumInNestedNSTypeTable, + NamespaceA::NamespaceB::UnionInNestedNSTypeTable, NamespaceA::NamespaceB::StructInNestedNSTypeTable }; static const char * const names[] = { "foo_table", "foo_enum", + "foo_union_type", + "foo_union", "foo_struct" }; static const flatbuffers::TypeTable tt = { - flatbuffers::ST_TABLE, 3, type_codes, type_refs, nullptr, nullptr, names + flatbuffers::ST_TABLE, 5, type_codes, type_refs, nullptr, nullptr, names }; return &tt; } diff --git a/tests/namespace_test/namespace_test2_generated.lobster b/tests/namespace_test/namespace_test2_generated.lobster index 4383e6857..3820928d5 100644 --- a/tests/namespace_test/namespace_test2_generated.lobster +++ b/tests/namespace_test/namespace_test2_generated.lobster @@ -19,8 +19,12 @@ class TableInFirstNS : flatbuffers_handle return if o: NamespaceA_NamespaceB_TableInNestedNS { buf_, o } else: nil def foo_enum(): return EnumInNestedNS(buf_.flatbuffers_field_int8(pos_, 6, 0)) + def foo_union_type(): + return UnionInNestedNS(buf_.flatbuffers_field_int8(pos_, 8, 0)) + def foo_union_as_TableInNestedNS(): + return NamespaceA_NamespaceB_TableInNestedNS { buf_, buf_.flatbuffers_field_table(pos_, 10) } def foo_struct(): - let o = buf_.flatbuffers_field_struct(pos_, 8) + let o = buf_.flatbuffers_field_struct(pos_, 12) return if o: NamespaceA_NamespaceB_StructInNestedNS { buf_, o } else: nil def GetRootAsTableInFirstNS(buf:string): return TableInFirstNS { buf, buf.flatbuffers_indirect(0) } @@ -28,7 +32,7 @@ def GetRootAsTableInFirstNS(buf:string): return TableInFirstNS { buf, buf.flatbu struct TableInFirstNSBuilder: b_:flatbuffers_builder def start(): - b_.StartObject(3) + b_.StartObject(5) return this def add_foo_table(foo_table:flatbuffers_offset): b_.PrependUOffsetTRelativeSlot(0, foo_table) @@ -36,8 +40,14 @@ struct TableInFirstNSBuilder: def add_foo_enum(foo_enum:EnumInNestedNS): b_.PrependInt8Slot(1, foo_enum, 0) return this + def add_foo_union_type(foo_union_type:UnionInNestedNS): + b_.PrependUint8Slot(2, foo_union_type, 0) + return this + def add_foo_union(foo_union:flatbuffers_offset): + b_.PrependUOffsetTRelativeSlot(3, foo_union) + return this def add_foo_struct(foo_struct:flatbuffers_offset): - b_.PrependStructSlot(2, foo_struct) + b_.PrependStructSlot(4, foo_struct) return this def end(): return b_.EndObject() diff --git a/tests/namespace_test/namespace_test2_generated.rs b/tests/namespace_test/namespace_test2_generated.rs index a774dbfc8..f86e424e2 100644 --- a/tests/namespace_test/namespace_test2_generated.rs +++ b/tests/namespace_test/namespace_test2_generated.rs @@ -3,7 +3,6 @@ #![allow(unused_imports, dead_code)] -use crate::namespace_test1_generated::*; use std::mem; use std::cmp::Ordering; @@ -13,12 +12,502 @@ use self::flatbuffers::EndianScalar; #[allow(unused_imports, dead_code)] pub mod namespace_a { - use crate::namespace_test1_generated::*; use std::mem; use std::cmp::Ordering; extern crate flatbuffers; use self::flatbuffers::EndianScalar; +#[allow(unused_imports, dead_code)] +pub mod namespace_b { + + use std::mem; + use std::cmp::Ordering; + + extern crate flatbuffers; + use self::flatbuffers::EndianScalar; + +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_UNION_IN_NESTED_NS: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_UNION_IN_NESTED_NS: u8 = 1; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_UNION_IN_NESTED_NS: [UnionInNestedNS; 2] = [ + UnionInNestedNS::NONE, + UnionInNestedNS::TableInNestedNS, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct UnionInNestedNS(pub u8); +#[allow(non_upper_case_globals)] +impl UnionInNestedNS { + pub const NONE: Self = Self(0); + pub const TableInNestedNS: Self = Self(1); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 1; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::TableInNestedNS, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::TableInNestedNS => Some("TableInNestedNS"), + _ => None, + } + } +} +impl std::fmt::Debug for UnionInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = flatbuffers::read_scalar_at::(buf, loc); + Self(b) + } +} + +impl flatbuffers::Push for UnionInNestedNS { + type Output = UnionInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, self.0); + } +} + +impl flatbuffers::EndianScalar for UnionInNestedNS { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for UnionInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for UnionInNestedNS {} +pub struct UnionInNestedNSUnionTableOffset {} + +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum UnionInNestedNST { + NONE, + TableInNestedNS(Box), +} +impl Default for UnionInNestedNST { + fn default() -> Self { + Self::NONE + } +} +impl UnionInNestedNST { + pub fn union_in_nested_ns_type(&self) -> UnionInNestedNS { + match self { + Self::NONE => UnionInNestedNS::NONE, + Self::TableInNestedNS(_) => UnionInNestedNS::TableInNestedNS, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::TableInNestedNS(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned TableInNestedNST, setting the union to NONE. + pub fn take_table_in_nested_ns(&mut self) -> Option> { + if let Self::TableInNestedNS(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::TableInNestedNS(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the TableInNestedNST. + pub fn as_table_in_nested_ns(&self) -> Option<&TableInNestedNST> { + if let Self::TableInNestedNS(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the TableInNestedNST. + pub fn as_table_in_nested_ns_mut(&mut self) -> Option<&mut TableInNestedNST> { + if let Self::TableInNestedNS(v) = self { Some(v.as_mut()) } else { None } + } +} +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_ENUM_IN_NESTED_NS: i8 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_ENUM_IN_NESTED_NS: [EnumInNestedNS; 3] = [ + EnumInNestedNS::A, + EnumInNestedNS::B, + EnumInNestedNS::C, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct EnumInNestedNS(pub i8); +#[allow(non_upper_case_globals)] +impl EnumInNestedNS { + pub const A: Self = Self(0); + pub const B: Self = Self(1); + pub const C: Self = Self(2); + + pub const ENUM_MIN: i8 = 0; + pub const ENUM_MAX: i8 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::A, + Self::B, + Self::C, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::A => Some("A"), + Self::B => Some("B"), + Self::C => Some("C"), + _ => None, + } + } +} +impl std::fmt::Debug for EnumInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = flatbuffers::read_scalar_at::(buf, loc); + Self(b) + } +} + +impl flatbuffers::Push for EnumInNestedNS { + type Output = EnumInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, self.0); + } +} + +impl flatbuffers::EndianScalar for EnumInNestedNS { + #[inline] + fn to_little_endian(self) -> Self { + let b = i8::to_le(self.0); + Self(b) + } + #[inline] + fn from_little_endian(self) -> Self { + let b = i8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for EnumInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for EnumInNestedNS {} +// struct StructInNestedNS, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Default)] +pub struct StructInNestedNS(pub [u8; 8]); +impl std::fmt::Debug for StructInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("StructInNestedNS") + .field("a", &self.a()) + .field("b", &self.b()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for StructInNestedNS {} +impl flatbuffers::SafeSliceAccess for StructInNestedNS {} +impl<'a> flatbuffers::Follow<'a> for StructInNestedNS { + type Inner = &'a StructInNestedNS; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a StructInNestedNS>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a StructInNestedNS { + type Inner = &'a StructInNestedNS; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for StructInNestedNS { + type Output = StructInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const StructInNestedNS as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b StructInNestedNS { + type Output = StructInNestedNS; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const StructInNestedNS as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for StructInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl StructInNestedNS { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: i32, + b: i32, + ) -> Self { + let mut s = Self([0; 8]); + s.set_a(a); + s.set_b(b); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceA.NamespaceB.StructInNestedNS" + } + + pub fn a(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn b(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[4..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_b(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[4..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> StructInNestedNST { + StructInNestedNST { + a: self.a(), + b: self.b(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct StructInNestedNST { + pub a: i32, + pub b: i32, +} +impl StructInNestedNST { + pub fn pack(&self) -> StructInNestedNS { + StructInNestedNS::new( + self.a, + self.b, + ) + } +} + +pub enum TableInNestedNSOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableInNestedNS<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableInNestedNS<'a> { + type Inner = TableInNestedNS<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableInNestedNS<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceA.NamespaceB.TableInNestedNS" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableInNestedNS { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableInNestedNSArgs) -> flatbuffers::WIPOffset> { + let mut builder = TableInNestedNSBuilder::new(_fbb); + builder.add_foo(args.foo); + builder.finish() + } + + pub fn unpack(&self) -> TableInNestedNST { + let foo = self.foo(); + TableInNestedNST { + foo, + } + } + pub const VT_FOO: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn foo(&self) -> i32 { + self._tab.get::(TableInNestedNS::VT_FOO, Some(0)).unwrap() + } +} + +impl flatbuffers::Verifiable for TableInNestedNS<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::(&"foo", Self::VT_FOO, false)? + .finish(); + Ok(()) + } +} +pub struct TableInNestedNSArgs { + pub foo: i32, +} +impl<'a> Default for TableInNestedNSArgs { + #[inline] + fn default() -> Self { + TableInNestedNSArgs { + foo: 0, + } + } +} +pub struct TableInNestedNSBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableInNestedNSBuilder<'a, 'b> { + #[inline] + pub fn add_foo(&mut self, foo: i32) { + self.fbb_.push_slot::(TableInNestedNS::VT_FOO, foo, 0); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInNestedNSBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableInNestedNSBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableInNestedNS<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableInNestedNS"); + ds.field("foo", &self.foo()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Default)] +pub struct TableInNestedNST { + pub foo: i32, +} +impl TableInNestedNST { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let foo = self.foo; + TableInNestedNS::create(_fbb, &TableInNestedNSArgs{ + foo, + }) + } +} +} // pub mod NamespaceB pub enum TableInFirstNSOffset {} #[derive(Copy, Clone, PartialEq)] @@ -50,7 +539,9 @@ impl<'a> TableInFirstNS<'a> { args: &'args TableInFirstNSArgs<'args>) -> flatbuffers::WIPOffset> { let mut builder = TableInFirstNSBuilder::new(_fbb); if let Some(x) = args.foo_struct { builder.add_foo_struct(x); } + if let Some(x) = args.foo_union { builder.add_foo_union(x); } if let Some(x) = args.foo_table { builder.add_foo_table(x); } + builder.add_foo_union_type(args.foo_union_type); builder.add_foo_enum(args.foo_enum); builder.finish() } @@ -60,18 +551,30 @@ impl<'a> TableInFirstNS<'a> { Box::new(x.unpack()) }); let foo_enum = self.foo_enum(); + let foo_union = match self.foo_union_type() { + namespace_b::UnionInNestedNS::NONE => namespace_b::UnionInNestedNST::NONE, + namespace_b::UnionInNestedNS::TableInNestedNS => namespace_b::UnionInNestedNST::TableInNestedNS(Box::new( + self.foo_union_as_table_in_nested_ns() + .expect("Invalid union table, expected `namespace_b::UnionInNestedNS::TableInNestedNS`.") + .unpack() + )), + _ => namespace_b::UnionInNestedNST::NONE, + }; let foo_struct = self.foo_struct().map(|x| { x.unpack() }); TableInFirstNST { foo_table, foo_enum, + foo_union, foo_struct, } } pub const VT_FOO_TABLE: flatbuffers::VOffsetT = 4; pub const VT_FOO_ENUM: flatbuffers::VOffsetT = 6; - pub const VT_FOO_STRUCT: flatbuffers::VOffsetT = 8; + pub const VT_FOO_UNION_TYPE: flatbuffers::VOffsetT = 8; + pub const VT_FOO_UNION: flatbuffers::VOffsetT = 10; + pub const VT_FOO_STRUCT: flatbuffers::VOffsetT = 12; #[inline] pub fn foo_table(&self) -> Option> { @@ -82,9 +585,27 @@ impl<'a> TableInFirstNS<'a> { self._tab.get::(TableInFirstNS::VT_FOO_ENUM, Some(namespace_b::EnumInNestedNS::A)).unwrap() } #[inline] + pub fn foo_union_type(&self) -> namespace_b::UnionInNestedNS { + self._tab.get::(TableInFirstNS::VT_FOO_UNION_TYPE, Some(namespace_b::UnionInNestedNS::NONE)).unwrap() + } + #[inline] + pub fn foo_union(&self) -> Option> { + self._tab.get::>>(TableInFirstNS::VT_FOO_UNION, None) + } + #[inline] pub fn foo_struct(&self) -> Option<&'a namespace_b::StructInNestedNS> { self._tab.get::(TableInFirstNS::VT_FOO_STRUCT, None) } + #[inline] + #[allow(non_snake_case)] + pub fn foo_union_as_table_in_nested_ns(&self) -> Option> { + if self.foo_union_type() == namespace_b::UnionInNestedNS::TableInNestedNS { + self.foo_union().map(namespace_b::TableInNestedNS::init_from_table) + } else { + None + } + } + } impl flatbuffers::Verifiable for TableInFirstNS<'_> { @@ -96,6 +617,12 @@ impl flatbuffers::Verifiable for TableInFirstNS<'_> { v.visit_table(pos)? .visit_field::>(&"foo_table", Self::VT_FOO_TABLE, false)? .visit_field::(&"foo_enum", Self::VT_FOO_ENUM, false)? + .visit_union::(&"foo_union_type", Self::VT_FOO_UNION_TYPE, &"foo_union", Self::VT_FOO_UNION, false, |key, v, pos| { + match key { + namespace_b::UnionInNestedNS::TableInNestedNS => v.verify_union_variant::>("namespace_b::UnionInNestedNS::TableInNestedNS", pos), + _ => Ok(()), + } + })? .visit_field::(&"foo_struct", Self::VT_FOO_STRUCT, false)? .finish(); Ok(()) @@ -104,6 +631,8 @@ impl flatbuffers::Verifiable for TableInFirstNS<'_> { pub struct TableInFirstNSArgs<'a> { pub foo_table: Option>>, pub foo_enum: namespace_b::EnumInNestedNS, + pub foo_union_type: namespace_b::UnionInNestedNS, + pub foo_union: Option>, pub foo_struct: Option<&'a namespace_b::StructInNestedNS>, } impl<'a> Default for TableInFirstNSArgs<'a> { @@ -112,6 +641,8 @@ impl<'a> Default for TableInFirstNSArgs<'a> { TableInFirstNSArgs { foo_table: None, foo_enum: namespace_b::EnumInNestedNS::A, + foo_union_type: namespace_b::UnionInNestedNS::NONE, + foo_union: None, foo_struct: None, } } @@ -130,6 +661,14 @@ impl<'a: 'b, 'b> TableInFirstNSBuilder<'a, 'b> { self.fbb_.push_slot::(TableInFirstNS::VT_FOO_ENUM, foo_enum, namespace_b::EnumInNestedNS::A); } #[inline] + pub fn add_foo_union_type(&mut self, foo_union_type: namespace_b::UnionInNestedNS) { + self.fbb_.push_slot::(TableInFirstNS::VT_FOO_UNION_TYPE, foo_union_type, namespace_b::UnionInNestedNS::NONE); + } + #[inline] + pub fn add_foo_union(&mut self, foo_union: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(TableInFirstNS::VT_FOO_UNION, foo_union); + } + #[inline] pub fn add_foo_struct(&mut self, foo_struct: &namespace_b::StructInNestedNS) { self.fbb_.push_slot_always::<&namespace_b::StructInNestedNS>(TableInFirstNS::VT_FOO_STRUCT, foo_struct); } @@ -153,6 +692,20 @@ impl std::fmt::Debug for TableInFirstNS<'_> { let mut ds = f.debug_struct("TableInFirstNS"); ds.field("foo_table", &self.foo_table()); ds.field("foo_enum", &self.foo_enum()); + ds.field("foo_union_type", &self.foo_union_type()); + match self.foo_union_type() { + namespace_b::UnionInNestedNS::TableInNestedNS => { + if let Some(x) = self.foo_union_as_table_in_nested_ns() { + ds.field("foo_union", &x) + } else { + ds.field("foo_union", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + _ => { + let x: Option<()> = None; + ds.field("foo_union", &x) + }, + }; ds.field("foo_struct", &self.foo_struct()); ds.finish() } @@ -162,6 +715,7 @@ impl std::fmt::Debug for TableInFirstNS<'_> { pub struct TableInFirstNST { pub foo_table: Option>, pub foo_enum: namespace_b::EnumInNestedNS, + pub foo_union: namespace_b::UnionInNestedNST, pub foo_struct: Option, } impl TableInFirstNST { @@ -173,11 +727,15 @@ impl TableInFirstNST { x.pack(_fbb) }); let foo_enum = self.foo_enum; + let foo_union_type = self.foo_union.union_in_nested_ns_type(); + let foo_union = self.foo_union.pack(_fbb); let foo_struct_tmp = self.foo_struct.as_ref().map(|x| x.pack()); let foo_struct = foo_struct_tmp.as_ref(); TableInFirstNS::create(_fbb, &TableInFirstNSArgs{ foo_table, foo_enum, + foo_union_type, + foo_union, foo_struct, }) } @@ -308,7 +866,6 @@ impl SecondTableInAT { #[allow(unused_imports, dead_code)] pub mod namespace_c { - use crate::namespace_test1_generated::*; use std::mem; use std::cmp::Ordering; diff --git a/tests/namespace_test/namespace_test2_namespace_a_generated.dart b/tests/namespace_test/namespace_test2_namespace_a_generated.dart index e10ad4b78..8c08420cf 100644 --- a/tests/namespace_test/namespace_test2_namespace_a_generated.dart +++ b/tests/namespace_test/namespace_test2_namespace_a_generated.dart @@ -22,11 +22,18 @@ class TableInFirstNS { namespace_a_namespace_b.TableInNestedNS get fooTable => namespace_a_namespace_b.TableInNestedNS.reader.vTableGet(_bc, _bcOffset, 4, null); EnumInNestedNS get fooEnum => new EnumInNestedNS.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 6, 0)); - namespace_a_namespace_b.StructInNestedNS get fooStruct => namespace_a_namespace_b.StructInNestedNS.reader.vTableGet(_bc, _bcOffset, 8, null); + UnionInNestedNSTypeId get fooUnionType => new UnionInNestedNSTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 8, 0)); + dynamic get fooUnion { + switch (fooUnionType?.value) { + case 1: return TableInNestedNS.reader.vTableGet(_bc, _bcOffset, 10, null); + default: return null; + } + } + namespace_a_namespace_b.StructInNestedNS get fooStruct => namespace_a_namespace_b.StructInNestedNS.reader.vTableGet(_bc, _bcOffset, 12, null); @override String toString() { - return 'TableInFirstNS{fooTable: $fooTable, fooEnum: $fooEnum, fooStruct: $fooStruct}'; + return 'TableInFirstNS{fooTable: $fooTable, fooEnum: $fooEnum, fooUnionType: $fooUnionType, fooUnion: $fooUnion, fooStruct: $fooStruct}'; } } @@ -57,8 +64,16 @@ class TableInFirstNSBuilder { fbBuilder.addInt8(1, fooEnum?.value); return fbBuilder.offset; } + int addFooUnionType(UnionInNestedNSTypeId fooUnionType) { + fbBuilder.addUint8(2, fooUnionType?.value); + return fbBuilder.offset; + } + int addFooUnionOffset(int offset) { + fbBuilder.addOffset(3, offset); + return fbBuilder.offset; + } int addFooStruct(int offset) { - fbBuilder.addStruct(2, offset); + fbBuilder.addStruct(4, offset); return fbBuilder.offset; } @@ -70,15 +85,21 @@ class TableInFirstNSBuilder { class TableInFirstNSObjectBuilder extends fb.ObjectBuilder { final namespace_a_namespace_b.TableInNestedNSObjectBuilder _fooTable; final EnumInNestedNS _fooEnum; + final UnionInNestedNSTypeId _fooUnionType; + final dynamic _fooUnion; final namespace_a_namespace_b.StructInNestedNSObjectBuilder _fooStruct; TableInFirstNSObjectBuilder({ namespace_a_namespace_b.TableInNestedNSObjectBuilder fooTable, EnumInNestedNS fooEnum, + UnionInNestedNSTypeId fooUnionType, + dynamic fooUnion, namespace_a_namespace_b.StructInNestedNSObjectBuilder fooStruct, }) : _fooTable = fooTable, _fooEnum = fooEnum, + _fooUnionType = fooUnionType, + _fooUnion = fooUnion, _fooStruct = fooStruct; /// Finish building, and store into the [fbBuilder]. @@ -87,14 +108,19 @@ class TableInFirstNSObjectBuilder extends fb.ObjectBuilder { fb.Builder fbBuilder) { assert(fbBuilder != null); final int fooTableOffset = _fooTable?.getOrCreateOffset(fbBuilder); + final int fooUnionOffset = _fooUnion?.getOrCreateOffset(fbBuilder); fbBuilder.startTable(); if (fooTableOffset != null) { fbBuilder.addOffset(0, fooTableOffset); } fbBuilder.addInt8(1, _fooEnum?.value); + fbBuilder.addUint8(2, _fooUnionType?.value); + if (fooUnionOffset != null) { + fbBuilder.addOffset(3, fooUnionOffset); + } if (_fooStruct != null) { - fbBuilder.addStruct(2, _fooStruct.finish(fbBuilder)); + fbBuilder.addStruct(4, _fooStruct.finish(fbBuilder)); } return fbBuilder.endTable(); } diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs index c5a0a56dc..652a098a7 100644 --- a/tests/rust_usage_test/tests/integration_test.rs +++ b/tests/rust_usage_test/tests/integration_test.rs @@ -37,6 +37,12 @@ pub mod include_test1_generated; #[path = "../../include_test/sub/include_test2_generated.rs"] pub mod include_test2_generated; +#[path = "../../namespace_test/namespace_test1_generated.rs"] +pub mod namespace_test1_generated; + +#[path = "../../namespace_test/namespace_test2_generated.rs"] +pub mod namespace_test2_generated; + #[path = "../../monster_test_generated.rs"] mod monster_test_generated; pub use monster_test_generated::my_game;