Ejemplo n.º 1
0
        public static void UseUnion(FlatBufferUnion <Dog, Cat, Fish> basicUnion)
        {
            if (basicUnion.Discriminator == 1)
            {
                Dog dog = basicUnion.Item1;
                dog.Name.ToLowerInvariant();
            }

            string breed = basicUnion.Switch(
                defaultCase: () => "unknown",
                case1: d => d.Breed.ToString(),
                case2: c => c.Breed.ToString(),
                case3: f => f.Kind.ToString());
        }
Ejemplo n.º 2
0
    public void CopyConstructorsTest()
    {
        string     schema   = $@"
{MetadataHelpers.AllAttributes}
namespace CopyConstructorTest;

union Union {{ OuterTable, InnerTable, OuterStruct, InnerStruct }} // Optionally add more tables.

table OuterTable ({MetadataKeys.SerializerKind}: ""Greedy"") {{
  A:string (id: 0);

  B:byte   (id: 1);
  C:ubyte  (id: 2);
  D:int16  (id: 3); 
  E:uint16 (id: 4);
  F:int32  (id: 5);
  G:uint32 (id: 6);
  H:int64  (id: 7);
  I:uint64 (id: 8);
  
  IntVector_List:[int] ({MetadataKeys.VectorKind}:""IList"", id: 9);
  IntVector_RoList:[int] ({MetadataKeys.VectorKind}:""IReadOnlyList"", id: 10);
  IntVector_Array:[int] ({MetadataKeys.VectorKind}:""Array"", id: 11);
  
  TableVector_List:[InnerTable] ({MetadataKeys.VectorKind}:""IList"", id: 12);
  TableVector_RoList:[InnerTable] ({MetadataKeys.VectorKind}:""IReadOnlyList"", id: 13);
  TableVector_Indexed:[InnerTable] ({MetadataKeys.VectorKind}:""IIndexedVector"", id: 14);
  TableVector_Array:[InnerTable] ({MetadataKeys.VectorKind}:""Array"", id: 15);

  ByteVector:[ubyte] ({MetadataKeys.VectorKind}:""Memory"", id: 16);
  ByteVector_RO:[ubyte] ({MetadataKeys.VectorKind}:""ReadOnlyMemory"", id: 17);
  UnionVal : Union (id: 19);

  VectorOfUnion : [Union] (id: 21);
  VectorOfUnion_RoList : [Union] (id: 23, {MetadataKeys.VectorKind}:""IReadOnlyList"");
  VectorOfUnion_Array : [Union] (id: 25, {MetadataKeys.VectorKind}:""Array"");
}}

struct InnerStruct {{
    LongValue:int64;
}}

struct OuterStruct {{
    Value:int;
    InnerStructVal:InnerStruct;
}}

table InnerTable {{
  Name:string ({MetadataKeys.Key});
  OuterStructVal:OuterStruct;
}}

";
        OuterTable original = new OuterTable
        {
            A = "string",
            B = 1,
            C = 2,
            D = 3,
            E = 4,
            F = 5,
            G = 6,
            H = 7,
            I = 8,

            ByteVector = new byte[] { 1, 2, 3, }.AsMemory(),
                   ByteVector_RO = new byte[] { 4, 5, 6, }.AsMemory(),

                   IntVector_Array = new[] { 7, 8, 9, },
                   IntVector_List = new[] { 10, 11, 12, }.ToList(),
                   IntVector_RoList = new[] { 13, 14, 15 }.ToList(),

                   TableVector_Array   = CreateInner("Rocket", "Molly", "Clementine"),
                   TableVector_Indexed = new IndexedVector <string, InnerTable>(CreateInner("Pudge", "Sunshine", "Gypsy"), false),
                   TableVector_List    = CreateInner("Finnegan", "Daisy"),
                   TableVector_RoList  = CreateInner("Gordita", "Lunchbox"),

                   UnionVal      = new FlatBufferUnion <OuterTable, InnerTable, OuterStruct, InnerStruct>(new OuterStruct()),
                   VectorOfUnion = new List <FlatBufferUnion <OuterTable, InnerTable, OuterStruct, InnerStruct> >
            {
                new(new OuterTable()),
                new(new InnerTable()),
                new(new OuterStruct()),
                new(new InnerStruct()),
            },