Porticle.Enumly

RSCG – Porticle.Enumly
 
 

name Porticle.Enumly
nuget https://www.nuget.org/packages/Porticle.Enumly/
link https://github.com/Machibuse/Porticle.Enumly
author Carsten Jendro

generate enum-to-enum mappings, so one enum values can be converted to another enum type with matching names or values

 

This is how you can use Porticle.Enumly .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Porticle.Enumly" Version="1.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

 
  
 

</Project>


The code that you will use is


using EnumDemo;

CarTypes carType = CarTypes.Dacia;
TypesCar typesCar = Mapper.ToTypesCar(carType); 
Console.WriteLine($"CarTypes: {carType} {typesCar}");


namespace EnumDemo;
public enum CarTypes
{
    None,
    Dacia,
    Tesla,
    BMW,
    Mercedes
}

public enum TypesCar
{
    CarNone,
    CarDacia,
    CarTesla,
    CarBMW,
    CarMercedes
}



using Porticle.Enumly;
namespace EnumDemo;

[EnumlyClass]
public static partial class Mapper
{
    [EnumlyMap(IgnoreNullSource = true)]
    public static partial TypesCar ToTypesCar(CarTypes value);

    [EnumlyMap(IgnoreNullSource = true)]
    public static partial CarTypes ToCarTypes(TypesCar value);
}

 

The code that is generated is

// <auto-generated/>
#nullable enable

namespace EnumDemo;

public static partial class Mapper
{
    public static partial global::EnumDemo.TypesCar ToTypesCar(global::EnumDemo.CarTypes value)
    {
        return value switch
        {
            global::EnumDemo.CarTypes.None => global::EnumDemo.TypesCar.CarNone,
            global::EnumDemo.CarTypes.Dacia => global::EnumDemo.TypesCar.CarDacia,
            global::EnumDemo.CarTypes.Tesla => global::EnumDemo.TypesCar.CarTesla,
            global::EnumDemo.CarTypes.BMW => global::EnumDemo.TypesCar.CarBMW,
            global::EnumDemo.CarTypes.Mercedes => global::EnumDemo.TypesCar.CarMercedes,
            _ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, $"The value of enum \"EnumDemo.CarTypes\" is not supported."),
        };
    }

    public static partial global::EnumDemo.CarTypes ToCarTypes(global::EnumDemo.TypesCar value)
    {
        return value switch
        {
            global::EnumDemo.TypesCar.CarNone => global::EnumDemo.CarTypes.None,
            global::EnumDemo.TypesCar.CarDacia => global::EnumDemo.CarTypes.Dacia,
            global::EnumDemo.TypesCar.CarTesla => global::EnumDemo.CarTypes.Tesla,
            global::EnumDemo.TypesCar.CarBMW => global::EnumDemo.CarTypes.BMW,
            global::EnumDemo.TypesCar.CarMercedes => global::EnumDemo.CarTypes.Mercedes,
            _ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, $"The value of enum \"EnumDemo.TypesCar\" is not supported."),
        };
    }
}

// <auto-generated/>
#nullable enable
namespace Porticle.Enumly
{
    [global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    internal sealed class EnumlyClassAttribute : global::System.Attribute { }

    [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    internal sealed class EnumlyMapAttribute : global::System.Attribute
    {
        /// <summary>
        /// Value of the source enum that should be mapped to <c>null</c> on the target side.
        /// Only meaningful when the target return type is a nullable enum.
        /// </summary>
        public object? NullSourceValue { get; set; }

        /// <summary>
        /// Value of the target enum that is produced when the source value is <c>null</c>.
        /// Only meaningful when the source parameter type is a nullable enum.
        /// </summary>
        public object? NullTargetValue { get; set; }

        /// <summary>
        /// When <c>true</c>, a <c>null</c> source value throws
        /// <see cref="global::System.ArgumentNullException"/> at runtime instead of
        /// producing a target. Only meaningful when the source parameter type is a
        /// nullable enum. Mutually exclusive with <see cref="NullTargetValue"/>.
        /// </summary>
        public bool IgnoreNullSource { get; set; }
    }

    /// <summary>
    /// Defines an explicit source-&gt;target enum value mapping for special cases.
    /// Can be applied multiple times. Overrides by-name matching for the given source value.
    /// </summary>
    [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    internal sealed class EnumlyMapValueAttribute : global::System.Attribute
    {
        public EnumlyMapValueAttribute(object source, object target) { }
    }

    /// <summary>
    /// Marks a value of the source enum as intentionally unmapped. Suppresses the
    /// 'no matching target member' diagnostic (EM0001) for that value. At runtime
    /// the ignored value will throw <see cref="global::System.ArgumentOutOfRangeException"/>
    /// with a message identifying it as explicitly excluded.
    /// Can be applied multiple times.
    /// </summary>
    [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    internal sealed class EnumlyIgnoreSourceAttribute : global::System.Attribute
    {
        public EnumlyIgnoreSourceAttribute(object value) { }
    }

    /// <summary>
    /// Marks a value of the target enum as intentionally unreachable. Suppresses
    /// the 'target value is not produced by any source mapping' diagnostic
    /// (EM0008) for that value. Can be applied multiple times.
    /// </summary>
    [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    internal sealed class EnumlyIgnoreTargetAttribute : global::System.Attribute
    {
        public EnumlyIgnoreTargetAttribute(object value) { }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Porticle.Enumly


Posted

in

,

by

Tags: