diff --git a/docs/docs/configuration/mapper.mdx b/docs/docs/configuration/mapper.mdx index c0be1a2719..9ef17b2626 100644 --- a/docs/docs/configuration/mapper.mdx +++ b/docs/docs/configuration/mapper.mdx @@ -184,7 +184,7 @@ dotnet_diagnostic.RMG020.severity = error # Unmapped source member ### Strict enum mappings -To enforce strict enum mappings set `RMG037` and `RMG038` to error, see [strict enum mappings](./enum.mdx). +To enforce strict enum mappings set `RMG037` and `RMG038` to error, see [strict enum mappings](./enum.mdx#strict-enum-mappings). ## Default Mapper configuration diff --git a/docs/docs/contributing/architecture.md b/docs/docs/contributing/architecture.md index 675b3ec25d..a21b727619 100644 --- a/docs/docs/contributing/architecture.md +++ b/docs/docs/contributing/architecture.md @@ -37,6 +37,10 @@ The `DescriptorBuilder` does this by following this process: one approach on how to map types (eg. an explicit cast is implemented by the `ExplicitCastMappingBuilder`). These mappings are queued in the queue of mappings which need the body to be built (currently body builders are only used for object to object (property-based) mappings). 5. The `SourceEmitter` emits the code described by the `MapperDescriptor` and all its mappings. + The syntax objects are created by using `SyntaxFactory` and `SyntaxFactoryHelper`. + The `SyntaxFactoryHelper` tries to simplify creating formatted syntax trees. + If indentation is needed, + the `SyntaxFactoryHelper` instance of the `SourceEmitterContext`/`TypeMappingBuildContext` can be used. ## Roslyn multi targeting diff --git a/docs/docs/contributing/debugging.md b/docs/docs/contributing/debugging.md index 5583c3b670..1808a4c6db 100644 --- a/docs/docs/contributing/debugging.md +++ b/docs/docs/contributing/debugging.md @@ -12,7 +12,7 @@ To debug the Mapperly in unit tests set a breakpoint in the code of Mappery which you want to debug and run the tests in debug mode. If your IDE supports the `DebugRoslynComponent` launch configuration command, -you can just set your breakpoints and debug the preconfigured `integration-tests` profile which will debug Mapperly +you can just set your breakpoints and debug the preconfigured `IntegrationTests` profile which will debug Mapperly in the context of the integration tests. JetBrains Rider and Visual Studio both support `DebugRoslynComponent`. Visual Studio requires the [Roslyn SDK](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/) to be installed. diff --git a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityInfo.cs b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityInfo.cs index cf899e5d66..9cc6cc2e5e 100644 --- a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityInfo.cs +++ b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityInfo.cs @@ -1,8 +1,7 @@ -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Mappings; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using Riok.Mapperly.Emit.Syntax; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; @@ -13,12 +12,13 @@ public abstract class EnsureCapacityInfo public abstract StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyntax target); protected static ExpressionStatementSyntax EnsureCapacityStatement( + SyntaxFactoryHelper syntaxFactory, ExpressionSyntax target, ExpressionSyntax sourceCount, ExpressionSyntax targetCount ) { - var sumMethod = BinaryExpression(SyntaxKind.AddExpression, sourceCount, targetCount); - return ExpressionStatement(Invocation(MemberAccess(target, EnsureCapacityName), sumMethod)); + var sum = Add(sourceCount, targetCount); + return syntaxFactory.ExpressionStatement(Invocation(MemberAccess(target, EnsureCapacityName), sum)); } } diff --git a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs index 24371506ff..d38147b511 100644 --- a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs +++ b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Mappings; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; @@ -25,6 +25,11 @@ public EnsureCapacityMember(string targetAccessor, string sourceAccessor) public override StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyntax target) { - return EnsureCapacityStatement(target, MemberAccess(ctx.Source, _sourceAccessor), MemberAccess(target, _targetAccessor)); + return EnsureCapacityStatement( + ctx.SyntaxFactory, + target, + MemberAccess(ctx.Source, _sourceAccessor), + MemberAccess(target, _targetAccessor) + ); } } diff --git a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityNonEnumerated.cs b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityNonEnumerated.cs index a212a58060..851e0b4204 100644 --- a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityNonEnumerated.cs +++ b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityNonEnumerated.cs @@ -3,7 +3,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Mappings; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; @@ -40,9 +40,10 @@ public override StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyn var enumerableArgument = Argument(ctx.Source); var outVarArgument = Argument(DeclarationExpression(VarIdentifier, SingleVariableDesignation(countIdentifier))) - .WithRefOrOutKeyword(Token(SyntaxKind.OutKeyword)); + .WithRefOrOutKeyword(TrailingSpacedToken(SyntaxKind.OutKeyword)); var getNonEnumeratedInvocation = StaticInvocation(_getNonEnumeratedMethod, enumerableArgument, outVarArgument); - return IfStatement(getNonEnumeratedInvocation, Block(EnsureCapacityStatement(target, countIdentifierName, targetCount))); + var ensureCapacity = EnsureCapacityStatement(ctx.SyntaxFactory.AddIndentation(), target, countIdentifierName, targetCount); + return ctx.SyntaxFactory.If(getNonEnumeratedInvocation, ensureCapacity); } } diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs index d3022b3d07..66825f03d5 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs @@ -5,7 +5,7 @@ using Riok.Mapperly.Descriptors.Mappings; using Riok.Mapperly.Descriptors.Mappings.ExistingTarget; using Riok.Mapperly.Diagnostics; -using Riok.Mapperly.Emit; +using Riok.Mapperly.Emit.Syntax; using Riok.Mapperly.Helpers; namespace Riok.Mapperly.Descriptors.MappingBuilders; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs index f48a25a7c2..3544024f79 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs index 818095ce86..575702c6bb 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -43,17 +43,17 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c var sourceLengthArrayRank = ArrayRankSpecifier( SingletonSeparatedList(MemberAccess(ctx.Source, _countPropertyName)) ); - var targetInitializationValue = ArrayCreationExpression( + var targetInitializationValue = CreateArray( ArrayType(FullyQualifiedIdentifier(_targetArrayElementType)).WithRankSpecifiers(SingletonList(sourceLengthArrayRank)) ); - yield return DeclareLocalVariable(targetVariableName, targetInitializationValue); + yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, targetInitializationValue); // var i = 0; - yield return DeclareLocalVariable(loopCounterVariableName, IntLiteral(0)); + yield return ctx.SyntaxFactory.DeclareLocalVariable(loopCounterVariableName, IntLiteral(0)); // target[i] = Map(item); var (loopItemCtx, loopItemVariableName) = ctx.WithNewSource(LoopItemVariableName); - var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx); + var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx.AddIndentation()); var assignment = Assignment( ElementAccess(IdentifierName(targetVariableName), IdentifierName(loopCounterVariableName)), @@ -63,18 +63,14 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // i++; var counterIncrement = PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, IdentifierName(loopCounterVariableName)); - // target[i] = Map(item); - // i++; - var assignmentBlock = Block(ExpressionStatement(assignment), ExpressionStatement(counterIncrement)); - // foreach(var item in source) //{ // target[i] = Map(item); // i++; //} - yield return ForEachStatement(VarIdentifier, Identifier(loopItemVariableName), ctx.Source, assignmentBlock); + yield return ctx.SyntaxFactory.ForEach(loopItemVariableName, ctx.Source, assignment, counterIncrement); // return target; - yield return ReturnVariable(targetVariableName); + yield return ctx.SyntaxFactory.ReturnVariable(targetVariableName); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs index ca32a67f8c..064d1196e2 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -35,25 +35,28 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c var sourceLengthArrayRank = ArrayRankSpecifier( SingletonSeparatedList(MemberAccess(ctx.Source, ArrayLengthProperty)) ); - var targetInitializationValue = ArrayCreationExpression( + var targetInitializationValue = CreateArray( ArrayType(FullyQualifiedIdentifier(_targetArrayElementType)).WithRankSpecifiers(SingletonList(sourceLengthArrayRank)) ); - yield return DeclareLocalVariable(targetVariableName, targetInitializationValue); + yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, targetInitializationValue); // target[i] = Map(source[i]); var forLoopBuilderCtx = ctx.WithSource(ElementAccess(ctx.Source, IdentifierName(loopCounterVariableName))); - var mappedIndexedSourceValue = _elementMapping.Build(forLoopBuilderCtx); + var mappedIndexedSourceValue = _elementMapping.Build(forLoopBuilderCtx.AddIndentation()); var assignment = Assignment( ElementAccess(IdentifierName(targetVariableName), IdentifierName(loopCounterVariableName)), mappedIndexedSourceValue ); - var assignmentBlock = Block(SingletonList(ExpressionStatement(assignment))); // for(var i = 0; i < source.Length; i++) // target[i] = Map(source[i]); - yield return IncrementalForLoop(loopCounterVariableName, assignmentBlock, MemberAccess(ctx.Source, ArrayLengthProperty)); + yield return ctx.SyntaxFactory.IncrementalForLoop( + loopCounterVariableName, + MemberAccess(ctx.Source, ArrayLengthProperty), + assignment + ); // return target; - yield return ReturnVariable(targetVariableName); + yield return ctx.SyntaxFactory.ReturnVariable(targetVariableName); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs index 5c51e5e658..d6ede730c3 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs index 3a697fb429..79256ab9e7 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs @@ -1,7 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -13,8 +12,5 @@ public class CtorMapping : NewInstanceMapping public CtorMapping(ITypeSymbol sourceType, ITypeSymbol targetType) : base(sourceType, targetType) { } - public override ExpressionSyntax Build(TypeMappingBuildContext ctx) - { - return ObjectCreationExpression(FullyQualifiedIdentifier(TargetType)).WithArgumentList(ArgumentList(ctx.Source)); - } + public override ExpressionSyntax Build(TypeMappingBuildContext ctx) => CreateInstance(TargetType, ctx.Source); } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs index 8ebd156ced..0fb5e0ee7a 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs @@ -1,8 +1,7 @@ using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -51,10 +50,7 @@ INewInstanceMapping mapping var castedSourceContext = ctx.WithSource( ParenthesizedExpression(CastExpression(FullyQualifiedIdentifier(mapping.SourceType), ctx.Source)) ); - return ConditionalExpression( - BinaryExpression(SyntaxKind.IsExpression, ctx.Source, FullyQualifiedIdentifier(mapping.SourceType)), - mapping.Build(castedSourceContext), - notMatched - ); + var condition = Is(ctx.Source, FullyQualifiedIdentifier(mapping.SourceType)); + return Conditional(condition, mapping.Build(castedSourceContext), notMatched); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs index f70103f3bc..bc4a55aa2a 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs @@ -1,7 +1,8 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Emit.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -25,7 +26,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { // _ => throw new ArgumentException(msg, nameof(ctx.Source)), var sourceType = Invocation(MemberAccess(ctx.Source, GetTypeMethodName)); - var fallbackArm = SwitchExpressionArm( + var fallbackArm = SwitchArm( DiscardPattern(), ThrowArgumentExpression( InterpolatedString($"Cannot map {sourceType} to {TargetType.ToDisplayString()} as there is no known derived type mapping"), @@ -38,13 +39,16 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) var arms = _typeMappings .Select(x => BuildSwitchArm(typeArmVariableName, x.SourceType, x.Build(typeArmContext))) .Append(fallbackArm); - return SwitchExpression(ctx.Source).WithArms(CommaSeparatedList(arms, true)); + return ctx.SyntaxFactory.Switch(ctx.Source, arms); } private SwitchExpressionArmSyntax BuildSwitchArm(string typeArmVariableName, ITypeSymbol type, ExpressionSyntax mapping) { // A x => MapToADto(x), - var declaration = DeclarationPattern(FullyQualifiedIdentifier(type), SingleVariableDesignation(Identifier(typeArmVariableName))); - return SwitchExpressionArm(declaration, mapping); + var declaration = DeclarationPattern( + FullyQualifiedIdentifier(type).AddTrailingSpace(), + SingleVariableDesignation(Identifier(typeArmVariableName)) + ); + return SwitchArm(declaration, mapping); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs index 3838ae1965..cdcfb073e0 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.Enums; @@ -54,7 +54,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) return casted; var valueDefinedCondition = BuildIsDefinedCondition(casted); - return ConditionalExpression(valueDefinedCondition, casted, _fallback.Build(ctx)); + return Conditional(valueDefinedCondition, casted, _fallback.Build(ctx)); } private ExpressionSyntax BuildIsDefinedCondition(ExpressionSyntax convertedSourceValue) @@ -64,7 +64,7 @@ private ExpressionSyntax BuildIsDefinedCondition(ExpressionSyntax convertedSourc { // (TargetEnum)v is TargetEnum.A or TargetEnum.B or ... CheckDefinedMode.Value - => IsPatternExpression(convertedSourceValue, OrPattern(allEnumMembers)), + => IsPattern(convertedSourceValue, OrPattern(allEnumMembers)), // (TargetEnum)v == ((TargetEnum)v & (TargetEnum.A | TargetEnum.B | ...)) CheckDefinedMode.Flags diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs index 3f45dd77cc..cc415523b0 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.Enums; @@ -27,7 +27,7 @@ public class EnumFallbackValueMapping : NewInstanceMapping public IFieldSymbol? FallbackMember { get; } - public SwitchExpressionArmSyntax BuildDiscardArm(TypeMappingBuildContext ctx) => SwitchExpressionArm(DiscardPattern(), Build(ctx)); + public SwitchExpressionArmSyntax BuildDiscardArm(TypeMappingBuildContext ctx) => SwitchArm(DiscardPattern(), Build(ctx)); public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs index fd8b0fd08c..d038a40952 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.Enums; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs index e630891e38..b87ea348f8 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.Enums; @@ -41,9 +41,8 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c var arms = _ignoreCase ? BuildArmsIgnoreCase(ctx) : _enumMembers.Select(BuildArm); arms = arms.Append(_fallbackMapping.BuildDiscardArm(ctx)); - var switchExpr = SwitchExpression(ctx.Source).WithArms(CommaSeparatedList(arms, true)); - - yield return ReturnStatement(switchExpr); + var switchExpr = ctx.SyntaxFactory.Switch(ctx.Source, arms); + yield return ctx.SyntaxFactory.Return(switchExpr); } private IEnumerable BuildArmsIgnoreCase(TypeMappingBuildContext ctx) @@ -63,7 +62,7 @@ private SwitchExpressionArmSyntax BuildArmIgnoreCase(string ignoreCaseSwitchDesi var typeMemberAccess = MemberAccess(field.ContainingType.NonNullable().FullyQualifiedIdentifierName(), field.Name); // when s.Equals(nameof(source.Value1), StringComparison.OrdinalIgnoreCase) - var whenClause = WhenClause( + var whenClause = SwitchWhen( Invocation( MemberAccess(ignoreCaseSwitchDesignatedVariableName, StringEqualsMethodName), NameOf(typeMemberAccess), @@ -72,7 +71,7 @@ private SwitchExpressionArmSyntax BuildArmIgnoreCase(string ignoreCaseSwitchDesi ); // { } s when s.Equals(nameof(source.Value1), StringComparison.OrdinalIgnoreCase) => source.Value1; - return SwitchExpressionArm(pattern, typeMemberAccess).WithWhenClause(whenClause); + return SwitchArm(pattern, typeMemberAccess).WithWhenClause(whenClause); } private SwitchExpressionArmSyntax BuildArm(IFieldSymbol field) @@ -80,6 +79,6 @@ private SwitchExpressionArmSyntax BuildArm(IFieldSymbol field) // nameof(source.Value1) => source.Value1; var typeMemberAccess = MemberAccess(FullyQualifiedIdentifier(field.ContainingType), field.Name); var pattern = ConstantPattern(NameOf(typeMemberAccess)); - return SwitchExpressionArm(pattern, typeMemberAccess); + return SwitchArm(pattern, typeMemberAccess); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs index 8a9ae97633..8f48d61e75 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.Enums; @@ -32,9 +32,8 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // switch for each name to the enum value // eg: Enum1.Value1 => Enum2.Value1, var arms = _enumMemberMappings.Select(x => BuildArm(x.Key, x.Value)).Append(_fallback.BuildDiscardArm(ctx)); - - var switchExpr = SwitchExpression(ctx.Source).WithArms(CommaSeparatedList(arms, true)); - yield return ReturnStatement(switchExpr); + var switchExpr = ctx.SyntaxFactory.Switch(ctx.Source, arms); + yield return ctx.SyntaxFactory.Return(switchExpr); } private SwitchExpressionArmSyntax BuildArm(IFieldSymbol sourceMemberField, IFieldSymbol targetMemberField) @@ -42,6 +41,6 @@ private SwitchExpressionArmSyntax BuildArm(IFieldSymbol sourceMemberField, IFiel var sourceMember = MemberAccess(FullyQualifiedIdentifier(SourceType), sourceMemberField.Name); var targetMember = MemberAccess(FullyQualifiedIdentifier(TargetType), targetMemberField.Name); var pattern = ConstantPattern(sourceMember); - return SwitchExpressionArm(pattern, targetMember); + return SwitchArm(pattern, targetMember); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs index f214b783e7..705b0d13bf 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.Enums; @@ -26,15 +26,13 @@ public EnumToStringMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IEnum public override IEnumerable BuildBody(TypeMappingBuildContext ctx) { // fallback switch arm: _ => source.ToString() - var fallbackArm = SwitchExpressionArm(DiscardPattern(), Invocation(MemberAccess(ctx.Source, ToStringMethodName))); + var fallbackArm = SwitchArm(DiscardPattern(), Invocation(MemberAccess(ctx.Source, ToStringMethodName))); // switch for each name to the enum value // eg: Enum1.Value1 => "Value1" var arms = _enumMembers.Select(BuildArm).Append(fallbackArm); - - var switchExpr = SwitchExpression(ctx.Source).WithArms(CommaSeparatedList(arms, true)); - - yield return ReturnStatement(switchExpr); + var switchExpr = ctx.SyntaxFactory.Switch(ctx.Source, arms); + yield return ctx.SyntaxFactory.Return(switchExpr); } private SwitchExpressionArmSyntax BuildArm(IFieldSymbol field) @@ -42,6 +40,6 @@ private SwitchExpressionArmSyntax BuildArm(IFieldSymbol field) var typeMemberAccess = MemberAccess(FullyQualifiedIdentifier(field.ContainingType.NonNullable()), field.Name); var pattern = ConstantPattern(typeMemberAccess); var nameOf = NameOf(typeMemberAccess); - return SwitchExpressionArm(pattern, nameOf); + return SwitchArm(pattern, nameOf); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs index 008db0d344..c6a59bec55 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs @@ -1,6 +1,5 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget; @@ -23,14 +22,14 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c { var targetVariableName = ctx.NameBuilder.New(TargetVariableName); - yield return DeclareLocalVariable(targetVariableName, CreateTargetInstance(ctx)); + yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, CreateTargetInstance(ctx)); foreach (var statement in _mapping.Build(ctx, IdentifierName(targetVariableName))) { yield return statement; } - yield return ReturnVariable(targetVariableName); + yield return ctx.SyntaxFactory.ReturnVariable(targetVariableName); } protected abstract ExpressionSyntax CreateTargetInstance(TypeMappingBuildContext ctx); diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs index c65d4d0dba..aa5f7df5dc 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs @@ -1,8 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget; @@ -34,20 +33,15 @@ public class ForEachAddEnumerableExistingTargetMapping : ExistingTargetMapping public override IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax target) { - var (loopItemCtx, loopItemVariableName) = ctx.WithNewSource(LoopItemVariableName); - var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx); - var addMethod = MemberAccess(target, _insertMethodName); - if (_ensureCapacityBuilder != null) { yield return _ensureCapacityBuilder.Build(ctx, target); } - yield return ForEachStatement( - VarIdentifier, - Identifier(loopItemVariableName), - ctx.Source, - Block(ExpressionStatement(Invocation(addMethod, convertedSourceItemExpression))) - ); + var (loopItemCtx, loopItemVariableName) = ctx.WithNewSource(LoopItemVariableName); + var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx); + var addMethod = MemberAccess(target, _insertMethodName); + var body = Invocation(addMethod, convertedSourceItemExpression); + yield return ctx.SyntaxFactory.ForEach(loopItemVariableName, ctx.Source, body); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs index 67a12129bf..1fb6eeddf5 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget; @@ -63,6 +63,6 @@ public override IEnumerable Build(TypeMappingBuildContext ctx, var assignment = Assignment(ElementAccess(target, convertedKeyExpression), convertedValueExpression); - yield return ForEachStatement(VarIdentifier, Identifier(loopItemVariableName), ctx.Source, Block(ExpressionStatement(assignment))); + yield return ctx.SyntaxFactory.ForEach(loopItemVariableName, ctx.Source, assignment); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs index 0f2fca229c..ac18599d0d 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs @@ -1,8 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget; @@ -27,21 +26,21 @@ IExistingTargetMapping delegateMapping public override IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax target) { - var body = _delegateMapping.Build(ctx, target); - // if the source or target type is nullable, add a null guard. if (!SourceType.IsNullable() && !TargetType.IsNullable()) - return body; + return _delegateMapping.Build(ctx, target); - var enumerated = body.ToArray(); + var body = _delegateMapping.Build(ctx.AddIndentation(), target).ToArray(); // if body is empty don't generate an if statement - if (enumerated.Length == 0) + if (body.Length == 0) { return Enumerable.Empty(); } // if (source != null && target != null) { body } - return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(enumerated)), }; + var condition = IfNoneNull((SourceType, ctx.Source), (TargetType, target)); + var ifStatement = ctx.SyntaxFactory.If(condition, body); + return new[] { ifStatement }; } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/SourceObjectMemberDelegateExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/SourceObjectMemberDelegateExistingTargetMapping.cs index b48e6198fd..67748b9aa9 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/SourceObjectMemberDelegateExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/SourceObjectMemberDelegateExistingTargetMapping.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ForEachAddEnumerableMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ForEachAddEnumerableMapping.cs index 15cbdf1138..c8bb80afb7 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ForEachAddEnumerableMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ForEachAddEnumerableMapping.cs @@ -3,7 +3,7 @@ using Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; using Riok.Mapperly.Descriptors.Mappings.ExistingTarget; using Riok.Mapperly.Descriptors.ObjectFactories; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ForEachSetDictionaryMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ForEachSetDictionaryMapping.cs index 439cbdf06c..a63fdd0dbe 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ForEachSetDictionaryMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ForEachSetDictionaryMapping.cs @@ -3,7 +3,7 @@ using Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity; using Riok.Mapperly.Descriptors.Mappings.ExistingTarget; using Riok.Mapperly.Descriptors.ObjectFactories; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs index 4c48fe7109..aeb2838043 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.cs @@ -1,7 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -37,7 +36,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource(); var sourceMapExpression = _elementMapping.Build(lambdaCtx); - var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaSourceName))).WithExpressionBody(sourceMapExpression); + var convertLambda = Lambda(lambdaSourceName, sourceMapExpression); mappedSource = Invocation(_selectMethod, ctx.Source, convertLambda); } else diff --git a/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs index f410d9e2de..c7876cae62 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs @@ -1,7 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -42,11 +41,11 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) // 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 keyExpression = Lambda(keyLambdaParamName, keyMapExpression); var (valueLambdaCtx, valueLambdaParamName) = ctx.WithNewScopedSource(src => MemberAccess(src, ValuePropertyName)); var valueMapExpression = _valueMapping.Build(valueLambdaCtx); - var valueExpression = SimpleLambdaExpression(Parameter(Identifier(valueLambdaParamName))).WithExpressionBody(valueMapExpression); + var valueExpression = Lambda(valueLambdaParamName, valueMapExpression); return Invocation(_collectMethod, ctx.Source, keyExpression, valueExpression); } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs index 03cae41eb6..0e1b39d757 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/LinqEnumerableMapping.cs @@ -1,7 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -37,7 +36,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource(); var sourceMapExpression = _elementMapping.Build(lambdaCtx); - var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaSourceName))).WithExpressionBody(sourceMapExpression); + var convertLambda = Lambda(lambdaSourceName, sourceMapExpression); mappedSource = Invocation(_selectMethod, ctx.Source, convertLambda); } else diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ConstructorParameterMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ConstructorParameterMapping.cs index 3cefa16245..61c84894ba 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ConstructorParameterMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ConstructorParameterMapping.cs @@ -1,6 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings; @@ -23,7 +24,7 @@ public ArgumentSyntax BuildArgument(TypeMappingBuildContext ctx) { var argumentExpression = DelegateMapping.Build(ctx); var arg = Argument(argumentExpression); - return _selfOrPreviousIsUnmappedOptional ? arg.WithNameColon(NameColon(Parameter.Name)) : arg; + return _selfOrPreviousIsUnmappedOptional ? arg.WithNameColon(SpacedNameColon(Parameter.Name)) : arg; } protected bool Equals(ConstructorParameterMapping other) => diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberAssignmentMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberAssignmentMapping.cs index 995aede839..548ebbdf50 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberAssignmentMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberAssignmentMapping.cs @@ -1,7 +1,7 @@ using System.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Symbols; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings; @@ -25,7 +25,7 @@ public MemberAssignmentMapping(MemberPath targetPath, IMemberMapping mapping) public MemberPath TargetPath { get; } public IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax targetAccess) => - SingleStatement(BuildExpression(ctx, targetAccess)); + ctx.SyntaxFactory.SingleStatement(BuildExpression(ctx, targetAccess)); public ExpressionSyntax BuildExpression(TypeMappingBuildContext ctx, ExpressionSyntax? targetAccess) { diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullAssignmentInitializerMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullAssignmentInitializerMapping.cs index b21cf034d0..c056963027 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullAssignmentInitializerMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullAssignmentInitializerMapping.cs @@ -1,9 +1,8 @@ using System.Diagnostics; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings; @@ -23,12 +22,8 @@ public MemberNullAssignmentInitializerMapping(MemberPath pathToInitialize) public override IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax targetAccess) { // source.Value ??= new(); - var initializer = ExpressionStatement( - Assignment( - _pathToInitialize.BuildAccess(targetAccess), - ImplicitObjectCreationExpression(), - SyntaxKind.CoalesceAssignmentExpression - ) + var initializer = ctx.SyntaxFactory.ExpressionStatement( + CoalesceAssignment(_pathToInitialize.BuildAccess(targetAccess), ImplicitObjectCreationExpression()) ); return base.Build(ctx, targetAccess).Prepend(initializer); } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullDelegateAssignmentMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullDelegateAssignmentMapping.cs index 0916b961cd..e7718ea30c 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullDelegateAssignmentMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/MemberNullDelegateAssignmentMapping.cs @@ -1,8 +1,7 @@ using System.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Symbols; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings; @@ -35,11 +34,13 @@ public override IEnumerable Build(TypeMappingBuildContext ctx, var sourceNullConditionalAccess = _nullConditionalSourcePath.BuildAccess(ctx.Source, true, true, true); var nameofSourceAccess = _nullConditionalSourcePath.BuildAccess(ctx.Source, true, false, true); var condition = IsNotNull(sourceNullConditionalAccess); + var conditionCtx = ctx.AddIndentation(); + var trueClause = base.Build(conditionCtx, targetAccess); var elseClause = _throwInsteadOfConditionalNullMapping - ? ElseClause(Block(ExpressionStatement(ThrowArgumentNullException(nameofSourceAccess)))) + ? new[] { conditionCtx.SyntaxFactory.ExpressionStatement(ThrowArgumentNullException(nameofSourceAccess)) } : null; - - return new[] { IfStatement(condition, Block(base.Build(ctx, targetAccess)), elseClause), }; + var ifExpression = ctx.SyntaxFactory.If(condition, trueClause, elseClause); + return new[] { ifExpression }; } public override bool Equals(object? obj) diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/NullMemberMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/NullMemberMapping.cs index 48f9ce0bb3..aba9b5ede1 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/NullMemberMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/NullMemberMapping.cs @@ -3,8 +3,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings; @@ -69,7 +68,7 @@ public ExpressionSyntax Build(TypeMappingBuildContext ctx) : SourcePath.BuildNonNullConditionWithoutConditionalAccess(ctx.Source)!; var sourceMemberAccess = SourcePath.BuildAccess(ctx.Source, true); ctx = ctx.WithSource(sourceMemberAccess); - return ConditionalExpression( + return Conditional( notNullCondition, _delegateMapping.Build(ctx), NullSubstitute(_delegateMapping.TargetType, sourceMemberAccess, _nullFallback) diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ValueTupleConstructorParameterMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ValueTupleConstructorParameterMapping.cs index d9e7a49e4a..1c0b659eb7 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ValueTupleConstructorParameterMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MemberMappings/ValueTupleConstructorParameterMapping.cs @@ -1,6 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings; @@ -29,7 +30,7 @@ public ArgumentSyntax BuildArgument(TypeMappingBuildContext ctx, bool emitFieldN // add field name if available return SymbolEqualityComparer.Default.Equals(Parameter.CorrespondingTupleField, Parameter) ? argument - : argument.WithNameColon(NameColon(IdentifierName(Parameter.Name))); + : argument.WithNameColon(SpacedNameColon(Parameter.Name)); } protected bool Equals(ValueTupleConstructorParameterMapping other) => diff --git a/src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs index 5bf8920cf6..48d6f6b680 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs @@ -2,10 +2,11 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Emit; +using Riok.Mapperly.Emit.Syntax; using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; using Accessibility = Microsoft.CodeAnalysis.Accessibility; namespace Riok.Mapperly.Descriptors.Mappings; @@ -65,21 +66,21 @@ ITypeSymbol targetType public virtual MethodDeclarationSyntax BuildMethod(SourceEmitterContext ctx) { - var returnType = FullyQualifiedIdentifier(_returnType); - var typeMappingBuildContext = new TypeMappingBuildContext( SourceParameter.Name, ReferenceHandlerParameter?.Name, - ctx.NameBuilder.NewScope() + ctx.NameBuilder.NewScope(), + ctx.SyntaxFactory.AddIndentation() ); var parameters = BuildParameterList(); ReserveParameterNames(typeMappingBuildContext.NameBuilder, parameters); - return MethodDeclaration(returnType, Identifier(MethodName)) + var returnType = FullyQualifiedIdentifier(_returnType); + return MethodDeclaration(returnType.AddTrailingSpace(), Identifier(MethodName)) .WithModifiers(TokenList(BuildModifiers(ctx.IsStatic))) .WithParameterList(parameters) - .WithBody(Block(BuildBody(typeMappingBuildContext))); + .WithBody(ctx.SyntaxFactory.Block(BuildBody(typeMappingBuildContext))); } public abstract IEnumerable BuildBody(TypeMappingBuildContext ctx); @@ -106,10 +107,10 @@ private IEnumerable BuildModifiers(bool isStatic) yield return Accessibility(_accessibility); if (isStatic) - yield return Token(SyntaxKind.StaticKeyword); + yield return TrailingSpacedToken(SyntaxKind.StaticKeyword); if (IsPartial) - yield return Token(SyntaxKind.PartialKeyword); + yield return TrailingSpacedToken(SyntaxKind.PartialKeyword); } private void ReserveParameterNames(UniqueNameBuilder nameBuilder, ParameterListSyntax parameters) diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectFactoryMemberMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectFactoryMemberMapping.cs index d4ae1b2e63..1c93e789d4 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectFactoryMemberMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectFactoryMemberMapping.cs @@ -3,7 +3,6 @@ using Riok.Mapperly.Descriptors.ObjectFactories; using Riok.Mapperly.Emit; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -35,18 +34,21 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c if (_enableReferenceHandling) { // TryGetReference - yield return ReferenceHandlingSyntaxFactoryHelper.TryGetReference(this, ctx); + yield return ReferenceHandlingSyntaxFactoryHelper.TryGetReference(ctx, this); } // var target = CreateMyObject(); - yield return DeclareLocalVariable(targetVariableName, _objectFactory.CreateType(SourceType, TargetType, ctx.Source)); + yield return ctx.SyntaxFactory.DeclareLocalVariable( + targetVariableName, + _objectFactory.CreateType(SourceType, TargetType, ctx.Source) + ); // set the reference as soon as it is created, // as property mappings could refer to the same instance. if (_enableReferenceHandling) { // SetReference - yield return ExpressionStatement( + yield return ctx.SyntaxFactory.ExpressionStatement( ReferenceHandlingSyntaxFactoryHelper.SetReference(this, ctx, IdentifierName(targetVariableName)) ); } @@ -58,6 +60,6 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c } // return target; - yield return ReturnVariable(targetVariableName); + yield return ctx.SyntaxFactory.ReturnVariable(targetVariableName); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMapping.cs index c6954af5d5..d4ba5119ee 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Mappings.MemberMappings; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -31,8 +31,9 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) // add initializer if (_initPropertyMappings.Count > 0) { - var initMappings = _initPropertyMappings.Select(x => x.BuildExpression(ctx, null)).ToArray(); - objectCreationExpression = objectCreationExpression.WithInitializer(ObjectInitializer(initMappings)); + var initPropertiesContext = ctx.AddIndentation(); + var initMappings = _initPropertyMappings.Select(x => x.BuildExpression(initPropertiesContext, null)).ToArray(); + objectCreationExpression = objectCreationExpression.WithInitializer(ctx.SyntaxFactory.ObjectInitializer(initMappings)); } return objectCreationExpression; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMethodMapping.cs index 0179459d4a..d3c0550818 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NewInstanceObjectMemberMethodMapping.cs @@ -3,7 +3,7 @@ using Riok.Mapperly.Descriptors.Mappings.MemberMappings; using Riok.Mapperly.Emit; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -35,7 +35,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c if (_enableReferenceHandling) { // TryGetReference - yield return ReferenceHandlingSyntaxFactoryHelper.TryGetReference(this, ctx); + yield return ReferenceHandlingSyntaxFactoryHelper.TryGetReference(ctx, this); } // new T(ctorArgs) { ... }; @@ -45,19 +45,20 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // add initializer if (_initPropertyMappings.Count > 0) { - var initMappings = _initPropertyMappings.Select(x => x.BuildExpression(ctx, null)).ToArray(); - objectCreationExpression = objectCreationExpression.WithInitializer(ObjectInitializer(initMappings)); + var initPropertiesContext = ctx.AddIndentation(); + var initMappings = _initPropertyMappings.Select(x => x.BuildExpression(initPropertiesContext, null)).ToArray(); + objectCreationExpression = objectCreationExpression.WithInitializer(ctx.SyntaxFactory.ObjectInitializer(initMappings)); } // var target = new T() { ... }; - yield return DeclareLocalVariable(targetVariableName, objectCreationExpression); + yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, objectCreationExpression); // set the reference as soon as it is created, // as property mappings could refer to the same instance. if (_enableReferenceHandling) { // SetReference - yield return ExpressionStatement( + yield return ctx.SyntaxFactory.ExpressionStatement( ReferenceHandlingSyntaxFactoryHelper.SetReference(this, ctx, IdentifierName(targetVariableName)) ); } @@ -69,6 +70,6 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c } // return target; - yield return ReturnVariable(targetVariableName); + yield return ctx.SyntaxFactory.ReturnVariable(targetVariableName); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleConstructorMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleConstructorMapping.cs index 79142d2ea5..f8d6c47c07 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleConstructorMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleConstructorMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Mappings.MemberMappings; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -25,9 +25,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) { // new ValueTuple(ctorArgs) var ctorArgs = _constructorPropertyMappings.Select(x => x.BuildArgument(ctx, emitFieldName: false)); - var genericName = GenericName(ValueTupleName); var typeArguments = TypeArgumentList(((INamedTypeSymbol)TargetType).TypeArguments.Select(NonNullableIdentifier)); - var typedValue = genericName.WithTypeArgumentList(typeArguments); - return ObjectCreationExpression(typedValue).WithArgumentList(ArgumentList(ctorArgs)); + return CreateGenericInstance(ValueTupleName, typeArguments, ctorArgs); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleExpressionMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleExpressionMapping.cs index 10cf306f46..86d435ac55 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleExpressionMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleExpressionMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors.Mappings.MemberMappings; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -14,7 +14,6 @@ namespace Riok.Mapperly.Descriptors.Mappings; public class NewValueTupleExpressionMapping : ObjectMemberMethodMapping, INewValueTupleMapping { private readonly int _argumentCount; - private const string NoMappingComment = "// Could not generate mapping"; private const string TargetVariableName = "target"; private readonly HashSet _constructorPropertyMappings = new(); @@ -31,7 +30,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) // generate error if constructor argument don't match if (_constructorPropertyMappings.Count != _argumentCount) { - return ThrowNotImplementedException().WithLeadingTrivia(TriviaList(Comment(NoMappingComment))); + return ctx.SyntaxFactory.ThrowMappingNotImplementedExceptionStatement(); } return base.Build(ctx); @@ -42,17 +41,17 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // generate error if constructor argument don't match if (_constructorPropertyMappings.Count != _argumentCount) { - yield return ExpressionStatement(ThrowNotImplementedException()).WithLeadingTrivia(TriviaList(Comment(NoMappingComment))); + yield return ctx.SyntaxFactory.ExpressionStatement(ctx.SyntaxFactory.ThrowMappingNotImplementedExceptionStatement()); yield break; } // (Name:.. ,..); var ctorArgs = _constructorPropertyMappings.Select(x => x.BuildArgument(ctx, emitFieldName: true)); - var tupleCreationExpression = TupleExpression(SeparatedList(ctorArgs)); + var tupleCreationExpression = TupleExpression(CommaSeparatedList(ctorArgs)); // var target = (Name:.. ,..); var targetVariableName = ctx.NameBuilder.New(TargetVariableName); - yield return DeclareLocalVariable(targetVariableName, tupleCreationExpression); + yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, tupleCreationExpression); // map properties // target.Name.Child = ... @@ -62,6 +61,6 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c } // return target; - yield return ReturnVariable(targetVariableName); + yield return ctx.SyntaxFactory.ReturnVariable(targetVariableName); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMapping.cs index 033e856374..b2a0af2a9c 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMapping.cs @@ -3,7 +3,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -67,7 +67,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) sourceValue = PostfixUnaryExpression(SyntaxKind.SuppressNullableWarningExpression, sourceValue); } - return ConditionalExpression( + return Conditional( IsNull(ctx.Source), NullSubstitute(TargetType.NonNullable(), ctx.Source, _nullFallbackValue), _delegateMapping.Build(ctx.WithSource(sourceValue)) diff --git a/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMethodMapping.cs index d5e81d4bcb..10c5e9d56c 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMethodMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -29,10 +29,10 @@ NullFallbackValue nullFallbackValue public override IEnumerable BuildBody(TypeMappingBuildContext ctx) { var body = _delegateMapping.BuildBody(ctx); - return AddPreNullHandling(ctx.Source, body); + return AddPreNullHandling(ctx, body); } - private IEnumerable AddPreNullHandling(ExpressionSyntax source, IEnumerable body) + private IEnumerable AddPreNullHandling(TypeMappingBuildContext ctx, IEnumerable body) { if (!SourceType.IsNullable() || _delegateMapping.SourceType.IsNullable()) return body; @@ -41,6 +41,8 @@ private IEnumerable AddPreNullHandling(ExpressionSyntax source, // call mapping only if source is not null. // if (source == null) // return ; - return body.Prepend(IfNullReturnOrThrow(source, NullSubstitute(TargetType.NonNullable(), source, _nullFallbackValue))); + var fallbackExpression = NullSubstitute(TargetType.NonNullable(), ctx.Source, _nullFallbackValue); + var ifExpression = ctx.SyntaxFactory.IfNullReturnOrThrow(ctx.Source, fallbackExpression); + return body.Prepend(ifExpression); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs index c50c2b17b8..8df39604ba 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/QueryableProjectionMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; @@ -31,11 +31,14 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource(); var delegateMapping = _delegateMapping.Build(lambdaCtx); - var projectionLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaSourceName))).WithExpressionBody(delegateMapping); + var projectionLambda = Lambda(lambdaSourceName, delegateMapping); var select = StaticInvocation(QueryableReceiverName, SelectMethodName, ctx.Source, projectionLambda); + var returnStatement = ctx.SyntaxFactory.Return(select); return new[] { - ReturnStatement(select).WithLeadingTrivia(TriviaList(Nullable(false))).WithTrailingTrivia(TriviaList(Nullable(true))) + returnStatement + .WithLeadingTrivia(returnStatement.GetLeadingTrivia().Insert(0, ElasticCarriageReturnLineFeed).Insert(1, Nullable(false))) + .WithTrailingTrivia(returnStatement.GetTrailingTrivia().Insert(0, ElasticCarriageReturnLineFeed).Insert(1, Nullable(true))) }; } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMemberMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMemberMapping.cs index 5615fc3e76..72223329a2 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMemberMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMemberMapping.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMethodMapping.cs index 5a2b27652d..bdc4d1bb9d 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/SourceObjectMethodMapping.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/StaticMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/StaticMethodMapping.cs index 2d88bfb38c..abde7a856a 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/StaticMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/StaticMethodMapping.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs b/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs index 464d2afc9d..afadf1584d 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs @@ -1,21 +1,33 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Emit.Syntax; using Riok.Mapperly.Helpers; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace Riok.Mapperly.Descriptors.Mappings; -public class TypeMappingBuildContext +public readonly record struct TypeMappingBuildContext { private const string DefaultSourceName = "x"; - public TypeMappingBuildContext(string source, string? referenceHandler, UniqueNameBuilder nameBuilder) - : this(IdentifierName(source), referenceHandler == null ? null : IdentifierName(referenceHandler), nameBuilder) { } + public TypeMappingBuildContext( + string source, + string? referenceHandler, + UniqueNameBuilder nameBuilder, + SyntaxFactoryHelper syntaxFactory + ) + : this(IdentifierName(source), referenceHandler == null ? null : IdentifierName(referenceHandler), nameBuilder, syntaxFactory) { } - private TypeMappingBuildContext(ExpressionSyntax source, ExpressionSyntax? referenceHandler, UniqueNameBuilder nameBuilder) + private TypeMappingBuildContext( + ExpressionSyntax source, + ExpressionSyntax? referenceHandler, + UniqueNameBuilder nameBuilder, + SyntaxFactoryHelper syntaxFactory + ) { Source = source; ReferenceHandler = referenceHandler; NameBuilder = nameBuilder; + SyntaxFactory = syntaxFactory; } public UniqueNameBuilder NameBuilder { get; } @@ -24,6 +36,10 @@ private TypeMappingBuildContext(ExpressionSyntax source, ExpressionSyntax? refer public ExpressionSyntax? ReferenceHandler { get; } + public SyntaxFactoryHelper SyntaxFactory { get; } + + public TypeMappingBuildContext AddIndentation() => new(Source, ReferenceHandler, NameBuilder, SyntaxFactory.AddIndentation()); + /// /// Creates a new scoped name builder, /// builds the name of the source in this new scope @@ -43,7 +59,7 @@ private TypeMappingBuildContext(ExpressionSyntax source, ExpressionSyntax? refer { var scopedNameBuilder = NameBuilder.NewScope(); var scopedSourceName = scopedNameBuilder.New(DefaultSourceName); - var ctx = new TypeMappingBuildContext(sourceBuilder(scopedSourceName), ReferenceHandler, scopedNameBuilder); + var ctx = new TypeMappingBuildContext(sourceBuilder(scopedSourceName), ReferenceHandler, scopedNameBuilder, SyntaxFactory); return (ctx, scopedSourceName); } @@ -53,9 +69,9 @@ private TypeMappingBuildContext(ExpressionSyntax source, ExpressionSyntax? refer return (WithSource(IdentifierName(scopedSourceName)), scopedSourceName); } - public TypeMappingBuildContext WithSource(ExpressionSyntax source) => new(source, ReferenceHandler, NameBuilder); + public TypeMappingBuildContext WithSource(ExpressionSyntax source) => new(source, ReferenceHandler, NameBuilder, SyntaxFactory); public TypeMappingBuildContext WithRefHandler(string refHandler) => WithRefHandler(IdentifierName(refHandler)); - public TypeMappingBuildContext WithRefHandler(ExpressionSyntax refHandler) => new(Source, refHandler, NameBuilder); + public TypeMappingBuildContext WithRefHandler(ExpressionSyntax refHandler) => new(Source, refHandler, NameBuilder, SyntaxFactory); } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedExistingTargetMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedExistingTargetMethodMapping.cs index 1563f67a55..59c32ac45b 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedExistingTargetMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedExistingTargetMethodMapping.cs @@ -4,7 +4,7 @@ using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; @@ -13,8 +13,6 @@ namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; /// public class UserDefinedExistingTargetMethodMapping : MethodMapping, IUserMapping { - private const string NoMappingComment = "// Could not generate mapping"; - private const string ReferenceHandlerTypeName = "global::Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler"; @@ -50,7 +48,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c { if (_delegateMapping == null) { - yield return ThrowStatement(ThrowNotImplementedException()).WithLeadingTrivia(TriviaList(Comment(NoMappingComment))); + yield return ThrowStatement(ctx.SyntaxFactory.ThrowMappingNotImplementedExceptionStatement()); yield break; } @@ -69,7 +67,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // var refHandler = new RefHandler(); var referenceHandlerName = ctx.NameBuilder.New(DefaultReferenceHandlerParameterName); var createRefHandler = CreateInstance(ReferenceHandlerTypeName); - yield return DeclareLocalVariable(referenceHandlerName, createRefHandler); + yield return ctx.SyntaxFactory.DeclareLocalVariable(referenceHandlerName, createRefHandler); ctx = ctx.WithRefHandler(referenceHandlerName); } @@ -91,6 +89,7 @@ internal override void EnableReferenceHandling(INamedTypeSymbol iReferenceHandle private StatementSyntax BuildNullGuard(TypeMappingBuildContext ctx) { - return IfStatement(IfAnyNull((SourceType, ctx.Source), (TargetType, IdentifierName(TargetParameter.Name))), ReturnStatement()); + var condition = IfAnyNull((SourceType, ctx.Source), (TargetType, IdentifierName(TargetParameter.Name))); + return ctx.SyntaxFactory.If(condition, ctx.SyntaxFactory.AddIndentation().Return()); } } diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceGenericTypeMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceGenericTypeMapping.cs index dc5d36098e..3b38606089 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceGenericTypeMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceGenericTypeMapping.cs @@ -3,7 +3,7 @@ using Riok.Mapperly.Emit; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceMethodMapping.cs index 145bcba175..4c4f88e0a4 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceMethodMapping.cs @@ -2,8 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; @@ -12,7 +11,6 @@ namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; /// public class UserDefinedNewInstanceMethodMapping : MethodMapping, IDelegateUserMapping { - private const string NoMappingComment = "// Could not generate mapping"; private const string ReferenceHandlerTypeName = "global::Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler"; @@ -40,7 +38,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c { if (DelegateMapping == null) { - return new[] { ExpressionStatement(ThrowNotImplementedException()).WithLeadingTrivia(TriviaList(Comment(NoMappingComment))), }; + return new[] { ctx.SyntaxFactory.ExpressionStatement(ctx.SyntaxFactory.ThrowMappingNotImplementedExceptionStatement()), }; } // if reference handling is enabled and no reference handler parameter is declared @@ -51,13 +49,13 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // new RefHandler(); var createRefHandler = CreateInstance(ReferenceHandlerTypeName); ctx = ctx.WithRefHandler(createRefHandler); - return new[] { ReturnStatement(DelegateMapping.Build(ctx)) }; + return new[] { ctx.SyntaxFactory.Return(DelegateMapping.Build(ctx)) }; } if (DelegateMapping is MethodMapping delegateMethodMapping) return delegateMethodMapping.BuildBody(ctx); - return new[] { ReturnStatement(DelegateMapping.Build(ctx)) }; + return new[] { ctx.SyntaxFactory.Return(DelegateMapping.Build(ctx)) }; } /// diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeMapping.cs index 5f179e9e6b..ba127b314c 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeMapping.cs @@ -1,9 +1,10 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Emit.Syntax; using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; @@ -61,7 +62,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // var refHandler = new RefHandler(); var referenceHandlerName = ctx.NameBuilder.New(DefaultReferenceHandlerParameterName); var createRefHandler = CreateInstance(ReferenceHandlerTypeName); - yield return DeclareLocalVariable(referenceHandlerName, createRefHandler); + yield return ctx.SyntaxFactory.DeclareLocalVariable(referenceHandlerName, createRefHandler); ctx = ctx.WithRefHandler(referenceHandlerName); } @@ -70,7 +71,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c // _ => throw new ArgumentException(msg, nameof(ctx.Source)), var sourceType = Invocation(MemberAccess(ctx.Source, GetTypeMethodName)); - var fallbackArm = SwitchExpressionArm( + var fallbackArm = SwitchArm( DiscardPattern(), ThrowArgumentExpression( InterpolatedString($"Cannot map {sourceType} to {targetType} as there is no known type mapping"), @@ -83,18 +84,17 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c var arms = _mappings.Select(x => BuildSwitchArm(typeArmContext, typeArmVariableName, x, targetType)); // null => default / throw - arms = arms.Append(SwitchExpressionArm(ConstantPattern(NullLiteral()), NullSubstitute(TargetType, ctx.Source, _nullArm))); - + arms = arms.Append(SwitchArm(ConstantPattern(NullLiteral()), NullSubstitute(TargetType, ctx.Source, _nullArm))); arms = arms.Append(fallbackArm); - var switchExpression = SwitchExpression(ctx.Source).WithArms(CommaSeparatedList(arms, true)); - yield return ReturnStatement(switchExpression); + var switchExpression = ctx.SyntaxFactory.Switch(ctx.Source, arms); + yield return ctx.SyntaxFactory.Return(switchExpression); } protected abstract ExpressionSyntax BuildTargetType(); protected virtual ExpressionSyntax? BuildSwitchArmWhenClause(ExpressionSyntax targetType, RuntimeTargetTypeMapping mapping) { - // targetType.IsAssignableFrom(typeof(ADto)) => MapToADto(x) + // targetType.IsAssignableFrom(typeof(ADto)) return Invocation( MemberAccess(targetType, IsAssignableFromMethodName), TypeOfExpression(FullyQualifiedIdentifier(mapping.Mapping.TargetType.NonNullable())) @@ -115,12 +115,12 @@ ExpressionSyntax targetType { // A x when targetType.IsAssignableFrom(typeof(ADto)) => MapToADto(x), var declaration = DeclarationPattern( - FullyQualifiedIdentifier(mapping.Mapping.SourceType.NonNullable()), + FullyQualifiedIdentifier(mapping.Mapping.SourceType.NonNullable()).AddTrailingSpace(), SingleVariableDesignation(Identifier(typeArmVariableName)) ); var whenCondition = BuildSwitchArmWhenClause(targetType, mapping); - var arm = SwitchExpressionArm(declaration, BuildSwitchArmMapping(mapping, typeArmContext)); - return whenCondition == null ? arm : arm.WithWhenClause(WhenClause(whenCondition)); + var arm = SwitchArm(declaration, BuildSwitchArmMapping(mapping, typeArmContext)); + return whenCondition == null ? arm : arm.WithWhenClause(SwitchWhen(whenCondition)); } private ExpressionSyntax BuildSwitchArmMapping(RuntimeTargetTypeMapping mapping, TypeMappingBuildContext ctx) diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeParameterMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeParameterMapping.cs index 59677216a8..ce15bc8e97 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeParameterMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserDefinedNewInstanceRuntimeTargetTypeParameterMapping.cs @@ -2,7 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs index 801061f9c3..ba91fd0dd6 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs @@ -4,7 +4,7 @@ using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; @@ -42,7 +42,7 @@ public override IEnumerable Build(TypeMappingBuildContext ctx, // we explicitly cast to be able to use the default interface implementation or explicit implementations if (Method.ReceiverType?.TypeKind != TypeKind.Interface) { - yield return ExpressionStatement( + yield return ctx.SyntaxFactory.ExpressionStatement( Invocation( _receiver == null ? IdentifierName(Method.Name) : MemberAccess(_receiver, Method.Name), _sourceParameter.WithArgument(ctx.Source), @@ -58,7 +58,7 @@ public override IEnumerable Build(TypeMappingBuildContext ctx, _receiver != null ? IdentifierName(_receiver) : ThisExpression() ); var method = MemberAccess(ParenthesizedExpression(castedThis), Method.Name); - yield return ExpressionStatement( + yield return ctx.SyntaxFactory.ExpressionStatement( Invocation( method, _sourceParameter.WithArgument(ctx.Source), diff --git a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedMethodMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedMethodMapping.cs index 99deced51b..2c7117261d 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedMethodMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedMethodMapping.cs @@ -3,7 +3,7 @@ using Riok.Mapperly.Helpers; using Riok.Mapperly.Symbols; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceObjectFactory.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceObjectFactory.cs index ba3ea6d72f..0a212ff984 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceObjectFactory.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceObjectFactory.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceTargetObjectFactory.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceTargetObjectFactory.cs index 6c1b1f7cd1..9edb93d48f 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceTargetObjectFactory.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericSourceTargetObjectFactory.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactory.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactory.cs index a5a3adc3d1..5ab2eba93b 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactory.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactory.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactoryWithSource.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactoryWithSource.cs index 607340a362..20d4c8ffbc 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactoryWithSource.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/GenericTargetObjectFactoryWithSource.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactory.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactory.cs index 72d5256cb7..26e22ed9b9 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactory.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactory.cs @@ -1,7 +1,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactory.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactory.cs index dd5e7ec4da..a2e4b4da8e 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactory.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactory.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactoryWithSource.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactoryWithSource.cs index e4b6f855ed..bc8232b192 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactoryWithSource.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/SimpleObjectFactoryWithSource.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Descriptors.ObjectFactories; diff --git a/src/Riok.Mapperly/Emit/ReferenceHandlingSyntaxFactoryHelper.cs b/src/Riok.Mapperly/Emit/ReferenceHandlingSyntaxFactoryHelper.cs index fa1ad114d7..e81c60a582 100644 --- a/src/Riok.Mapperly/Emit/ReferenceHandlingSyntaxFactoryHelper.cs +++ b/src/Riok.Mapperly/Emit/ReferenceHandlingSyntaxFactoryHelper.cs @@ -3,7 +3,7 @@ using Riok.Mapperly.Abstractions.ReferenceHandling; using Riok.Mapperly.Descriptors.Mappings; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Emit; @@ -11,7 +11,7 @@ public static class ReferenceHandlingSyntaxFactoryHelper { private const string ExistingTargetVariableName = "existingTargetReference"; - public static IfStatementSyntax TryGetReference(INewInstanceMapping mapping, TypeMappingBuildContext ctx) + public static IfStatementSyntax TryGetReference(TypeMappingBuildContext ctx, INewInstanceMapping mapping) { // GetReference var refHandler = ctx.ReferenceHandler ?? throw new ArgumentException("Reference handler is not set", nameof(ctx)); @@ -22,17 +22,18 @@ public static IfStatementSyntax TryGetReference(INewInstanceMapping mapping, Typ var method = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, refHandler, methodName); // out var target + var existingTargetVariableName = ctx.NameBuilder.New(ExistingTargetVariableName); var targetArgument = Argument( - DeclarationExpression(VarIdentifier, SingleVariableDesignation(Identifier(ExistingTargetVariableName))) + DeclarationExpression(VarIdentifier, SingleVariableDesignation(Identifier(existingTargetVariableName))) ) - .WithRefOrOutKeyword(Token(SyntaxKind.OutKeyword)); + .WithRefOrOutKeyword(TrailingSpacedToken(SyntaxKind.OutKeyword)); // GetReference(source, out var target) var invocation = Invocation(method, Argument(ctx.Source), targetArgument); // if (_referenceHandler.GetReference(source, out var target)) // return target; - return IfStatement(invocation, ReturnStatement(IdentifierName(ExistingTargetVariableName))); + return ctx.SyntaxFactory.If(invocation, ctx.SyntaxFactory.AddIndentation().Return(IdentifierName(existingTargetVariableName))); } public static ExpressionSyntax SetReference(INewInstanceMapping mapping, TypeMappingBuildContext ctx, ExpressionSyntax target) diff --git a/src/Riok.Mapperly/Emit/SourceEmitter.cs b/src/Riok.Mapperly/Emit/SourceEmitter.cs index 32dfefcd2f..a6d99f1a03 100644 --- a/src/Riok.Mapperly/Emit/SourceEmitter.cs +++ b/src/Riok.Mapperly/Emit/SourceEmitter.cs @@ -1,8 +1,9 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors; +using Riok.Mapperly.Emit.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Emit; @@ -12,50 +13,76 @@ public static class SourceEmitter public static CompilationUnitSyntax Build(MapperDescriptor descriptor, CancellationToken cancellationToken) { - var sourceEmitterContext = new SourceEmitterContext(descriptor.Symbol.IsStatic, descriptor.NameBuilder); - MemberDeclarationSyntax member = ClassDeclaration(descriptor.Syntax.Identifier) - .WithModifiers(descriptor.Syntax.Modifiers) - .WithMembers(List(BuildMembers(descriptor, sourceEmitterContext, cancellationToken))); + var ctx = new SourceEmitterContext(descriptor.Symbol.IsStatic, descriptor.NameBuilder, new SyntaxFactoryHelper()); + ctx = IndentForMapper(ctx, descriptor.Symbol); - member = WrapInClassesAsNeeded(descriptor.Symbol, member); - member = WrapInNamespaceIfNeeded(descriptor.Namespace, member); + var memberCtx = ctx.AddIndentation(); + var members = BuildMembers(memberCtx, descriptor, cancellationToken); + members = members.SeparateByLineFeed(memberCtx.SyntaxFactory.Indentation); + MemberDeclarationSyntax member = ctx.SyntaxFactory.Class(descriptor.Symbol.Name, descriptor.Syntax.Modifiers, List(members)); - return CompilationUnit().WithMembers(SingletonList(member)).WithLeadingTrivia(Comment(AutoGeneratedComment), Nullable(true)); + ctx = ctx.RemoveIndentation(); + member = WrapInClassesAsNeeded(ref ctx, descriptor.Symbol, member); + member = WrapInNamespaceIfNeeded(ctx, descriptor.Namespace, member); + + return CompilationUnit() + .WithMembers(SingletonList(member)) + .WithLeadingTrivia(Comment(AutoGeneratedComment), ElasticCarriageReturnLineFeed, Nullable(true), ElasticCarriageReturnLineFeed); } private static IEnumerable BuildMembers( + SourceEmitterContext ctx, MapperDescriptor descriptor, - SourceEmitterContext sourceEmitterContext, CancellationToken cancellationToken ) { foreach (var mapping in descriptor.MethodTypeMappings) { cancellationToken.ThrowIfCancellationRequested(); - yield return mapping.BuildMethod(sourceEmitterContext); + yield return mapping.BuildMethod(ctx); } } - private static MemberDeclarationSyntax WrapInClassesAsNeeded(INamedTypeSymbol symbol, MemberDeclarationSyntax syntax) + private static MemberDeclarationSyntax WrapInClassesAsNeeded( + ref SourceEmitterContext ctx, + INamedTypeSymbol symbol, + MemberDeclarationSyntax syntax + ) { var containingType = symbol.ContainingType; while (containingType != null) { if (containingType.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not ClassDeclarationSyntax containingTypeSyntax) - return syntax; + break; - syntax = containingTypeSyntax - .WithMembers(SingletonList(syntax)) - .WithAttributeLists(List()) - .WithBaseList(null); + syntax = ctx.SyntaxFactory.Class(containingType.Name, containingTypeSyntax.Modifiers, SingletonList(syntax)); + ctx = ctx.RemoveIndentation(); containingType = containingType.ContainingType; } return syntax; } - private static MemberDeclarationSyntax WrapInNamespaceIfNeeded(string? namespaceName, MemberDeclarationSyntax classDeclaration) + private static MemberDeclarationSyntax WrapInNamespaceIfNeeded( + SourceEmitterContext ctx, + string? namespaceName, + MemberDeclarationSyntax classDeclaration + ) + { + if (namespaceName == null) + return classDeclaration; + + return ctx.SyntaxFactory.Namespace(namespaceName).WithMembers(SingletonList(classDeclaration)); + } + + private static SourceEmitterContext IndentForMapper(SourceEmitterContext ctx, INamedTypeSymbol symbol) { - return namespaceName == null ? classDeclaration : Namespace(namespaceName).WithMembers(SingletonList(classDeclaration)); + while (symbol.ContainingType != null) + { + ctx = ctx.AddIndentation(); + symbol = symbol.ContainingType; + } + + return symbol.ContainingNamespace.ContainingNamespace == null ? ctx : ctx.AddIndentation(); } } diff --git a/src/Riok.Mapperly/Emit/SourceEmitterContext.cs b/src/Riok.Mapperly/Emit/SourceEmitterContext.cs index 7274b004a6..f48c2e40b1 100644 --- a/src/Riok.Mapperly/Emit/SourceEmitterContext.cs +++ b/src/Riok.Mapperly/Emit/SourceEmitterContext.cs @@ -1,16 +1,11 @@ +using Riok.Mapperly.Emit.Syntax; using Riok.Mapperly.Helpers; namespace Riok.Mapperly.Emit; -public class SourceEmitterContext +public record SourceEmitterContext(bool IsStatic, UniqueNameBuilder NameBuilder, SyntaxFactoryHelper SyntaxFactory) { - public SourceEmitterContext(bool isStatic, UniqueNameBuilder nameBuilder) - { - IsStatic = isStatic; - NameBuilder = nameBuilder; - } + public SourceEmitterContext AddIndentation() => this with { SyntaxFactory = SyntaxFactory.AddIndentation() }; - public bool IsStatic { get; } - - public UniqueNameBuilder NameBuilder { get; } + public SourceEmitterContext RemoveIndentation() => this with { SyntaxFactory = SyntaxFactory.RemoveIndentation() }; } diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Array.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Array.cs new file mode 100644 index 0000000000..d67947edf2 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Array.cs @@ -0,0 +1,13 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static ArrayCreationExpressionSyntax CreateArray(ArrayTypeSyntax type) + { + return ArrayCreationExpression(TrailingSpacedToken(SyntaxKind.NewKeyword), type, default); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.BitOperation.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.BitOperation.cs new file mode 100644 index 0000000000..b75724a8cb --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.BitOperation.cs @@ -0,0 +1,13 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static ExpressionSyntax BitwiseAnd(params ExpressionSyntax?[] values) => + BinaryExpression(SyntaxKind.BitwiseAndExpression, values); + + public static ExpressionSyntax BitwiseOr(IEnumerable values) => + BinaryExpression(SyntaxKind.BitwiseOrExpression, values); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs new file mode 100644 index 0000000000..8a4cc48242 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs @@ -0,0 +1,100 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Helpers; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public IfStatementSyntax If(ExpressionSyntax condition, StatementSyntax statement, StatementSyntax? elseStatement = null) + { + var elseClause = elseStatement == null ? null : ElseClause(BlockIfNotReturnOrThrow(elseStatement)).AddLeadingLineFeed(Indentation); + return IfStatement( + LeadingLineFeedToken(SyntaxKind.IfKeyword), + LeadingSpacedToken(SyntaxKind.OpenParenToken), + condition, + Token(SyntaxKind.CloseParenToken), + BlockIfNotReturnOrThrow(statement), + elseClause + ); + } + + public IfStatementSyntax If( + ExpressionSyntax condition, + IEnumerable statements, + IEnumerable? elseStatements = null + ) + { + ElseClauseSyntax? elseClause = null; + if (elseStatements != null) + { + elseClause = ElseClause(Block(elseStatements)).AddLeadingLineFeed(Indentation).AddTrailingLineFeed(Indentation); + } + + return IfStatement( + LeadingLineFeedTrailingSpaceToken(SyntaxKind.IfKeyword), + Token(SyntaxKind.OpenParenToken), + condition, + Token(SyntaxKind.CloseParenToken), + Block(statements), + elseClause + ); + } + + public static ConditionalExpressionSyntax Conditional(ExpressionSyntax condition, ExpressionSyntax whenTrue, ExpressionSyntax whenFalse) + { + return ConditionalExpression( + condition, + SpacedToken(SyntaxKind.QuestionToken), + whenTrue, + SpacedToken(SyntaxKind.ColonToken), + whenFalse + ); + } + + public static ExpressionSyntax Equal(ExpressionSyntax left, ExpressionSyntax right) => + BinaryExpression(SyntaxKind.EqualsExpression, left, right); + + public static ExpressionSyntax IfNoneNull(params (ITypeSymbol Type, ExpressionSyntax Access)[] values) + { + var conditions = values.Where(x => x.Type.IsNullable()).Select(x => IsNotNull(x.Access)); + return And(conditions); + } + + public static ExpressionSyntax IfAnyNull(params (ITypeSymbol Type, ExpressionSyntax Access)[] values) + { + var conditions = values.Where(x => x.Type.IsNullable()).Select(x => IsNull(x.Access)); + return Or(conditions); + } + + public static BinaryExpressionSyntax IsNull(ExpressionSyntax expression) => + BinaryExpression(SyntaxKind.EqualsExpression, expression, NullLiteral()); + + public static BinaryExpressionSyntax IsNotNull(ExpressionSyntax expression) => + BinaryExpression(SyntaxKind.NotEqualsExpression, expression, NullLiteral()); + + public static BinaryExpressionSyntax Is(ExpressionSyntax left, ExpressionSyntax right) => + BinaryExpression(SyntaxKind.IsExpression, left, right); + + 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) => BinaryExpression(SyntaxKind.LogicalAndExpression, values); + + public static ExpressionSyntax Add(ExpressionSyntax one, ExpressionSyntax two) => BinaryExpression(SyntaxKind.AddExpression, one, two); + + private static BinaryExpressionSyntax BinaryExpression(SyntaxKind kind, ExpressionSyntax left, ExpressionSyntax right) + { + var binaryExpression = SyntaxFactory.BinaryExpression(kind, left, right); + return binaryExpression.WithOperatorToken(SpacedToken(binaryExpression.OperatorToken.Kind())); + } + + 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) => BinaryExpression(kind, left, right)); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Exception.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Exception.cs new file mode 100644 index 0000000000..f18db71081 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Exception.cs @@ -0,0 +1,51 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + private const string NoMappingComment = "// Could not generate mapping"; + + private const string ArgumentOutOfRangeExceptionClassName = "System.ArgumentOutOfRangeException"; + private const string ArgumentNullExceptionClassName = "System.ArgumentNullException"; + private const string ArgumentExceptionClassName = "System.ArgumentException"; + private const string NotImplementedExceptionClassName = "System.NotImplementedException"; + private const string NullReferenceExceptionClassName = "System.NullReferenceException"; + + public static ThrowExpressionSyntax ThrowNullReferenceException(string message) => ThrowNullReferenceException(StringLiteral(message)); + + private static ThrowExpressionSyntax ThrowNullReferenceException(ExpressionSyntax arg) => + Throw(NullReferenceExceptionClassName, ArgumentList(arg)); + + public static ThrowExpressionSyntax ThrowArgumentOutOfRangeException(ExpressionSyntax arg, string message) => + Throw(ArgumentOutOfRangeExceptionClassName, ArgumentList(NameOf(arg), arg, StringLiteral(message))); + + public static ThrowExpressionSyntax ThrowArgumentNullException(ExpressionSyntax arg) => + Throw(ArgumentNullExceptionClassName, ArgumentList(NameOf(arg))); + + public static ThrowExpressionSyntax ThrowArgumentExpression(ExpressionSyntax message, ExpressionSyntax arg) => + Throw(ArgumentExceptionClassName, ArgumentList(message, NameOf(arg))); + + public ThrowExpressionSyntax ThrowMappingNotImplementedExceptionStatement() + { + return Throw(NotImplementedExceptionClassName, SyntaxFactory.ArgumentList()).AddLeadingLineComment(NoMappingComment, Indentation); + } + + private ThrowStatementSyntax ThrowStatement(ExpressionSyntax? expression = null) + { + return SyntaxFactory.ThrowStatement( + default, + LeadingLineFeedTrailingSpaceToken(SyntaxKind.ThrowKeyword), + expression, + Token(SyntaxKind.SemicolonToken) + ); + } + + private static ThrowExpressionSyntax Throw(string name, ArgumentListSyntax args) + { + var ex = CreateObject(IdentifierName(name), args); + return ThrowExpression(TrailingSpacedToken(SyntaxKind.ThrowKeyword), ex); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs new file mode 100644 index 0000000000..4a2a0e0afd --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs @@ -0,0 +1,136 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Helpers; +using Riok.Mapperly.Symbols; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static InvocationExpressionSyntax GenericInvocation( + string receiver, + string methodName, + IEnumerable typeParams, + params ExpressionSyntax[] arguments + ) + { + var method = GenericName(methodName).WithTypeArgumentList(TypeArgumentList(typeParams.ToArray())); + return InvocationExpression(MemberAccess(IdentifierName(receiver), method)).WithArgumentList(ArgumentList(arguments)); + } + + public static InvocationExpressionSyntax GenericInvocation( + string methodName, + IEnumerable typeParams, + params ExpressionSyntax[] arguments + ) + { + var method = GenericName(methodName).WithTypeArgumentList(TypeArgumentList(typeParams.ToArray())); + return InvocationExpression(method).WithArgumentList(ArgumentList(arguments)); + } + + public static InvocationExpressionSyntax Invocation(string methodName, params MethodArgument?[] arguments) => + Invocation(IdentifierName(methodName), arguments); + + public static InvocationExpressionSyntax Invocation(ExpressionSyntax method, params MethodArgument?[] arguments) => + Invocation(method, arguments.WhereNotNull().OrderBy(x => x.Parameter.Ordinal).Select(x => x.Argument).ToArray()); + + public static InvocationExpressionSyntax Invocation(string methodName, params ExpressionSyntax[] arguments) => + Invocation(IdentifierName(methodName), arguments); + + public static InvocationExpressionSyntax Invocation(ExpressionSyntax method, params ExpressionSyntax[] arguments) + { + return InvocationExpression(method).WithArgumentList(ArgumentList(arguments)); + } + + public static InvocationExpressionSyntax Invocation(ExpressionSyntax method) => Invocation(method, Array.Empty()); + + public static InvocationExpressionSyntax Invocation(ExpressionSyntax method, params ArgumentSyntax[] arguments) + { + return InvocationExpression(method).WithArgumentList(ArgumentList(arguments)); + } + + public static InvocationExpressionSyntax StaticInvocation(IMethodSymbol method, params ExpressionSyntax[] arguments) + { + var receiver = method.ReceiverType ?? throw new ArgumentException(nameof(method.ReceiverType) + " is null", nameof(method)); + var qualifiedReceiverName = receiver.NonNullable().FullyQualifiedIdentifierName(); + return StaticInvocation(qualifiedReceiverName, method.Name, arguments); + } + + public static InvocationExpressionSyntax StaticInvocation(IMethodSymbol method, params ArgumentSyntax[] arguments) + { + var receiver = method.ReceiverType ?? throw new ArgumentException(nameof(method.ReceiverType) + " is null", nameof(method)); + var qualifiedReceiverName = receiver.NonNullable().FullyQualifiedIdentifierName(); + + var receiverTypeIdentifier = IdentifierName(qualifiedReceiverName); + var methodAccess = MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + receiverTypeIdentifier, + IdentifierName(method.Name) + ); + return InvocationExpression(methodAccess).WithArgumentList(ArgumentList(arguments)); + } + + public static TypeParameterListSyntax TypeParameterList(params ITypeParameterSymbol?[] parameters) + { + var typeParameters = parameters.WhereNotNull().OrderBy(x => x.Ordinal).Select(x => TypeParameter(x.Name)); + return SyntaxFactory.TypeParameterList(CommaSeparatedList(typeParameters)); + } + + public static ParameterListSyntax ParameterList(bool extensionMethod, params MethodParameter?[] parameters) + { + var parameterSyntaxes = parameters + .WhereNotNull() + .DistinctBy(x => x.Ordinal) + .OrderBy(x => x.Ordinal) + .Select(p => Parameter(extensionMethod, p)); + return SyntaxFactory.ParameterList(CommaSeparatedList(parameterSyntaxes)); + } + + private static ParameterSyntax Parameter(bool addThisKeyword, MethodParameter parameter) + { + var param = SyntaxFactory + .Parameter(Identifier(parameter.Name)) + .WithType(FullyQualifiedIdentifier(parameter.Type).AddTrailingSpace()); + + if (addThisKeyword && parameter.Ordinal == 0) + { + param = param.WithModifiers(TokenList(TrailingSpacedToken(SyntaxKind.ThisKeyword))); + } + + return param; + } + + public static InvocationExpressionSyntax StaticInvocation(string receiverType, string methodName, params ExpressionSyntax[] arguments) + { + var receiverTypeIdentifier = IdentifierName(receiverType); + var methodAccess = MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + receiverTypeIdentifier, + IdentifierName(methodName) + ); + return InvocationExpression(methodAccess).WithArgumentList(ArgumentList(arguments)); + } + + public static string StaticMethodString(IMethodSymbol method) + { + var receiver = method.ReceiverType ?? throw new ArgumentException(nameof(method.ReceiverType) + " is null", nameof(method)); + var qualifiedReceiverName = receiver.NonNullable().FullyQualifiedIdentifierName(); + return $"{qualifiedReceiverName}.{method.Name}"; + } + + private static ArgumentListSyntax ArgumentList(params ExpressionSyntax[] argSyntaxes) => + SyntaxFactory.ArgumentList(CommaSeparatedList(argSyntaxes.Select(Argument))); + + public static TypeArgumentListSyntax TypeArgumentList(params TypeSyntax[] argSyntaxes) => + SyntaxFactory.TypeArgumentList(CommaSeparatedList(argSyntaxes)); + + public static TypeArgumentListSyntax TypeArgumentList(IEnumerable argSyntaxes) => + SyntaxFactory.TypeArgumentList(CommaSeparatedList(argSyntaxes)); + + private static ArgumentListSyntax ArgumentList(params ArgumentSyntax[] args) => SyntaxFactory.ArgumentList(CommaSeparatedList(args)); + + private static ArgumentListSyntax ArgumentList(IEnumerable args) => + SyntaxFactory.ArgumentList(CommaSeparatedList(args)); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Lambda.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Lambda.cs new file mode 100644 index 0000000000..cc94f5f15f --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Lambda.cs @@ -0,0 +1,15 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static SimpleLambdaExpressionSyntax Lambda(string paramName, ExpressionSyntax body) + { + return SimpleLambdaExpression(SyntaxFactory.Parameter(Identifier(paramName))) + .WithExpressionBody(body) + .WithArrowToken(SpacedToken(SyntaxKind.EqualsGreaterThanToken)); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Literal.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Literal.cs new file mode 100644 index 0000000000..22ed0af275 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Literal.cs @@ -0,0 +1,20 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static LiteralExpressionSyntax DefaultLiteral() => LiteralExpression(SyntaxKind.DefaultLiteralExpression); + + public static LiteralExpressionSyntax NullLiteral() => LiteralExpression(SyntaxKind.NullLiteralExpression); + + public static LiteralExpressionSyntax BooleanLiteral(bool b) => + LiteralExpression(b ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression); + + public static LiteralExpressionSyntax IntLiteral(int i) => LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i)); + + private static LiteralExpressionSyntax StringLiteral(string content) => + LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(content)); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Loop.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Loop.cs new file mode 100644 index 0000000000..a17ac14132 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Loop.cs @@ -0,0 +1,52 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public ForEachStatementSyntax ForEach( + string identifierName, + ExpressionSyntax enumerableExpression, + params ExpressionSyntax[] expressions + ) + { + return ForEachStatement( + default, + default, + LeadingLineFeedTrailingSpaceToken(SyntaxKind.ForEachKeyword), + Token(SyntaxKind.OpenParenToken), + VarIdentifier, + Identifier(identifierName), + SpacedToken(SyntaxKind.InKeyword), + enumerableExpression, + Token(SyntaxKind.CloseParenToken), + Block(expressions) + ); + } + + public ForStatementSyntax IncrementalForLoop( + string counterName, + ExpressionSyntax maxValueExclusive, + params ExpressionSyntax[] expressions + ) + { + var counterDeclaration = DeclareVariable(counterName, IntLiteral(0)); + var counterIncrement = PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, IdentifierName(counterName)); + var condition = BinaryExpression(SyntaxKind.LessThanExpression, IdentifierName(counterName), maxValueExclusive); + return ForStatement( + default, + LeadingLineFeedTrailingSpaceToken(SyntaxKind.ForKeyword), + Token(SyntaxKind.OpenParenToken), + counterDeclaration, + default, + TrailingSpacedToken(SyntaxKind.SemicolonToken), + condition, + TrailingSpacedToken(SyntaxKind.SemicolonToken), + SingletonSeparatedList(counterIncrement), + Token(SyntaxKind.CloseParenToken), + Block(expressions) + ); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.MemberAccess.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.MemberAccess.cs new file mode 100644 index 0000000000..f7459447d7 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.MemberAccess.cs @@ -0,0 +1,23 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static MemberAccessExpressionSyntax MemberAccess(string identifierName, string propertyIdentifierName) => + MemberAccess(IdentifierName(identifierName), propertyIdentifierName); + + public static MemberAccessExpressionSyntax MemberAccess(ExpressionSyntax idExpression, string propertyIdentifierName) => + MemberAccess(idExpression, IdentifierName(propertyIdentifierName)); + + private static MemberAccessExpressionSyntax MemberAccess(ExpressionSyntax idExpression, SimpleNameSyntax property) => + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, idExpression, property); + + public static ElementAccessExpressionSyntax ElementAccess(ExpressionSyntax idExpression, ExpressionSyntax index) => + ElementAccessExpression(idExpression).WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index)))); + + public static ConditionalAccessExpressionSyntax ConditionalAccess(ExpressionSyntax idExpression, string propertyIdentifierName) => + ConditionalAccessExpression(idExpression, MemberBindingExpression(IdentifierName(propertyIdentifierName))); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.New.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.New.cs new file mode 100644 index 0000000000..65f8c40b54 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.New.cs @@ -0,0 +1,56 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static ObjectCreationExpressionSyntax CreateInstance(string typeName) + { + var type = IdentifierName(typeName); + return CreateObject(type, SyntaxFactory.ArgumentList()); + } + + public static ObjectCreationExpressionSyntax CreateGenericInstance( + string typeName, + TypeArgumentListSyntax typeArguments, + IEnumerable arguments + ) + { + var type = GenericName(typeName).WithTypeArgumentList(typeArguments); + return CreateObject(type, ArgumentList(arguments)); + } + + public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol) + { + var type = NonNullableIdentifier(typeSymbol); + return CreateObject(type, SyntaxFactory.ArgumentList()); + } + + public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol, params ExpressionSyntax[] args) + { + var type = NonNullableIdentifier(typeSymbol); + return CreateObject(type, ArgumentList(args)); + } + + public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol, params ArgumentSyntax[] args) + { + var type = NonNullableIdentifier(typeSymbol); + return CreateObject(type, ArgumentList(args)); + } + + public InitializerExpressionSyntax ObjectInitializer(params ExpressionSyntax[] expressions) + { + return InitializerExpression( + SyntaxKind.ObjectInitializerExpression, + LeadingLineFeedToken(SyntaxKind.OpenBraceToken).AddTrailingLineFeed(Indentation + 1), + AddIndentation().CommaLineFeedSeparatedList(expressions), + LeadingLineFeedToken(SyntaxKind.CloseBraceToken) + ); + } + + private static ObjectCreationExpressionSyntax CreateObject(TypeSyntax type, ArgumentListSyntax argumentList) => + ObjectCreationExpression(TrailingSpacedToken(SyntaxKind.NewKeyword), type, argumentList, default); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs new file mode 100644 index 0000000000..847bc1fe5b --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs @@ -0,0 +1,59 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Descriptors.Mappings; +using Riok.Mapperly.Helpers; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static AssignmentExpressionSyntax CoalesceAssignment(ExpressionSyntax target, ExpressionSyntax source) + { + return AssignmentExpression( + SyntaxKind.CoalesceAssignmentExpression, + target, + SpacedToken(SyntaxKind.QuestionQuestionEqualsToken), + source + ); + } + + public static SyntaxTrivia Nullable(bool enabled) + { + return Trivia(NullableDirectiveTrivia(LeadingSpacedToken(enabled ? SyntaxKind.EnableKeyword : SyntaxKind.DisableKeyword), true)); + } + + public static BinaryExpressionSyntax Coalesce(ExpressionSyntax expr, ExpressionSyntax coalesceExpr) => + BinaryExpression(SyntaxKind.CoalesceExpression, expr, coalesceExpr); + + public static IdentifierNameSyntax NonNullableIdentifier(ITypeSymbol t) => FullyQualifiedIdentifier(t.NonNullable()); + + public static ExpressionSyntax NullSubstitute(ITypeSymbol t, ExpressionSyntax argument, NullFallbackValue nullFallbackValue) + { + return nullFallbackValue switch + { + NullFallbackValue.Default => DefaultLiteral(), + NullFallbackValue.EmptyString => StringLiteral(string.Empty), + NullFallbackValue.CreateInstance => CreateInstance(t), + _ when argument is ElementAccessExpressionSyntax memberAccess + => ThrowNullReferenceException( + InterpolatedString( + $"Sequence {NameOf(memberAccess.Expression)}, contained a null value at index {memberAccess.ArgumentList.Arguments[0].Expression}." + ) + ), + _ => ThrowArgumentNullException(argument), + }; + } + + public StatementSyntax IfNullReturnOrThrow(ExpressionSyntax expression, ExpressionSyntax? returnOrThrowExpression = null) + { + StatementSyntax ifExpression = returnOrThrowExpression switch + { + ThrowExpressionSyntax throwSyntax => ThrowStatement(throwSyntax.Expression), + _ => AddIndentation().Return(returnOrThrowExpression), + }; + + return If(IsNull(expression), ifExpression); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Pattern.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Pattern.cs new file mode 100644 index 0000000000..4bad9249ca --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Pattern.cs @@ -0,0 +1,24 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Helpers; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public static PatternSyntax OrPattern(IEnumerable values) => + values + .WhereNotNull() + .Select(ConstantPattern) + .Aggregate((left, right) => BinaryPattern(SyntaxKind.OrPattern, left, right)); + + public static IsPatternExpressionSyntax IsPattern(ExpressionSyntax expression, PatternSyntax pattern) => + IsPatternExpression(expression, SpacedToken(SyntaxKind.IsKeyword), pattern); + + private static BinaryPatternSyntax BinaryPattern(SyntaxKind kind, PatternSyntax left, PatternSyntax right) + { + var binaryPattern = SyntaxFactory.BinaryPattern(kind, left, right); + return binaryPattern.WithOperatorToken(SpacedToken(binaryPattern.OperatorToken.Kind())); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Return.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Return.cs new file mode 100644 index 0000000000..fb5fd6b8c4 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Return.cs @@ -0,0 +1,22 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public ReturnStatementSyntax Return(ExpressionSyntax? expression = default) + { + return expression == null + ? ReturnStatement(default, LeadingLineFeedToken(SyntaxKind.ReturnKeyword), null, Token(SyntaxKind.SemicolonToken)) + : ReturnStatement( + default, + LeadingLineFeedTrailingSpaceToken(SyntaxKind.ReturnKeyword), + expression, + Token(SyntaxKind.SemicolonToken) + ); + } + + public StatementSyntax ReturnVariable(string identifierName) => Return(IdentifierName(identifierName)); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.String.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.String.cs new file mode 100644 index 0000000000..ccba9110c3 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.String.cs @@ -0,0 +1,61 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Helpers; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + private static readonly IdentifierNameSyntax _nameofIdentifier = IdentifierName("nameof"); + + private static readonly Regex FormattableStringPlaceholder = new Regex( + @"\{(?\d+)\}", + RegexOptions.Compiled, + TimeSpan.FromMilliseconds(100) + ); + + public static InvocationExpressionSyntax NameOf(ExpressionSyntax expression) => Invocation(_nameofIdentifier, expression); + + public static IdentifierNameSyntax FullyQualifiedIdentifier(ITypeSymbol typeSymbol) => + IdentifierName(typeSymbol.FullyQualifiedIdentifierName()); + + public static InterpolatedStringExpressionSyntax InterpolatedString(FormattableString str) + { + var matches = FormattableStringPlaceholder.Matches(str.Format); + var contents = new List(); + var previousIndex = 0; + foreach (Match match in matches) + { + var text = str.Format.Substring(previousIndex, match.Index - previousIndex); + contents.Add(InterpolatedStringText(text)); + + var arg = str.GetArgument(int.Parse(match.Groups["placeholder"].Value, CultureInfo.InvariantCulture)); + InterpolatedStringContentSyntax argSyntax = arg switch + { + ExpressionSyntax x => Interpolation(x), + string x => InterpolatedStringText(x), + _ => throw new InvalidOperationException(arg?.GetType() + " cannot be converted into a string interpolation"), + }; + contents.Add(argSyntax); + previousIndex = match.Index + match.Length; + } + + if (previousIndex <= str.Format.Length) + { + contents.Add(InterpolatedStringText(str.Format.Substring(previousIndex))); + } + + return InterpolatedStringExpression(Token(SyntaxKind.InterpolatedStringStartToken)).WithContents(List(contents)); + } + + private static InterpolatedStringTextSyntax InterpolatedStringText(string text) + { + return SyntaxFactory.InterpolatedStringText( + Token(SyntaxTriviaList.Empty, SyntaxKind.InterpolatedStringTextToken, text, text, SyntaxTriviaList.Empty) + ); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Switch.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Switch.cs new file mode 100644 index 0000000000..dfcc32c433 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Switch.cs @@ -0,0 +1,26 @@ +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public SwitchExpressionSyntax Switch(ExpressionSyntax governingExpression, IEnumerable arms) + { + return SwitchExpression( + governingExpression, + LeadingSpacedToken(SyntaxKind.SwitchKeyword), + Token(SyntaxKind.OpenBraceToken).AddLeadingLineFeed(Indentation).AddTrailingLineFeed(Indentation + 1), + AddIndentation().CommaLineFeedSeparatedList(arms), + LeadingLineFeedToken(SyntaxKind.CloseBraceToken) + ); + } + + public static SwitchExpressionArmSyntax SwitchArm(PatternSyntax pattern, ExpressionSyntax expression) + { + return SwitchExpressionArm(pattern, default, SpacedToken(SyntaxKind.EqualsGreaterThanToken), expression); + } + + public static WhenClauseSyntax SwitchWhen(ExpressionSyntax condition) => WhenClause(SpacedToken(SyntaxKind.WhenKeyword), condition); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.SymbolDeclaration.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.SymbolDeclaration.cs new file mode 100644 index 0000000000..40318d5435 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.SymbolDeclaration.cs @@ -0,0 +1,29 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + public NamespaceDeclarationSyntax Namespace(string ns) + { + return NamespaceDeclaration(IdentifierName(ns)) + .WithNamespaceKeyword(TrailingSpacedToken(SyntaxKind.NamespaceKeyword)) + .WithOpenBraceToken(LeadingLineFeedToken(SyntaxKind.OpenBraceToken)) + .WithCloseBraceToken(LeadingLineFeedToken(SyntaxKind.CloseBraceToken)); + } + + public ClassDeclarationSyntax Class(string name, SyntaxTokenList modifiers, SyntaxList members) + { + return ClassDeclaration(Identifier(name)) + .WithModifiers(modifiers) + .WithMembers(members) + .WithoutTrivia() + .WithKeyword(TrailingSpacedToken(SyntaxKind.ClassKeyword)) + .WithOpenBraceToken(LeadingLineFeedToken(SyntaxKind.OpenBraceToken)) + .WithCloseBraceToken(LeadingLineFeedToken(SyntaxKind.CloseBraceToken)) + .AddLeadingLineFeed(Indentation); + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Token.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Token.cs new file mode 100644 index 0000000000..dcedb4d0bd --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Token.cs @@ -0,0 +1,26 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +public partial struct SyntaxFactoryHelper +{ + private static readonly SyntaxTriviaList _spaceTriviaList = SyntaxTriviaList.Create(ElasticSpace); + + private SyntaxToken LeadingLineFeedToken(SyntaxKind kind) + { + return Token(SyntaxTriviaList.Empty.AddLineFeedAndIndentation(Indentation), kind, SyntaxTriviaList.Empty); + } + + private SyntaxToken LeadingLineFeedTrailingSpaceToken(SyntaxKind kind) + { + return Token(SyntaxTriviaList.Empty.AddLineFeedAndIndentation(Indentation), kind, _spaceTriviaList); + } + + private static SyntaxToken LeadingSpacedToken(SyntaxKind kind) => Token(_spaceTriviaList, kind, SyntaxTriviaList.Empty); + + public static SyntaxToken TrailingSpacedToken(SyntaxKind kind) => Token(SyntaxTriviaList.Empty, kind, _spaceTriviaList); + + private static SyntaxToken SpacedToken(SyntaxKind kind) => Token(_spaceTriviaList, kind, _spaceTriviaList); +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.cs new file mode 100644 index 0000000000..aabb9ccec9 --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.cs @@ -0,0 +1,115 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +// useful to create syntax factories: https://roslynquoter.azurewebsites.net/ and https://sharplab.io/ +public readonly partial struct SyntaxFactoryHelper +{ + public static readonly IdentifierNameSyntax VarIdentifier = IdentifierName("var").AddTrailingSpace(); + + private SyntaxFactoryHelper(int indentation) + { + Indentation = indentation; + } + + public int Indentation { get; } + + public SyntaxFactoryHelper AddIndentation() => new(Indentation + 1); + + public SyntaxFactoryHelper RemoveIndentation() => new(Indentation - 1); + + public static SyntaxToken Accessibility(Accessibility accessibility) + { + return accessibility switch + { + Microsoft.CodeAnalysis.Accessibility.Private => TrailingSpacedToken(SyntaxKind.PrivateKeyword), + Microsoft.CodeAnalysis.Accessibility.Protected => TrailingSpacedToken(SyntaxKind.ProtectedKeyword), + Microsoft.CodeAnalysis.Accessibility.Internal => TrailingSpacedToken(SyntaxKind.InternalKeyword), + Microsoft.CodeAnalysis.Accessibility.Public => TrailingSpacedToken(SyntaxKind.PublicKeyword), + _ => throw new ArgumentOutOfRangeException(nameof(accessibility), accessibility, null) + }; + } + + public static AssignmentExpressionSyntax Assignment(ExpressionSyntax target, ExpressionSyntax source) + { + return AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, target, SpacedToken(SyntaxKind.EqualsToken), source); + } + + public BlockSyntax Block(IEnumerable statements) + { + return SyntaxFactory + .Block(statements) + .WithOpenBraceToken(LeadingLineFeedToken(SyntaxKind.OpenBraceToken)) + .WithCloseBraceToken(LeadingLineFeedToken(SyntaxKind.CloseBraceToken)); + } + + private StatementSyntax BlockIfNotReturnOrThrow(StatementSyntax statement) + { + return statement.IsKind(SyntaxKind.ReturnStatement) || statement.IsKind(SyntaxKind.ThrowStatement) + ? statement + : Block(new[] { statement }); + } + + private BlockSyntax Block(IEnumerable statements) => Block(statements.Select(AddIndentation().ExpressionStatement)); + + public IReadOnlyCollection SingleStatement(ExpressionSyntax expression) => new[] { ExpressionStatement(expression) }; + + public ExpressionStatementSyntax ExpressionStatement(ExpressionSyntax expression) => + SyntaxFactory.ExpressionStatement(expression).AddLeadingLineFeed(Indentation); + + public static SeparatedSyntaxList CommaSeparatedList(IEnumerable nodes, bool insertTrailingComma = false) + where T : SyntaxNode + { + var sep = TrailingSpacedToken(SyntaxKind.CommaToken); + var joinedNodes = Join(sep, insertTrailingComma, nodes); + return SeparatedList(joinedNodes); + } + + private SeparatedSyntaxList CommaLineFeedSeparatedList(IEnumerable nodes) + where T : SyntaxNode + { + // append a comma at the end but no line feed + var sep = Token(SyntaxKind.CommaToken).AddTrailingLineFeed(Indentation); + var joinedNodes = Join(sep, false, nodes).Append(Token(SyntaxKind.CommaToken)); + return SeparatedList(joinedNodes); + } + + public static VariableDeclarationSyntax DeclareVariable(string variableName, ExpressionSyntax initializationValue) + { + var initializer = EqualsValueClause(SpacedToken(SyntaxKind.EqualsToken), initializationValue); + var declarator = VariableDeclarator(Identifier(variableName)).WithInitializer(initializer); + return VariableDeclaration(VarIdentifier).WithVariables(SingletonSeparatedList(declarator)); + } + + public LocalDeclarationStatementSyntax DeclareLocalVariable(string variableName, ExpressionSyntax initializationValue) + { + var variableDeclaration = DeclareVariable(variableName, initializationValue); + return LocalDeclarationStatement(variableDeclaration).AddLeadingLineFeed(Indentation); + } + + public static NameColonSyntax SpacedNameColon(string name) => + NameColon(IdentifierName(name), TrailingSpacedToken(SyntaxKind.ColonToken)); + + private static IEnumerable Join(SyntaxToken sep, bool insertTrailingSeparator, IEnumerable nodes) + { + using var enumerator = nodes.GetEnumerator(); + if (!enumerator.MoveNext()) + yield break; + + yield return enumerator.Current; + + while (enumerator.MoveNext()) + { + yield return sep; + yield return enumerator.Current; + } + + if (insertTrailingSeparator) + { + yield return sep; + } + } +} diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxIndentationExtensions.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxIndentationExtensions.cs new file mode 100644 index 0000000000..cf3d87d36c --- /dev/null +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxIndentationExtensions.cs @@ -0,0 +1,106 @@ +using System.Collections.Concurrent; +using Microsoft.CodeAnalysis; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Riok.Mapperly.Emit.Syntax; + +internal static class SyntaxIndentationExtensions +{ + private static readonly SyntaxTrivia _indentation = ElasticWhitespace(" "); + private static readonly ConcurrentDictionary _indentationLineFeedSyntaxTriviaCache = new(); + + public static TSyntax AddLeadingLineComment(this TSyntax syntax, string comment, int indentation) + where TSyntax : SyntaxNode + { + var trivia = syntax.GetLeadingTrivia(); + trivia = trivia.AddLineFeedAndIndentation(indentation); + trivia = trivia.Insert(0, Comment(comment)); + return syntax.WithLeadingTrivia(trivia); + } + + public static IEnumerable SeparateByLineFeed(this IEnumerable syntax, int indentation) + where TSyntax : SyntaxNode + { + using var enumerator = syntax.GetEnumerator(); + if (!enumerator.MoveNext()) + yield break; + + yield return enumerator.Current!.AddLeadingLineFeed(indentation); + + while (enumerator.MoveNext()) + { + var node = enumerator.Current!; + yield return node.AddLeadingLineFeed(indentation).AddLeadingLineFeed(0); + } + } + + /// + /// Adds a leading line feed to the first found trivia. + /// If the first token is known by the caller, use for the first token instead + /// (should have better performance) + /// + /// The syntax. + /// The indentation. + /// The type of the syntax. + /// The updated syntax. + public static TSyntax AddLeadingLineFeed(this TSyntax syntax, int indentation) + where TSyntax : SyntaxNode + { + var trivia = syntax.GetLeadingTrivia(); + trivia = trivia.AddLineFeedAndIndentation(indentation); + return syntax.WithLeadingTrivia(trivia); + } + + /// + /// Adds a trailing line feed to the last found trivia. + /// If the last token is known by the caller, use for the last token instead + /// (should have better performance). + /// + /// The syntax. + /// The indentation. + /// The type of the syntax. + /// The updated syntax. + public static TSyntax AddTrailingLineFeed(this TSyntax syntax, int indentation) + where TSyntax : SyntaxNode + { + var trivia = syntax.GetTrailingTrivia(); + trivia = trivia.AddLineFeedAndIndentation(indentation); + return syntax.WithTrailingTrivia(trivia); + } + + public static TSyntax AddTrailingSpace(this TSyntax syntax) + where TSyntax : SyntaxNode => syntax.WithTrailingTrivia(syntax.GetTrailingTrivia().Add(ElasticSpace)); + + public static SyntaxToken AddLeadingLineFeed(this SyntaxToken token, int indentation) + { + var trivia = token.LeadingTrivia.AddLineFeedAndIndentation(indentation); + return token.WithLeadingTrivia(trivia); + } + + public static SyntaxToken AddTrailingLineFeed(this SyntaxToken token, int indentation) + { + var trivia = token.TrailingTrivia.AddLineFeedAndIndentation(indentation); + return token.WithTrailingTrivia(trivia); + } + + public static SyntaxTriviaList AddLineFeedAndIndentation(this SyntaxTriviaList trivia, int indentation) + { + var triviaToInsert = _indentationLineFeedSyntaxTriviaCache.GetOrAdd( + indentation, + static indentationLevel => + { + // +1 for the new line at the beginning of the indentation + var toInsert = new SyntaxTrivia[indentationLevel + 1]; + toInsert[0] = ElasticCarriageReturnLineFeed; + for (var i = 1; i < toInsert.Length; i++) + { + toInsert[i] = _indentation; + } + + return toInsert; + } + ); + + return trivia.InsertRange(0, triviaToInsert); + } +} diff --git a/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs b/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs deleted file mode 100644 index 5d5969ecf3..0000000000 --- a/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs +++ /dev/null @@ -1,454 +0,0 @@ -using System.Globalization; -using System.Text.RegularExpressions; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Riok.Mapperly.Descriptors.Mappings; -using Riok.Mapperly.Helpers; -using Riok.Mapperly.Symbols; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace Riok.Mapperly.Emit; - -// created with the help of https://roslynquoter.azurewebsites.net/ and https://sharplab.io/ -public static class SyntaxFactoryHelper -{ - private const string ArgumentOutOfRangeExceptionClassName = "System.ArgumentOutOfRangeException"; - private const string ArgumentNullExceptionClassName = "System.ArgumentNullException"; - private const string ArgumentExceptionClassName = "System.ArgumentException"; - private const string NotImplementedExceptionClassName = "System.NotImplementedException"; - private const string NullReferenceExceptionClassName = "System.NullReferenceException"; - - public static readonly IdentifierNameSyntax VarIdentifier = IdentifierName("var"); - private static readonly IdentifierNameSyntax _nameofIdentifier = IdentifierName("nameof"); - - private static readonly Regex FormattableStringPlaceholder = new Regex( - @"\{(?\d+)\}", - RegexOptions.Compiled, - TimeSpan.FromMilliseconds(100) - ); - - public static SyntaxToken Accessibility(Accessibility accessibility) - { - return accessibility switch - { - Microsoft.CodeAnalysis.Accessibility.Private => Token(SyntaxKind.PrivateKeyword), - Microsoft.CodeAnalysis.Accessibility.Protected => Token(SyntaxKind.ProtectedKeyword), - Microsoft.CodeAnalysis.Accessibility.Internal => Token(SyntaxKind.InternalKeyword), - Microsoft.CodeAnalysis.Accessibility.Public => Token(SyntaxKind.PublicKeyword), - _ => throw new ArgumentOutOfRangeException(nameof(accessibility), accessibility, null) - }; - } - - public static BinaryExpressionSyntax Coalesce(ExpressionSyntax expr, ExpressionSyntax coalesceExpr) => - SyntaxFactory.BinaryExpression(SyntaxKind.CoalesceExpression, expr, coalesceExpr); - - 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) => 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) - { - var conditions = values.Where(x => x.Type.IsNullable()).Select(x => IsNotNull(x.Access)); - return And(conditions); - } - - public static ExpressionSyntax IfAnyNull(params (ITypeSymbol Type, ExpressionSyntax Access)[] values) - { - var conditions = values.Where(x => x.Type.IsNullable()).Select(x => IsNull(x.Access)); - return Or(conditions); - } - - public static BinaryExpressionSyntax IsNull(ExpressionSyntax expression) => - SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, expression, NullLiteral()); - - public static BinaryExpressionSyntax IsNotNull(ExpressionSyntax expression) => - SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, expression, NullLiteral()); - - public static ExpressionSyntax NullSubstitute(ITypeSymbol t, ExpressionSyntax argument, NullFallbackValue nullFallbackValue) - { - return nullFallbackValue switch - { - NullFallbackValue.Default => DefaultLiteral(), - NullFallbackValue.EmptyString => StringLiteral(string.Empty), - NullFallbackValue.CreateInstance => CreateInstance(t), - _ when argument is ElementAccessExpressionSyntax memberAccess - => ThrowNullReferenceException( - InterpolatedString( - $"Sequence {NameOf(memberAccess.Expression)}, contained a null value at index {memberAccess.ArgumentList.Arguments[0].Expression}." - ) - ), - _ => ThrowArgumentNullException(argument), - }; - } - - public static StatementSyntax IfNullReturnOrThrow(ExpressionSyntax expression, ExpressionSyntax? returnOrThrowExpression = null) - { - StatementSyntax ifExpression = returnOrThrowExpression switch - { - ThrowExpressionSyntax throwSyntax => ThrowStatement(throwSyntax.Expression), - _ => ReturnStatement(returnOrThrowExpression), - }; - - return IfStatement(IsNull(expression), ifExpression); - } - - public static InterpolatedStringExpressionSyntax InterpolatedString(FormattableString str) - { - var matches = FormattableStringPlaceholder.Matches(str.Format); - var contents = new List(); - var previousIndex = 0; - foreach (Match match in matches) - { - var text = str.Format.Substring(previousIndex, match.Index - previousIndex); - contents.Add(InterpolatedStringText(text)); - - var arg = str.GetArgument(int.Parse(match.Groups["placeholder"].Value, CultureInfo.InvariantCulture)); - InterpolatedStringContentSyntax argSyntax = arg switch - { - ExpressionSyntax x => Interpolation(x), - string x => InterpolatedStringText(x), - _ => throw new InvalidOperationException(arg?.GetType() + " cannot be converted into a string interpolation"), - }; - contents.Add(argSyntax); - previousIndex = match.Index + match.Length; - } - - if (previousIndex <= str.Format.Length) - { - contents.Add(InterpolatedStringText(str.Format.Substring(previousIndex))); - } - - return InterpolatedStringExpression(Token(SyntaxKind.InterpolatedStringStartToken)).WithContents(List(contents)); - } - - public static LiteralExpressionSyntax DefaultLiteral() => LiteralExpression(SyntaxKind.DefaultLiteralExpression); - - public static LiteralExpressionSyntax NullLiteral() => LiteralExpression(SyntaxKind.NullLiteralExpression); - - public static LiteralExpressionSyntax StringLiteral(string content) => - LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(content)); - - public static LiteralExpressionSyntax BooleanLiteral(bool b) => - LiteralExpression(b ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression); - - public static LiteralExpressionSyntax IntLiteral(int i) => LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i)); - - public static StatementSyntax ReturnVariable(string identifierName) => ReturnStatement(IdentifierName(identifierName)); - - public static MemberAccessExpressionSyntax MemberAccess(string identifierName, string propertyIdentifierName) => - MemberAccess(IdentifierName(identifierName), propertyIdentifierName); - - public static MemberAccessExpressionSyntax MemberAccess(ExpressionSyntax idExpression, string propertyIdentifierName) => - MemberAccess(idExpression, IdentifierName(propertyIdentifierName)); - - public static MemberAccessExpressionSyntax MemberAccess(ExpressionSyntax idExpression, SimpleNameSyntax property) => - MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, idExpression, property); - - public static AssignmentExpressionSyntax Assignment( - ExpressionSyntax target, - ExpressionSyntax source, - SyntaxKind kind = SyntaxKind.SimpleAssignmentExpression - ) => AssignmentExpression(kind, target, source); - - public static ElementAccessExpressionSyntax ElementAccess(ExpressionSyntax idExpression, ExpressionSyntax index) => - ElementAccessExpression(idExpression).WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index)))); - - public static ConditionalAccessExpressionSyntax ConditionalAccess(ExpressionSyntax idExpression, string propertyIdentifierName) => - ConditionalAccessExpression(idExpression, MemberBindingExpression(IdentifierName(propertyIdentifierName))); - - public static InvocationExpressionSyntax NameOf(ExpressionSyntax expression) => Invocation(_nameofIdentifier, expression); - - public static ThrowExpressionSyntax ThrowNullReferenceException(string message) - { - return ThrowExpression( - ObjectCreationExpression(IdentifierName(NullReferenceExceptionClassName)).WithArgumentList(ArgumentList(StringLiteral(message))) - ); - } - - public static ThrowExpressionSyntax ThrowNullReferenceException(ExpressionSyntax arg) - { - return ThrowExpression( - ObjectCreationExpression(IdentifierName(NullReferenceExceptionClassName)).WithArgumentList(ArgumentList(arg)) - ); - } - - public static ThrowExpressionSyntax ThrowArgumentOutOfRangeException(ExpressionSyntax arg, string message) - { - return ThrowExpression( - ObjectCreationExpression(IdentifierName(ArgumentOutOfRangeExceptionClassName)) - .WithArgumentList(ArgumentList(NameOf(arg), arg, StringLiteral(message))) - ); - } - - public static ThrowExpressionSyntax ThrowArgumentNullException(ExpressionSyntax arg) - { - return ThrowExpression( - ObjectCreationExpression(IdentifierName(ArgumentNullExceptionClassName)).WithArgumentList(ArgumentList(NameOf(arg))) - ); - } - - public static ThrowExpressionSyntax ThrowArgumentExpression(ExpressionSyntax message, ExpressionSyntax arg) - { - return ThrowExpression( - ObjectCreationExpression(IdentifierName(ArgumentExceptionClassName)).WithArgumentList(ArgumentList(message, NameOf(arg))) - ); - } - - public static ThrowExpressionSyntax ThrowNotImplementedException() - { - return ThrowExpression( - ObjectCreationExpression(IdentifierName(NotImplementedExceptionClassName)).WithArgumentList(SyntaxFactory.ArgumentList()) - ); - } - - public static InvocationExpressionSyntax GenericInvocation( - string receiver, - string methodName, - IEnumerable typeParams, - params ExpressionSyntax[] arguments - ) - { - var method = GenericName(methodName).WithTypeArgumentList(TypeArgumentList(typeParams.ToArray())); - return InvocationExpression(MemberAccess(IdentifierName(receiver), method)).WithArgumentList(ArgumentList(arguments)); - } - - public static InvocationExpressionSyntax GenericInvocation( - string methodName, - IEnumerable typeParams, - params ExpressionSyntax[] arguments - ) - { - var method = GenericName(methodName).WithTypeArgumentList(TypeArgumentList(typeParams.ToArray())); - return InvocationExpression(method).WithArgumentList(ArgumentList(arguments)); - } - - public static InvocationExpressionSyntax Invocation(string methodName, params MethodArgument?[] arguments) => - Invocation(IdentifierName(methodName), arguments); - - public static InvocationExpressionSyntax Invocation(ExpressionSyntax method, params MethodArgument?[] arguments) => - Invocation(method, arguments.WhereNotNull().OrderBy(x => x.Parameter.Ordinal).Select(x => x.Argument).ToArray()); - - public static InvocationExpressionSyntax Invocation(string methodName, params ExpressionSyntax[] arguments) => - Invocation(IdentifierName(methodName), arguments); - - public static InvocationExpressionSyntax Invocation(ExpressionSyntax method, params ExpressionSyntax[] arguments) - { - return InvocationExpression(method).WithArgumentList(ArgumentList(arguments)); - } - - public static InvocationExpressionSyntax Invocation(ExpressionSyntax method) => Invocation(method, Array.Empty()); - - public static InvocationExpressionSyntax Invocation(ExpressionSyntax method, params ArgumentSyntax[] arguments) - { - return InvocationExpression(method).WithArgumentList(ArgumentList(arguments)); - } - - public static TypeParameterListSyntax TypeParameterList(params ITypeParameterSymbol?[] parameters) - { - var typeParameters = parameters.WhereNotNull().OrderBy(x => x.Ordinal).Select(x => TypeParameter(x.Name)); - return SyntaxFactory.TypeParameterList(CommaSeparatedList(typeParameters)); - } - - public static ParameterListSyntax ParameterList(bool extensionMethod, params MethodParameter?[] parameters) - { - var parameterSyntaxes = parameters - .WhereNotNull() - .DistinctBy(x => x.Ordinal) - .OrderBy(x => x.Ordinal) - .Select(p => Parameter(extensionMethod, p)); - return SyntaxFactory.ParameterList(CommaSeparatedList(parameterSyntaxes)); - } - - public static ParameterSyntax Parameter(bool addThisKeyword, MethodParameter parameter) - { - var param = SyntaxFactory.Parameter(Identifier(parameter.Name)).WithType(FullyQualifiedIdentifier(parameter.Type)); - - if (addThisKeyword && parameter.Ordinal == 0) - { - param = param.WithModifiers(TokenList(Token(SyntaxKind.ThisKeyword))); - } - - return param; - } - - public static InvocationExpressionSyntax StaticInvocation(string receiverType, string methodName, params ExpressionSyntax[] arguments) - { - var receiverTypeIdentifier = IdentifierName(receiverType); - var methodAccess = MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - receiverTypeIdentifier, - IdentifierName(methodName) - ); - return InvocationExpression(methodAccess).WithArgumentList(ArgumentList(arguments)); - } - - public static string StaticMethodString(IMethodSymbol method) - { - var receiver = method.ReceiverType ?? throw new ArgumentException(nameof(method.ReceiverType) + " is null", nameof(method)); - var qualifiedReceiverName = receiver.NonNullable().FullyQualifiedIdentifierName(); - return $"{qualifiedReceiverName}.{method.Name}"; - } - - public static InvocationExpressionSyntax StaticInvocation(IMethodSymbol method, params ExpressionSyntax[] arguments) - { - var receiver = method.ReceiverType ?? throw new ArgumentException(nameof(method.ReceiverType) + " is null", nameof(method)); - var qualifiedReceiverName = receiver.NonNullable().FullyQualifiedIdentifierName(); - return StaticInvocation(qualifiedReceiverName, method.Name, arguments); - } - - public static InvocationExpressionSyntax StaticInvocation(IMethodSymbol method, params ArgumentSyntax[] arguments) - { - var receiver = method.ReceiverType ?? throw new ArgumentException(nameof(method.ReceiverType) + " is null", nameof(method)); - var qualifiedReceiverName = receiver.NonNullable().FullyQualifiedIdentifierName(); - - var receiverTypeIdentifier = IdentifierName(qualifiedReceiverName); - var methodAccess = MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - receiverTypeIdentifier, - IdentifierName(method.Name) - ); - return InvocationExpression(methodAccess).WithArgumentList(ArgumentList(arguments)); - } - - public static ForStatementSyntax IncrementalForLoop(string counterName, StatementSyntax body, ExpressionSyntax maxValueExclusive) - { - var counterDeclaration = DeclareVariable(counterName, IntLiteral(0)); - var counterIncrement = PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, IdentifierName(counterName)); - var condition = BinaryExpression(SyntaxKind.LessThanExpression, IdentifierName(counterName), maxValueExclusive); - return ForStatement(body) - .WithDeclaration(counterDeclaration) - .WithCondition(condition) - .WithIncrementors(SingletonSeparatedList(counterIncrement)); - } - - public static VariableDeclarationSyntax DeclareVariable(string variableName, ExpressionSyntax initializationValue) - { - var initializer = EqualsValueClause(initializationValue); - var declarator = VariableDeclarator(Identifier(variableName)).WithInitializer(initializer); - return VariableDeclaration(VarIdentifier).WithVariables(SingletonSeparatedList(declarator)); - } - - public static LocalDeclarationStatementSyntax DeclareLocalVariable(string variableName, ExpressionSyntax initializationValue) - { - var variableDeclaration = DeclareVariable(variableName, initializationValue); - return LocalDeclarationStatement(variableDeclaration); - } - - public static StatementSyntax CreateInstance(string variableName, ITypeSymbol typeSymbol) => - DeclareLocalVariable(variableName, CreateInstance(typeSymbol)); - - public static ObjectCreationExpressionSyntax CreateInstance(string typeName) - { - var type = IdentifierName(typeName); - return ObjectCreationExpression(type).WithArgumentList(SyntaxFactory.ArgumentList()); - } - - public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol) - { - var type = NonNullableIdentifier(typeSymbol); - return ObjectCreationExpression(type).WithArgumentList(SyntaxFactory.ArgumentList()); - } - - public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol, params ExpressionSyntax[] args) - { - var type = NonNullableIdentifier(typeSymbol); - return ObjectCreationExpression(type).WithArgumentList(ArgumentList(args)); - } - - public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol, params ArgumentSyntax[] args) - { - var type = NonNullableIdentifier(typeSymbol); - return ObjectCreationExpression(type).WithArgumentList(ArgumentList(args)); - } - - public static InitializerExpressionSyntax ObjectInitializer(params ExpressionSyntax[] expressions) - { - return InitializerExpression(SyntaxKind.ObjectInitializerExpression, CommaSeparatedList(expressions)); - } - - public static SyntaxTrivia Nullable(bool enabled) - { - return Trivia(NullableDirectiveTrivia(Token(enabled ? SyntaxKind.EnableKeyword : SyntaxKind.DisableKeyword), true)); - } - - public static NamespaceDeclarationSyntax Namespace(string ns) => NamespaceDeclaration(IdentifierName(ns)); - - public static ArgumentListSyntax ArgumentList(params ExpressionSyntax[] argSyntaxes) => - SyntaxFactory.ArgumentList(CommaSeparatedList(argSyntaxes.Select(Argument))); - - public static TypeArgumentListSyntax TypeArgumentList(params TypeSyntax[] argSyntaxes) => - SyntaxFactory.TypeArgumentList(CommaSeparatedList(argSyntaxes)); - - public static TypeArgumentListSyntax TypeArgumentList(IEnumerable argSyntaxes) => - SyntaxFactory.TypeArgumentList(CommaSeparatedList(argSyntaxes)); - - public static ArgumentListSyntax ArgumentList(params ArgumentSyntax[] args) => SyntaxFactory.ArgumentList(CommaSeparatedList(args)); - - public static ArgumentListSyntax ArgumentList(IEnumerable args) => SyntaxFactory.ArgumentList(CommaSeparatedList(args)); - - public static SeparatedSyntaxList CommaSeparatedList(IEnumerable nodes, bool insertTrailingComma = false) - where T : SyntaxNode => SeparatedList(JoinByComma(nodes, insertTrailingComma)); - - public static IdentifierNameSyntax NonNullableIdentifier(ITypeSymbol t) => FullyQualifiedIdentifier(t.NonNullable()); - - public static IdentifierNameSyntax FullyQualifiedIdentifier(ITypeSymbol typeSymbol) => - IdentifierName(typeSymbol.FullyQualifiedIdentifierName()); - - 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) - ); - - private static IEnumerable JoinByComma(IEnumerable nodes, bool insertTrailingComma = false) => - Join(Token(SyntaxKind.CommaToken), insertTrailingComma, nodes); - - private static IEnumerable Join(SyntaxToken sep, bool insertTrailingSeparator, IEnumerable nodes) - { - var first = true; - foreach (var node in nodes) - { - if (first) - { - first = false; - } - else - { - yield return sep; - } - - yield return node; - } - - if (insertTrailingSeparator) - { - yield return sep; - } - } -} diff --git a/src/Riok.Mapperly/Helpers/IncrementalValuesProviderExtensions.cs b/src/Riok.Mapperly/Helpers/IncrementalValuesProviderExtensions.cs index 0627cc16f4..7a5a3196e2 100644 --- a/src/Riok.Mapperly/Helpers/IncrementalValuesProviderExtensions.cs +++ b/src/Riok.Mapperly/Helpers/IncrementalValuesProviderExtensions.cs @@ -7,13 +7,6 @@ namespace Riok.Mapperly.Helpers; internal static class IncrementalValuesProviderExtensions { - public static IncrementalValuesProvider WhereNotNull(this IncrementalValuesProvider source) - { -#nullable disable - return source.Where(x => x != null); -#nullable enable - } - public static IncrementalValuesProvider WhereNotNull(this IncrementalValuesProvider source) where TSource : struct { @@ -71,13 +64,20 @@ IncrementalValuesProvider mappers mappers, static (spc, mapper) => { - var mapperText = mapper.Body.NormalizeWhitespace().ToFullString(); + var mapperText = mapper.Body.ToFullString(); spc.AddSource(mapper.FileName, SourceText.From(mapperText, Encoding.UTF8)); } ); } #if !ROSLYN4_4_OR_GREATER + public static IncrementalValuesProvider WhereNotNull(this IncrementalValuesProvider source) + { +#nullable disable + return source.Where(x => x != null); +#nullable enable + } + public static IncrementalValueProvider WithTrackingName(this IncrementalValueProvider source, string name) => source; diff --git a/src/Riok.Mapperly/MapperGeneratorStepNames.cs b/src/Riok.Mapperly/MapperGeneratorStepNames.cs index 5a060ebfde..2cb986a932 100644 --- a/src/Riok.Mapperly/MapperGeneratorStepNames.cs +++ b/src/Riok.Mapperly/MapperGeneratorStepNames.cs @@ -2,9 +2,9 @@ namespace Riok.Mapperly; public static class MapperGeneratorStepNames { - public const string BuildCompilationContext = "bulidCompilationContext"; - public const string BuildMapperDefaults = "buildMapperDefaults"; - public const string ReportDiagnostics = "reportDiagnostics"; - public const string BuildMappers = "buildMappers"; - public const string ImplementationSourceOutput = "ImplementationSourceOutput"; + public const string BuildCompilationContext = nameof(BuildCompilationContext); + public const string BuildMapperDefaults = nameof(BuildMapperDefaults); + public const string ReportDiagnostics = nameof(ReportDiagnostics); + public const string BuildMappers = nameof(BuildMappers); + public const string ImplementationSourceOutput = nameof(ImplementationSourceOutput); } diff --git a/src/Riok.Mapperly/Properties/launchSettings.json b/src/Riok.Mapperly/Properties/launchSettings.json index 595894efd0..28f49b1e27 100644 --- a/src/Riok.Mapperly/Properties/launchSettings.json +++ b/src/Riok.Mapperly/Properties/launchSettings.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { - "integration-tests": { + "IntegrationTests": { "commandName": "DebugRoslynComponent", "targetProject": "../../test/Riok.Mapperly.IntegrationTests/Riok.Mapperly.IntegrationTests.csproj" } diff --git a/src/Riok.Mapperly/Symbols/MemberPath.cs b/src/Riok.Mapperly/Symbols/MemberPath.cs index 4ed923cdbf..9b3d858e30 100644 --- a/src/Riok.Mapperly/Symbols/MemberPath.cs +++ b/src/Riok.Mapperly/Symbols/MemberPath.cs @@ -3,7 +3,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Helpers; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using static Riok.Mapperly.Emit.SyntaxFactoryHelper; +using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; namespace Riok.Mapperly.Symbols; diff --git a/test/Riok.Mapperly.IntegrationTests/DeepCloningMapperTest.cs b/test/Riok.Mapperly.IntegrationTests/DeepCloningMapperTest.cs index ea5c7ab8c0..d7b7524f2c 100644 --- a/test/Riok.Mapperly.IntegrationTests/DeepCloningMapperTest.cs +++ b/test/Riok.Mapperly.IntegrationTests/DeepCloningMapperTest.cs @@ -12,7 +12,7 @@ namespace Riok.Mapperly.IntegrationTests public class DeepCloningMapperTest : BaseMapperTest { [Fact] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public Task SnapshotGeneratedSource() { var path = GetGeneratedMapperFilePath(nameof(DeepCloningMapper)); @@ -20,7 +20,7 @@ public Task SnapshotGeneratedSource() } [Fact] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public Task RunMappingShouldWork() { var model = NewTestObj(); diff --git a/test/Riok.Mapperly.IntegrationTests/MapperTest.cs b/test/Riok.Mapperly.IntegrationTests/MapperTest.cs index 326afc130e..f8b5a7af42 100644 --- a/test/Riok.Mapperly.IntegrationTests/MapperTest.cs +++ b/test/Riok.Mapperly.IntegrationTests/MapperTest.cs @@ -7,7 +7,7 @@ namespace Riok.Mapperly.IntegrationTests { [UsesVerify] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public class MapperTest : BaseMapperTest { [Fact] diff --git a/test/Riok.Mapperly.IntegrationTests/ProjectionMapperTest.cs b/test/Riok.Mapperly.IntegrationTests/ProjectionMapperTest.cs index 94ca2a357f..3553b09ce5 100644 --- a/test/Riok.Mapperly.IntegrationTests/ProjectionMapperTest.cs +++ b/test/Riok.Mapperly.IntegrationTests/ProjectionMapperTest.cs @@ -17,7 +17,7 @@ namespace Riok.Mapperly.IntegrationTests public class ProjectionMapperTest : BaseMapperTest { [Fact] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public Task SnapshotGeneratedSource() { var path = GetGeneratedMapperFilePath(nameof(ProjectionMapper)); diff --git a/test/Riok.Mapperly.IntegrationTests/StaticMapperTest.cs b/test/Riok.Mapperly.IntegrationTests/StaticMapperTest.cs index a7f886bc67..54db76989d 100644 --- a/test/Riok.Mapperly.IntegrationTests/StaticMapperTest.cs +++ b/test/Riok.Mapperly.IntegrationTests/StaticMapperTest.cs @@ -13,7 +13,7 @@ namespace Riok.Mapperly.IntegrationTests public class StaticMapperTest : BaseMapperTest { [Fact] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public Task SnapshotGeneratedSource() { var path = GetGeneratedMapperFilePath(nameof(StaticTestMapper)); @@ -21,7 +21,7 @@ public Task SnapshotGeneratedSource() } [Fact] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public Task RunMappingShouldWork() { var model = NewTestObj(); @@ -30,7 +30,7 @@ public Task RunMappingShouldWork() } [Fact] - [VersionedSnapshot(Versions.NET6_0 | Versions.NET7_0)] + [VersionedSnapshot(Versions.NET6_0)] public Task RunExtensionMappingShouldWork() { var model = NewTestObj(); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs index 6ed133c413..72f0deda4c 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/CircularReferenceMapperTest.SnapshotGeneratedSource.verified.cs @@ -19,7 +19,6 @@ public static partial class CircularReferenceMapper { target.Parent = MapToCircularReferenceDto(source.Parent, refHandler); } - target.Value = source.Value; return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.RunMappingShouldWork_NET7_0.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.RunMappingShouldWork_NET7_0.verified.txt deleted file mode 100644 index 97299a4121..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.RunMappingShouldWork_NET7_0.verified.txt +++ /dev/null @@ -1,168 +0,0 @@ -{ - CtorValue: 7, - CtorValue2: 100, - IntValue: 10, - IntInitOnlyValue: 3, - RequiredValue: 4, - StringValue: fooBar, - RenamedStringValue: fooBar2, - Flattening: { - IdValue: 10 - }, - NullableFlattening: { - IdValue: 100 - }, - UnflatteningIdValue: 20, - NullableUnflatteningIdValue: 200, - NestedNullable: { - IntValue: 100 - }, - NestedNullableTargetNotNullable: {}, - StringNullableTargetNotNullable: fooBar3, - TupleValue: { - Item1: 10, - Item2: 20 - }, - RecursiveObject: { - CtorValue: 5, - CtorValue2: 100, - RequiredValue: 4, - StringValue: , - RenamedStringValue: , - Flattening: {}, - MemoryValue: { - IsEmpty: true - }, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumValue: Value10, - EnumName: Value30, - EnumReverseStringValue: DtoValue3 - }, - SourceTargetSameObjectType: { - CtorValue: 8, - CtorValue2: 100, - IntValue: 99, - RequiredValue: 98, - StringValue: , - RenamedStringValue: , - Flattening: {}, - MemoryValue: { - IsEmpty: true - }, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumReverseStringValue: - }, - NullableReadOnlyObjectCollection: [ - { - IntValue: 10 - }, - { - IntValue: 20 - } - ], - MemoryValue: { - Length: 3, - IsEmpty: false - }, - StackValue: [ - 1, - 2, - 3 - ], - QueueValue: [ - 1, - 2, - 3 - ], - ImmutableArrayValue: [ - 1, - 2, - 3 - ], - ImmutableListValue: [ - 1, - 2, - 3 - ], - ImmutableQueueValue: [ - 1, - 2, - 3 - ], - ImmutableStackValue: [ - 1, - 2, - 3 - ], - ImmutableSortedSetValue: [ - 1, - 2, - 3 - ], - ImmutableDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ImmutableSortedDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ExistingISet: [ - 1, - 2, - 3 - ], - ExistingHashSet: [ - 1, - 2, - 3 - ], - ExistingSortedSet: [ - 1, - 2, - 3 - ], - ExistingList: [ - 1, - 2, - 3 - ], - ISet: [ - 1, - 2, - 3 - ], - IReadOnlySet: [ - 1, - 2, - 3 - ], - HashSet: [ - 1, - 2, - 3 - ], - SortedSet: [ - 1, - 2, - 3 - ], - EnumValue: Value10, - FlagsEnumValue: V1, V4, - EnumName: Value10, - EnumRawValue: Value20, - EnumStringValue: Value30, - EnumReverseStringValue: DtoValue3, - SubObject: { - SubIntValue: 2, - BaseIntValue: 1 - }, - DateTimeValueTargetDateOnly: 2020-01-03 15:10:05 Utc, - DateTimeValueTargetTimeOnly: 2020-01-03 15:10:05 Utc -} \ No newline at end of file diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs index 072ab89e10..7ce76976a9 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource.verified.cs @@ -16,48 +16,40 @@ public static partial class DeepCloningMapper var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(src.CtorValue, ctorValue2: src.CtorValue2) { IntInitOnlyValue = src.IntInitOnlyValue, - RequiredValue = src.RequiredValue + RequiredValue = src.RequiredValue, }; if (src.NullableFlattening != null) { target.NullableFlattening = Copy(src.NullableFlattening); } - if (src.NestedNullable != null) { target.NestedNullable = MapToTestObjectNested(src.NestedNullable); } - if (src.NestedNullableTargetNotNullable != null) { target.NestedNullableTargetNotNullable = MapToTestObjectNested(src.NestedNullableTargetNotNullable); } - if (src.TupleValue != null) { target.TupleValue = MapToValueTuple(src.TupleValue.Value); } - if (src.RecursiveObject != null) { target.RecursiveObject = Copy(src.RecursiveObject); } - if (src.SourceTargetSameObjectType != null) { target.SourceTargetSameObjectType = Copy(src.SourceTargetSameObjectType); } - if (src.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(src.NullableReadOnlyObjectCollection); } - if (src.SubObject != null) { target.SubObject = MapToInheritanceSubObject(src.SubObject); } - target.IntValue = src.IntValue; target.StringValue = src.StringValue; target.RenamedStringValue = src.RenamedStringValue; @@ -79,22 +71,18 @@ public static partial class DeepCloningMapper { target.ExistingISet.Add(item); } - foreach (var item1 in src.ExistingHashSet) { target.ExistingHashSet.Add(item1); } - foreach (var item2 in src.ExistingSortedSet) { target.ExistingSortedSet.Add(item2); } - foreach (var item3 in src.ExistingList) { target.ExistingList.Add(item3); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(src.ISet); target.HashSet = global::System.Linq.Enumerable.ToHashSet(src.HashSet); target.SortedSet = new global::System.Collections.Generic.SortedSet(src.SortedSet); @@ -131,7 +119,6 @@ private static (string A, string) MapToValueTuple((string A, string) source) target[i] = MapToTestObjectNested(item); i++; } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs index 7663863af3..18bc0c942b 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs @@ -14,47 +14,42 @@ public static partial class DeepCloningMapper public static partial global::Riok.Mapperly.IntegrationTests.Models.TestObject Copy(global::Riok.Mapperly.IntegrationTests.Models.TestObject src) { var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(src.CtorValue, ctorValue2: src.CtorValue2) - {IntInitOnlyValue = src.IntInitOnlyValue, RequiredValue = src.RequiredValue}; + { + IntInitOnlyValue = src.IntInitOnlyValue, + RequiredValue = src.RequiredValue, + }; if (src.NullableFlattening != null) { target.NullableFlattening = Copy(src.NullableFlattening); } - if (src.NestedNullable != null) { target.NestedNullable = MapToTestObjectNested(src.NestedNullable); } - if (src.NestedNullableTargetNotNullable != null) { target.NestedNullableTargetNotNullable = MapToTestObjectNested(src.NestedNullableTargetNotNullable); } - if (src.TupleValue != null) { target.TupleValue = MapToValueTuple(src.TupleValue.Value); } - if (src.RecursiveObject != null) { target.RecursiveObject = Copy(src.RecursiveObject); } - if (src.SourceTargetSameObjectType != null) { target.SourceTargetSameObjectType = Copy(src.SourceTargetSameObjectType); } - if (src.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(src.NullableReadOnlyObjectCollection); } - if (src.SubObject != null) { target.SubObject = MapToInheritanceSubObject(src.SubObject); } - target.IntValue = src.IntValue; target.StringValue = src.StringValue; target.RenamedStringValue = src.RenamedStringValue; @@ -76,24 +71,20 @@ public static partial class DeepCloningMapper { target.ExistingISet.Add(item); } - target.ExistingHashSet.EnsureCapacity(src.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in src.ExistingHashSet) { target.ExistingHashSet.Add(item1); } - foreach (var item2 in src.ExistingSortedSet) { target.ExistingSortedSet.Add(item2); } - target.ExistingList.EnsureCapacity(src.ExistingList.Count + target.ExistingList.Count); foreach (var item3 in src.ExistingList) { target.ExistingList.Add(item3); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(src.ISet); target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(src.IReadOnlySet); target.HashSet = global::System.Linq.Enumerable.ToHashSet(src.HashSet); @@ -131,7 +122,6 @@ private static (string A, string) MapToValueTuple((string A, string) source) target[i] = MapToTestObjectNested(item); i++; } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs deleted file mode 100644 index b57ff290f6..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/DeepCloningMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs +++ /dev/null @@ -1,149 +0,0 @@ -// -#nullable enable -namespace Riok.Mapperly.IntegrationTests.Mapper -{ - public static partial class DeepCloningMapper - { - public static partial global::Riok.Mapperly.IntegrationTests.Models.IdObject Copy(global::Riok.Mapperly.IntegrationTests.Models.IdObject src) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.IdObject(); - target.IdValue = src.IdValue; - return target; - } - - public static partial global::Riok.Mapperly.IntegrationTests.Models.TestObject Copy(global::Riok.Mapperly.IntegrationTests.Models.TestObject src) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(src.CtorValue, ctorValue2: src.CtorValue2) - { - IntInitOnlyValue = src.IntInitOnlyValue, - RequiredValue = src.RequiredValue - }; - if (src.NullableFlattening != null) - { - target.NullableFlattening = Copy(src.NullableFlattening); - } - - if (src.NestedNullable != null) - { - target.NestedNullable = MapToTestObjectNested(src.NestedNullable); - } - - if (src.NestedNullableTargetNotNullable != null) - { - target.NestedNullableTargetNotNullable = MapToTestObjectNested(src.NestedNullableTargetNotNullable); - } - - if (src.TupleValue != null) - { - target.TupleValue = MapToValueTuple(src.TupleValue.Value); - } - - if (src.RecursiveObject != null) - { - target.RecursiveObject = Copy(src.RecursiveObject); - } - - if (src.SourceTargetSameObjectType != null) - { - target.SourceTargetSameObjectType = Copy(src.SourceTargetSameObjectType); - } - - if (src.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(src.NullableReadOnlyObjectCollection); - } - - if (src.SubObject != null) - { - target.SubObject = MapToInheritanceSubObject(src.SubObject); - } - - target.IntValue = src.IntValue; - target.StringValue = src.StringValue; - target.RenamedStringValue = src.RenamedStringValue; - target.Flattening = Copy(src.Flattening); - target.UnflatteningIdValue = src.UnflatteningIdValue; - target.NullableUnflatteningIdValue = src.NullableUnflatteningIdValue; - target.StringNullableTargetNotNullable = src.StringNullableTargetNotNullable; - target.MemoryValue = src.MemoryValue.Span.ToArray(); - target.StackValue = new global::System.Collections.Generic.Stack(src.StackValue); - target.QueueValue = new global::System.Collections.Generic.Queue(src.QueueValue); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(src.ImmutableArrayValue); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(src.ImmutableListValue); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(src.ImmutableQueueValue); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(src.ImmutableStackValue); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(src.ImmutableSortedSetValue); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(src.ImmutableDictionaryValue); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(src.ImmutableSortedDictionaryValue); - foreach (var item in src.ExistingISet) - { - target.ExistingISet.Add(item); - } - - target.ExistingHashSet.EnsureCapacity(src.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in src.ExistingHashSet) - { - target.ExistingHashSet.Add(item1); - } - - foreach (var item2 in src.ExistingSortedSet) - { - target.ExistingSortedSet.Add(item2); - } - - target.ExistingList.EnsureCapacity(src.ExistingList.Count + target.ExistingList.Count); - foreach (var item3 in src.ExistingList) - { - target.ExistingList.Add(item3); - } - - target.ISet = global::System.Linq.Enumerable.ToHashSet(src.ISet); - target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(src.IReadOnlySet); - 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; - target.EnumReverseStringValue = src.EnumReverseStringValue; - target.DateTimeValueTargetDateOnly = src.DateTimeValueTargetDateOnly; - target.DateTimeValueTargetTimeOnly = src.DateTimeValueTargetTimeOnly; - return target; - } - - private static global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested MapToTestObjectNested(global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested(); - target.IntValue = source.IntValue; - return target; - } - - private static (string A, string) MapToValueTuple((string A, string) source) - { - var target = (A: source.A, source.Item2); - return target; - } - - private static global::System.Collections.Generic.IReadOnlyCollection MapToIReadOnlyCollection(global::System.Collections.Generic.IReadOnlyCollection source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Count]; - var i = 0; - foreach (var item in source) - { - target[i] = MapToTestObjectNested(item); - i++; - } - - return target; - } - - private static global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInheritanceSubObject(global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject(); - target.SubIntValue = source.SubIntValue; - target.BaseIntValue = source.BaseIntValue; - return target; - } - } -} \ No newline at end of file diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.RunMappingShouldWork_NET7_0.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.RunMappingShouldWork_NET7_0.verified.txt deleted file mode 100644 index 128e6b3084..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.RunMappingShouldWork_NET7_0.verified.txt +++ /dev/null @@ -1,184 +0,0 @@ -{ - 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, - TupleValue: { - Item1: 10, - Item2: 20 - }, - RecursiveObject: { - CtorValue: 5, - CtorValue2: 100, - RequiredValue: 4, - StringValue: +after-map, - RenamedStringValue2: , - Unflattening: {}, - NestedNullableTargetNotNullable: {}, - StringNullableTargetNotNullable: , - SpanValue: [ - 1, - 2, - 3 - ], - MemoryValue: { - IsEmpty: true - }, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumValue: DtoValue1, - EnumName: Value30, - EnumStringValue: 0, - EnumReverseStringValue: DtoValue3 - }, - SourceTargetSameObjectType: { - CtorValue: 8, - CtorValue2: 100, - IntValue: 99, - RequiredValue: 98, - StringValue: , - RenamedStringValue: , - Flattening: {}, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumReverseStringValue: - }, - NullableReadOnlyObjectCollection: [ - { - IntValue: 10 - }, - { - IntValue: 20 - } - ], - SpanValue: [ - 1, - 2, - 3 - ], - MemoryValue: { - Length: 3, - IsEmpty: false - }, - StackValue: [ - 1, - 2, - 3 - ], - QueueValue: [ - 1, - 2, - 3 - ], - ImmutableArrayValue: [ - 1, - 2, - 3 - ], - ImmutableListValue: [ - 1, - 2, - 3 - ], - ImmutableHashSetValue: [ - 1, - 2, - 3 - ], - ImmutableQueueValue: [ - 1, - 2, - 3 - ], - ImmutableStackValue: [ - 1, - 2, - 3 - ], - ImmutableSortedSetValue: [ - 1, - 2, - 3 - ], - ImmutableDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ImmutableSortedDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ExistingISet: [ - 1, - 2, - 3 - ], - ExistingHashSet: [ - 1, - 2, - 3 - ], - ExistingSortedSet: [ - 1, - 2, - 3 - ], - ExistingList: [ - 1, - 2, - 3 - ], - ISet: [ - 1, - 2, - 3 - ], - IReadOnlySet: [ - 1, - 2, - 3 - ], - HashSet: [ - 1, - 2, - 3 - ], - SortedSet: [ - 1, - 2, - 3 - ], - EnumValue: DtoValue1, - FlagsEnumValue: V1, V3, - 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/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs index 5f74267f7e..e04e6dc2e9 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs @@ -49,55 +49,46 @@ public partial class TestMapper var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2)) { IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), - RequiredValue = DirectInt(testObject.RequiredValue) + 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.TupleValue != null) { target.TupleValue = MapToValueTuple(testObject.TupleValue.Value); } - if (testObject.RecursiveObject != null) { target.RecursiveObject = MapToDto(testObject.RecursiveObject); } - if (testObject.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(testObject.NullableReadOnlyObjectCollection); } - if (testObject.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); } - target.IntValue = DirectInt(testObject.IntValue); target.StringValue = testObject.StringValue; target.RenamedStringValue2 = testObject.RenamedStringValue; @@ -120,22 +111,18 @@ public partial class TestMapper { target.ExistingISet.Add(ParseableInt(item)); } - foreach (var item1 in testObject.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in testObject.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - foreach (var item3 in testObject.ExistingList) { target.ExistingList.Add(ParseableInt(item3)); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.ISet, x => ParseableInt(x))); 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))); @@ -155,38 +142,32 @@ public partial class TestMapper var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2)) { IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), - RequiredValue = DirectInt(dto.RequiredValue) + 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.TupleValue != null) { target.TupleValue = MapToValueTuple1(dto.TupleValue.Value); } - 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); @@ -208,22 +189,18 @@ public partial class TestMapper { target.ExistingISet.Add(item.ToString()); } - foreach (var item1 in dto.ExistingHashSet) { target.ExistingHashSet.Add(item1.ToString()); } - foreach (var item2 in dto.ExistingSortedSet) { target.ExistingSortedSet.Add(item2.ToString()); } - foreach (var item3 in dto.ExistingList) { target.ExistingList.Add(item3.ToString()); } - 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())); @@ -242,43 +219,35 @@ public partial class TestMapper { 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.TupleValue != null) { target.TupleValue = MapToValueTuple(source.TupleValue.Value); } - if (source.RecursiveObject != null) { target.RecursiveObject = MapToDto(source.RecursiveObject); } - if (source.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(source.NullableReadOnlyObjectCollection); } - if (source.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(source.SubObject); } - target.CtorValue = DirectInt(source.CtorValue); target.CtorValue2 = DirectInt(source.CtorValue2); target.IntValue = DirectInt(source.IntValue); @@ -301,22 +270,18 @@ public partial class TestMapper { target.ExistingISet.Add(ParseableInt(item)); } - foreach (var item1 in source.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in source.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - foreach (var item3 in source.ExistingList) { target.ExistingList.Add(ParseableInt(item3)); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.ISet, x => ParseableInt(x))); 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))); @@ -368,7 +333,6 @@ public partial class TestMapper target[i] = MapToTestObjectNestedDto(item); i++; } - return target; } @@ -379,7 +343,6 @@ private int[] MapToInt32Array(global::System.Span source) { target[i] = ParseableInt(source[i]); } - return target; } @@ -390,7 +353,6 @@ private int[] MapToInt32Array1(global::System.ReadOnlySpan source) { target[i] = ParseableInt(source[i]); } - return target; } @@ -444,7 +406,6 @@ private string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnu { target[i] = MapToTestObjectNested(source[i]); } - return target; } @@ -455,7 +416,6 @@ private string[] MapToStringArray(global::System.ReadOnlySpan source) { target[i] = source[i].ToString(); } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET6_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET6_0.verified.cs index e391c171ed..782d0d233f 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET6_0.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET6_0.verified.cs @@ -47,54 +47,48 @@ public partial class TestMapper private partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(global::Riok.Mapperly.IntegrationTests.Models.TestObject testObject) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2)) - {IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), RequiredValue = DirectInt(testObject.RequiredValue)}; + { + 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.TupleValue != null) { target.TupleValue = MapToValueTuple(testObject.TupleValue.Value); } - if (testObject.RecursiveObject != null) { target.RecursiveObject = MapToDto(testObject.RecursiveObject); } - if (testObject.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(testObject.NullableReadOnlyObjectCollection); } - if (testObject.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); } - target.IntValue = DirectInt(testObject.IntValue); target.StringValue = testObject.StringValue; target.RenamedStringValue2 = testObject.RenamedStringValue; @@ -117,24 +111,20 @@ public partial class TestMapper { target.ExistingISet.Add(ParseableInt(item)); } - target.ExistingHashSet.EnsureCapacity(testObject.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in testObject.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in testObject.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - target.ExistingList.EnsureCapacity(testObject.ExistingList.Count + target.ExistingList.Count); foreach (var item3 in testObject.ExistingList) { target.ExistingList.Add(ParseableInt(item3)); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.ISet, x => ParseableInt(x))); target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.IReadOnlySet, x => ParseableInt(x))); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); @@ -153,37 +143,34 @@ public partial class TestMapper public partial global::Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto) { var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2)) - {IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), RequiredValue = DirectInt(dto.RequiredValue)}; + { + 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.TupleValue != null) { target.TupleValue = MapToValueTuple1(dto.TupleValue.Value); } - 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); @@ -205,24 +192,20 @@ public partial class TestMapper { target.ExistingISet.Add(item.ToString()); } - target.ExistingHashSet.EnsureCapacity(dto.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in dto.ExistingHashSet) { target.ExistingHashSet.Add(item1.ToString()); } - foreach (var item2 in dto.ExistingSortedSet) { target.ExistingSortedSet.Add(item2.ToString()); } - target.ExistingList.EnsureCapacity(dto.ExistingList.Count + target.ExistingList.Count); foreach (var item3 in dto.ExistingList) { target.ExistingList.Add(item3.ToString()); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.ISet, x => x.ToString())); 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())); @@ -242,43 +225,35 @@ public partial class TestMapper { 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.TupleValue != null) { target.TupleValue = MapToValueTuple(source.TupleValue.Value); } - if (source.RecursiveObject != null) { target.RecursiveObject = MapToDto(source.RecursiveObject); } - if (source.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(source.NullableReadOnlyObjectCollection); } - if (source.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(source.SubObject); } - target.CtorValue = DirectInt(source.CtorValue); target.CtorValue2 = DirectInt(source.CtorValue2); target.IntValue = DirectInt(source.IntValue); @@ -301,24 +276,20 @@ public partial class TestMapper { target.ExistingISet.Add(ParseableInt(item)); } - target.ExistingHashSet.EnsureCapacity(source.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in source.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in source.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - target.ExistingList.EnsureCapacity(source.ExistingList.Count + target.ExistingList.Count); foreach (var item3 in source.ExistingList) { target.ExistingList.Add(ParseableInt(item3)); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.ISet, x => ParseableInt(x))); target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.IReadOnlySet, x => ParseableInt(x))); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); @@ -371,7 +342,6 @@ public partial class TestMapper target[i] = MapToTestObjectNestedDto(item); i++; } - return target; } @@ -382,7 +352,6 @@ private int[] MapToInt32Array(global::System.Span source) { target[i] = ParseableInt(source[i]); } - return target; } @@ -393,7 +362,6 @@ private int[] MapToInt32Array1(global::System.ReadOnlySpan source) { target[i] = ParseableInt(source[i]); } - return target; } @@ -447,7 +415,6 @@ private string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnu { target[i] = MapToTestObjectNested(source[i]); } - return target; } @@ -458,7 +425,6 @@ private string[] MapToStringArray(global::System.ReadOnlySpan source) { target[i] = source[i].ToString(); } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET7_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET7_0.verified.cs deleted file mode 100644 index e8f038d264..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource_NET7_0.verified.cs +++ /dev/null @@ -1,501 +0,0 @@ -// -#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 global::System.Guid ParseableGuid(string id) - { - return global::System.Guid.Parse(id); - } - - public partial int ParseableInt(string value) - { - return int.Parse(value); - } - - public partial global::System.DateTime DirectDateTime(global::System.DateTime dateTime) - { - return dateTime; - } - - public partial global::System.Collections.Generic.IEnumerable MapAllDtos(global::System.Collections.Generic.IEnumerable objects) - { - return global::System.Linq.Enumerable.Select(objects, x => MapToDto(x)); - } - - private partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(global::Riok.Mapperly.IntegrationTests.Models.TestObject testObject) - { - var target = new global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple(testObject.TupleValue.Value); - } - - if (testObject.RecursiveObject != null) - { - target.RecursiveObject = MapToDto(testObject.RecursiveObject); - } - - if (testObject.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(testObject.NullableReadOnlyObjectCollection); - } - - 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.SpanValue = MapToInt32Array(testObject.SpanValue); - target.MemoryValue = MapToInt32Array1(testObject.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(testObject.StackValue, x => ParseableInt(x))); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(testObject.QueueValue, x => ParseableInt(x))); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(testObject.ImmutableArrayValue, x => ParseableInt(x))); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(testObject.ImmutableListValue, x => ParseableInt(x))); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(testObject.ImmutableHashSetValue, x => ParseableInt(x))); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(testObject.ImmutableQueueValue, x => ParseableInt(x))); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(testObject.ImmutableStackValue, x => ParseableInt(x))); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(testObject.ImmutableSortedSetValue, x => ParseableInt(x))); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(testObject.ImmutableDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(testObject.ImmutableSortedDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - foreach (var item in testObject.ExistingISet) - { - target.ExistingISet.Add(ParseableInt(item)); - } - - target.ExistingHashSet.EnsureCapacity(testObject.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in testObject.ExistingHashSet) - { - target.ExistingHashSet.Add(ParseableInt(item1)); - } - - foreach (var item2 in testObject.ExistingSortedSet) - { - target.ExistingSortedSet.Add(ParseableInt(item2)); - } - - target.ExistingList.EnsureCapacity(testObject.ExistingList.Count + target.ExistingList.Count); - foreach (var item3 in testObject.ExistingList) - { - target.ExistingList.Add(ParseableInt(item3)); - } - - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.ISet, x => ParseableInt(x))); - target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.IReadOnlySet, x => ParseableInt(x))); - 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); - target.EnumReverseStringValue = MapToTestEnumDtoByValue(testObject.EnumReverseStringValue); - target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(testObject.DateTimeValueTargetDateOnly); - target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(testObject.DateTimeValueTargetTimeOnly); - return target; - } - - public partial global::Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto) - { - var target = new global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple1(dto.TupleValue.Value); - } - - 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.MemoryValue = MapToStringArray(dto.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(dto.ImmutableArrayValue, x => x.ToString())); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(dto.ImmutableListValue, x => x.ToString())); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(dto.ImmutableHashSetValue, x => x.ToString())); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(dto.ImmutableQueueValue, x => x.ToString())); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(dto.ImmutableStackValue, x => x.ToString())); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(dto.ImmutableSortedSetValue, x => x.ToString())); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(dto.ImmutableDictionaryValue, x => x.Key.ToString(), x => x.Value.ToString()); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(dto.ImmutableSortedDictionaryValue, x => x.Key.ToString(), x => x.Value.ToString()); - foreach (var item in dto.ExistingISet) - { - target.ExistingISet.Add(item.ToString()); - } - - target.ExistingHashSet.EnsureCapacity(dto.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in dto.ExistingHashSet) - { - target.ExistingHashSet.Add(item1.ToString()); - } - - foreach (var item2 in dto.ExistingSortedSet) - { - target.ExistingSortedSet.Add(item2.ToString()); - } - - target.ExistingList.EnsureCapacity(dto.ExistingList.Count + target.ExistingList.Count); - foreach (var item3 in dto.ExistingList) - { - target.ExistingList.Add(item3.ToString()); - } - - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.ISet, x => x.ToString())); - 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.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 void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple(source.TupleValue.Value); - } - - if (source.RecursiveObject != null) - { - target.RecursiveObject = MapToDto(source.RecursiveObject); - } - - if (source.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(source.NullableReadOnlyObjectCollection); - } - - 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.SpanValue = MapToInt32Array(source.SpanValue); - target.MemoryValue = MapToInt32Array1(source.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(source.StackValue, x => ParseableInt(x))); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(source.QueueValue, x => ParseableInt(x))); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(source.ImmutableArrayValue, x => ParseableInt(x))); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(source.ImmutableListValue, x => ParseableInt(x))); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(source.ImmutableHashSetValue, x => ParseableInt(x))); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(source.ImmutableQueueValue, x => ParseableInt(x))); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(source.ImmutableStackValue, x => ParseableInt(x))); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(source.ImmutableSortedSetValue, x => ParseableInt(x))); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(source.ImmutableDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(source.ImmutableSortedDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - foreach (var item in source.ExistingISet) - { - target.ExistingISet.Add(ParseableInt(item)); - } - - target.ExistingHashSet.EnsureCapacity(source.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in source.ExistingHashSet) - { - target.ExistingHashSet.Add(ParseableInt(item1)); - } - - foreach (var item2 in source.ExistingSortedSet) - { - target.ExistingSortedSet.Add(ParseableInt(item2)); - } - - target.ExistingList.EnsureCapacity(source.ExistingList.Count + target.ExistingList.Count); - foreach (var item3 in source.ExistingList) - { - target.ExistingList.Add(ParseableInt(item3)); - } - - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.ISet, x => ParseableInt(x))); - target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.IReadOnlySet, x => ParseableInt(x))); - 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); - target.EnumReverseStringValue = MapToTestEnumDtoByValue(source.EnumReverseStringValue); - target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(source.DateTimeValueTargetDateOnly); - 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; - } - - private global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto MapToTestObjectNestedDto(global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto(); - target.IntValue = DirectInt(source.IntValue); - return target; - } - - private (int A, int) MapToValueTuple((string A, string) source) - { - var target = (A: ParseableInt(source.A), ParseableInt(source.Item2)); - return target; - } - - private global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] MapToTestObjectNestedDtoArray(global::System.Collections.Generic.IReadOnlyCollection source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[source.Count]; - var i = 0; - foreach (var item in source) - { - target[i] = MapToTestObjectNestedDto(item); - i++; - } - - return target; - } - - private int[] MapToInt32Array(global::System.Span source) - { - var target = new int[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = ParseableInt(source[i]); - } - - return target; - } - - private int[] MapToInt32Array1(global::System.ReadOnlySpan source) - { - var target = new int[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = ParseableInt(source[i]); - } - - return target; - } - - private string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnum source) - { - return source switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10), - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20), - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30), - _ => source.ToString(), - }; - } - - private global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue MapToTestEnumDtoByValue(string source) - { - return source switch - { - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1, - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2, - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3, - _ => System.Enum.Parse(source, false), - }; - } - - private global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto MapToInheritanceSubObjectDto(global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto(); - target.SubIntValue = DirectInt(source.SubIntValue); - target.BaseIntValue = DirectInt(source.BaseIntValue); - return target; - } - - private global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested MapToTestObjectNested(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested(); - target.IntValue = DirectInt(source.IntValue); - return target; - } - - private (string A, string) MapToValueTuple1((int A, int) source) - { - var target = (A: source.A.ToString(), source.Item2.ToString()); - return target; - } - - private global::System.Collections.Generic.IReadOnlyCollection MapToIReadOnlyCollection(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = MapToTestObjectNested(source[i]); - } - - return target; - } - - private string[] MapToStringArray(global::System.ReadOnlySpan source) - { - var target = new string[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = source[i].ToString(); - } - - return target; - } - - private global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToTestEnum(string source) - { - return source switch - { - nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10) => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20) => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30) => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => System.Enum.Parse(source, false), - }; - } - - private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue source) - { - return source switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1 => nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1), - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2 => nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2), - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3 => nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3), - _ => source.ToString(), - }; - } - - private global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInheritanceSubObject(global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto source) - { - var target = new global::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/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs index b7e9836422..3b623594a9 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource.verified.cs @@ -7,14 +7,61 @@ 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, 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) }); + 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 } public static partial global::System.Linq.IQueryable ProjectToDto(this global::System.Linq.IQueryable q) { #nullable disable - return System.Linq.Queryable.Select(q, x => (global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType)(x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA() { ValueA = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).ValueA, Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).Id, BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).BaseValue } : x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB() { ValueB = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).ValueB, Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).Id, BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue } : default)); + return System.Linq.Queryable.Select(q, x => (global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType)(x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA() + { + ValueA = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).ValueA, + Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).Id, + BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).BaseValue, + } : x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB() + { + ValueB = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).ValueB, + Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).Id, + BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue, + } : default)); #nullable enable } @@ -23,34 +70,29 @@ public static partial class ProjectionMapper var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(testObject.CtorValue) { IntInitOnlyValue = testObject.IntInitOnlyValue, - RequiredValue = testObject.RequiredValue + RequiredValue = testObject.RequiredValue, }; if (testObject.NestedNullable != null) { target.NestedNullableIntValue = 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.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(testObject.NullableReadOnlyObjectCollection); } - if (testObject.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); } - target.IntValue = testObject.IntValue; target.StringValue = testObject.StringValue; target.RenamedStringValue2 = testObject.RenamedStringValue; @@ -112,7 +154,6 @@ public static partial class ProjectionMapper target[i] = MapToTestObjectNestedDto(item); i++; } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs index 4f26bacd2b..6582b675ad 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs @@ -8,11 +8,43 @@ public static partial class ProjectionMapper { #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, 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)}); + { + 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 } @@ -20,41 +52,47 @@ public static partial class ProjectionMapper { #nullable disable return System.Linq.Queryable.Select(q, x => (global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType)(x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA() - {ValueA = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).ValueA, Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).Id, BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).BaseValue} : x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB() - {ValueB = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).ValueB, Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).Id, BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue} : default)); + { + ValueA = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).ValueA, + Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).Id, + BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).BaseValue, + } : x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB() + { + ValueB = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).ValueB, + Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).Id, + BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue, + } : default)); #nullable enable } private static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection ProjectToDto(this global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjection testObject) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(testObject.CtorValue) - {IntInitOnlyValue = testObject.IntInitOnlyValue, RequiredValue = testObject.RequiredValue}; + { + IntInitOnlyValue = testObject.IntInitOnlyValue, + RequiredValue = testObject.RequiredValue, + }; if (testObject.NestedNullable != null) { target.NestedNullableIntValue = 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.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(testObject.NullableReadOnlyObjectCollection); } - if (testObject.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); } - target.IntValue = testObject.IntValue; target.StringValue = testObject.StringValue; target.RenamedStringValue2 = testObject.RenamedStringValue; @@ -116,7 +154,6 @@ public static partial class ProjectionMapper target[i] = MapToTestObjectNestedDto(item); i++; } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs deleted file mode 100644 index 70f2d178d6..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/ProjectionMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs +++ /dev/null @@ -1,149 +0,0 @@ -// -#nullable enable -namespace Riok.Mapperly.IntegrationTests.Mapper -{ - 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, 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 - } - - public static partial global::System.Linq.IQueryable ProjectToDto(this global::System.Linq.IQueryable q) - { -#nullable disable - return System.Linq.Queryable.Select(q, x => (global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType)(x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA() { ValueA = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).ValueA, Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).Id, BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA)x).BaseValue } : x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB() { ValueB = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).ValueB, Id = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).Id, BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue } : default)); -#nullable enable - } - - private static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection ProjectToDto(this global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjection testObject) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(testObject.CtorValue) - { - IntInitOnlyValue = testObject.IntInitOnlyValue, - RequiredValue = testObject.RequiredValue - }; - if (testObject.NestedNullable != null) - { - target.NestedNullableIntValue = 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.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToIReadOnlyCollection(testObject.NullableReadOnlyObjectCollection); - } - - if (testObject.SubObject != null) - { - target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); - } - - target.IntValue = testObject.IntValue; - target.StringValue = testObject.StringValue; - target.RenamedStringValue2 = testObject.RenamedStringValue; - target.FlatteningIdValue = testObject.Flattening.IdValue; - target.NullableFlatteningIdValue = testObject.NullableFlattening?.IdValue; - target.SourceTargetSameObjectType = testObject.SourceTargetSameObjectType; - target.EnumValue = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue)testObject.EnumValue; - target.EnumName = (global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName)testObject.EnumName; - target.EnumRawValue = (byte)testObject.EnumRawValue; - target.EnumStringValue = MapToString(testObject.EnumStringValue); - target.EnumReverseStringValue = MapToTestEnumDtoByName(testObject.EnumReverseStringValue); - target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(testObject.DateTimeValueTargetDateOnly); - target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(testObject.DateTimeValueTargetTimeOnly); - target.ManuallyMapped = MapManual(testObject.ManuallyMapped); - return target; - } - - private static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType MapDerived(global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionBaseType source) - { - return source switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA x => MapToTestObjectDtoProjectionTypeA(x), - global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB x => MapToTestObjectDtoProjectionTypeB(x), - _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType as there is no known derived type mapping", nameof(source)), - }; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA MapToTestObjectDtoProjectionTypeA(global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA(); - target.ValueA = source.ValueA; - target.Id = source.Id; - target.BaseValue = source.BaseValue; - return target; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB MapToTestObjectDtoProjectionTypeB(global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeB(); - target.ValueB = source.ValueB; - target.Id = source.Id; - target.BaseValue = source.BaseValue; - return target; - } - - 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(); - target.IntValue = source.IntValue; - return target; - } - - private static global::System.Collections.Generic.IReadOnlyCollection MapToIReadOnlyCollection(global::System.Collections.Generic.List source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[source.Count]; - var i = 0; - foreach (var item in source) - { - target[i] = MapToTestObjectNestedDto(item); - i++; - } - - return target; - } - - private static string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnum source) - { - return source switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10), - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20), - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30), - _ => source.ToString(), - }; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToTestEnumDtoByName(string source) - { - return source switch - { - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => System.Enum.Parse(source, false), - }; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto MapToInheritanceSubObjectDto(global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto(); - target.SubIntValue = source.SubIntValue; - target.BaseIntValue = source.BaseIntValue; - return target; - } - } -} \ No newline at end of file diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunExtensionMappingShouldWork_NET7_0.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunExtensionMappingShouldWork_NET7_0.verified.txt deleted file mode 100644 index 6b3cde8e51..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunExtensionMappingShouldWork_NET7_0.verified.txt +++ /dev/null @@ -1,179 +0,0 @@ -{ - 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, - TupleValue: { - Item1: 10, - Item2: 20 - }, - RecursiveObject: { - CtorValue: 5, - CtorValue2: 100, - RequiredValue: 4, - StringValue: , - RenamedStringValue2: , - Unflattening: {}, - NestedNullableTargetNotNullable: {}, - StringNullableTargetNotNullable: , - SpanValue: [ - 1, - 2, - 3 - ], - MemoryValue: { - IsEmpty: true - }, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumValue: DtoValue1, - EnumName: Value30, - EnumStringValue: 0, - EnumReverseStringValue: DtoValue3 - }, - SourceTargetSameObjectType: { - CtorValue: 8, - CtorValue2: 100, - IntValue: 99, - RequiredValue: 98, - StringValue: , - RenamedStringValue: , - Flattening: {}, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumReverseStringValue: - }, - NullableReadOnlyObjectCollection: [ - { - IntValue: 10 - }, - { - IntValue: 20 - } - ], - SpanValue: [ - 1, - 2, - 3 - ], - MemoryValue: { - Length: 3, - IsEmpty: false - }, - StackValue: [ - 1, - 2, - 3 - ], - QueueValue: [ - 1, - 2, - 3 - ], - ImmutableArrayValue: [ - 1, - 2, - 3 - ], - ImmutableListValue: [ - 1, - 2, - 3 - ], - ImmutableHashSetValue: [ - 1, - 2, - 3 - ], - ImmutableQueueValue: [ - 1, - 2, - 3 - ], - ImmutableStackValue: [ - 1, - 2, - 3 - ], - ImmutableSortedSetValue: [ - 1, - 2, - 3 - ], - ImmutableDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ImmutableSortedDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ExistingISet: [ - 1, - 2, - 3 - ], - ExistingHashSet: [ - 1, - 2, - 3 - ], - ExistingSortedSet: [ - 1, - 2, - 3 - ], - ExistingList: [ - 1, - 2, - 3 - ], - ISet: [ - 1, - 2, - 3 - ], - IReadOnlySet: [ - 1, - 2, - 3 - ], - HashSet: [ - 1, - 2, - 3 - ], - SortedSet: [ - 1, - 2, - 3 - ], - EnumValue: DtoValue1, - FlagsEnumValue: V1, V3, - 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/StaticMapperTest.RunMappingShouldWork_NET7_0.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunMappingShouldWork_NET7_0.verified.txt deleted file mode 100644 index 4812c0f332..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.RunMappingShouldWork_NET7_0.verified.txt +++ /dev/null @@ -1,184 +0,0 @@ -{ - 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, - TupleValue: { - Item1: 10, - Item2: 20 - }, - RecursiveObject: { - CtorValue: 5, - CtorValue2: 100, - RequiredValue: 4, - StringValue: , - RenamedStringValue2: , - Unflattening: {}, - NestedNullableTargetNotNullable: {}, - StringNullableTargetNotNullable: , - SpanValue: [ - 1, - 2, - 3 - ], - MemoryValue: { - IsEmpty: true - }, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumValue: DtoValue1, - EnumName: Value30, - EnumStringValue: 0, - EnumReverseStringValue: DtoValue3 - }, - SourceTargetSameObjectType: { - CtorValue: 8, - CtorValue2: 100, - IntValue: 99, - RequiredValue: 98, - StringValue: , - RenamedStringValue: , - Flattening: {}, - ImmutableArrayValue: null, - ImmutableQueueValue: [], - ImmutableStackValue: [], - EnumReverseStringValue: - }, - NullableReadOnlyObjectCollection: [ - { - IntValue: 10 - }, - { - IntValue: 20 - } - ], - SpanValue: [ - 1, - 2, - 3 - ], - MemoryValue: { - Length: 3, - IsEmpty: false - }, - StackValue: [ - 1, - 2, - 3 - ], - QueueValue: [ - 1, - 2, - 3 - ], - ImmutableArrayValue: [ - 1, - 2, - 3 - ], - ImmutableListValue: [ - 1, - 2, - 3 - ], - ImmutableHashSetValue: [ - 1, - 2, - 3 - ], - ImmutableQueueValue: [ - 1, - 2, - 3 - ], - ImmutableStackValue: [ - 1, - 2, - 3 - ], - ImmutableSortedSetValue: [ - 1, - 2, - 3 - ], - ImmutableDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ImmutableSortedDictionaryValue: { - 1: 1, - 2: 2, - 3: 3 - }, - ExistingISet: [ - 1, - 2, - 3 - ], - ExistingHashSet: [ - 1, - 2, - 3 - ], - ExistingSortedSet: [ - 1, - 2, - 3 - ], - ExistingList: [ - 1, - 2, - 3 - ], - ISet: [ - 1, - 2, - 3 - ], - IReadOnlySet: [ - 1, - 2, - 3 - ], - HashSet: [ - 1, - 2, - 3 - ], - SortedSet: [ - 1, - 2, - 3 - ], - EnumValue: DtoValue1, - FlagsEnumValue: V1, V3, - 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/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs index bb31fd6f91..93c5d77fd9 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -54,49 +54,41 @@ public static partial class StaticTestMapper var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(src.CtorValue), ctorValue2: DirectInt(src.CtorValue2)) { IntInitOnlyValue = DirectInt(src.IntInitOnlyValue), - RequiredValue = DirectInt(src.RequiredValue) + 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.TupleValue != null) { target.TupleValue = MapToValueTuple(src.TupleValue.Value); } - if (src.RecursiveObject != null) { target.RecursiveObject = MapToDtoExt(src.RecursiveObject); } - if (src.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(src.NullableReadOnlyObjectCollection); } - if (src.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(src.SubObject); } - target.IntValue = DirectInt(src.IntValue); target.StringValue = src.StringValue; target.FlatteningIdValue = DirectInt(src.Flattening.IdValue); @@ -117,17 +109,14 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(ParseableInt(item)); } - foreach (var item1 in src.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in src.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - MapExistingList(src.ExistingList, target.ExistingList); target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.ISet, x => ParseableInt(x))); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.HashSet, x => ParseableInt(x))); @@ -148,55 +137,46 @@ public static partial class StaticTestMapper var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2)) { IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), - RequiredValue = DirectInt(testObject.RequiredValue) + 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.TupleValue != null) { target.TupleValue = MapToValueTuple(testObject.TupleValue.Value); } - if (testObject.RecursiveObject != null) { target.RecursiveObject = MapToDtoExt(testObject.RecursiveObject); } - if (testObject.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(testObject.NullableReadOnlyObjectCollection); } - if (testObject.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); } - target.IntValue = DirectInt(testObject.IntValue); target.StringValue = testObject.StringValue; target.RenamedStringValue2 = testObject.RenamedStringValue; @@ -219,17 +199,14 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(ParseableInt(item)); } - foreach (var item1 in testObject.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in testObject.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - MapExistingList(testObject.ExistingList, target.ExistingList); target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.ISet, x => ParseableInt(x))); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.HashSet, x => ParseableInt(x))); @@ -250,38 +227,32 @@ public static partial class StaticTestMapper var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2)) { IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), - RequiredValue = DirectInt(dto.RequiredValue) + 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.TupleValue != null) { target.TupleValue = MapToValueTuple1(dto.TupleValue.Value); } - 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); @@ -303,22 +274,18 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(item.ToString()); } - foreach (var item1 in dto.ExistingHashSet) { target.ExistingHashSet.Add(item1.ToString()); } - foreach (var item2 in dto.ExistingSortedSet) { target.ExistingSortedSet.Add(item2.ToString()); } - foreach (var item3 in dto.ExistingList) { target.ExistingList.Add(item3.ToString()); } - 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())); @@ -337,43 +304,35 @@ public static partial class StaticTestMapper { 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.TupleValue != null) { target.TupleValue = MapToValueTuple(source.TupleValue.Value); } - if (source.RecursiveObject != null) { target.RecursiveObject = MapToDtoExt(source.RecursiveObject); } - if (source.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(source.NullableReadOnlyObjectCollection); } - if (source.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(source.SubObject); } - target.CtorValue = DirectInt(source.CtorValue); target.CtorValue2 = DirectInt(source.CtorValue2); target.IntValue = DirectInt(source.IntValue); @@ -396,17 +355,14 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(ParseableInt(item)); } - foreach (var item1 in source.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in source.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - MapExistingList(source.ExistingList, target.ExistingList); target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.ISet, x => ParseableInt(x))); target.HashSet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.HashSet, x => ParseableInt(x))); @@ -600,7 +556,6 @@ private static (int A, int) MapToValueTuple((string A, string) source) target[i] = MapToTestObjectNestedDto(item); i++; } - return target; } @@ -611,7 +566,6 @@ private static int[] MapToInt32Array(global::System.Span source) { target[i] = ParseableInt(source[i]); } - return target; } @@ -622,7 +576,6 @@ private static int[] MapToInt32Array1(global::System.ReadOnlySpan source { target[i] = ParseableInt(source[i]); } - return target; } @@ -676,7 +629,6 @@ private static (string A, string) MapToValueTuple1((int A, int) source) { target[i] = MapToTestObjectNested(source[i]); } - return target; } @@ -687,7 +639,6 @@ private static string[] MapToStringArray(global::System.ReadOnlySpan source { target[i] = source[i].ToString(); } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs index d237657d8a..faf056f65a 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs @@ -52,48 +52,43 @@ public static partial class StaticTestMapper public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoExt(this global::Riok.Mapperly.IntegrationTests.Models.TestObject src) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(src.CtorValue), ctorValue2: DirectInt(src.CtorValue2)) - {IntInitOnlyValue = DirectInt(src.IntInitOnlyValue), RequiredValue = DirectInt(src.RequiredValue)}; + { + 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.TupleValue != null) { target.TupleValue = MapToValueTuple(src.TupleValue.Value); } - if (src.RecursiveObject != null) { target.RecursiveObject = MapToDtoExt(src.RecursiveObject); } - if (src.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(src.NullableReadOnlyObjectCollection); } - if (src.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(src.SubObject); } - target.IntValue = DirectInt(src.IntValue); target.StringValue = src.StringValue; target.FlatteningIdValue = DirectInt(src.Flattening.IdValue); @@ -114,18 +109,15 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(ParseableInt(item)); } - target.ExistingHashSet.EnsureCapacity(src.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in src.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in src.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - MapExistingList(src.ExistingList, target.ExistingList); target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.ISet, x => ParseableInt(x))); target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.IReadOnlySet, x => ParseableInt(x))); @@ -145,54 +137,48 @@ public static partial class StaticTestMapper private static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(global::Riok.Mapperly.IntegrationTests.Models.TestObject testObject) { var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2)) - {IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), RequiredValue = DirectInt(testObject.RequiredValue)}; + { + 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.TupleValue != null) { target.TupleValue = MapToValueTuple(testObject.TupleValue.Value); } - if (testObject.RecursiveObject != null) { target.RecursiveObject = MapToDtoExt(testObject.RecursiveObject); } - if (testObject.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(testObject.NullableReadOnlyObjectCollection); } - if (testObject.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(testObject.SubObject); } - target.IntValue = DirectInt(testObject.IntValue); target.StringValue = testObject.StringValue; target.RenamedStringValue2 = testObject.RenamedStringValue; @@ -215,18 +201,15 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(ParseableInt(item)); } - target.ExistingHashSet.EnsureCapacity(testObject.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in testObject.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in testObject.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - MapExistingList(testObject.ExistingList, target.ExistingList); target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.ISet, x => ParseableInt(x))); target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.IReadOnlySet, x => ParseableInt(x))); @@ -246,37 +229,34 @@ public static partial class StaticTestMapper public static partial global::Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto) { var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2)) - {IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), RequiredValue = DirectInt(dto.RequiredValue)}; + { + 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.TupleValue != null) { target.TupleValue = MapToValueTuple1(dto.TupleValue.Value); } - 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); @@ -298,24 +278,20 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(item.ToString()); } - target.ExistingHashSet.EnsureCapacity(dto.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in dto.ExistingHashSet) { target.ExistingHashSet.Add(item1.ToString()); } - foreach (var item2 in dto.ExistingSortedSet) { target.ExistingSortedSet.Add(item2.ToString()); } - target.ExistingList.EnsureCapacity(dto.ExistingList.Count + target.ExistingList.Count); foreach (var item3 in dto.ExistingList) { target.ExistingList.Add(item3.ToString()); } - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.ISet, x => x.ToString())); 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())); @@ -335,43 +311,35 @@ public static partial class StaticTestMapper { 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.TupleValue != null) { target.TupleValue = MapToValueTuple(source.TupleValue.Value); } - if (source.RecursiveObject != null) { target.RecursiveObject = MapToDtoExt(source.RecursiveObject); } - if (source.NullableReadOnlyObjectCollection != null) { target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(source.NullableReadOnlyObjectCollection); } - if (source.SubObject != null) { target.SubObject = MapToInheritanceSubObjectDto(source.SubObject); } - target.CtorValue = DirectInt(source.CtorValue); target.CtorValue2 = DirectInt(source.CtorValue2); target.IntValue = DirectInt(source.IntValue); @@ -394,18 +362,15 @@ public static partial class StaticTestMapper { target.ExistingISet.Add(ParseableInt(item)); } - target.ExistingHashSet.EnsureCapacity(source.ExistingHashSet.Count + target.ExistingHashSet.Count); foreach (var item1 in source.ExistingHashSet) { target.ExistingHashSet.Add(ParseableInt(item1)); } - foreach (var item2 in source.ExistingSortedSet) { target.ExistingSortedSet.Add(ParseableInt(item2)); } - MapExistingList(source.ExistingList, target.ExistingList); target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.ISet, x => ParseableInt(x))); target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.IReadOnlySet, x => ParseableInt(x))); @@ -600,7 +565,6 @@ private static (int A, int) MapToValueTuple((string A, string) source) target[i] = MapToTestObjectNestedDto(item); i++; } - return target; } @@ -611,7 +575,6 @@ private static int[] MapToInt32Array(global::System.Span source) { target[i] = ParseableInt(source[i]); } - return target; } @@ -622,7 +585,6 @@ private static int[] MapToInt32Array1(global::System.ReadOnlySpan source { target[i] = ParseableInt(source[i]); } - return target; } @@ -676,7 +638,6 @@ private static (string A, string) MapToValueTuple1((int A, int) source) { target[i] = MapToTestObjectNested(source[i]); } - return target; } @@ -687,7 +648,6 @@ private static string[] MapToStringArray(global::System.ReadOnlySpan source { target[i] = source[i].ToString(); } - return target; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs deleted file mode 100644 index bd68a4d507..0000000000 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs +++ /dev/null @@ -1,733 +0,0 @@ -// -#nullable enable -namespace Riok.Mapperly.IntegrationTests.Mapper -{ - public static partial class StaticTestMapper - { - public static partial int DirectInt(int value) - { - return value; - } - - public static partial int? DirectIntNullable(int? value) - { - return value == null ? default : value.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 global::System.Guid ParseableGuid(string id) - { - return global::System.Guid.Parse(id); - } - - public static partial int ParseableInt(string value) - { - return int.Parse(value); - } - - public static partial global::System.DateTime DirectDateTime(global::System.DateTime dateTime) - { - return dateTime; - } - - public static partial global::System.Collections.Generic.IEnumerable MapAllDtos(global::System.Collections.Generic.IEnumerable objects) - { - return global::System.Linq.Enumerable.Select(objects, x => MapToDtoExt(x)); - } - - public static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoExt(this global::Riok.Mapperly.IntegrationTests.Models.TestObject src) - { - var target = new global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple(src.TupleValue.Value); - } - - if (src.RecursiveObject != null) - { - target.RecursiveObject = MapToDtoExt(src.RecursiveObject); - } - - if (src.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(src.NullableReadOnlyObjectCollection); - } - - 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.SpanValue = MapToInt32Array(src.SpanValue); - target.MemoryValue = MapToInt32Array1(src.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(src.StackValue, x => ParseableInt(x))); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(src.QueueValue, x => ParseableInt(x))); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(src.ImmutableArrayValue, x => ParseableInt(x))); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(src.ImmutableListValue, x => ParseableInt(x))); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(src.ImmutableHashSetValue, x => ParseableInt(x))); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(src.ImmutableQueueValue, x => ParseableInt(x))); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(src.ImmutableStackValue, x => ParseableInt(x))); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(src.ImmutableSortedSetValue, x => ParseableInt(x))); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(src.ImmutableDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(src.ImmutableSortedDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - foreach (var item in src.ExistingISet) - { - target.ExistingISet.Add(ParseableInt(item)); - } - - target.ExistingHashSet.EnsureCapacity(src.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in src.ExistingHashSet) - { - target.ExistingHashSet.Add(ParseableInt(item1)); - } - - foreach (var item2 in src.ExistingSortedSet) - { - target.ExistingSortedSet.Add(ParseableInt(item2)); - } - - MapExistingList(src.ExistingList, target.ExistingList); - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.ISet, x => ParseableInt(x))); - target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(src.IReadOnlySet, x => ParseableInt(x))); - 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); - target.EnumReverseStringValue = MapToTestEnumDtoByValue(src.EnumReverseStringValue); - target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(src.DateTimeValueTargetDateOnly); - target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(src.DateTimeValueTargetTimeOnly); - return target; - } - - private static partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(global::Riok.Mapperly.IntegrationTests.Models.TestObject testObject) - { - var target = new global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple(testObject.TupleValue.Value); - } - - if (testObject.RecursiveObject != null) - { - target.RecursiveObject = MapToDtoExt(testObject.RecursiveObject); - } - - if (testObject.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(testObject.NullableReadOnlyObjectCollection); - } - - 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.SpanValue = MapToInt32Array(testObject.SpanValue); - target.MemoryValue = MapToInt32Array1(testObject.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(testObject.StackValue, x => ParseableInt(x))); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(testObject.QueueValue, x => ParseableInt(x))); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(testObject.ImmutableArrayValue, x => ParseableInt(x))); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(testObject.ImmutableListValue, x => ParseableInt(x))); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(testObject.ImmutableHashSetValue, x => ParseableInt(x))); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(testObject.ImmutableQueueValue, x => ParseableInt(x))); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(testObject.ImmutableStackValue, x => ParseableInt(x))); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(testObject.ImmutableSortedSetValue, x => ParseableInt(x))); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(testObject.ImmutableDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(testObject.ImmutableSortedDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - foreach (var item in testObject.ExistingISet) - { - target.ExistingISet.Add(ParseableInt(item)); - } - - target.ExistingHashSet.EnsureCapacity(testObject.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in testObject.ExistingHashSet) - { - target.ExistingHashSet.Add(ParseableInt(item1)); - } - - foreach (var item2 in testObject.ExistingSortedSet) - { - target.ExistingSortedSet.Add(ParseableInt(item2)); - } - - MapExistingList(testObject.ExistingList, target.ExistingList); - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.ISet, x => ParseableInt(x))); - target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(testObject.IReadOnlySet, x => ParseableInt(x))); - 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); - target.EnumReverseStringValue = MapToTestEnumDtoByValue(testObject.EnumReverseStringValue); - target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(testObject.DateTimeValueTargetDateOnly); - target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(testObject.DateTimeValueTargetTimeOnly); - return target; - } - - public static partial global::Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto) - { - var target = new global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple1(dto.TupleValue.Value); - } - - 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.MemoryValue = MapToStringArray(dto.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(dto.ImmutableArrayValue, x => x.ToString())); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(dto.ImmutableListValue, x => x.ToString())); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(dto.ImmutableHashSetValue, x => x.ToString())); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(dto.ImmutableQueueValue, x => x.ToString())); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(dto.ImmutableStackValue, x => x.ToString())); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(dto.ImmutableSortedSetValue, x => x.ToString())); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(dto.ImmutableDictionaryValue, x => x.Key.ToString(), x => x.Value.ToString()); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(dto.ImmutableSortedDictionaryValue, x => x.Key.ToString(), x => x.Value.ToString()); - foreach (var item in dto.ExistingISet) - { - target.ExistingISet.Add(item.ToString()); - } - - target.ExistingHashSet.EnsureCapacity(dto.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in dto.ExistingHashSet) - { - target.ExistingHashSet.Add(item1.ToString()); - } - - foreach (var item2 in dto.ExistingSortedSet) - { - target.ExistingSortedSet.Add(item2.ToString()); - } - - target.ExistingList.EnsureCapacity(dto.ExistingList.Count + target.ExistingList.Count); - foreach (var item3 in dto.ExistingList) - { - target.ExistingList.Add(item3.ToString()); - } - - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(dto.ISet, x => x.ToString())); - 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.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 void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.TestObject source, global::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.TupleValue != null) - { - target.TupleValue = MapToValueTuple(source.TupleValue.Value); - } - - if (source.RecursiveObject != null) - { - target.RecursiveObject = MapToDtoExt(source.RecursiveObject); - } - - if (source.NullableReadOnlyObjectCollection != null) - { - target.NullableReadOnlyObjectCollection = MapToTestObjectNestedDtoArray(source.NullableReadOnlyObjectCollection); - } - - 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.SpanValue = MapToInt32Array(source.SpanValue); - target.MemoryValue = MapToInt32Array1(source.MemoryValue.Span); - target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(source.StackValue, x => ParseableInt(x))); - target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(source.QueueValue, x => ParseableInt(x))); - target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(source.ImmutableArrayValue, x => ParseableInt(x))); - target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(source.ImmutableListValue, x => ParseableInt(x))); - target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(source.ImmutableHashSetValue, x => ParseableInt(x))); - target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(source.ImmutableQueueValue, x => ParseableInt(x))); - target.ImmutableStackValue = global::System.Collections.Immutable.ImmutableStack.CreateRange(global::System.Linq.Enumerable.Select(source.ImmutableStackValue, x => ParseableInt(x))); - target.ImmutableSortedSetValue = global::System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(global::System.Linq.Enumerable.Select(source.ImmutableSortedSetValue, x => ParseableInt(x))); - target.ImmutableDictionaryValue = global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary(source.ImmutableDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - target.ImmutableSortedDictionaryValue = global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary(source.ImmutableSortedDictionaryValue, x => ParseableInt(x.Key), x => ParseableInt(x.Value)); - foreach (var item in source.ExistingISet) - { - target.ExistingISet.Add(ParseableInt(item)); - } - - target.ExistingHashSet.EnsureCapacity(source.ExistingHashSet.Count + target.ExistingHashSet.Count); - foreach (var item1 in source.ExistingHashSet) - { - target.ExistingHashSet.Add(ParseableInt(item1)); - } - - foreach (var item2 in source.ExistingSortedSet) - { - target.ExistingSortedSet.Add(ParseableInt(item2)); - } - - MapExistingList(source.ExistingList, target.ExistingList); - target.ISet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.ISet, x => ParseableInt(x))); - target.IReadOnlySet = global::System.Linq.Enumerable.ToHashSet(global::System.Linq.Enumerable.Select(source.IReadOnlySet, x => ParseableInt(x))); - 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); - target.EnumReverseStringValue = MapToTestEnumDtoByValue(source.EnumReverseStringValue); - target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(source.DateTimeValueTargetDateOnly); - target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly); - } - - private static partial int PrivateDirectInt(int value) - { - return value; - } - - public static partial object DerivedTypes(object source) - { - return source switch - { - string x => ParseableInt(x), - int x => x.ToString(), - _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to object as there is no known derived type mapping", nameof(source)), - }; - } - - public static partial object MapWithRuntimeTargetType(object source, global::System.Type targetType) - { - 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), - global::System.DateTime x when targetType.IsAssignableFrom(typeof(global::System.DateTime)) => DirectDateTime(x), - string x when targetType.IsAssignableFrom(typeof(global::System.Guid)) => ParseableGuid(x), - string x when targetType.IsAssignableFrom(typeof(int)) => ParseableInt(x), - global::Riok.Mapperly.IntegrationTests.Models.TestObject x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto)) => MapToDtoExt(x), - global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestObject)) => MapFromDto(x), - global::System.Collections.Generic.IEnumerable x when targetType.IsAssignableFrom(typeof(global::System.Collections.Generic.IEnumerable)) => MapAllDtos(x), - object x when targetType.IsAssignableFrom(typeof(object)) => DerivedTypes(x), - null => throw new System.ArgumentNullException(nameof(source)), - _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to {targetType} as there is no known type mapping", nameof(source)), - }; - } - - public static partial object? MapNullableWithRuntimeTargetType(object? source, global::System.Type targetType) - { - 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), - global::System.DateTime x when targetType.IsAssignableFrom(typeof(global::System.DateTime)) => DirectDateTime(x), - string x when targetType.IsAssignableFrom(typeof(global::System.Guid)) => ParseableGuid(x), - string x when targetType.IsAssignableFrom(typeof(int)) => ParseableInt(x), - global::Riok.Mapperly.IntegrationTests.Models.TestObject x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto)) => MapToDtoExt(x), - global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto x when targetType.IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestObject)) => MapFromDto(x), - global::System.Collections.Generic.IEnumerable x when targetType.IsAssignableFrom(typeof(global::System.Collections.Generic.IEnumerable)) => MapAllDtos(x), - object x when targetType.IsAssignableFrom(typeof(object)) => DerivedTypes(x), - null => default, - _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to {targetType} as there is no known type mapping", nameof(source)), - }; - } - - public static partial TTarget MapGeneric(TSource source) - { - 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), - global::System.DateTime x when typeof(TTarget).IsAssignableFrom(typeof(global::System.DateTime)) => (TTarget)(object)DirectDateTime(x), - string x when typeof(TTarget).IsAssignableFrom(typeof(global::System.Guid)) => (TTarget)(object)ParseableGuid(x), - string x when typeof(TTarget).IsAssignableFrom(typeof(int)) => (TTarget)(object)ParseableInt(x), - global::Riok.Mapperly.IntegrationTests.Models.TestObject x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto)) => (TTarget)(object)MapToDtoExt(x), - global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto x when typeof(TTarget).IsAssignableFrom(typeof(global::Riok.Mapperly.IntegrationTests.Models.TestObject)) => (TTarget)(object)MapFromDto(x), - global::System.Collections.Generic.IEnumerable x when typeof(TTarget).IsAssignableFrom(typeof(global::System.Collections.Generic.IEnumerable)) => (TTarget)(object)MapAllDtos(x), - object x when typeof(TTarget).IsAssignableFrom(typeof(object)) => (TTarget)(object)DerivedTypes(x), - null => throw new System.ArgumentNullException(nameof(source)), - _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to {typeof(TTarget)} as there is no known type mapping", nameof(source)), - }; - } - - 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, - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoAdditionalValue.Value50 => 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.Dto.TestEnumDtoAdditionalValue.Value50 => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)v, - }; - } - - public static partial global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToEnumByNameWithIgnored(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, - _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnumDtoAdditionalValue is not supported"), - }; - } - - 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(); - target.IntValue = DirectInt(source.IntValue); - return target; - } - - private static (int A, int) MapToValueTuple((string A, string) source) - { - var target = (A: ParseableInt(source.A), ParseableInt(source.Item2)); - return target; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] MapToTestObjectNestedDtoArray(global::System.Collections.Generic.IReadOnlyCollection source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[source.Count]; - var i = 0; - foreach (var item in source) - { - target[i] = MapToTestObjectNestedDto(item); - i++; - } - - return target; - } - - private static int[] MapToInt32Array(global::System.Span source) - { - var target = new int[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = ParseableInt(source[i]); - } - - return target; - } - - private static int[] MapToInt32Array1(global::System.ReadOnlySpan source) - { - var target = new int[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = ParseableInt(source[i]); - } - - return target; - } - - private static string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnum source) - { - return source switch - { - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10), - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20), - global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30), - _ => source.ToString(), - }; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue MapToTestEnumDtoByValue(string source) - { - return source switch - { - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1, - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2, - nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3) => global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3, - _ => System.Enum.Parse(source, false), - }; - } - - private static global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto MapToInheritanceSubObjectDto(global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto(); - target.SubIntValue = DirectInt(source.SubIntValue); - target.BaseIntValue = DirectInt(source.BaseIntValue); - return target; - } - - private static global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested MapToTestObjectNested(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested(); - target.IntValue = DirectInt(source.IntValue); - return target; - } - - private static (string A, string) MapToValueTuple1((int A, int) source) - { - var target = (A: source.A.ToString(), source.Item2.ToString()); - return target; - } - - private static global::System.Collections.Generic.IReadOnlyCollection MapToIReadOnlyCollection(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source) - { - var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = MapToTestObjectNested(source[i]); - } - - return target; - } - - private static string[] MapToStringArray(global::System.ReadOnlySpan source) - { - var target = new string[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = source[i].ToString(); - } - - return target; - } - - private static global::Riok.Mapperly.IntegrationTests.Models.TestEnum MapToTestEnum(string source) - { - return source switch - { - nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10) => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10, - nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20) => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20, - nameof(global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30) => global::Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30, - _ => System.Enum.Parse(source, false), - }; - } - - private static string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue source) - { - return source switch - { - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1 => nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue1), - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2 => nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue2), - global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3 => nameof(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByValue.DtoValue3), - _ => source.ToString(), - }; - } - - private static global::Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInheritanceSubObject(global::Riok.Mapperly.IntegrationTests.Dto.InheritanceSubObjectDto source) - { - var target = new global::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/GeneratedMethod.cs b/test/Riok.Mapperly.Tests/GeneratedMethod.cs index d949b40750..5659fbcdd8 100644 --- a/test/Riok.Mapperly.Tests/GeneratedMethod.cs +++ b/test/Riok.Mapperly.Tests/GeneratedMethod.cs @@ -5,7 +5,7 @@ namespace Riok.Mapperly.Tests; public class GeneratedMethod { - private const string MethodIndention = " "; + private const string MethodIndentation = " "; public GeneratedMethod(MethodDeclarationSyntax declarationSyntax) { @@ -21,7 +21,7 @@ public GeneratedMethod(MethodDeclarationSyntax declarationSyntax) public string Body { get; } /// - /// Builds the method body without the method body braces and without the method body level indention. + /// Builds the method body without the method body braces and without the method body level indentation. /// /// The syntax of the method. /// The cleaned body. @@ -38,6 +38,6 @@ private static string ExtractBody(MethodDeclarationSyntax declarationSyntax) var lines = body.Split(Environment.NewLine); return lines.Length == 0 ? string.Empty - : string.Join(Environment.NewLine, lines.Select(l => l.StartsWith(MethodIndention) ? l[MethodIndention.Length..] : l)); + : string.Join(Environment.NewLine, lines.Select(l => l.StartsWith(MethodIndentation) ? l[MethodIndentation.Length..] : l)); } } diff --git a/test/Riok.Mapperly.Tests/Mapping/IgnoreObsoleteTest.cs b/test/Riok.Mapperly.Tests/Mapping/IgnoreObsoleteTest.cs index 86ea41f597..c32bf50b8e 100644 --- a/test/Riok.Mapperly.Tests/Mapping/IgnoreObsoleteTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/IgnoreObsoleteTest.cs @@ -337,7 +337,7 @@ class B """ var target = new global::B() { - Ignored = source.Ignored + Ignored = source.Ignored, }; target.Value = source.Value; return target; @@ -373,7 +373,7 @@ class B """ var target = new global::B() { - Ignored = source.Ignored + Ignored = source.Ignored, }; target.Value = source.Value; return target; diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs index 5f678290f8..1177e77a21 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs @@ -22,7 +22,7 @@ public void InitOnlyProperty() """ var target = new global::B() { - StringValue = source.StringValue + StringValue = source.StringValue, }; target.IntValue = source.IntValue; return target; @@ -48,7 +48,7 @@ public void MultipleInitOnlyProperties() var target = new global::B() { StringValue = source.StringValue, - IntValue = source.IntValue + IntValue = source.IntValue, }; return target; """ @@ -72,7 +72,7 @@ public void InitOnlyPropertyWithNullableSource() """ var target = new global::B() { - Value = source.Value ?? throw new System.ArgumentNullException(nameof(source.Value)) + Value = source.Value ?? throw new System.ArgumentNullException(nameof(source.Value)), }; return target; """ @@ -100,7 +100,7 @@ TestSourceBuilderOptions.Default with """ var target = new global::B() { - Value = source.Value ?? "" + Value = source.Value ?? "", }; return target; """ @@ -123,7 +123,7 @@ public void InitOnlyPropertyWithConfiguration() """ var target = new global::B() { - StringValue = source.StringValue2 + StringValue = source.StringValue2, }; return target; """ @@ -147,7 +147,7 @@ public void InitOnlyReferenceLoop() """ var target = new global::B() { - Parent = source.Parent != null ? Map(source.Parent) : default + Parent = source.Parent != null ? Map(source.Parent) : default, }; return target; """ @@ -172,7 +172,7 @@ public void InitOnlyPropertyWithAutoFlattenedNullablePath() """ var target = new global::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; """ @@ -197,7 +197,7 @@ public void InitOnlyPropertyWithAutoFlattened() """ var target = new global::B() { - NestedValue = source.Nested.Value + NestedValue = source.Nested.Value, }; return target; """ @@ -304,7 +304,7 @@ public void RequiredProperty() """ var target = new global::B() { - StringValue = source.StringValue + StringValue = source.StringValue, }; target.IntValue = source.IntValue; return target; @@ -334,7 +334,7 @@ public void IgnoredTargetRequiredPropertyWithConfiguration() """ var target = new global::B() { - StringValue = source.StringValue + StringValue = source.StringValue, }; target.IntValue = source.IntValue; return target; @@ -364,7 +364,7 @@ public void IgnoredTargetInitPropertyWithConfiguration() """ var target = new global::B() { - StringValue = source.StringValue + StringValue = source.StringValue, }; target.IntValue = source.IntValue; return target; diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs index 6bce6c1412..770ad2d9f4 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs @@ -187,7 +187,7 @@ public void WithPropertyNameMappingStrategyCaseInsensitive() """ var target = new global::B() { - value = source.Value + value = source.Value, }; target.stringvalue = source.StringValue; return target; diff --git a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs index b2e90dd9f6..0f1e3cf82e 100644 --- a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs @@ -31,6 +31,21 @@ public Task ClassToClassMultipleProperties() return TestHelper.VerifyGenerator(source); } + [Fact] + public Task ClassToClassNested() + { + var source = TestSourceBuilder.Mapping( + "System.Linq.IQueryable", + "System.Linq.IQueryable", + "class A { public string StringValue { get; set; } public C OtherValue { get; set; } }", + "class B { public string StringValue { get; set; } public D OtherValue { get; set; } }", + "class C { public int IntValue { get; set; } }", + "class D { public int IntValue { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + [Fact] public Task ClassToClassWithConfigs() { diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToCollectionShouldUpgradeNullability#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToCollectionShouldUpgradeNullability#Mapper.g.verified.cs index eaf0d2ab6b..5267eb47d5 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToCollectionShouldUpgradeNullability#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToCollectionShouldUpgradeNullability#Mapper.g.verified.cs @@ -12,7 +12,6 @@ public partial class Mapper { target.Value = MapToICollection(source.Value); } - return target; } @@ -23,7 +22,6 @@ public partial class Mapper { target.Add(item.ToString()); } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs index 8d11626f8e..0f146fd1c0 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ArrayToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs @@ -12,7 +12,6 @@ public partial class Mapper { target.Value = MapToIReadOnlyCollection(source.Value); } - return target; } @@ -23,7 +22,6 @@ public partial class Mapper { target[i] = source[i].ToString(); } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.CollectionToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.CollectionToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs index 398b09076b..55e9a83148 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.CollectionToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.CollectionToReadOnlyCollectionShouldUpgradeNullability#Mapper.g.verified.cs @@ -12,7 +12,6 @@ public partial class Mapper { target.Value = MapToIReadOnlyCollection(source.Value); } - return target; } @@ -25,7 +24,6 @@ public partial class Mapper target[i] = item.ToString(); i++; } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionProperty#Mapper.g.verified.cs index 66e55b4130..c77980e08a 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionProperty#Mapper.g.verified.cs @@ -10,7 +10,6 @@ public partial class Mapper { target.Value.Add((long)item); } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionPropertyFromNullable#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionPropertyFromNullable#Mapper.g.verified.cs index 163a50c846..77720d1280 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionPropertyFromNullable#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyCollectionPropertyFromNullable#Mapper.g.verified.cs @@ -13,7 +13,6 @@ public partial class Mapper target.Value.Add((long)item); } } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionProperty#Mapper.g.verified.cs index 55c3df5410..4e1f0026b3 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionProperty#Mapper.g.verified.cs @@ -13,7 +13,6 @@ public partial class Mapper target.Value.Add((long)item); } } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionPropertyFromNullable#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionPropertyFromNullable#Mapper.g.verified.cs index ea7afc4bc1..7817c5839a 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionPropertyFromNullable#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableCollectionPropertyFromNullable#Mapper.g.verified.cs @@ -13,7 +13,6 @@ public partial class Mapper target.Value.Add((long)item); } } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableICollectionProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableICollectionProperty#Mapper.g.verified.cs index 55c3df5410..4e1f0026b3 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableICollectionProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.MapToReadOnlyNullableICollectionProperty#Mapper.g.verified.cs @@ -13,7 +13,6 @@ public partial class Mapper target.Value.Add((long)item); } } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ShouldUpgradeNullabilityInDisabledNullableContextInSelectClause#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ShouldUpgradeNullabilityInDisabledNullableContextInSelectClause#Mapper.g.verified.cs index a6bf2b34f0..e370d2585d 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ShouldUpgradeNullabilityInDisabledNullableContextInSelectClause#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumerableTest.ShouldUpgradeNullabilityInDisabledNullableContextInSelectClause#Mapper.g.verified.cs @@ -12,7 +12,6 @@ public partial class Mapper { target.Value = MapToDArray(source.Value); } - return target; } @@ -32,7 +31,6 @@ public partial class Mapper { target[i] = MapToD(source[i]); } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToList#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToList#Mapper.g.verified.cs index cc45371d58..1be27ea71b 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToList#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToList#Mapper.g.verified.cs @@ -16,7 +16,6 @@ public partial class Mapper { target.Add(item); } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToStack#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToStack#Mapper.g.verified.cs index 06911b25f8..6a62abee0a 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToStack#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/MemoryTest.ReadOnlyMemoryToStack#Mapper.g.verified.cs @@ -16,7 +16,6 @@ public partial class Mapper { target.Push(item); } - return target; } } \ No newline at end of file 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 96fb16d249..36a75278de 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 @@ -7,7 +7,7 @@ public partial class Mapper { var target = new global::B() { - StringValue = source.StringValue2 + StringValue = source.StringValue2, }; return target; } diff --git a/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.NullableIntWithAdditionalFlattenedValueToNonNullableIntProperties#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.NullableIntWithAdditionalFlattenedValueToNonNullableIntProperties#Mapper.g.verified.cs index 7fd77318e5..1e314bb36f 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.NullableIntWithAdditionalFlattenedValueToNonNullableIntProperties#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.NullableIntWithAdditionalFlattenedValueToNonNullableIntProperties#Mapper.g.verified.cs @@ -12,10 +12,8 @@ public partial class Mapper { target.NestedValue2 = source.Nested.Value2.Value; } - target.Nested = MapToD(source.Nested); } - return target; } @@ -26,7 +24,6 @@ public partial class Mapper { target.Value1 = source.Value1.Value; } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.ShouldUpgradeNullabilityInDisabledNullableContextInNestedProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.ShouldUpgradeNullabilityInDisabledNullableContextInNestedProperty#Mapper.g.verified.cs index c8a01ffaa5..1606f4b591 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.ShouldUpgradeNullabilityInDisabledNullableContextInNestedProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/ObjectPropertyNullableTest.ShouldUpgradeNullabilityInDisabledNullableContextInNestedProperty#Mapper.g.verified.cs @@ -12,7 +12,6 @@ public partial class Mapper { target.Value = MapToD(source.Value); } - return target; } diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionDerivedTypeTest.AbstractBaseClassDerivedTypesShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionDerivedTypeTest.AbstractBaseClassDerivedTypesShouldWork#Mapper.g.verified.cs index 0642cbbd24..0500ee3514 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionDerivedTypeTest.AbstractBaseClassDerivedTypesShouldWork#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionDerivedTypeTest.AbstractBaseClassDerivedTypesShouldWork#Mapper.g.verified.cs @@ -6,7 +6,17 @@ 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 => (global::B)(x is global::ASubType1 ? new global::BSubType1() { Value1 = ((global::ASubType1)x).Value1, BaseValueB = ((global::ASubType1)x).BaseValueA, StringValue = ((global::ASubType1)x).StringValue } : x is global::ASubType2 ? new global::BSubType2() { Value2 = ((global::ASubType2)x).Value2, BaseValueB = ((global::ASubType2)x).BaseValueA, StringValue = ((global::ASubType2)x).StringValue } : default)); + return System.Linq.Queryable.Select(source, x => (global::B)(x is global::ASubType1 ? new global::BSubType1() + { + Value1 = ((global::ASubType1)x).Value1, + BaseValueB = ((global::ASubType1)x).BaseValueA, + StringValue = ((global::ASubType1)x).StringValue, + } : x is global::ASubType2 ? new global::BSubType2() + { + Value2 = ((global::ASubType2)x).Value2, + BaseValueB = ((global::ASubType2)x).BaseValueA, + StringValue = ((global::ASubType2)x).StringValue, + } : default)); #nullable enable } diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumFromString#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumFromString#Mapper.g.verified.cs index f698e7e009..158a1bda5b 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumFromString#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumFromString#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { Value = System.Enum.Parse(x.Value, false) }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + Value = System.Enum.Parse(x.Value, false), + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToAnotherEnum#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToAnotherEnum#Mapper.g.verified.cs index bb7ca60dff..9d3358dabe 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToAnotherEnum#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToAnotherEnum#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { Value = (global::D)x.Value }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + Value = (global::D)x.Value, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToString#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToString#Mapper.g.verified.cs index de3982d61e..d381a0ee85 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToString#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionEnumTest.EnumToString#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { Value = (string)x.Value.ToString() }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + Value = (string)x.Value.ToString(), + }); #nullable enable } } \ No newline at end of file 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 2f4e52d4d2..470a751e8e 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 @@ -6,7 +6,10 @@ 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, x1 => (int)x1)) }); + 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 dadfbbaffe..48f783bb9b 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 @@ -6,7 +6,10 @@ 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, x1 => (int)x1) }); + 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 diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetProperty#Mapper.g.verified.cs index 0544a452c9..4548d45427 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { StringValue = x.StringValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetPropertyWithNoNullAssignmentAndThrowShouldBeIgnored#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetPropertyWithNoNullAssignmentAndThrowShouldBeIgnored#Mapper.g.verified.cs index 0544a452c9..4548d45427 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetPropertyWithNoNullAssignmentAndThrowShouldBeIgnored#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetPropertyWithNoNullAssignmentAndThrowShouldBeIgnored#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { StringValue = x.StringValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetValueTypeProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetValueTypeProperty#Mapper.g.verified.cs index cec02f4fab..ee465e0802 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetValueTypeProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceAndTargetValueTypeProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { IntValue = x.IntValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + IntValue = x.IntValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlatten#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlatten#Mapper.g.verified.cs index ed018c9ce3..d53764ad90 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlatten#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlatten#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { NestedValue = x.Nested != null ? x.Nested.Value : default }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + NestedValue = x.Nested != null ? x.Nested.Value : default, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlattenString#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlattenString#Mapper.g.verified.cs index 55222f0e49..57f35fb515 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlattenString#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathAutoFlattenString#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { NestedValue = x.Nested != null ? x.Nested.Value : "" }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + NestedValue = x.Nested != null ? x.Nested.Value : "", + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathManuallyFlatten#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathManuallyFlatten#Mapper.g.verified.cs index 5065443507..fe295d24d0 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathManuallyFlatten#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourcePathManuallyFlatten#Mapper.g.verified.cs @@ -6,7 +6,10 @@ public partial class Mapper public partial global::System.Linq.IQueryable Map(global::System.Linq.IQueryable q) { #nullable disable - return System.Linq.Queryable.Select(q, x => new global::B() { NestedValue4 = x.Nested != null && x.Nested.Nested2 != null ? x.Nested.Nested2.Value3 : default }); + return System.Linq.Queryable.Select(q, x => new global::B() + { + NestedValue4 = x.Nested != null && x.Nested.Nested2 != null ? x.Nested.Nested2.Value3 : default, + }); #nullable enable } @@ -17,7 +20,6 @@ public partial class Mapper { target.NestedValue4 = source.Nested.Nested2.Value3; } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceProperty#Mapper.g.verified.cs index 1bc37847ac..ceee727751 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { StringValue = x.StringValue ?? "" }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue ?? "", + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceValueTypeProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceValueTypeProperty#Mapper.g.verified.cs index b0d2dd1a05..d300a1c8ef 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceValueTypeProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableSourceValueTypeProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { IntValue = x.IntValue ?? default }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + IntValue = x.IntValue ?? default, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetProperty#Mapper.g.verified.cs index 0544a452c9..4548d45427 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { StringValue = x.StringValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetValueTypeProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetValueTypeProperty#Mapper.g.verified.cs index cec02f4fab..ee465e0802 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetValueTypeProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionNullableTest.ClassToClassNullableTargetValueTypeProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { IntValue = x.IntValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + IntValue = x.IntValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClass#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClass#Mapper.g.verified.cs index 0544a452c9..4548d45427 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClass#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClass#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { StringValue = x.StringValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassMultipleProperties#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassMultipleProperties#Mapper.g.verified.cs index 9f41fa4d67..5fd697fc29 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassMultipleProperties#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassMultipleProperties#Mapper.g.verified.cs @@ -6,7 +6,12 @@ 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() { StringValue = x.StringValue, IntValue = x.IntValue, CharValue = x.CharValue }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue, + IntValue = x.IntValue, + CharValue = x.CharValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassNested#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassNested#Mapper.g.verified.cs new file mode 100644 index 0000000000..2dde797ead --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassNested#Mapper.g.verified.cs @@ -0,0 +1,19 @@ +//HintName: Mapper.g.cs +// +#nullable enable +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() + { + StringValue = x.StringValue, + OtherValue = new global::D() + { + IntValue = x.OtherValue.IntValue, + }, + }); +#nullable enable + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithConfigs#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithConfigs#Mapper.g.verified.cs index 48accbdac5..a87a13a454 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithConfigs#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithConfigs#Mapper.g.verified.cs @@ -6,7 +6,18 @@ 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() { StringValue2 = x.StringValue, NestedValue = new global::D() { IntValue = (int)x.NestedValue.LongValue, NestedValue = new global::F() { ShortValue = x.NestedValue.NestedValue.ShortValue } } }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue2 = x.StringValue, + NestedValue = new global::D() + { + IntValue = (int)x.NestedValue.LongValue, + NestedValue = new global::F() + { + ShortValue = x.NestedValue.NestedValue.ShortValue, + }, + }, + }); #nullable enable } diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithUserImplemented#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithUserImplemented#Mapper.g.verified.cs index b8781d075a..5279a6cdeb 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithUserImplemented#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ClassToClassWithUserImplemented#Mapper.g.verified.cs @@ -6,7 +6,11 @@ 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() { StringValue = x.StringValue, NestedValue = MapToD(x.NestedValue) }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + StringValue = x.StringValue, + NestedValue = MapToD(x.NestedValue), + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.CtorShouldSkipUnmatchedOptionalParameters#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.CtorShouldSkipUnmatchedOptionalParameters#Mapper.g.verified.cs index b89b5a33a8..98716e47d5 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.CtorShouldSkipUnmatchedOptionalParameters#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.CtorShouldSkipUnmatchedOptionalParameters#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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(x.StringValue) { IntValue = x.IntValue }); + return System.Linq.Queryable.Select(source, x => new global::B(x.StringValue) + { + IntValue = x.IntValue, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ReferenceLoopInitProperty#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ReferenceLoopInitProperty#Mapper.g.verified.cs index bcac43bd43..50028a80a5 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ReferenceLoopInitProperty#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionTest.ReferenceLoopInitProperty#Mapper.g.verified.cs @@ -6,7 +6,10 @@ 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() { Parent = x.Parent != null ? new global::B() : default }); + return System.Linq.Queryable.Select(source, x => new global::B() + { + Parent = x.Parent != null ? new global::B() : default, + }); #nullable enable } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/ReferenceHandlingTest.ArrayShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/ReferenceHandlingTest.ArrayShouldWork#Mapper.g.verified.cs index f5a4bae73f..a2d64fc5cf 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/ReferenceHandlingTest.ArrayShouldWork#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/ReferenceHandlingTest.ArrayShouldWork#Mapper.g.verified.cs @@ -26,7 +26,6 @@ public partial class Mapper { target[i] = MapToB(source[i], refHandler); } - return target; } diff --git a/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToICollection#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToICollection#Mapper.g.verified.cs index e0572b3b3a..0ed9c27b9b 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToICollection#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToICollection#Mapper.g.verified.cs @@ -16,7 +16,6 @@ public partial class Mapper { target.Add(item); } - return target; } } \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToIList#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToIList#Mapper.g.verified.cs index 200f3bc583..7dbc5edd1f 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToIList#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/SpanTest.SpanToIList#Mapper.g.verified.cs @@ -16,7 +16,6 @@ public partial class Mapper { target.Add(item); } - return target; } } \ No newline at end of file