diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeMapping.cs index 4a052636b9..4bd671e236 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeMapping.cs @@ -11,7 +11,6 @@ namespace Riok.Mapperly.Descriptors.Mappings; /// public class DerivedTypeMapping : TypeMapping { - private const string TypeArmVariableName = "x"; private const string GetTypeMethodName = "GetType"; private readonly IReadOnlyDictionary _typeMappings; @@ -35,20 +34,15 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) ); // source switch { A x => MapToA(x), B x => MapToB(x) } - var typeArmVariableName = ctx.NameBuilder.New(TypeArmVariableName); - var arms = _typeMappings.Select(x => BuildSwitchArm(ctx, typeArmVariableName, x.Key, x.Value)).Append(fallbackArm); + var (typeArmContext, typeArmVariableName) = ctx.WithNewSource(); + var arms = _typeMappings.Select(x => BuildSwitchArm(typeArmVariableName, x.Key, x.Value.Build(typeArmContext))).Append(fallbackArm); return SwitchExpression(ctx.Source).WithArms(CommaSeparatedList(arms, true)); } - private SwitchExpressionArmSyntax BuildSwitchArm( - TypeMappingBuildContext ctx, - string typeArmVariableName, - ITypeSymbol type, - ITypeMapping typeMapping - ) + private SwitchExpressionArmSyntax BuildSwitchArm(string typeArmVariableName, ITypeSymbol type, ExpressionSyntax mapping) { // A x => MapToA(x), var declaration = DeclarationPattern(FullyQualifiedIdentifier(type), SingleVariableDesignation(Identifier(typeArmVariableName))); - return SwitchExpressionArm(declaration, typeMapping.Build(ctx.WithSource(typeArmVariableName))); + return SwitchExpressionArm(declaration, mapping); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs index c061103f57..5f53867253 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs @@ -34,8 +34,8 @@ public class ForEachAddEnumerableExistingTargetMapping : ExistingTargetMapping public override IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax target) { - var loopItemVariableName = ctx.NameBuilder.New(LoopItemVariableName); - var convertedSourceItemExpression = _elementMapping.Build(ctx.WithSource(loopItemVariableName)); + var (loopItemCtx, loopItemVariableName) = ctx.WithNewSource(LoopItemVariableName); + var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx); var addMethod = MemberAccess(target, _insertMethodName); if (_ensureCapacityBuilder != null) diff --git a/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs index 6246a12c28..a5c434b25e 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs @@ -10,8 +10,6 @@ namespace Riok.Mapperly.Descriptors.Mappings; /// public class LinqConstructorMapping : TypeMapping { - private const string LambdaParamName = "x"; - private readonly ITypeMapping _elementMapping; private readonly IMethodSymbol? _selectMethod; @@ -24,16 +22,14 @@ public LinqConstructorMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IT public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { - var scopedNameBuilder = ctx.NameBuilder.NewScope(); - var lambdaParamName = scopedNameBuilder.New(LambdaParamName); - ExpressionSyntax mappedSource; // Select / Map if needed if (_selectMethod != null) { - var sourceMapExpression = _elementMapping.Build(ctx.WithSource(lambdaParamName)); - var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))).WithExpressionBody(sourceMapExpression); + var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource(); + var sourceMapExpression = _elementMapping.Build(lambdaCtx); + var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaSourceName))).WithExpressionBody(sourceMapExpression); mappedSource = StaticInvocation(_selectMethod, ctx.Source, convertLambda); } else diff --git a/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs index 24d0efed41..240c077d6b 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs @@ -10,8 +10,6 @@ namespace Riok.Mapperly.Descriptors.Mappings; /// public class LinqDicitonaryMapping : TypeMapping { - private const string LambdaParamName = "x"; - private const string KeyPropertyName = nameof(KeyValuePair.Key); private const string ValuePropertyName = nameof(KeyValuePair.Value); @@ -35,21 +33,20 @@ ITypeMapping valueMapping public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { - var scopedNameBuilder = ctx.NameBuilder.NewScope(); - var lambdaParamName = scopedNameBuilder.New(LambdaParamName); - // if key and value types do not change then use a simple call // ie: source.ToImmutableDictionary(); if (_keyMapping.IsSynthetic && _valueMapping.IsSynthetic) return StaticInvocation(_collectMethod, ctx.Source); // create expressions mapping the key and value and then create the final expression - // ie: source.ToImmutableDictionary(x => x.Key, x=> (int)x.Value); - var keyMapExpression = _keyMapping.Build(ctx.WithSource(MemberAccess(lambdaParamName, KeyPropertyName))); - var keyExpression = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))).WithExpressionBody(keyMapExpression); - - var valueMapExpression = _valueMapping.Build(ctx.WithSource(MemberAccess(lambdaParamName, ValuePropertyName))); - var valueExpression = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))).WithExpressionBody(valueMapExpression); + // ie: source.ToImmutableDictionary(x => x.Key, x => (int)x.Value); + var (keyLambdaCtx, keyLambdaParamName) = ctx.WithNewScopedSource(src => MemberAccess(src, KeyPropertyName)); + var keyMapExpression = _keyMapping.Build(keyLambdaCtx); + var keyExpression = SimpleLambdaExpression(Parameter(Identifier(keyLambdaParamName))).WithExpressionBody(keyMapExpression); + + var (valueLambdaCtx, valueLambdaParamName) = ctx.WithNewScopedSource(src => MemberAccess(src, ValuePropertyName)); + var valueMapExpression = _valueMapping.Build(valueLambdaCtx); + var valueExpression = SimpleLambdaExpression(Parameter(Identifier(valueLambdaParamName))).WithExpressionBody(valueMapExpression); return StaticInvocation(_collectMethod, ctx.Source, keyExpression, valueExpression); } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs index 6059303d76..4e93d99f3f 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs @@ -10,8 +10,6 @@ namespace Riok.Mapperly.Descriptors.Mappings; /// public class LinqEnumerableMapping : TypeMapping { - private const string LambdaParamName = "x"; - private readonly ITypeMapping _elementMapping; private readonly IMethodSymbol? _selectMethod; private readonly IMethodSymbol? _collectMethod; @@ -32,16 +30,14 @@ public class LinqEnumerableMapping : TypeMapping public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { - var scopedNameBuilder = ctx.NameBuilder.NewScope(); - var lambdaParamName = scopedNameBuilder.New(LambdaParamName); - ExpressionSyntax mappedSource; // Select / Map if needed if (_selectMethod != null) { - var sourceMapExpression = _elementMapping.Build(ctx.WithSource(lambdaParamName)); - var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))).WithExpressionBody(sourceMapExpression); + var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource(); + var sourceMapExpression = _elementMapping.Build(lambdaCtx); + var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaSourceName))).WithExpressionBody(sourceMapExpression); mappedSource = StaticInvocation(_selectMethod, ctx.Source, convertLambda); } else diff --git a/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs index 89aa28280f..eec9f7870b 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs @@ -11,8 +11,6 @@ namespace Riok.Mapperly.Descriptors.Mappings; /// public class QueryableProjectionMapping : MethodMapping { - private const string SelectLambdaParameterName = "x"; - private const string QueryableReceiverName = "System.Linq.Queryable"; private const string SelectMethodName = nameof(Queryable.Select); @@ -30,11 +28,10 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // #nullable disable // return System.Linq.Enumerable.Select(source, x => ...); // #nullable enable - var scopedNameBuilder = ctx.NameBuilder.NewScope(); - var lambdaParamName = scopedNameBuilder.New(SelectLambdaParameterName); + var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource(); - var delegateMapping = _delegateMapping.Build(ctx.WithSource(IdentifierName(lambdaParamName))); - var projectionLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))).WithExpressionBody(delegateMapping); + var delegateMapping = _delegateMapping.Build(lambdaCtx); + var projectionLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaSourceName))).WithExpressionBody(delegateMapping); var select = StaticInvocation(QueryableReceiverName, SelectMethodName, ctx.Source, projectionLambda); return new[] { diff --git a/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs b/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs index f492fcddd6..464d2afc9d 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs @@ -6,6 +6,8 @@ namespace Riok.Mapperly.Descriptors.Mappings; public class TypeMappingBuildContext { + private const string DefaultSourceName = "x"; + public TypeMappingBuildContext(string source, string? referenceHandler, UniqueNameBuilder nameBuilder) : this(IdentifierName(source), referenceHandler == null ? null : IdentifierName(referenceHandler), nameBuilder) { } @@ -22,9 +24,36 @@ private TypeMappingBuildContext(ExpressionSyntax source, ExpressionSyntax? refer public ExpressionSyntax? ReferenceHandler { get; } - public TypeMappingBuildContext WithSource(ExpressionSyntax source) => new(source, ReferenceHandler, NameBuilder); + /// + /// Creates a new scoped name builder, + /// builds the name of the source in this new scope + /// and creates a new context with the new source. + /// + /// The new context and the scoped name of the source. + public (TypeMappingBuildContext Context, string SourceName) WithNewScopedSource() => WithNewScopedSource(IdentifierName); + + /// + /// Creates a new scoped name builder, + /// builds the name of the source in this new scope + /// and creates a new context with the new source. + /// + /// A function to build the source access for the new context. + /// The new context and the scoped name of the source. + public (TypeMappingBuildContext Context, string SourceName) WithNewScopedSource(Func sourceBuilder) + { + var scopedNameBuilder = NameBuilder.NewScope(); + var scopedSourceName = scopedNameBuilder.New(DefaultSourceName); + var ctx = new TypeMappingBuildContext(sourceBuilder(scopedSourceName), ReferenceHandler, scopedNameBuilder); + return (ctx, scopedSourceName); + } - public TypeMappingBuildContext WithSource(string source) => WithSource(IdentifierName(source)); + public (TypeMappingBuildContext Context, string SourceName) WithNewSource(string sourceName = DefaultSourceName) + { + var scopedSourceName = NameBuilder.New(sourceName); + return (WithSource(IdentifierName(scopedSourceName)), scopedSourceName); + } + + public TypeMappingBuildContext WithSource(ExpressionSyntax source) => new(source, ReferenceHandler, NameBuilder); public TypeMappingBuildContext WithRefHandler(string refHandler) => WithRefHandler(IdentifierName(refHandler)); diff --git a/src/Riok.Mapperly/Helpers/UniqueNameBuilder.cs b/src/Riok.Mapperly/Helpers/UniqueNameBuilder.cs index e16082195a..f467675f44 100644 --- a/src/Riok.Mapperly/Helpers/UniqueNameBuilder.cs +++ b/src/Riok.Mapperly/Helpers/UniqueNameBuilder.cs @@ -9,7 +9,7 @@ public UniqueNameBuilder() _usedNames = new HashSet(); } - public UniqueNameBuilder(IEnumerable usedNames) + private UniqueNameBuilder(IEnumerable usedNames) { _usedNames = new HashSet(usedNames); } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs index 87854aa4c9..3a46f08b22 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs @@ -6,7 +6,7 @@ public static partial class ProjectionMapper public static partial global::System.Linq.IQueryable ProjectToDto(this global::System.Linq.IQueryable q) { #nullable disable - return System.Linq.Queryable.Select(q, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue) { IntValue = x.IntValue, IntInitOnlyValue = x.IntInitOnlyValue, RequiredValue = x.RequiredValue, StringValue = x.StringValue, RenamedStringValue2 = x.RenamedStringValue, FlatteningIdValue = x.Flattening.IdValue, NullableFlatteningIdValue = x.NullableFlattening != null ? x.NullableFlattening.IdValue : default, NestedNullableIntValue = x.NestedNullable != null ? x.NestedNullable.IntValue : default, NestedNullable = x.NestedNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullable.IntValue } : default, NestedNullableTargetNotNullable = x.NestedNullableTargetNotNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullableTargetNotNullable.IntValue } : new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(), StringNullableTargetNotNullable = x.StringNullableTargetNotNullable ?? "", SourceTargetSameObjectType = x.SourceTargetSameObjectType, NullableReadOnlyObjectCollection = x.NullableReadOnlyObjectCollection != null ? global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.NullableReadOnlyObjectCollection, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.IntValue })) : default, EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)x.EnumValue, EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)x.EnumName, EnumRawValue = (byte)x.EnumRawValue, EnumStringValue = (string)x.EnumStringValue.ToString(), EnumReverseStringValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)System.Enum.Parse(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName), x.EnumReverseStringValue, false), SubObject = x.SubObject != null ? new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto() { SubIntValue = x.SubObject.SubIntValue, BaseIntValue = x.SubObject.BaseIntValue } : default, DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly), DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly), ManuallyMapped = MapManual(x.ManuallyMapped) }); + return System.Linq.Queryable.Select(q, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue) { IntValue = x.IntValue, IntInitOnlyValue = x.IntInitOnlyValue, RequiredValue = x.RequiredValue, StringValue = x.StringValue, RenamedStringValue2 = x.RenamedStringValue, FlatteningIdValue = x.Flattening.IdValue, NullableFlatteningIdValue = x.NullableFlattening != null ? x.NullableFlattening.IdValue : default, NestedNullableIntValue = x.NestedNullable != null ? x.NestedNullable.IntValue : default, NestedNullable = x.NestedNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullable.IntValue } : default, NestedNullableTargetNotNullable = x.NestedNullableTargetNotNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullableTargetNotNullable.IntValue } : new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(), StringNullableTargetNotNullable = x.StringNullableTargetNotNullable ?? "", SourceTargetSameObjectType = x.SourceTargetSameObjectType, NullableReadOnlyObjectCollection = x.NullableReadOnlyObjectCollection != null ? global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.NullableReadOnlyObjectCollection, x1 => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x1.IntValue })) : default, EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)x.EnumValue, EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)x.EnumName, EnumRawValue = (byte)x.EnumRawValue, EnumStringValue = (string)x.EnumStringValue.ToString(), EnumReverseStringValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)System.Enum.Parse(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName), x.EnumReverseStringValue, false), SubObject = x.SubObject != null ? new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto() { SubIntValue = x.SubObject.SubIntValue, BaseIntValue = x.SubObject.BaseIntValue } : default, DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly), DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly), ManuallyMapped = MapManual(x.ManuallyMapped) }); #nullable enable } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs index b7a609746e..153103672a 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs @@ -9,8 +9,8 @@ public static partial class ProjectionMapper return System.Linq.Queryable.Select(q, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue) {IntValue = x.IntValue, IntInitOnlyValue = x.IntInitOnlyValue, RequiredValue = x.RequiredValue, StringValue = x.StringValue, RenamedStringValue2 = x.RenamedStringValue, FlatteningIdValue = x.Flattening.IdValue, NullableFlatteningIdValue = x.NullableFlattening != null ? x.NullableFlattening.IdValue : default, NestedNullableIntValue = x.NestedNullable != null ? x.NestedNullable.IntValue : default, NestedNullable = x.NestedNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() {IntValue = x.NestedNullable.IntValue} : default, NestedNullableTargetNotNullable = x.NestedNullableTargetNotNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() - {IntValue = x.NestedNullableTargetNotNullable.IntValue} : new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(), StringNullableTargetNotNullable = x.StringNullableTargetNotNullable ?? "", SourceTargetSameObjectType = x.SourceTargetSameObjectType, NullableReadOnlyObjectCollection = x.NullableReadOnlyObjectCollection != null ? global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.NullableReadOnlyObjectCollection, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() - {IntValue = x.IntValue})) : default, EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)x.EnumValue, EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)x.EnumName, EnumRawValue = (byte)x.EnumRawValue, EnumStringValue = (string)x.EnumStringValue.ToString(), EnumReverseStringValue = System.Enum.Parse(x.EnumReverseStringValue, false), SubObject = x.SubObject != null ? new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto() + {IntValue = x.NestedNullableTargetNotNullable.IntValue} : new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(), StringNullableTargetNotNullable = x.StringNullableTargetNotNullable ?? "", SourceTargetSameObjectType = x.SourceTargetSameObjectType, NullableReadOnlyObjectCollection = x.NullableReadOnlyObjectCollection != null ? global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.NullableReadOnlyObjectCollection, x1 => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() + {IntValue = x1.IntValue})) : default, EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)x.EnumValue, EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)x.EnumName, EnumRawValue = (byte)x.EnumRawValue, EnumStringValue = (string)x.EnumStringValue.ToString(), EnumReverseStringValue = System.Enum.Parse(x.EnumReverseStringValue, false), SubObject = x.SubObject != null ? new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto() {SubIntValue = x.SubObject.SubIntValue, BaseIntValue = x.SubObject.BaseIntValue} : default, DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly), DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly), ManuallyMapped = MapManual(x.ManuallyMapped)}); #nullable enable } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs index a6c5dd649a..9d79f9eaee 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs @@ -6,7 +6,7 @@ public static partial class ProjectionMapper public static partial global::System.Linq.IQueryable ProjectToDto(this global::System.Linq.IQueryable q) { #nullable disable - return System.Linq.Queryable.Select(q, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue) { IntValue = x.IntValue, IntInitOnlyValue = x.IntInitOnlyValue, RequiredValue = x.RequiredValue, StringValue = x.StringValue, RenamedStringValue2 = x.RenamedStringValue, FlatteningIdValue = x.Flattening.IdValue, NullableFlatteningIdValue = x.NullableFlattening != null ? x.NullableFlattening.IdValue : default, NestedNullableIntValue = x.NestedNullable != null ? x.NestedNullable.IntValue : default, NestedNullable = x.NestedNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullable.IntValue } : default, NestedNullableTargetNotNullable = x.NestedNullableTargetNotNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullableTargetNotNullable.IntValue } : new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(), StringNullableTargetNotNullable = x.StringNullableTargetNotNullable ?? "", SourceTargetSameObjectType = x.SourceTargetSameObjectType, NullableReadOnlyObjectCollection = x.NullableReadOnlyObjectCollection != null ? global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.NullableReadOnlyObjectCollection, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.IntValue })) : default, EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)x.EnumValue, EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)x.EnumName, EnumRawValue = (byte)x.EnumRawValue, EnumStringValue = (string)x.EnumStringValue.ToString(), EnumReverseStringValue = System.Enum.Parse(x.EnumReverseStringValue, false), SubObject = x.SubObject != null ? new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto() { SubIntValue = x.SubObject.SubIntValue, BaseIntValue = x.SubObject.BaseIntValue } : default, DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly), DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly), ManuallyMapped = MapManual(x.ManuallyMapped) }); + return System.Linq.Queryable.Select(q, x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue) { IntValue = x.IntValue, IntInitOnlyValue = x.IntInitOnlyValue, RequiredValue = x.RequiredValue, StringValue = x.StringValue, RenamedStringValue2 = x.RenamedStringValue, FlatteningIdValue = x.Flattening.IdValue, NullableFlatteningIdValue = x.NullableFlattening != null ? x.NullableFlattening.IdValue : default, NestedNullableIntValue = x.NestedNullable != null ? x.NestedNullable.IntValue : default, NestedNullable = x.NestedNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullable.IntValue } : default, NestedNullableTargetNotNullable = x.NestedNullableTargetNotNullable != null ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x.NestedNullableTargetNotNullable.IntValue } : new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(), StringNullableTargetNotNullable = x.StringNullableTargetNotNullable ?? "", SourceTargetSameObjectType = x.SourceTargetSameObjectType, NullableReadOnlyObjectCollection = x.NullableReadOnlyObjectCollection != null ? global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.NullableReadOnlyObjectCollection, x1 => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto() { IntValue = x1.IntValue })) : default, EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)x.EnumValue, EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)x.EnumName, EnumRawValue = (byte)x.EnumRawValue, EnumStringValue = (string)x.EnumStringValue.ToString(), EnumReverseStringValue = System.Enum.Parse(x.EnumReverseStringValue, false), SubObject = x.SubObject != null ? new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto() { SubIntValue = x.SubObject.SubIntValue, BaseIntValue = x.SubObject.BaseIntValue } : default, DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly), DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly), ManuallyMapped = MapManual(x.ManuallyMapped) }); #nullable enable } diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ArrayToArrayExplicitCast#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ArrayToArrayExplicitCast#Mapper.g.verified.cs index 2ea0f3997b..658c591783 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ArrayToArrayExplicitCast#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ArrayToArrayExplicitCast#Mapper.g.verified.cs @@ -5,7 +5,7 @@ public partial class Mapper private partial global::System.Linq.IQueryable Map(global::System.Linq.IQueryable source) { #nullable disable - return System.Linq.Queryable.Select(source, x => new global::B() { Values = global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.Values, x => (int)x)) }); + return System.Linq.Queryable.Select(source, x => new global::B() { Values = global::System.Linq.Enumerable.ToArray(global::System.Linq.Enumerable.Select(x.Values, x1 => (int)x1)) }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ExplicitCast#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ExplicitCast#Mapper.g.verified.cs index 608d0b0d91..ea93b08d3a 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ExplicitCast#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumerableTest.ExplicitCast#Mapper.g.verified.cs @@ -5,7 +5,7 @@ public partial class Mapper private partial global::System.Linq.IQueryable Map(global::System.Linq.IQueryable source) { #nullable disable - return System.Linq.Queryable.Select(source, x => new global::B() { Values = global::System.Linq.Enumerable.Select(x.Values, x => (int)x) }); + return System.Linq.Queryable.Select(source, x => new global::B() { Values = global::System.Linq.Enumerable.Select(x.Values, x1 => (int)x1) }); #nullable enable } } \ No newline at end of file