diff --git a/docs/docs/02-configuration/04-enum.mdx b/docs/docs/02-configuration/04-enum.mdx index 6edeb2aad0..44eb3869bc 100644 --- a/docs/docs/02-configuration/04-enum.mdx +++ b/docs/docs/02-configuration/04-enum.mdx @@ -88,7 +88,7 @@ public partial class CarMapper } ``` -### Strict enum mappings +## Strict enum mappings To enforce strict enum mappings (all source enum values have to be mapped to a target enum value diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumMappingBuilder.cs index 58957ad073..ee8e8203d2 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumMappingBuilder.cs @@ -72,6 +72,7 @@ private static TypeMapping BuildCastMappingAndDiagnostic(MappingBuilderContext c .OfType() .Where(x => !explicitMappingTargetNames.Contains(x.Name)) .ToDictionary(field => field.Name, field => field.ConstantValue); + var targetMemberNames = ctx.Target.GetMembers().OfType().Select(x => x.Name).ToHashSet(); var missingTargetValues = targetValues.Where( field => @@ -95,7 +96,14 @@ private static TypeMapping BuildCastMappingAndDiagnostic(MappingBuilderContext c checkTargetDefined = true; } - var castFallbackMapping = new EnumCastMapping(ctx.Types.Get(), ctx.Source, ctx.Target, checkTargetDefined, fallbackMapping); + var checkDefinedMode = checkTargetDefined switch + { + false => EnumCastMapping.CheckDefinedMode.NoCheck, + _ when ctx.Target.HasAttribute(ctx.Types.Get()) => EnumCastMapping.CheckDefinedMode.Flags, + _ => EnumCastMapping.CheckDefinedMode.Value, + }; + + var castFallbackMapping = new EnumCastMapping(ctx.Source, ctx.Target, checkDefinedMode, targetMemberNames, fallbackMapping); if (explicitMappings.Count == 0) return castFallbackMapping; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs index a551ff473e..b1fdcab1d7 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs @@ -5,36 +5,74 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums; +/// +/// An enum cast mapping which casts the source to the target type and optionally checks whether the target is defined. +/// If it is not defined an optional fallback value is used. +/// public class EnumCastMapping : CastMapping { - private const string IsDefinedMethodName = nameof(Enum.IsDefined); - - private readonly ITypeSymbol _enumType; - private readonly bool _checkDefined; + private readonly CheckDefinedMode _checkDefinedMode; + private readonly IReadOnlyCollection _targetEnumMemberNames; private readonly EnumFallbackValueMapping _fallback; + public enum CheckDefinedMode + { + /// + /// No check is performed at all, the value is just casted. + /// + NoCheck, + + /// + /// It is checked if the casted value is defined in the target enum. + /// + Value, + + /// + /// It is checked if the casted value is a defined flags combination of the target enum. + /// + Flags + } + public EnumCastMapping( - ITypeSymbol enumType, ITypeSymbol sourceType, ITypeSymbol targetType, - bool checkDefined, + CheckDefinedMode checkDefinedMode, + IReadOnlyCollection targetEnumMemberNames, EnumFallbackValueMapping fallback ) : base(sourceType, targetType) { - _enumType = enumType; - _checkDefined = checkDefined; + _checkDefinedMode = checkDefinedMode; + _targetEnumMemberNames = targetEnumMemberNames; _fallback = fallback; } public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { var casted = base.Build(ctx); - if (!_checkDefined) + if (_checkDefinedMode == CheckDefinedMode.NoCheck) return casted; - var isDefinedMethod = MemberAccess(FullyQualifiedIdentifier(_enumType), IsDefinedMethodName); - var isDefined = Invocation(isDefinedMethod, TypeOfExpression(FullyQualifiedIdentifier(TargetType)), casted); - return ConditionalExpression(isDefined, casted, _fallback.Build(ctx)); + var valueDefinedCondition = BuildIsDefinedCondition(casted); + return ConditionalExpression(valueDefinedCondition, casted, _fallback.Build(ctx)); + } + + private ExpressionSyntax BuildIsDefinedCondition(ExpressionSyntax convertedSourceValue) + { + var allEnumMembers = _targetEnumMemberNames.Select(x => MemberAccess(FullyQualifiedIdentifier(TargetType), x)); + return _checkDefinedMode switch + { + // (TargetEnum)v is TargetEnum.A or TargetEnum.B or ... + CheckDefinedMode.Value + => IsPatternExpression(convertedSourceValue, OrPattern(allEnumMembers)), + + // (TargetEnum)v == ((TargetEnum)v & (TargetEnum.A | TargetEnum.B | ...)) + CheckDefinedMode.Flags + => Equal( + convertedSourceValue, + ParenthesizedExpression(BitwiseAnd(convertedSourceValue, ParenthesizedExpression(BitwiseOr(allEnumMembers)))) + ), + _ => throw new ArgumentOutOfRangeException($"{nameof(_checkDefinedMode)} has an unknown value {_checkDefinedMode}") + }; } } diff --git a/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs b/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs index fea1b6de9d..5ebf8cc2ea 100644 --- a/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs +++ b/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs @@ -39,18 +39,29 @@ public static SyntaxToken Accessibility(Accessibility accessibility) }; } - public static BinaryExpressionSyntax Coalesce(ExpressionSyntax expr, ExpressionSyntax coalesceExpr) - { - return BinaryExpression(SyntaxKind.CoalesceExpression, expr, coalesceExpr); - } + public static BinaryExpressionSyntax Coalesce(ExpressionSyntax expr, ExpressionSyntax coalesceExpr) => + SyntaxFactory.BinaryExpression(SyntaxKind.CoalesceExpression, expr, coalesceExpr); - public static ExpressionSyntax Or(IEnumerable values) => - values.WhereNotNull().Aggregate((a, b) => BinaryExpression(SyntaxKind.LogicalOrExpression, a, b)); + public static ExpressionSyntax Or(IEnumerable values) => BinaryExpression(SyntaxKind.LogicalOrExpression, values); public static ExpressionSyntax And(params ExpressionSyntax?[] values) => And((IEnumerable)values); - public static ExpressionSyntax And(IEnumerable values) => - values.WhereNotNull().Aggregate((a, b) => BinaryExpression(SyntaxKind.LogicalAndExpression, a, b)); + public static ExpressionSyntax And(IEnumerable values) => BinaryExpression(SyntaxKind.LogicalAndExpression, values); + + public static ExpressionSyntax BitwiseAnd(params ExpressionSyntax?[] values) => + BinaryExpression(SyntaxKind.BitwiseAndExpression, values); + + public static ExpressionSyntax BitwiseOr(IEnumerable values) => + BinaryExpression(SyntaxKind.BitwiseOrExpression, values); + + public static PatternSyntax OrPattern(IEnumerable values) => + values + .WhereNotNull() + .Select(ConstantPattern) + .Aggregate((left, right) => BinaryPattern(SyntaxKind.OrPattern, left, right)); + + public static ExpressionSyntax Equal(ExpressionSyntax left, ExpressionSyntax right) => + BinaryExpression(SyntaxKind.EqualsExpression, left, right); public static ExpressionSyntax IfNoneNull(params (ITypeSymbol Type, ExpressionSyntax Access)[] values) { @@ -65,10 +76,10 @@ public static ExpressionSyntax IfAnyNull(params (ITypeSymbol Type, ExpressionSyn } public static BinaryExpressionSyntax IsNull(ExpressionSyntax expression) => - BinaryExpression(SyntaxKind.EqualsExpression, expression, NullLiteral()); + SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, expression, NullLiteral()); public static BinaryExpressionSyntax IsNotNull(ExpressionSyntax expression) => - BinaryExpression(SyntaxKind.NotEqualsExpression, expression, NullLiteral()); + SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, expression, NullLiteral()); public static ExpressionSyntax NullSubstitute(ITypeSymbol t, ExpressionSyntax argument, NullFallbackValue nullFallbackValue) { @@ -389,6 +400,12 @@ public static SeparatedSyntaxList CommaSeparatedList(IEnumerable nodes, public static IReadOnlyCollection SingleStatement(ExpressionSyntax expression) => new[] { ExpressionStatement(expression) }; + private static ExpressionSyntax BinaryExpression(SyntaxKind kind, params ExpressionSyntax?[] values) => + BinaryExpression(kind, (IEnumerable)values); + + private static ExpressionSyntax BinaryExpression(SyntaxKind kind, IEnumerable values) => + values.WhereNotNull().Aggregate((left, right) => SyntaxFactory.BinaryExpression(kind, left, right)); + private static InterpolatedStringTextSyntax InterpolatedStringText(string text) => SyntaxFactory.InterpolatedStringText( Token(SyntaxTriviaList.Empty, SyntaxKind.InterpolatedStringTextToken, text, text, SyntaxTriviaList.Empty) diff --git a/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs b/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs index 0c3cc3c898..55513be2fd 100644 --- a/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs +++ b/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs @@ -53,6 +53,7 @@ public static TestObject NewTestObj() IntValue = 10, EnumName = TestEnum.Value10, EnumValue = TestEnum.Value10, + FlagsEnumValue = TestFlagsEnum.V1 | TestFlagsEnum.V4, IntInitOnlyValue = 3, RequiredValue = 4, NestedNullable = new TestObjectNested { IntValue = 100, }, diff --git a/test/Riok.Mapperly.IntegrationTests/Dto/TestFlagsEnumDto.cs b/test/Riok.Mapperly.IntegrationTests/Dto/TestFlagsEnumDto.cs new file mode 100644 index 0000000000..f43f029ba6 --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/Dto/TestFlagsEnumDto.cs @@ -0,0 +1,14 @@ +using System; + +namespace Riok.Mapperly.IntegrationTests.Dto +{ + [Flags] + public enum TestFlagsEnumDto + { + V1 = 1 << 0, + V2 = 1 << 1, + + // use another name to test mapping by value + V3 = 1 << 2, + } +} diff --git a/test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDto.cs b/test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDto.cs index c5f7384a3a..2ed9670aac 100644 --- a/test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDto.cs +++ b/test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDto.cs @@ -91,6 +91,8 @@ public TestObjectDto(int ctorValue, int unknownValue = 10, int ctorValue2 = 100) public TestEnumDtoByValue EnumValue { get; set; } + public TestFlagsEnumDto FlagsEnumValue { get; set; } + public TestEnumDtoByName EnumName { get; set; } public byte EnumRawValue { get; set; } diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs index f1e9aee450..bc6b4cd4a9 100644 --- a/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs @@ -65,9 +65,6 @@ public static TestObjectDto MapToDto(TestObject src) [MapperIgnoreSource(nameof(TestObjectDto.IgnoredIntValue))] public static partial TestObject MapFromDto(TestObjectDto dto); - [MapEnum(EnumMappingStrategy.ByName)] - public static partial TestEnumDtoByName MapToEnumDtoByName(TestEnum v); - [MapperIgnoreTarget(nameof(TestObjectDto.IgnoredIntValue))] [MapperIgnoreSource(nameof(TestObject.IgnoredStringValue))] public static partial void UpdateDto(TestObject source, TestObjectDto target); @@ -88,5 +85,28 @@ public static TestObjectDto MapToDto(TestObject src) public static partial object? MapNullableWithRuntimeTargetType(object? source, Type targetType); public static partial TTarget MapGeneric(TSource source); + + [MapEnum(EnumMappingStrategy.ByName)] + public static partial TestEnumDtoByName MapToEnumDtoByName(TestEnum v); + + [MapEnum(EnumMappingStrategy.ByName)] + [MapEnumValue(TestEnumDtoAdditionalValue.Value40, TestEnum.Value30)] + public static partial TestEnum MapToEnumByNameWithExplicit(TestEnumDtoAdditionalValue v); + + [MapEnum(EnumMappingStrategy.ByValue)] + [MapEnumValue(TestEnumDtoAdditionalValue.Value40, TestEnum.Value30)] + public static partial TestEnum MapToEnumByValueWithExplicit(TestEnumDtoAdditionalValue v); + + [MapEnum(EnumMappingStrategy.ByValueCheckDefined)] + public static partial TestEnum MapToEnumByValueCheckDefined(TestEnumDtoByValue v); + + [MapEnum(EnumMappingStrategy.ByValueCheckDefined, FallbackValue = TestEnum.Value10)] + public static partial TestEnum MapToEnumByValueCheckDefinedWithFallback(TestEnumDtoByValue v); + + [MapEnum(EnumMappingStrategy.ByValueCheckDefined)] + public static partial TestFlagsEnum MapToFlagsEnumByValueCheckDefined(TestFlagsEnumDto v); + + [MapEnum(EnumMappingStrategy.ByName, FallbackValue = TestEnum.Value10)] + public static partial TestEnum MapToEnumByNameWithFallback(TestEnumDtoByName v); } } diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs index 791e65b588..05aa8f2b9c 100644 --- a/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs @@ -59,30 +59,13 @@ public TestObjectDto MapToDto(TestObject src) [MapperIgnoreSource(nameof(TestObjectDto.IgnoredIntValue))] public partial TestObject MapFromDto(TestObjectDto dto); - [MapEnum(EnumMappingStrategy.ByName)] - public partial TestEnumDtoByName MapToEnumDtoByName(TestEnum v); - - [MapEnum(EnumMappingStrategy.ByName)] - [MapEnumValue(TestEnumDtoAdditionalValue.Value40, TestEnum.Value30)] - public partial TestEnum MapToEnumByNameWithExplicit(TestEnumDtoAdditionalValue v); - - [MapEnum(EnumMappingStrategy.ByValue)] - [MapEnumValue(TestEnumDtoAdditionalValue.Value40, TestEnum.Value30)] - public partial TestEnum MapToEnumByValueWithExplicit(TestEnumDtoAdditionalValue v); - - [MapEnum(EnumMappingStrategy.ByValueCheckDefined)] - public partial TestEnum MapToEnumByValueCheckDefined(TestEnumDtoByValue v); - - [MapEnum(EnumMappingStrategy.ByValueCheckDefined, FallbackValue = TestEnum.Value10)] - public partial TestEnum MapToEnumByValueCheckDefinedWithFallback(TestEnumDtoByValue v); - - [MapEnum(EnumMappingStrategy.ByName, FallbackValue = TestEnum.Value10)] - public partial TestEnum MapToEnumByNameWithFallback(TestEnumDtoByName v); - [MapperIgnoreTarget(nameof(TestObjectDto.IgnoredIntValue))] [MapperIgnoreSource(nameof(TestObject.IgnoredStringValue))] public partial void UpdateDto(TestObject source, TestObjectDto target); + [MapEnum(EnumMappingStrategy.ByName)] + public partial TestEnumDtoByName MapToEnumDtoByName(TestEnum v); + private partial int PrivateDirectInt(int value); } } diff --git a/test/Riok.Mapperly.IntegrationTests/Models/TestFlagsEnum.cs b/test/Riok.Mapperly.IntegrationTests/Models/TestFlagsEnum.cs new file mode 100644 index 0000000000..9da4f717ab --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/Models/TestFlagsEnum.cs @@ -0,0 +1,12 @@ +using System; + +namespace Riok.Mapperly.IntegrationTests.Models +{ + [Flags] + public enum TestFlagsEnum + { + V1 = 1 << 0, + V2 = 1 << 1, + V4 = 1 << 2, + } +} diff --git a/test/Riok.Mapperly.IntegrationTests/Models/TestObject.cs b/test/Riok.Mapperly.IntegrationTests/Models/TestObject.cs index c87b87239a..2917598d22 100644 --- a/test/Riok.Mapperly.IntegrationTests/Models/TestObject.cs +++ b/test/Riok.Mapperly.IntegrationTests/Models/TestObject.cs @@ -89,6 +89,8 @@ public TestObject(int ctorValue, int unknownValue = 10, int ctorValue2 = 100) public TestEnum EnumValue { get; set; } + public TestFlagsEnum FlagsEnumValue { get; set; } + public TestEnum EnumName { get; set; } public TestEnum EnumRawValue { get; set; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.RunMappingShouldWork.verified.txt index 69e6a9177b..56590ac836 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.RunMappingShouldWork.verified.txt @@ -130,6 +130,7 @@ 3 ], EnumValue: Value10, + FlagsEnumValue: V1, V4, EnumName: Value10, EnumRawValue: Value20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs index 63d9612ffa..50ec24ac7b 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs @@ -88,6 +88,7 @@ public static partial class DeepCloningMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(src.HashSet); target.SortedSet = new global::System.Collections.Generic.SortedSet(src.SortedSet); target.EnumValue = src.EnumValue; + target.FlagsEnumValue = src.FlagsEnumValue; target.EnumName = src.EnumName; target.EnumRawValue = src.EnumRawValue; target.EnumStringValue = src.EnumStringValue; diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.RunMappingShouldWork.verified.txt index cd45c6fc2c..0623e9c638 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.RunMappingShouldWork.verified.txt @@ -139,6 +139,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs index 8a1ce16408..b76f0ae367 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs @@ -128,6 +128,7 @@ public partial class TestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(testObject.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)testObject.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(testObject.EnumName); target.EnumRawValue = (byte)testObject.EnumRawValue; target.EnumStringValue = MapToString(testObject.EnumStringValue); @@ -203,67 +204,15 @@ public partial class TestMapper target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.ISet, x => x.ToString())); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.HashSet, x => x.ToString())); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(dto.SortedSet, x => x.ToString())); - target.EnumValue = MapToEnumByValueCheckDefined(dto.EnumValue); - target.EnumName = MapToEnumByNameWithFallback(dto.EnumName); + target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)dto.FlagsEnumValue; + target.EnumName = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); return target; } - public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) - { - return global::System.Enum.IsDefined(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum), (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v) ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoByValue is not supported"); - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefinedWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) - { - return global::System.Enum.IsDefined(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum), (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v) ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - }; - } - public partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) { if (source.NullableFlattening != null) @@ -337,6 +286,7 @@ public partial class TestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(source.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)source.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(source.EnumName); target.EnumRawValue = (byte)source.EnumRawValue; target.EnumStringValue = MapToString(source.EnumStringValue); @@ -345,6 +295,17 @@ public partial class TestMapper target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly); } + public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + private partial int PrivateDirectInt(int value) { return value; diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt index 131816f179..785b03edee 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt @@ -134,6 +134,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunMappingShouldWork.verified.txt index 57f8460c7c..07a06778c8 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.RunMappingShouldWork.verified.txt @@ -139,6 +139,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs index 3f56e492a6..3a79ff9af3 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -125,6 +125,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(src.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)src.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)src.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(src.EnumName); target.EnumRawValue = (byte)src.EnumRawValue; target.EnumStringValue = MapToString(src.EnumStringValue); @@ -218,6 +219,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(testObject.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)testObject.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(testObject.EnumName); target.EnumRawValue = (byte)testObject.EnumRawValue; target.EnumStringValue = MapToString(testObject.EnumStringValue); @@ -293,25 +295,15 @@ public static partial class StaticTestMapper target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.ISet, x => x.ToString())); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.HashSet, x => x.ToString())); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(dto.SortedSet, x => x.ToString())); - target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; - target.EnumName = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; + target.EnumValue = MapToEnumByValueCheckDefined(dto.EnumValue); + target.FlagsEnumValue = MapToFlagsEnumByValueCheckDefined(dto.FlagsEnumValue); + target.EnumName = MapToEnumByNameWithFallback(dto.EnumName); target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); return target; } - public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), - }; - } - public static partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) { if (source.NullableFlattening != null) @@ -385,6 +377,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(source.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)source.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(source.EnumName); target.EnumRawValue = (byte)source.EnumRawValue; target.EnumStringValue = MapToString(source.EnumStringValue); @@ -413,6 +406,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithFallback(x), int x when targetType.IsAssignableFrom(typeof(int)) => DirectInt(x), int x when targetType.IsAssignableFrom(typeof(long)) => ImplicitCastInt(x), uint x when targetType.IsAssignableFrom(typeof(int)) => ExplicitCastInt(x), @@ -433,6 +430,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithFallback(x), int x when targetType.IsAssignableFrom(typeof(int)) => DirectInt(x), int x when targetType.IsAssignableFrom(typeof(long)) => ImplicitCastInt(x), uint x when targetType.IsAssignableFrom(typeof(int)) => ExplicitCastInt(x), @@ -453,6 +454,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => (TTarget)(object)MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => (TTarget)(object)MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByNameWithFallback(x), int x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)DirectInt(x), int x when typeof(TTarget).IsAssignableFrom(typeof(long)) => (TTarget)(object)ImplicitCastInt(x), uint x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)ExplicitCastInt(x), @@ -468,6 +473,64 @@ public static partial class StaticTestMapper }; } + public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v is global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoByValue is not supported"); + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefinedWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v is global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum MapToFlagsEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v == ((global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v & (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V1 | global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V2 | global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V4)) ? (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestFlagsEnumDto is not supported"); + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + }; + } + private static global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto MapToTestObjectNestedDto(global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.RunMappingShouldWork.verified.txt index eb4c239014..f4d606123b 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.RunMappingShouldWork.verified.txt @@ -135,6 +135,7 @@ 3 ], EnumValue: Value10, + FlagsEnumValue: V1, V4, EnumName: Value10, EnumRawValue: Value20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs index 52c3a5c3bd..307941c487 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs @@ -87,6 +87,7 @@ public static partial class DeepCloningMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(src.HashSet); target.SortedSet = new global::System.Collections.Generic.SortedSet(src.SortedSet); target.EnumValue = src.EnumValue; + target.FlagsEnumValue = src.FlagsEnumValue; target.EnumName = src.EnumName; target.EnumRawValue = src.EnumRawValue; target.EnumStringValue = src.EnumStringValue; diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.RunMappingShouldWork.verified.txt index 10c9512a77..05b49567fb 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.RunMappingShouldWork.verified.txt @@ -144,6 +144,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs index 114291b82b..31a7efd2f4 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs @@ -127,6 +127,7 @@ public partial class TestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(testObject.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)testObject.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(testObject.EnumName); target.EnumRawValue = (byte)testObject.EnumRawValue; target.EnumStringValue = MapToString(testObject.EnumStringValue); @@ -201,67 +202,15 @@ public partial class TestMapper target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.IReadOnlySet, x => x.ToString())); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.HashSet, x => x.ToString())); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(dto.SortedSet, x => x.ToString())); - target.EnumValue = MapToEnumByValueCheckDefined(dto.EnumValue); - target.EnumName = MapToEnumByNameWithFallback(dto.EnumName); + target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)dto.FlagsEnumValue; + target.EnumName = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); return target; } - public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) - { - return global::System.Enum.IsDefined(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum), (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v) ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoByValue is not supported"); - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefinedWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) - { - return global::System.Enum.IsDefined(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum), (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v) ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - }; - } - public partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) { if (source.NullableFlattening != null) @@ -337,6 +286,7 @@ public partial class TestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(source.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)source.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(source.EnumName); target.EnumRawValue = (byte)source.EnumRawValue; target.EnumStringValue = MapToString(source.EnumStringValue); @@ -345,6 +295,17 @@ public partial class TestMapper target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly); } + public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + private partial int PrivateDirectInt(int value) { return value; diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt index ed0505d774..28a9fe91f8 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt @@ -139,6 +139,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunMappingShouldWork.verified.txt index 467e347f56..d840a32975 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunMappingShouldWork.verified.txt @@ -144,6 +144,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs index bb24639cb3..a0b3348157 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -124,6 +124,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(src.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)src.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)src.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(src.EnumName); target.EnumRawValue = (byte)src.EnumRawValue; target.EnumStringValue = MapToString(src.EnumStringValue); @@ -216,6 +217,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(testObject.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)testObject.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(testObject.EnumName); target.EnumRawValue = (byte)testObject.EnumRawValue; target.EnumStringValue = MapToString(testObject.EnumStringValue); @@ -290,25 +292,15 @@ public static partial class StaticTestMapper target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.IReadOnlySet, x => x.ToString())); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.HashSet, x => x.ToString())); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(dto.SortedSet, x => x.ToString())); - target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; - target.EnumName = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; + target.EnumValue = MapToEnumByValueCheckDefined(dto.EnumValue); + target.FlagsEnumValue = MapToFlagsEnumByValueCheckDefined(dto.FlagsEnumValue); + target.EnumName = MapToEnumByNameWithFallback(dto.EnumName); target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); return target; } - public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), - }; - } - public static partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) { if (source.NullableFlattening != null) @@ -384,6 +376,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(source.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)source.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(source.EnumName); target.EnumRawValue = (byte)source.EnumRawValue; target.EnumStringValue = MapToString(source.EnumStringValue); @@ -412,6 +405,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithFallback(x), int x when targetType.IsAssignableFrom(typeof(int)) => DirectInt(x), int x when targetType.IsAssignableFrom(typeof(long)) => ImplicitCastInt(x), uint x when targetType.IsAssignableFrom(typeof(int)) => ExplicitCastInt(x), @@ -432,6 +429,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithFallback(x), int x when targetType.IsAssignableFrom(typeof(int)) => DirectInt(x), int x when targetType.IsAssignableFrom(typeof(long)) => ImplicitCastInt(x), uint x when targetType.IsAssignableFrom(typeof(int)) => ExplicitCastInt(x), @@ -452,6 +453,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => (TTarget)(object)MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => (TTarget)(object)MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByNameWithFallback(x), int x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)DirectInt(x), int x when typeof(TTarget).IsAssignableFrom(typeof(long)) => (TTarget)(object)ImplicitCastInt(x), uint x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)ExplicitCastInt(x), @@ -467,6 +472,64 @@ public static partial class StaticTestMapper }; } + public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v is global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoByValue is not supported"); + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefinedWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v is global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum MapToFlagsEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v == ((global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v & (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V1 | global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V2 | global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V4)) ? (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestFlagsEnumDto is not supported"); + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + }; + } + private static global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto MapToTestObjectNestedDto(global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.RunMappingShouldWork.verified.txt index eb4c239014..f4d606123b 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.RunMappingShouldWork.verified.txt @@ -135,6 +135,7 @@ 3 ], EnumValue: Value10, + FlagsEnumValue: V1, V4, EnumName: Value10, EnumRawValue: Value20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs index 9b45aaae5a..e06b8e1208 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs @@ -90,6 +90,7 @@ public static partial class DeepCloningMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(src.HashSet); target.SortedSet = new global::System.Collections.Generic.SortedSet(src.SortedSet); target.EnumValue = src.EnumValue; + target.FlagsEnumValue = src.FlagsEnumValue; target.EnumName = src.EnumName; target.EnumRawValue = src.EnumRawValue; target.EnumStringValue = src.EnumStringValue; diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.RunMappingShouldWork.verified.txt index 10c9512a77..05b49567fb 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.RunMappingShouldWork.verified.txt @@ -144,6 +144,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs index 5cb7ccc6cd..43aa3d6210 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs @@ -130,6 +130,7 @@ public partial class TestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(testObject.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)testObject.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(testObject.EnumName); target.EnumRawValue = (byte)testObject.EnumRawValue; target.EnumStringValue = MapToString(testObject.EnumStringValue); @@ -207,67 +208,15 @@ public partial class TestMapper target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.IReadOnlySet, x => x.ToString())); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.HashSet, x => x.ToString())); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(dto.SortedSet, x => x.ToString())); - target.EnumValue = MapToEnumByValueCheckDefined(dto.EnumValue); - target.EnumName = MapToEnumByNameWithFallback(dto.EnumName); + target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)dto.FlagsEnumValue; + target.EnumName = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); return target; } - public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, - }; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) - { - return global::System.Enum.IsDefined(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum), (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v) ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoByValue is not supported"); - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefinedWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) - { - return global::System.Enum.IsDefined(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum), (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v) ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - }; - } - public partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) { if (source.NullableFlattening != null) @@ -343,6 +292,7 @@ public partial class TestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(source.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)source.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(source.EnumName); target.EnumRawValue = (byte)source.EnumRawValue; target.EnumStringValue = MapToString(source.EnumStringValue); @@ -351,6 +301,17 @@ public partial class TestMapper target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly); } + public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + private partial int PrivateDirectInt(int value) { return value; diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt index ed0505d774..28a9fe91f8 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt @@ -139,6 +139,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunMappingShouldWork.verified.txt index 467e347f56..d840a32975 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunMappingShouldWork.verified.txt +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunMappingShouldWork.verified.txt @@ -144,6 +144,7 @@ 3 ], EnumValue: DtoValue1, + FlagsEnumValue: V1, V3, EnumName: Value10, EnumRawValue: 20, EnumStringValue: Value30, diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs index 6f17c9de89..bd3fa12a3f 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -127,6 +127,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(src.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)src.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)src.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(src.EnumName); target.EnumRawValue = (byte)src.EnumRawValue; target.EnumStringValue = MapToString(src.EnumStringValue); @@ -222,6 +223,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(testObject.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)testObject.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(testObject.EnumName); target.EnumRawValue = (byte)testObject.EnumRawValue; target.EnumStringValue = MapToString(testObject.EnumStringValue); @@ -299,25 +301,15 @@ public static partial class StaticTestMapper target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.IReadOnlySet, x => x.ToString())); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.HashSet, x => x.ToString())); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(dto.SortedSet, x => x.ToString())); - target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; - target.EnumName = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; + target.EnumValue = MapToEnumByValueCheckDefined(dto.EnumValue); + target.FlagsEnumValue = MapToFlagsEnumByValueCheckDefined(dto.FlagsEnumValue); + target.EnumName = MapToEnumByNameWithFallback(dto.EnumName); target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); return target; } - public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) - { - return v switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), - }; - } - public static partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) { if (source.NullableFlattening != null) @@ -393,6 +385,7 @@ public static partial class StaticTestMapper target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); target.SortedSet = new global::System.Collections.Generic.SortedSet(global::System.Linq.Enumerable.Select(source.SortedSet, x => ParseableInt(x))); target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.FlagsEnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto)source.FlagsEnumValue; target.EnumName = MapToEnumDtoByName(source.EnumName); target.EnumRawValue = (byte)source.EnumRawValue; target.EnumStringValue = MapToString(source.EnumStringValue); @@ -421,6 +414,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithFallback(x), int x when targetType.IsAssignableFrom(typeof(int)) => DirectInt(x), int x when targetType.IsAssignableFrom(typeof(long)) => ImplicitCastInt(x), uint x when targetType.IsAssignableFrom(typeof(int)) => ExplicitCastInt(x), @@ -441,6 +438,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => MapToEnumByNameWithFallback(x), int x when targetType.IsAssignableFrom(typeof(int)) => DirectInt(x), int x when targetType.IsAssignableFrom(typeof(long)) => ImplicitCastInt(x), uint x when targetType.IsAssignableFrom(typeof(int)) => ExplicitCastInt(x), @@ -461,6 +462,10 @@ public static partial class StaticTestMapper return source switch { global::Riok.Mapperly.IntegrationTests.Models.TestEnum x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)) => (TTarget)(object)MapToEnumDtoByName(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByNameWithExplicit(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)) => (TTarget)(object)MapToFlagsEnumByValueCheckDefined(x), + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum)) => (TTarget)(object)MapToEnumByNameWithFallback(x), int x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)DirectInt(x), int x when typeof(TTarget).IsAssignableFrom(typeof(long)) => (TTarget)(object)ImplicitCastInt(x), uint x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)ExplicitCastInt(x), @@ -476,6 +481,64 @@ public static partial class StaticTestMapper }; } + public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueWithExplicit(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value40 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, + }; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v is global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoByValue is not supported"); + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByValueCheckDefinedWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v is global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 or global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 ? (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v : global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10; + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum MapToFlagsEnumByValueCheckDefined(global::Riok.Mapperly.IntegrationTests.Dto.TestFlagsEnumDto v) + { + return (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v == ((global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v & (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V1 | global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V2 | global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum.V4)) ? (global::Riok.Mapperly.IntegrationTests.Models.TestFlagsEnum)v : throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestFlagsEnumDto is not supported"); + } + + public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithFallback(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName v) + { + return v switch + { + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + }; + } + private static global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto MapToTestObjectNestedDto(global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(); diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs index a04e05b8e7..467d9e3d0a 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs @@ -35,15 +35,32 @@ public void EnumByValueCheckDefinedWithFallback() { var source = TestSourceBuilder.MapperWithBodyAndTypes( "[MapEnum(EnumMappingStrategy.ByValueCheckDefined, FallbackValue = E2.Unknown)] partial E2 ToE1(E1 source);", - "enum E1 {A, B, C, D, E}", - "enum E2 {Unknown = -1, A, B, C, D, E}" + "enum E1 {A, B, C}", + "enum E2 {Unknown = -1, A, B, C}" + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveSingleMethodBody( + "return (global::E2)source is global::E2.Unknown or global::E2.A or global::E2.B or global::E2.C ? (global::E2)source : global::E2.Unknown;" + ); + } + + [Fact] + public void FlagsEnumByValueCheckDefinedWithFallback() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "[MapEnum(EnumMappingStrategy.ByValueCheckDefined, FallbackValue = E2.Unknown)] partial E2 ToE1(E1 source);", + "enum E1 {A = 1 << 0, B = 1 << 1, C = 1 << 2}", + "[Flags] enum E2 {Unknown = -1, A = 1 << 0, B = 1 << 1, C = 1 << 2}" ); TestHelper .GenerateMapper(source) .Should() .HaveSingleMethodBody( - "return global::System.Enum.IsDefined(typeof(global::E2), (global::E2)source) ? (global::E2)source : global::E2.Unknown;" + "return (global::E2)source == ((global::E2)source & (global::E2.Unknown | global::E2.A | global::E2.B | global::E2.C)) ? (global::E2)source : global::E2.Unknown;" ); } @@ -60,7 +77,7 @@ public void EnumByValueWithFallbackShouldDiagnostic() .GenerateMapper(source, TestHelperOptions.AllowDiagnostics) .Should() .HaveSingleMethodBody( - "return global::System.Enum.IsDefined(typeof(global::E2), (global::E2)source) ? (global::E2)source : global::E2.Unknown;" + "return (global::E2)source is global::E2.Unknown or global::E2.A or global::E2.B or global::E2.C or global::E2.D or global::E2.E ? (global::E2)source : global::E2.Unknown;" ) .HaveDiagnostic( DiagnosticDescriptors.EnumFallbackValueRequiresByValueCheckDefinedStrategy, diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs index 525fc6b11f..59d416b9da 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs @@ -71,7 +71,24 @@ public void EnumToOtherEnumByValueCheckDefinedShouldCast() .GenerateMapper(source) .Should() .HaveSingleMethodBody( - """return global::System.Enum.IsDefined(typeof(global::E2), (global::E2)source) ? (global::E2)source : throw new System.ArgumentOutOfRangeException(nameof(source), source, "The value of enum E1 is not supported");""" + """return (global::E2)source is global::E2.A or global::E2.B or global::E2.C ? (global::E2)source : throw new System.ArgumentOutOfRangeException(nameof(source), source, "The value of enum E1 is not supported");""" + ); + } + + [Fact] + public void FlagsEnumToOtherEnumByValueCheckDefinedShouldCast() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "[MapEnum(EnumMappingStrategy.ByValueCheckDefined)] partial E2 ToE1(E1 source);", + "enum E1 {B = 1 << 0, A = 1 << 1, C = 1 << 2}", + "[Flags] enum E2 {A = 1 << 0, B = 1 << 1, C = 1 << 2}" + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveSingleMethodBody( + """return (global::E2)source == ((global::E2)source & (global::E2.A | global::E2.B | global::E2.C)) ? (global::E2)source : throw new System.ArgumentOutOfRangeException(nameof(source), source, "The value of enum E1 is not supported");""" ); }