diff --git a/build/package.sh b/build/package.sh index e3e85871e8..65a636d7a1 100755 --- a/build/package.sh +++ b/build/package.sh @@ -5,7 +5,7 @@ set -Eeuo pipefail # pack a nupkg for each roslyn version that is supported by Mapperly # and merge them together into one nupkg -roslyn_versions=('4.0' '4.4') +roslyn_versions=('4.0' '4.4' '4.5') RELEASE_VERSION=${RELEASE_VERSION:-'0.0.1-dev'} RELEASE_NOTES=${RELEASE_NOTES:-''} diff --git a/docs/docs/98-contributing/01-architecture.md b/docs/docs/98-contributing/01-architecture.md index 2f827df260..02f101f5eb 100644 --- a/docs/docs/98-contributing/01-architecture.md +++ b/docs/docs/98-contributing/01-architecture.md @@ -49,6 +49,9 @@ See `build/package.sh` for details. 2. Create a new file `Riok.Mapperly.Roslyn$(Version).props` in `src/Riok.Mapperly` similar to the existing ones and define constants as needed. 3. Update the default `ROSLYN_VERSION` in `src/Riok.Mapperly/Riok.Mapperly.csproj`. -4. Adjust the .NET version matrix of the `integration-test` GitHub Actions job (defined in `.github/workflows/test.yml`) +4. Update the `Microsoft.CodeAnalysis.CSharp` dependency version in `test/Riok.Mapperly.Tests/Riok.Mapperly.Tests.csproj`. +5. Adjust the .NET version matrix of the `integration-test` GitHub Actions job (defined in `.github/workflows/test.yml`) to include a dotnet version which is based on the added Roslyn version. -5. Adjust the .NET version in the `global.json` file as needed. +6. Adjust the .NET version in the `global.json` file as needed. +7. If generated code changes based on the new Roslyn version, + introduce a new `roslynVersionName` in `Riok.Mapperly.IntegrationTests.BaseMapperTest.GetRoslynVersion()` and generate the new snapshots. diff --git a/docs/docs/98-contributing/02-tests.md b/docs/docs/98-contributing/02-tests.md index 95a983f9cf..cdb8ae703b 100644 --- a/docs/docs/98-contributing/02-tests.md +++ b/docs/docs/98-contributing/02-tests.md @@ -31,6 +31,9 @@ These tests run locally by referencing the source generator as an analyzer. In the CI pipeline, the integration tests reference the built NuGet package and are run on several supported target frameworks (including .NET 7.0 but also .NET Framework). +Snapshots of the generated code are stored per Roslyn version. +You may need to manually update older versions. + Debugging integration tests is a lot harder than debugging unit tests. Therefore if an integration test needs to be debugged, it is often easier to implement an unit test for the to be tested behaviour diff --git a/global.json b/global.json index 34db1db96c..7aed64df9d 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.100", - "rollForward": "minor" + "version": "7.0.200", + "rollForward": "latestPatch" } } diff --git a/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs b/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs index 2be9300682..8ca74060a1 100644 --- a/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs +++ b/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs @@ -177,7 +177,7 @@ private static bool BuildParameters( method.ContainingType.ToDisplayString(), method.Name, ctx.Types.IReferenceHandler.ToDisplayString(), - refHandlerParameterSymbol.ToDisplayString()); + refHandlerParameterSymbol.Type.ToDisplayString()); } if (!ctx.MapperConfiguration.UseReferenceHandling) diff --git a/src/Riok.Mapperly/Riok.Mapperly.Roslyn4.5.props b/src/Riok.Mapperly/Riok.Mapperly.Roslyn4.5.props new file mode 100644 index 0000000000..4293f3c10b --- /dev/null +++ b/src/Riok.Mapperly/Riok.Mapperly.Roslyn4.5.props @@ -0,0 +1,11 @@ + + + + $(DefineConstants);ROSLYN4_4_OR_GREATER + + + + + + + diff --git a/src/Riok.Mapperly/Riok.Mapperly.csproj b/src/Riok.Mapperly/Riok.Mapperly.csproj index d13eeab8a3..9dea2d753b 100644 --- a/src/Riok.Mapperly/Riok.Mapperly.csproj +++ b/src/Riok.Mapperly/Riok.Mapperly.csproj @@ -18,7 +18,7 @@ logo.png - 4.4 + 4.5 @@ -44,5 +44,4 @@ - diff --git a/test/Directory.Build.targets b/test/Directory.Build.targets index 0716cea487..ac83c9be34 100644 --- a/test/Directory.Build.targets +++ b/test/Directory.Build.targets @@ -1,5 +1,5 @@ - + diff --git a/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs b/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs index 75d853b779..532f0b0447 100644 --- a/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs +++ b/test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs @@ -2,12 +2,12 @@ using System.IO; using System.Runtime.CompilerServices; using Riok.Mapperly.IntegrationTests.Dto; -#if !NET6_0_OR_GREATER -using Riok.Mapperly.IntegrationTests.Helpers; -#endif using Riok.Mapperly.IntegrationTests.Models; using VerifyTests; using VerifyXunit; +#if !NET6_0_OR_GREATER +using Riok.Mapperly.IntegrationTests.Helpers; +#endif namespace Riok.Mapperly.IntegrationTests { @@ -26,7 +26,7 @@ static BaseMapperTest() VerifierSettings.DontScrubDateTimes(); Verifier.DerivePathInfo((file, _, type, method) - => new PathInfo(Path.Combine(Path.GetDirectoryName(file)!, "_snapshots"), type.Name, method.Name)); + => new PathInfo(Path.Combine(Path.GetDirectoryName(file)!, "_snapshots", "Roslyn_" + GetRoslynVersion()), type.Name, method.Name)); } protected string GetGeneratedMapperFilePath(string name, [CallerFilePath] string filePath = "") @@ -78,5 +78,14 @@ protected TestObject NewTestObj() SourceTargetSameObjectType = new TestObject(8) { IntValue = 99, RequiredValue = 98, } }; } + + private static string GetRoslynVersion() + { +#if NET7_0_OR_GREATER || NET48_OR_GREATER + return "4_5"; +#else + return "4_4_OR_LOWER"; +#endif + } } } diff --git a/test/Riok.Mapperly.IntegrationTests/Riok.Mapperly.IntegrationTests.csproj b/test/Riok.Mapperly.IntegrationTests/Riok.Mapperly.IntegrationTests.csproj index 39c1c1acff..2daea5b09d 100644 --- a/test/Riok.Mapperly.IntegrationTests/Riok.Mapperly.IntegrationTests.csproj +++ b/test/Riok.Mapperly.IntegrationTests/Riok.Mapperly.IntegrationTests.csproj @@ -32,5 +32,4 @@ - diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs similarity index 100% rename from test/Riok.Mapperly.IntegrationTests/_snapshots/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs rename to test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.RunMappingShouldWork.verified.txt similarity index 100% rename from test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.RunMappingShouldWork.verified.txt rename to test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.RunMappingShouldWork.verified.txt diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs similarity index 99% rename from test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs rename to test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs index 88beea0ff4..3384a2ceed 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs @@ -303,4 +303,4 @@ private Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInherita return target; } } -} \ No newline at end of file +} diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt similarity index 100% rename from test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt rename to test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunMappingShouldWork.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunMappingShouldWork.verified.txt similarity index 100% rename from test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunMappingShouldWork.verified.txt rename to test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.RunMappingShouldWork.verified.txt diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs similarity index 99% rename from test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs rename to test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs index a6ce7549a3..648cce5197 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -358,4 +358,4 @@ private static Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToI return target; } } -} \ No newline at end of file +} diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs new file mode 100644 index 0000000000..e643d503f6 --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs @@ -0,0 +1,26 @@ +#nullable enable +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + public static partial class CircularReferenceMapper + { + public static partial Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto ToDto(Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject obj) + { + return MapToCircularReferenceDto(obj, new Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler()); + } + + private static Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto MapToCircularReferenceDto(Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject source, Riok.Mapperly.Abstractions.ReferenceHandling.IReferenceHandler refHandler) + { + if (refHandler.TryGetReference(source, out var existingTargetReference)) + return existingTargetReference; + var target = new Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto(); + refHandler.SetReference(source, target); + if (source.Parent != null) + { + target.Parent = MapToCircularReferenceDto(source.Parent, refHandler); + } + + target.Value = source.Value; + return target; + } + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..9618e0d2cc --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.RunMappingShouldWork.verified.txt @@ -0,0 +1,66 @@ +{ + CtorValue: 7, + CtorValue2: 100, + IntValue: 10, + IntInitOnlyValue: 3, + RequiredValue: 4, + StringValue: fooBar+after-map, + RenamedStringValue2: fooBar2, + FlatteningIdValue: 10, + NullableFlatteningIdValue: 100, + Unflattening: { + IdValue: 20 + }, + NullableUnflattening: { + IdValue: 200 + }, + NestedNullableIntValue: 100, + NestedNullable: { + IntValue: 100 + }, + NestedNullableTargetNotNullable: {}, + StringNullableTargetNotNullable: fooBar3, + RecursiveObject: { + CtorValue: 5, + CtorValue2: 100, + RequiredValue: 4, + StringValue: +after-map, + RenamedStringValue2: , + Unflattening: {}, + NestedNullableTargetNotNullable: {}, + StringNullableTargetNotNullable: , + EnumValue: DtoValue1, + EnumName: Value30, + EnumStringValue: 0, + EnumReverseStringValue: DtoValue3 + }, + SourceTargetSameObjectType: { + CtorValue: 8, + CtorValue2: 100, + IntValue: 99, + RequiredValue: 98, + StringValue: , + RenamedStringValue: , + Flattening: {}, + EnumReverseStringValue: + }, + NullableReadOnlyObjectCollection: [ + { + IntValue: 10 + }, + { + IntValue: 20 + } + ], + EnumValue: DtoValue1, + EnumName: Value10, + EnumRawValue: 20, + EnumStringValue: Value30, + EnumReverseStringValue: DtoValue3, + SubObject: { + SubIntValue: 2, + BaseIntValue: 1 + }, + DateTimeValueTargetDateOnly: 2020-01-03, + DateTimeValueTargetTimeOnly: 3:10 PM +} \ No newline at end of file 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 new file mode 100644 index 0000000000..94490f400e --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs @@ -0,0 +1,312 @@ +#nullable enable +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + public partial class TestMapper + { + public partial int DirectInt(int value) + { + return value; + } + + public partial long ImplicitCastInt(int value) + { + return (long)value; + } + + public partial int ExplicitCastInt(uint value) + { + return (int)value; + } + + public partial int? CastIntNullable(int value) + { + return (int?)value; + } + + public partial System.Guid ParseableGuid(string id) + { + return System.Guid.Parse(id); + } + + public partial int ParseableInt(string value) + { + return int.Parse(value); + } + + public partial System.DateTime DirectDateTime(System.DateTime dateTime) + { + return dateTime; + } + + public partial System.Collections.Generic.IEnumerable MapAllDtos(System.Collections.Generic.IEnumerable objects) + { + return System.Linq.Enumerable.Select(objects, x => MapToDto(x)); + } + + private partial Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(Riok.Mapperly.IntegrationTests.Models.TestObject testObject) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2)) + { + IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), + RequiredValue = DirectInt(testObject.RequiredValue) + }; + if (testObject.NullableFlattening != null) + { + target.NullableFlatteningIdValue = CastIntNullable(testObject.NullableFlattening.IdValue); + } + + if (testObject.NullableUnflatteningIdValue != null) + { + target.NullableUnflattening ??= new(); + target.NullableUnflattening.IdValue = DirectInt(testObject.NullableUnflatteningIdValue.Value); + } + + if (testObject.NestedNullable != null) + { + target.NestedNullableIntValue = DirectInt(testObject.NestedNullable.IntValue); + target.NestedNullable = MapToTestObjectNestedDto(testObject.NestedNullable); + } + + if (testObject.NestedNullableTargetNotNullable != null) + { + target.NestedNullableTargetNotNullable = MapToTestObjectNestedDto(testObject.NestedNullableTargetNotNullable); + } + + if (testObject.StringNullableTargetNotNullable != null) + { + target.StringNullableTargetNotNullable = testObject.StringNullableTargetNotNullable; + } + + if (testObject.RecursiveObject != null) + { + target.RecursiveObject = MapToDto(testObject.RecursiveObject); + } + + if (testObject.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(testObject.NullableReadOnlyObjectCollection, x => MapToTestObjectNestedDto(x))); + } + + if (testObject.SubObject != null) + { + target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); + } + + target.IntValue = DirectInt(testObject.IntValue); + target.StringValue = testObject.StringValue; + target.RenamedStringValue2 = testObject.RenamedStringValue; + target.FlatteningIdValue = DirectInt(testObject.Flattening.IdValue); + target.Unflattening.IdValue = DirectInt(testObject.UnflatteningIdValue); + target.SourceTargetSameObjectType = testObject.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.EnumName = MapToEnumDtoByName(testObject.EnumName); + target.EnumRawValue = (byte)testObject.EnumRawValue; + target.EnumStringValue = MapToString(testObject.EnumStringValue); + target.EnumReverseStringValue = MapToTestEnumDtoByValue(testObject.EnumReverseStringValue); + target.DateTimeValueTargetDateOnly = System.DateOnly.FromDateTime(testObject.DateTimeValueTargetDateOnly); + target.DateTimeValueTargetTimeOnly = System.TimeOnly.FromDateTime(testObject.DateTimeValueTargetTimeOnly); + return target; + } + + public partial Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto) + { + var target = new Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2)) + { + IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), + RequiredValue = DirectInt(dto.RequiredValue) + }; + if (dto.NullableUnflattening != null) + { + target.NullableUnflatteningIdValue = CastIntNullable(dto.NullableUnflattening.IdValue); + } + + if (dto.NestedNullable != null) + { + target.NestedNullable = MapToTestObjectNested(dto.NestedNullable); + } + + if (dto.RecursiveObject != null) + { + target.RecursiveObject = MapFromDto(dto.RecursiveObject); + } + + if (dto.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(dto.NullableReadOnlyObjectCollection); + } + + if (dto.SubObject != null) + { + target.SubObject = MapToInheritanceSubObject(dto.SubObject); + } + + target.IntValue = DirectInt(dto.IntValue); + target.StringValue = dto.StringValue; + target.UnflatteningIdValue = DirectInt(dto.Unflattening.IdValue); + target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); + target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; + target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; + target.EnumName = (Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; + target.EnumRawValue = (Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; + target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); + target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); + return target; + } + + public partial Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), + }; + } + + public partial void UpdateDto(Riok.Mapperly.IntegrationTests.Models.TestObject source, Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) + { + if (source.NullableFlattening != null) + { + target.NullableFlatteningIdValue = CastIntNullable(source.NullableFlattening.IdValue); + } + + if (source.NestedNullable != null) + { + target.NestedNullableIntValue = DirectInt(source.NestedNullable.IntValue); + target.NestedNullable = MapToTestObjectNestedDto(source.NestedNullable); + } + + if (source.NestedNullableTargetNotNullable != null) + { + target.NestedNullableTargetNotNullable = MapToTestObjectNestedDto(source.NestedNullableTargetNotNullable); + } + + if (source.StringNullableTargetNotNullable != null) + { + target.StringNullableTargetNotNullable = source.StringNullableTargetNotNullable; + } + + if (source.RecursiveObject != null) + { + target.RecursiveObject = MapToDto(source.RecursiveObject); + } + + if (source.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(source.NullableReadOnlyObjectCollection, x => MapToTestObjectNestedDto(x))); + } + + if (source.SubObject != null) + { + target.SubObject = MapToInheritanceSubObjectDto(source.SubObject); + } + + target.CtorValue = DirectInt(source.CtorValue); + target.CtorValue2 = DirectInt(source.CtorValue2); + target.IntValue = DirectInt(source.IntValue); + target.StringValue = source.StringValue; + target.FlatteningIdValue = DirectInt(source.Flattening.IdValue); + target.SourceTargetSameObjectType = source.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.EnumName = MapToEnumDtoByName(source.EnumName); + target.EnumRawValue = (byte)source.EnumRawValue; + target.EnumStringValue = MapToString(source.EnumStringValue); + target.EnumReverseStringValue = MapToTestEnumDtoByValue(source.EnumReverseStringValue); + target.IgnoredStringValue = source.IgnoredStringValue; + target.DateTimeValueTargetDateOnly = System.DateOnly.FromDateTime(source.DateTimeValueTargetDateOnly); + target.DateTimeValueTargetTimeOnly = System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly); + } + + private partial int PrivateDirectInt(int value) + { + return value; + } + + private Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto MapToTestObjectNestedDto(Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(); + target.IntValue = DirectInt(source.IntValue); + return target; + } + + private string MapToString(Riok.Mapperly.IntegrationTests.Models.TestEnum source) + { + return source switch + { + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10), + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20), + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30), + _ => source.ToString(), + }; + } + + private Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue MapToTestEnumDtoByValue(string source) + { + return source switch + { + nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1) => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1, + nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2) => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2, + nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3) => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3, + _ => (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)System.Enum.Parse(typeof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue), source, false), + }; + } + + private Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto MapToInheritanceSubObjectDto(Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject source) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto(); + target.SubIntValue = DirectInt(source.SubIntValue); + target.BaseIntValue = DirectInt(source.BaseIntValue); + return target; + } + + private Riok.Mapperly.IntegrationTests.Models.TestObjectNested MapToTestObjectNested(Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto source) + { + var target = new Riok.Mapperly.IntegrationTests.Models.TestObjectNested(); + target.IntValue = DirectInt(source.IntValue); + return target; + } + + private System.Collections.Generic.IReadOnlyCollection MapToIReadOnlyCollection(Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source) + { + var target = new Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = MapToTestObjectNested(source[i]); + } + + return target; + } + + private Riok.Mapperly.IntegrationTests.Models.TestEnum MapToTestEnum(string source) + { + return source switch + { + nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10) => Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20) => Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30) => Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => (Riok.Mapperly.IntegrationTests.Models.TestEnum)System.Enum.Parse(typeof(Riok.Mapperly.IntegrationTests.Models.TestEnum), source, false), + }; + } + + private string MapToString1(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue source) + { + return source switch + { + Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1 => nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1), + Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2 => nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2), + Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3 => nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3), + _ => source.ToString(), + }; + } + + private Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInheritanceSubObject(Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto source) + { + var target = new Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject(); + target.SubIntValue = DirectInt(source.SubIntValue); + target.BaseIntValue = DirectInt(source.BaseIntValue); + return target; + } + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..3d4586103b --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunExtensionMappingShouldWork.verified.txt @@ -0,0 +1,62 @@ +{ + CtorValue: 7, + CtorValue2: 100, + IntValue: 10, + IntInitOnlyValue: 3, + RequiredValue: 4, + StringValue: fooBar, + RenamedStringValue2: , + FlatteningIdValue: 10, + NullableFlatteningIdValue: 100, + Unflattening: {}, + NestedNullableIntValue: 100, + NestedNullable: { + IntValue: 100 + }, + NestedNullableTargetNotNullable: {}, + StringNullableTargetNotNullable: fooBar3, + RecursiveObject: { + CtorValue: 5, + CtorValue2: 100, + RequiredValue: 4, + StringValue: , + RenamedStringValue2: , + Unflattening: {}, + NestedNullableTargetNotNullable: {}, + StringNullableTargetNotNullable: , + EnumValue: DtoValue1, + EnumName: Value30, + EnumStringValue: 0, + EnumReverseStringValue: DtoValue3 + }, + SourceTargetSameObjectType: { + CtorValue: 8, + CtorValue2: 100, + IntValue: 99, + RequiredValue: 98, + StringValue: , + RenamedStringValue: , + Flattening: {}, + EnumReverseStringValue: + }, + NullableReadOnlyObjectCollection: [ + { + IntValue: 10 + }, + { + IntValue: 20 + } + ], + EnumValue: DtoValue1, + EnumName: Value10, + EnumRawValue: 20, + EnumStringValue: Value30, + EnumReverseStringValue: DtoValue3, + SubObject: { + SubIntValue: 2, + BaseIntValue: 1 + }, + IgnoredStringValue: ignored, + DateTimeValueTargetDateOnly: 2020-01-03, + DateTimeValueTargetTimeOnly: 3:10 PM +} \ No newline at end of file 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 new file mode 100644 index 0000000000..e31744e1f7 --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.RunMappingShouldWork.verified.txt @@ -0,0 +1,66 @@ +{ + CtorValue: 7, + CtorValue2: 100, + IntValue: 10, + IntInitOnlyValue: 3, + RequiredValue: 4, + StringValue: fooBar+after-map, + RenamedStringValue2: fooBar2, + FlatteningIdValue: 10, + NullableFlatteningIdValue: 100, + Unflattening: { + IdValue: 20 + }, + NullableUnflattening: { + IdValue: 200 + }, + NestedNullableIntValue: 100, + NestedNullable: { + IntValue: 100 + }, + NestedNullableTargetNotNullable: {}, + StringNullableTargetNotNullable: fooBar3, + RecursiveObject: { + CtorValue: 5, + CtorValue2: 100, + RequiredValue: 4, + StringValue: , + RenamedStringValue2: , + Unflattening: {}, + NestedNullableTargetNotNullable: {}, + StringNullableTargetNotNullable: , + EnumValue: DtoValue1, + EnumName: Value30, + EnumStringValue: 0, + EnumReverseStringValue: DtoValue3 + }, + SourceTargetSameObjectType: { + CtorValue: 8, + CtorValue2: 100, + IntValue: 99, + RequiredValue: 98, + StringValue: , + RenamedStringValue: , + Flattening: {}, + EnumReverseStringValue: + }, + NullableReadOnlyObjectCollection: [ + { + IntValue: 10 + }, + { + IntValue: 20 + } + ], + EnumValue: DtoValue1, + EnumName: Value10, + EnumRawValue: 20, + EnumStringValue: Value30, + EnumReverseStringValue: DtoValue3, + SubObject: { + SubIntValue: 2, + BaseIntValue: 1 + }, + DateTimeValueTargetDateOnly: 2020-01-03, + DateTimeValueTargetTimeOnly: 3:10 PM +} \ No newline at end of file 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 new file mode 100644 index 0000000000..ed18af272b --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -0,0 +1,370 @@ +#nullable enable +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + public static partial class StaticTestMapper + { + public static partial int DirectInt(int value) + { + return value; + } + + public static partial long ImplicitCastInt(int value) + { + return (long)value; + } + + public static partial int ExplicitCastInt(uint value) + { + return (int)value; + } + + public static partial int? CastIntNullable(int value) + { + return (int?)value; + } + + public static partial System.Guid ParseableGuid(string id) + { + return System.Guid.Parse(id); + } + + public static partial int ParseableInt(string value) + { + return int.Parse(value); + } + + public static partial System.DateTime DirectDateTime(System.DateTime dateTime) + { + return dateTime; + } + + public static partial System.Collections.Generic.IEnumerable MapAllDtos(System.Collections.Generic.IEnumerable objects) + { + return System.Linq.Enumerable.Select(objects, x => MapToDtoExt(x)); + } + + public static partial Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoExt(this Riok.Mapperly.IntegrationTests.Models.TestObject src) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(src.CtorValue), ctorValue2: DirectInt(src.CtorValue2)) + { + IntInitOnlyValue = DirectInt(src.IntInitOnlyValue), + RequiredValue = DirectInt(src.RequiredValue) + }; + if (src.NullableFlattening != null) + { + target.NullableFlatteningIdValue = CastIntNullable(src.NullableFlattening.IdValue); + } + + if (src.NestedNullable != null) + { + target.NestedNullableIntValue = DirectInt(src.NestedNullable.IntValue); + target.NestedNullable = MapToTestObjectNestedDto(src.NestedNullable); + } + + if (src.NestedNullableTargetNotNullable != null) + { + target.NestedNullableTargetNotNullable = MapToTestObjectNestedDto(src.NestedNullableTargetNotNullable); + } + + if (src.StringNullableTargetNotNullable != null) + { + target.StringNullableTargetNotNullable = src.StringNullableTargetNotNullable; + } + + if (src.RecursiveObject != null) + { + target.RecursiveObject = MapToDtoExt(src.RecursiveObject); + } + + if (src.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(src.NullableReadOnlyObjectCollection, x => MapToTestObjectNestedDto(x))); + } + + if (src.SubObject != null) + { + target.SubObject = MapToInheritanceSubObjectDto(src.SubObject); + } + + target.IntValue = DirectInt(src.IntValue); + target.StringValue = src.StringValue; + target.FlatteningIdValue = DirectInt(src.Flattening.IdValue); + target.SourceTargetSameObjectType = src.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)src.EnumValue; + target.EnumName = MapToEnumDtoByName(src.EnumName); + target.EnumRawValue = (byte)src.EnumRawValue; + target.EnumStringValue = MapToString(src.EnumStringValue); + target.EnumReverseStringValue = MapToTestEnumDtoByValue(src.EnumReverseStringValue); + target.IgnoredStringValue = src.IgnoredStringValue; + target.DateTimeValueTargetDateOnly = System.DateOnly.FromDateTime(src.DateTimeValueTargetDateOnly); + target.DateTimeValueTargetTimeOnly = System.TimeOnly.FromDateTime(src.DateTimeValueTargetTimeOnly); + return target; + } + + private static partial Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(Riok.Mapperly.IntegrationTests.Models.TestObject testObject) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2)) + { + IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), + RequiredValue = DirectInt(testObject.RequiredValue) + }; + if (testObject.NullableFlattening != null) + { + target.NullableFlatteningIdValue = CastIntNullable(testObject.NullableFlattening.IdValue); + } + + if (testObject.NullableUnflatteningIdValue != null) + { + target.NullableUnflattening ??= new(); + target.NullableUnflattening.IdValue = DirectInt(testObject.NullableUnflatteningIdValue.Value); + } + + if (testObject.NestedNullable != null) + { + target.NestedNullableIntValue = DirectInt(testObject.NestedNullable.IntValue); + target.NestedNullable = MapToTestObjectNestedDto(testObject.NestedNullable); + } + + if (testObject.NestedNullableTargetNotNullable != null) + { + target.NestedNullableTargetNotNullable = MapToTestObjectNestedDto(testObject.NestedNullableTargetNotNullable); + } + + if (testObject.StringNullableTargetNotNullable != null) + { + target.StringNullableTargetNotNullable = testObject.StringNullableTargetNotNullable; + } + + if (testObject.RecursiveObject != null) + { + target.RecursiveObject = MapToDtoExt(testObject.RecursiveObject); + } + + if (testObject.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(testObject.NullableReadOnlyObjectCollection, x => MapToTestObjectNestedDto(x))); + } + + if (testObject.SubObject != null) + { + target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); + } + + target.IntValue = DirectInt(testObject.IntValue); + target.StringValue = testObject.StringValue; + target.RenamedStringValue2 = testObject.RenamedStringValue; + target.FlatteningIdValue = DirectInt(testObject.Flattening.IdValue); + target.Unflattening.IdValue = DirectInt(testObject.UnflatteningIdValue); + target.SourceTargetSameObjectType = testObject.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; + target.EnumName = MapToEnumDtoByName(testObject.EnumName); + target.EnumRawValue = (byte)testObject.EnumRawValue; + target.EnumStringValue = MapToString(testObject.EnumStringValue); + target.EnumReverseStringValue = MapToTestEnumDtoByValue(testObject.EnumReverseStringValue); + target.DateTimeValueTargetDateOnly = System.DateOnly.FromDateTime(testObject.DateTimeValueTargetDateOnly); + target.DateTimeValueTargetTimeOnly = System.TimeOnly.FromDateTime(testObject.DateTimeValueTargetTimeOnly); + return target; + } + + public static partial Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto) + { + var target = new Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2)) + { + IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), + RequiredValue = DirectInt(dto.RequiredValue) + }; + if (dto.NullableUnflattening != null) + { + target.NullableUnflatteningIdValue = CastIntNullable(dto.NullableUnflattening.IdValue); + } + + if (dto.NestedNullable != null) + { + target.NestedNullable = MapToTestObjectNested(dto.NestedNullable); + } + + if (dto.RecursiveObject != null) + { + target.RecursiveObject = MapFromDto(dto.RecursiveObject); + } + + if (dto.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(dto.NullableReadOnlyObjectCollection); + } + + if (dto.SubObject != null) + { + target.SubObject = MapToInheritanceSubObject(dto.SubObject); + } + + target.IntValue = DirectInt(dto.IntValue); + target.StringValue = dto.StringValue; + target.UnflatteningIdValue = DirectInt(dto.Unflattening.IdValue); + target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); + target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; + target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumValue; + target.EnumName = (Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumName; + target.EnumRawValue = (Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue; + target.EnumStringValue = MapToTestEnum(dto.EnumStringValue); + target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue); + return target; + } + + public static partial Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(Riok.Mapperly.IntegrationTests.Models.TestEnum v) + { + return v switch + { + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => 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(Riok.Mapperly.IntegrationTests.Models.TestObject source, Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target) + { + if (source.NullableFlattening != null) + { + target.NullableFlatteningIdValue = CastIntNullable(source.NullableFlattening.IdValue); + } + + if (source.NestedNullable != null) + { + target.NestedNullableIntValue = DirectInt(source.NestedNullable.IntValue); + target.NestedNullable = MapToTestObjectNestedDto(source.NestedNullable); + } + + if (source.NestedNullableTargetNotNullable != null) + { + target.NestedNullableTargetNotNullable = MapToTestObjectNestedDto(source.NestedNullableTargetNotNullable); + } + + if (source.StringNullableTargetNotNullable != null) + { + target.StringNullableTargetNotNullable = source.StringNullableTargetNotNullable; + } + + if (source.RecursiveObject != null) + { + target.RecursiveObject = MapToDtoExt(source.RecursiveObject); + } + + if (source.NullableReadOnlyObjectCollection != null) + { + target.NullableReadOnlyObjectCollection = System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(source.NullableReadOnlyObjectCollection, x => MapToTestObjectNestedDto(x))); + } + + if (source.SubObject != null) + { + target.SubObject = MapToInheritanceSubObjectDto(source.SubObject); + } + + target.CtorValue = DirectInt(source.CtorValue); + target.CtorValue2 = DirectInt(source.CtorValue2); + target.IntValue = DirectInt(source.IntValue); + target.StringValue = source.StringValue; + target.FlatteningIdValue = DirectInt(source.Flattening.IdValue); + target.SourceTargetSameObjectType = source.SourceTargetSameObjectType; + target.EnumValue = (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)source.EnumValue; + target.EnumName = MapToEnumDtoByName(source.EnumName); + target.EnumRawValue = (byte)source.EnumRawValue; + target.EnumStringValue = MapToString(source.EnumStringValue); + target.EnumReverseStringValue = MapToTestEnumDtoByValue(source.EnumReverseStringValue); + target.IgnoredStringValue = source.IgnoredStringValue; + target.DateTimeValueTargetDateOnly = System.DateOnly.FromDateTime(source.DateTimeValueTargetDateOnly); + target.DateTimeValueTargetTimeOnly = System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly); + } + + private static partial int PrivateDirectInt(int value) + { + return value; + } + + private static Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto MapToTestObjectNestedDto(Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(); + target.IntValue = DirectInt(source.IntValue); + return target; + } + + private static string MapToString(Riok.Mapperly.IntegrationTests.Models.TestEnum source) + { + return source switch + { + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10), + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20), + Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30), + _ => source.ToString(), + }; + } + + private static Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue MapToTestEnumDtoByValue(string source) + { + return source switch + { + nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1) => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1, + nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2) => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2, + nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3) => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3, + _ => (Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)System.Enum.Parse(typeof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue), source, false), + }; + } + + private static Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto MapToInheritanceSubObjectDto(Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject source) + { + var target = new Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto(); + target.SubIntValue = DirectInt(source.SubIntValue); + target.BaseIntValue = DirectInt(source.BaseIntValue); + return target; + } + + private static Riok.Mapperly.IntegrationTests.Models.TestObjectNested MapToTestObjectNested(Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto source) + { + var target = new Riok.Mapperly.IntegrationTests.Models.TestObjectNested(); + target.IntValue = DirectInt(source.IntValue); + return target; + } + + private static System.Collections.Generic.IReadOnlyCollection MapToIReadOnlyCollection(Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source) + { + var target = new Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = MapToTestObjectNested(source[i]); + } + + return target; + } + + private static Riok.Mapperly.IntegrationTests.Models.TestEnum MapToTestEnum(string source) + { + return source switch + { + nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10) => Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, + nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20) => Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, + nameof(Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30) => Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, + _ => (Riok.Mapperly.IntegrationTests.Models.TestEnum)System.Enum.Parse(typeof(Riok.Mapperly.IntegrationTests.Models.TestEnum), source, false), + }; + } + + private static string MapToString1(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue source) + { + return source switch + { + Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1 => nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1), + Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2 => nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2), + Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3 => nameof(Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3), + _ => source.ToString(), + }; + } + + private static Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInheritanceSubObject(Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto source) + { + var target = new Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject(); + target.SubIntValue = DirectInt(source.SubIntValue); + target.BaseIntValue = DirectInt(source.BaseIntValue); + return target; + } + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs index 8dc1bde005..351bccfd2d 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs @@ -17,7 +17,9 @@ public void InitOnlyProperty() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {StringValue = source.StringValue}; + { + StringValue = source.StringValue + }; target.IntValue = source.IntValue; return target;"); } @@ -34,7 +36,10 @@ public void MultipleInitOnlyProperties() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {StringValue = source.StringValue, IntValue = source.IntValue}; + { + StringValue = source.StringValue, + IntValue = source.IntValue + }; return target;"); } @@ -50,7 +55,9 @@ public void InitOnlyPropertyWithNullableSource() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {Value = source.Value ?? throw new System.ArgumentNullException(nameof(source.Value))}; + { + Value = source.Value ?? throw new System.ArgumentNullException(nameof(source.Value)) + }; return target;"); } @@ -67,7 +74,9 @@ public void InitOnlyPropertyWithNullableSourceNoThrow() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {Value = source.Value ?? """"}; + { + Value = source.Value ?? """" + }; return target;"); } @@ -82,7 +91,9 @@ public void InitOnlyPropertyWithConfiguration() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {StringValue = source.StringValue2}; + { + StringValue = source.StringValue2 + }; return target;"); } @@ -98,7 +109,9 @@ public void InitOnlyReferenceLoop() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {Parent = source.Parent == null ? default : Map(source.Parent)}; + { + Parent = source.Parent == null ? default : Map(source.Parent) + }; return target;"); } @@ -115,7 +128,9 @@ public void InitOnlyPropertyWithAutoFlattenedNullablePath() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {NestedValue = source.Nested?.Value ?? throw new System.ArgumentNullException(nameof(source.Nested?.Value))}; + { + NestedValue = source.Nested?.Value ?? throw new System.ArgumentNullException(nameof(source.Nested?.Value)) + }; return target;"); } @@ -132,7 +147,9 @@ public void InitOnlyPropertyWithAutoFlattened() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {NestedValue = source.Nested.Value}; + { + NestedValue = source.Nested.Value + }; return target;"); } @@ -211,7 +228,9 @@ public void RequiredProperty() TestHelper.GenerateMapper(source) .Should() .HaveSingleMethodBody(@"var target = new B() - {StringValue = source.StringValue}; + { + StringValue = source.StringValue + }; target.IntValue = source.IntValue; return target;"); } diff --git a/test/Riok.Mapperly.Tests/Riok.Mapperly.Tests.csproj b/test/Riok.Mapperly.Tests/Riok.Mapperly.Tests.csproj index b5e8319bad..b2a7b692d7 100644 --- a/test/Riok.Mapperly.Tests/Riok.Mapperly.Tests.csproj +++ b/test/Riok.Mapperly.Tests/Riok.Mapperly.Tests.csproj @@ -1,7 +1,7 @@ - + diff --git a/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyInitPropertyTest.InitOnlyPropertyWithMultipleConfigurationsShouldDiagnostic#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyInitPropertyTest.InitOnlyPropertyWithMultipleConfigurationsShouldDiagnostic#Mapper.g.verified.cs index 299d99ddd1..80c536ecd3 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyInitPropertyTest.InitOnlyPropertyWithMultipleConfigurationsShouldDiagnostic#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyInitPropertyTest.InitOnlyPropertyWithMultipleConfigurationsShouldDiagnostic#Mapper.g.verified.cs @@ -5,7 +5,9 @@ public partial class Mapper private partial B Map(A source) { var target = new B() - {StringValue = source.StringValue2}; + { + StringValue = source.StringValue2 + }; return target; } -} \ No newline at end of file +}