Skip to content

Commit

Permalink
Autogenerated Union Types (#50)
Browse files Browse the repository at this point in the history
* Compiler generated union subclasses

* More tests
  • Loading branch information
jamescourtney authored Jul 30, 2020
1 parent 90055e9 commit 625c779
Show file tree
Hide file tree
Showing 14 changed files with 4,195 additions and 3,981 deletions.
2 changes: 1 addition & 1 deletion samples/Example4-IOOptions/IOOperationsExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void Run()
Age = 24,
Cats = new[] { grumpyCat },
Dogs = new[] { tony, rocket, peaches },
FavoritePet = new FlatBufferUnion<Dog, Cat>(rocket),
FavoritePet = new FavoritePet(rocket),
Name = "Nikola Tesla"
};

Expand Down
42 changes: 42 additions & 0 deletions samples/Example9-Unions/Unions.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020 James Courtney
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Samples.Unions;

enum DogBreed : byte { Golden, Lab, Corgi }
enum CatBreed : byte { Calico, Shorthair, Bengal }
enum FishKind : byte { Coelacanth, Dorado, Piranha }

// FlatSharp generates a "Pet" class that derives from the "FlatBufferUnion" class. The derived class
// is syntax sugar and just contains some quality-of-life improvements.
// In this example, the "Doggo" label is applied, so all of the union's properties in C# will
// refer to "Doggo". For fish and Cat, the names are left as-is.
union Pet { Doggo:Dog, Cat, Fish }

// Don't generate a custom type for this union.
union BasicPet (NoCustomType) { Dog, Cat, Fish }

table Dog { Breed:DogBreed; Name:string; }
table Cat { Breed:CatBreed; Name:string; }
table Fish { Kind:FishKind; Weight:float64; Name:string; }

table Person
{
// A person can have one pet. Either a dog, cat, or fish.
Pet:Pet;

BasicPet:BasicPet;
}
86 changes: 86 additions & 0 deletions samples/Example9-Unions/UnionsExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2020 James Courtney
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using FlatSharp;
using FlatSharp.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Samples.Unions
{
/// <summary>
/// This example shows how to use FlatSharp with unions. FlatBuffer unions are discriminated unions, which means exactly one of the fields may be set.
/// FlatSharp can
/// </summary>
public class UnionsExample
{
public static void Run()
{
Cat simon = new Cat { Breed = CatBreed.Bengal, Name = "Simon" };
Dog george = new Dog { Breed = DogBreed.Corgi, Name = "George" };
Fish brick = new Fish { Kind = FishKind.Coelacanth, Name = "Brick" };

Person person = new Person
{
// The difference between Person.Pet and Person.BasicPet is that Person.Pet is
// a custom convenience class that derives from FlatBufferUnion. In
Pet = new Pet(brick),
BasicPet = new FlatBufferUnion<Dog, Cat, Fish>(brick),
};

UsePet(person.Pet);
UseUnion(person.BasicPet);

UseUnion(person.Pet); // Custom unions derive from FlatBufferUnion.
}

// These two methods do the same thing. They are coded separately to
// demonstrate the usability you get with FlatSharp's compiled union
// types.

public static void UsePet(Pet pet)
{
// Enum members
if (pet.Kind == Pet.ItemKind.Doggo)
{
Dog dog = pet.Doggo;
dog.Name.ToLowerInvariant();
}

string breed = pet.Switch(
caseDefault: () => "unknown",
caseDoggo: d => d.Breed.ToString(),
caseCat: c => c.Breed.ToString(),
caseFish: f => f.Kind.ToString());
}

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());
}
}
}
9 changes: 5 additions & 4 deletions samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
<FlatSharpSchema Include="Example7-Includes\A.fbs" />
<FlatSharpSchema Include="Example7-Includes\Subdirectory\B.fbs" />
<FlatSharpSchema Include="Example7-Includes\IncludesExample.fbs" />
<FlatSharpSchema Include="Example9-Unions\Unions.fbs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FlatSharp" Version="3.0.0" />
<PackageReference Include="FlatSharp.Compiler" Version="3.0.0">
<PackageReference Include="FlatSharp" Version="3.2.0" />
<PackageReference Include="FlatSharp.Compiler" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FlatSharp.Runtime" Version="3.0.0" />
<PackageReference Include="FlatSharp.Unsafe" Version="3.0.0" />
<PackageReference Include="FlatSharp.Runtime" Version="3.2.0" />
<PackageReference Include="FlatSharp.Unsafe" Version="3.2.0" />
<PackageReference Include="Grpc" Version="2.27.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/ExperimentalBenchmark/ExperimentalBenchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FlatSharp.Compiler" Version="3.0.0">
<PackageReference Include="FlatSharp.Compiler" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit 625c779

Please sign in to comment.