OneOf.TypeExtensions 1.0.5

dotnet add package OneOf.TypeExtensions --version 1.0.5                
NuGet\Install-Package OneOf.TypeExtensions -Version 1.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OneOf.TypeExtensions" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OneOf.TypeExtensions --version 1.0.5                
#r "nuget: OneOf.TypeExtensions, 1.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OneOf.TypeExtensions as a Cake Addin
#addin nuget:?package=OneOf.TypeExtensions&version=1.0.5

// Install OneOf.TypeExtensions as a Cake Tool
#tool nuget:?package=OneOf.TypeExtensions&version=1.0.5                

OneOf.TypeExtensions

An incremental source generator that creates extension methods for OneOf<> types for more readable code.

Important

Source generated files can have very long names and will likely exceed the path length limit of 256 if you have not disabled it on your computer. See this article for instructions on increasing the limit on Windows.

Installation

Via nuget NuGet\Install-Package OneOf.TypeExtensions

Breaking changes

This project is still in beta and therefore breaking changes may occur. The only thing I forsee changing is how generated functions will be named when there are generics. Currently, OneOf<int, Dictionary<string, int>> will create extension methods like so oneOf.IsDictionaryOfString_Int. This looks fine but it can get a little ugly when there are multiple nested generics.

Also, generated code currently has no namespace which is fine with me but may be changed based on feedback.

Performance

I have tried to make the generator somewhat performant, but I am by no means an expert at incremental source generators. Feel free to submit a PR improving it's efficiency.

Example Generated Code

For the type OneOf<int, string>, these extension methods will be generated.

OneOfExtensions_String_Int.g.cs

// <auto-generated>

using OneOf;

public static partial class OneOfTypeExtensions
{
    public static bool IsString(this OneOf<string, int> oneOf)
    {
        return oneOf.IsT0;
    }
    public static string AsString(this OneOf<string, int> oneOf)
    {
        return oneOf.AsT0;
    }
    public static OneOf<TResult, int> MapString<TResult>(this OneOf<string, int> oneOf, Func<string, TResult> mapFunc)
    {
        return oneOf.MapT0(mapFunc);
    }
    public static bool TryPickString(this OneOf<string, int> oneOf, out string value, out int remainder)
    {
        return oneOf.TryPickT0(out value, out remainder);
    }
    public static bool IsInt(this OneOf<string, int> oneOf)
    {
        return oneOf.IsT1;
    }
    public static int AsInt(this OneOf<string, int> oneOf)
    {
        return oneOf.AsT1;
    }
    public static OneOf<string, TResult> MapInt<TResult>(this OneOf<string, int> oneOf, Func<int, TResult> mapFunc)
    {
        return oneOf.MapT1(mapFunc);
    }
    public static bool TryPickInt(this OneOf<string, int> oneOf, out int value, out string remainder)
    {
        return oneOf.TryPickT1(out value, out remainder);
    }
}

Supported Types

I believe the generator should support practically any type, if you find a type or combination of types that causes an error, feel free to create an issue or submit a PR.

Special consideration was given to support these types

  • Nullable value types int?
  • Nullable annotated reference types string?
  • Value Tuples (int?, string?)
  • Generics List<int?>
  • Multiple Generics Dictionary<string?, int?>
  • Nested generics List<List<int?>>
  • Nested types Class1.Class2
  • Any combination of the above

Roadmap

The project is still very much at it's infancy. I have a few ideas for future features.

  • Control what extension methods get generated through csproj properties
  • Improve efficiency of generator
  • Better extension method names with nested generics
  • Keep hint names shorter and more understandable while ensuring they are unique
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 1,252 12/20/2023
1.0.4 198 12/20/2023
1.0.3 246 12/4/2023
1.0.2 217 11/27/2023
0.1.0-beta 187 11/24/2023