Skip to content

MapperDefaults configuration API for MSBuild (csproj, Directory.Build.props, ...) #1581

Description

@pomianowski

Is your feature request related to a problem? Please describe.
The default enum mapping option uses values that represent some items in some order. The ByName option, on the other hand, seems to make more business sense.

Example:

gRPC API model

public enum AttachmentVisibilityGenerated {
  Unknown,
  User,
  System,
}

domain API model

public enum AttachmentVisibility {
  User,
  System,
}

Default extension class like

[Mapper]
internal static partial class AttachmentVisibilityExtensions
{
    public static partial AttachmentVisibility ConvertToDomainModel(
        AttachmentVisibilityGenerated from
    );
}

generates:

// <auto-generated />
#nullable enable
namespace MyNamespace
{
    internal static partial class AttachmentVisibilityExtensions
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.0.0")]
        public static partial global::AttachmentVisibility ConvertToDomainModel(this global::AttachmentVisibilityGenerated from)
        {
            return (global::AttachmentVisibility)from;
        }
    }
}

, and will return User, instead of Unknown.

Describe the solution you'd like

By default, the enum should map 1:1 by name and throw an exception if the value is not present.

[Mapper]
internal static partial class AttachmentVisibilityExtensions
{
    public static partial AttachmentVisibility ConvertToDomainModel(
        AttachmentVisibilityGenerated from
    );
}

should generate

// <auto-generated />
#nullable enable
namespace MyNamespace
{
    internal static partial class AttachmentVisibilityExtensions
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.0.0")]
        public static partial global::AttachmentVisibility ConvertToDomainModel(this global::AttachmentVisibilityGenerated from)
        {
            return from switch
            {
                global::AttachmentVisibilityGenerated.User => global::AttachmentVisibility.User,
                global::AttachmentVisibilityGenerated.System=> global::AttachmentVisibility.System,
                _ => throw new System.ArgumentOutOfRangeException(nameof(from), from, "The value of enum AttachmentVisibility is not supported"),
            }
        }
    }
}

like when using

[Mapper(
    EnumMappingStrategy = EnumMappingStrategy.ByName,
    RequiredMappingStrategy = RequiredMappingStrategy.Target
)]

Describe alternatives you've considered

Could use already available

[assembly: MapperDefaults(EnumMappingStrategy = EnumMappingStrategy.ByName)]

, but the above is not very convenient for split dlls and is easily forgotten

Or add new global default switch to csproj like:

<PropertyGroup>
  <DefineConstants>$(DefineConstants);MAPPERLY_ENUM_MAPPING_BY_NAME</DefineConstants>
</PropertyGroup>

or

<PropertyGroup>
  <MapperlyEnumMappingByName>true</MapperlyEnumMappingByName>
</PropertyGroup>

, could be added to Directory.Build.props.

Additional context
It seems to me that much more often the name of an enumeration in a business concourse is more important than its number.

#325
#336
#337
related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions