Skip to content

v1.2.0

Choose a tag to compare

@VPuzdriak VPuzdriak released this 25 Feb 11:44
· 9 commits to main since this release
Immutable release. Only release title and notes can be modified.
dc69faf

Release Notes - v1.2.0

Release Date: February 25, 2026

🎉 What's New

Enum Mapping Support

We're excited to announce that Mapgen v1.2.0 introduces comprehensive support for automatic enum mapping! This feature enables seamless conversion between different enum types with matching member names.

Key Features

  • Name-Based Mapping - Enums are mapped by member name, not by value, ensuring semantic correctness even when enum members are in different orders
  • Automatic Helper Methods - Mapgen generates optimized static helper methods using switch expressions for efficient enum conversion
  • Nullable Support - Both nullable and non-nullable enum mappings are automatically generated
  • Cross-Namespace Support - Enums in different namespaces are handled automatically with proper using statements
  • Collection Support - Collections of enums (List, IEnumerable, arrays, etc.) are mapped element-by-element
  • Type Safety - Includes ArgumentOutOfRangeException for unexpected enum values at runtime

Example

public enum OrderStatus
{
    Shipped,    // Value: 0
    Pending,    // Value: 1
    Delivered   // Value: 2
}

public enum OrderStatusDto
{
    Pending,    // Value: 0
    Shipped,    // Value: 1
    Delivered   // Value: 2
}

[Mapper]
public partial class OrderMapper
{
    public partial OrderDto ToDto(Order order);
    // Status enum automatically mapped BY NAME!
    // OrderStatus.Shipped => OrderStatusDto.Shipped (not by value)
}

Generated Code:

public OrderDto ToDto(Order order)
{
    return new OrderDto
    {
        Id = order.Id,
        Status = MapToOrderStatusDto(order.Status)
    };
}

// Non-nullable helper method
private static OrderStatusDto MapToOrderStatusDto(OrderStatus value) 
  => value switch
  {
    OrderStatus.Shipped => OrderStatusDto.Shipped,
    OrderStatus.Pending => OrderStatusDto.Pending,
    OrderStatus.Delivered => OrderStatusDto.Delivered,
    _ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unexpected enum value")
  };

// Nullable helper method
private static OrderStatusDto? MapToOrderStatusDto(OrderStatus? value) 
  => value.HasValue ? MapToOrderStatusDto(value.Value) : null;

🔧 Technical Details

Architecture Improvements

  • New Helper Classes:

    • EnumMappingHelpers.cs - Utility methods for enum mapping and switch expression formatting
    • TypeCompatibilityChecker.cs - Type compatibility validation for enum and other types
    • EnumMappingMethodInfo.cs - Metadata for enum mapping methods
  • Enhanced Template Engine:

    • MapperTemplateEngine.cs - Extended to generate both nullable and non-nullable enum mapping methods with proper indentation
  • Improved Mapping Strategies:

    • DirectMappingStrategy.cs - Enhanced to detect and handle enum-to-enum mapping
    • ConstructorMappingStrategy.cs - Updated to support enum mapping in constructor parameters
  • Better Diagnostics:

    • New diagnostic for incompatible enum mappings
    • Improved error messages with quotes for better readability

📦 Installation

Install the package version 1.2.0:

dotnet add package Mapgen.Analyzer --version 1.2.0

🙏 Acknowledgments

Thank you to all contributors and users who provided feedback and suggestions for this release!