Ooak 0.1.0
dotnet add package Ooak --version 0.1.0
NuGet\Install-Package Ooak -Version 0.1.0
<PackageReference Include="Ooak" Version="0.1.0" />
<PackageVersion Include="Ooak" Version="0.1.0" />
<PackageReference Include="Ooak" />
paket add Ooak --version 0.1.0
#r "nuget: Ooak, 0.1.0"
#:package Ooak@0.1.0
#addin nuget:?package=Ooak&version=0.1.0
#tool nuget:?package=Ooak&version=0.1.0
Ooak - The TypeUnion implementation
This package allows to use type unions in C#. A type union represents a value that can be one of two types.
Creating TypeUnion instances
Create TypeUnion instances:
/// <summary>
/// Tries to parse the date. Returns either a DateTime on success, the original string otherwise.
/// This example is stupid, but demonstrates how things work
/// </summary>
TypeUnion<DateTime, string> TryParseDate(string date)
{
if (DateTime.TryParse(date, out var outDateTime))
{
// Return a `Left`, i.e. a `DateTime` instance
return new TypeUnion<DateTime, string>.Left(outDateTime);
}
// Return a `Right`, i.e. a `DateTime` instance
return new TypeUnion<DateTime, string>.Right(date);
}
Sometimes, the value you represent can be of two types at the same time (the anyOf case of the JSON deserialization):
// Yes, C#9 records are supported !
public record Paging(int Page, int ItemsPerPage);
public record Ordering(bool Ascending);
public void DemoBoth()
{
TypeUnion<Paging, Ordering> queryParameters;
// Can be left (Paging)
queryParameters = new TypeUnion<Paging, Ordering>.Left(new Paging(1, 50));
// Can be right (Ordering)
queryParameters = new TypeUnion<Paging, Ordering>.Right(new Ordering(true));
// But can also be both at the same time!
queryParameters = new TypeUnion<Paging, Ordering>.Both(new Paging(10, 10), new Ordering(false));
}
Using the TypeUnion object
public void UsingTypeUnion(TypeUnion<Paging, Ordering> queryParameters)
{
// Detect with `is` and the Left, Right and Both classes
if (queryParameters is TypeUnion<Paging, Ordering>.Left p)
{
Console.WriteLine($"This is a Paging only at page {p.Value.Page} with {p.Value.ItemsPerPage} items per page");
}
// With pattern matching
switch (queryParameters)
{
case TypeUnion<Paging, Ordering>.Left left:
Console.WriteLine("Left");
break;
case TypeUnion<Paging, Ordering>.Right right:
Console.WriteLine("Right");
break;
case TypeUnion<Paging, Ordering>.Both right:
Console.WriteLine("Both");
break;
}
// But if you need ordering info, you will need to test both Right and Both.
// Or you could use this helper syntax :
if (queryParameters.TryGetRight(out var ordering))
{
Console.WriteLine($"Ordering {(ordering.Ascending ? "Ascending" : "Descending")}");
}
}
Special notes
The type parameters cannot contain nullable types (you cannot write TypeUnion<string?, DateTime> nor TypeUnion<string, DateTime?>), but you can make your whole union nullable (TypeUnion<string, DateTime>?).
This library only supports 2 type parameters for now. If you need more parameters, nest them : TypeUnion<A, TypeUnion<B, C>>.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Ooak:
| Package | Downloads |
|---|---|
|
Ooak.SystemTextJson
Ooak deserializer for System.Text.Json that allows the use of oneOf/anyOf/allOf |
|
|
Ooak.NewtonsoftJson
Ooak deserializer for Newtonsoft.Json that allows the use of oneOf/anyOf/allOf |
GitHub repositories
This package is not used by any popular GitHub repositories.