mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-21 09:45:45 +00:00
[C#] Add Union Constructor Utility for ObjectAPI. (#6105)
* [C#] add union constructor utility. * add union utility test. * std::tolower -> CharToLower * use std::transform instead * rebase to master. and regenerate test code
This commit is contained in:
@@ -488,6 +488,26 @@ namespace FlatBuffers.Test
|
||||
TestObjectAPI(movie);
|
||||
}
|
||||
|
||||
[FlatBuffersTestMethod]
|
||||
public void TestUnionUtility()
|
||||
{
|
||||
var movie = new MovieT
|
||||
{
|
||||
MainCharacter = CharacterUnion.FromRapunzel(new RapunzelT { HairLength = 40 }),
|
||||
Characters = new System.Collections.Generic.List<CharacterUnion>
|
||||
{
|
||||
CharacterUnion.FromMuLan(new AttackerT { SwordAttackDamage = 10 }),
|
||||
CharacterUnion.FromBelle(new BookReaderT { BooksRead = 20 }),
|
||||
CharacterUnion.FromOther("Chip"),
|
||||
},
|
||||
};
|
||||
|
||||
var fbb = new FlatBufferBuilder(100);
|
||||
Movie.FinishMovieBuffer(fbb, Movie.Pack(fbb, movie));
|
||||
|
||||
TestObjectAPI(Movie.GetRootAsMovie(fbb.DataBuffer));
|
||||
}
|
||||
|
||||
private void AreEqual(Monster a, MonsterT b)
|
||||
{
|
||||
Assert.AreEqual(a.Hp, b.Hp);
|
||||
|
||||
@@ -25,8 +25,11 @@ public class AnyUnion {
|
||||
|
||||
public T As<T>() where T : class { return this.Value as T; }
|
||||
public MyGame.Example.MonsterT AsMonster() { return this.As<MyGame.Example.MonsterT>(); }
|
||||
public static AnyUnion FromMonster(MyGame.Example.MonsterT _monster) { return new AnyUnion{ Type = Any.Monster, Value = _monster }; }
|
||||
internal MyGame.Example.TestSimpleTableWithEnumT AsTestSimpleTableWithEnum() { return this.As<MyGame.Example.TestSimpleTableWithEnumT>(); }
|
||||
internal static AnyUnion FromTestSimpleTableWithEnum(MyGame.Example.TestSimpleTableWithEnumT _testsimpletablewithenum) { return new AnyUnion{ Type = Any.TestSimpleTableWithEnum, Value = _testsimpletablewithenum }; }
|
||||
public MyGame.Example2.MonsterT AsMyGame_Example2_Monster() { return this.As<MyGame.Example2.MonsterT>(); }
|
||||
public static AnyUnion FromMyGame_Example2_Monster(MyGame.Example2.MonsterT _mygame_example2_monster) { return new AnyUnion{ Type = Any.MyGame_Example2_Monster, Value = _mygame_example2_monster }; }
|
||||
|
||||
public static int Pack(FlatBuffers.FlatBufferBuilder builder, AnyUnion _o) {
|
||||
switch (_o.Type) {
|
||||
|
||||
@@ -25,8 +25,11 @@ public class AnyAmbiguousAliasesUnion {
|
||||
|
||||
public T As<T>() where T : class { return this.Value as T; }
|
||||
public MyGame.Example.MonsterT AsM1() { return this.As<MyGame.Example.MonsterT>(); }
|
||||
public static AnyAmbiguousAliasesUnion FromM1(MyGame.Example.MonsterT _m1) { return new AnyAmbiguousAliasesUnion{ Type = AnyAmbiguousAliases.M1, Value = _m1 }; }
|
||||
public MyGame.Example.MonsterT AsM2() { return this.As<MyGame.Example.MonsterT>(); }
|
||||
public static AnyAmbiguousAliasesUnion FromM2(MyGame.Example.MonsterT _m2) { return new AnyAmbiguousAliasesUnion{ Type = AnyAmbiguousAliases.M2, Value = _m2 }; }
|
||||
public MyGame.Example.MonsterT AsM3() { return this.As<MyGame.Example.MonsterT>(); }
|
||||
public static AnyAmbiguousAliasesUnion FromM3(MyGame.Example.MonsterT _m3) { return new AnyAmbiguousAliasesUnion{ Type = AnyAmbiguousAliases.M3, Value = _m3 }; }
|
||||
|
||||
public static int Pack(FlatBuffers.FlatBufferBuilder builder, AnyAmbiguousAliasesUnion _o) {
|
||||
switch (_o.Type) {
|
||||
|
||||
@@ -25,8 +25,11 @@ public class AnyUniqueAliasesUnion {
|
||||
|
||||
public T As<T>() where T : class { return this.Value as T; }
|
||||
public MyGame.Example.MonsterT AsM() { return this.As<MyGame.Example.MonsterT>(); }
|
||||
public static AnyUniqueAliasesUnion FromM(MyGame.Example.MonsterT _m) { return new AnyUniqueAliasesUnion{ Type = AnyUniqueAliases.M, Value = _m }; }
|
||||
internal MyGame.Example.TestSimpleTableWithEnumT AsTS() { return this.As<MyGame.Example.TestSimpleTableWithEnumT>(); }
|
||||
internal static AnyUniqueAliasesUnion FromTS(MyGame.Example.TestSimpleTableWithEnumT _ts) { return new AnyUniqueAliasesUnion{ Type = AnyUniqueAliases.TS, Value = _ts }; }
|
||||
public MyGame.Example2.MonsterT AsM2() { return this.As<MyGame.Example2.MonsterT>(); }
|
||||
public static AnyUniqueAliasesUnion FromM2(MyGame.Example2.MonsterT _m2) { return new AnyUniqueAliasesUnion{ Type = AnyUniqueAliases.M2, Value = _m2 }; }
|
||||
|
||||
public static int Pack(FlatBuffers.FlatBufferBuilder builder, AnyUniqueAliasesUnion _o) {
|
||||
switch (_o.Type) {
|
||||
|
||||
@@ -23,6 +23,7 @@ public class UnionInNestedNSUnion {
|
||||
|
||||
public T As<T>() where T : class { return this.Value as T; }
|
||||
public NamespaceA.NamespaceB.TableInNestedNST AsTableInNestedNS() { return this.As<NamespaceA.NamespaceB.TableInNestedNST>(); }
|
||||
public static UnionInNestedNSUnion FromTableInNestedNS(NamespaceA.NamespaceB.TableInNestedNST _tableinnestedns) { return new UnionInNestedNSUnion{ Type = UnionInNestedNS.TableInNestedNS, Value = _tableinnestedns }; }
|
||||
|
||||
public static int Pack(FlatBuffers.FlatBufferBuilder builder, UnionInNestedNSUnion _o) {
|
||||
switch (_o.Type) {
|
||||
|
||||
@@ -25,11 +25,17 @@ public class CharacterUnion {
|
||||
|
||||
public T As<T>() where T : class { return this.Value as T; }
|
||||
public AttackerT AsMuLan() { return this.As<AttackerT>(); }
|
||||
public static CharacterUnion FromMuLan(AttackerT _mulan) { return new CharacterUnion{ Type = Character.MuLan, Value = _mulan }; }
|
||||
public RapunzelT AsRapunzel() { return this.As<RapunzelT>(); }
|
||||
public static CharacterUnion FromRapunzel(RapunzelT _rapunzel) { return new CharacterUnion{ Type = Character.Rapunzel, Value = _rapunzel }; }
|
||||
public BookReaderT AsBelle() { return this.As<BookReaderT>(); }
|
||||
public static CharacterUnion FromBelle(BookReaderT _belle) { return new CharacterUnion{ Type = Character.Belle, Value = _belle }; }
|
||||
public BookReaderT AsBookFan() { return this.As<BookReaderT>(); }
|
||||
public static CharacterUnion FromBookFan(BookReaderT _bookfan) { return new CharacterUnion{ Type = Character.BookFan, Value = _bookfan }; }
|
||||
public string AsOther() { return this.As<string>(); }
|
||||
public static CharacterUnion FromOther(string _other) { return new CharacterUnion{ Type = Character.Other, Value = _other }; }
|
||||
public string AsUnused() { return this.As<string>(); }
|
||||
public static CharacterUnion FromUnused(string _unused) { return new CharacterUnion{ Type = Character.Unused, Value = _unused }; }
|
||||
|
||||
public static int Pack(FlatBuffers.FlatBufferBuilder builder, CharacterUnion _o) {
|
||||
switch (_o.Type) {
|
||||
|
||||
Reference in New Issue
Block a user