Skip to content

Commit

Permalink
fix: remove inline tuple mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jul 21, 2023
1 parent 0d960a9 commit d384b57
Show file tree
Hide file tree
Showing 24 changed files with 336 additions and 106 deletions.
Expand Up @@ -20,7 +20,7 @@ public void AddTupleConstructorParameterMapping(ValueTupleConstructorParameterMa
// remove the mapping used to map the tuple constructor
value.RemoveAll(x => x.Target.Path.Count == 1);

// remove form dictionary if there aren't nested mappings
// remove from dictionary and target members if there aren't any more mappings
if (!value.Any())
{
MemberConfigsByRootTargetName.Remove(mapping.Parameter.Name);
Expand Down
Expand Up @@ -196,9 +196,8 @@ out sourcePath
)
return true;

// if standard matching fails, use the underlying field
// if standard matching fails, try to use the positional fields
// if source is a tuple compare the underlying field ie, Item1, Item2
// if source is not a tuple compare top level members
if (ctx.Mapping.SourceType.IsTupleType)
{
if (ctx.Mapping.SourceType is not INamedTypeSymbol namedType)
Expand Down
Expand Up @@ -7,8 +7,6 @@ namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings;
/// </summary>
public interface IMemberAssignmentMappingContainer
{
bool HasMemberMappings();

bool HasMemberMapping(IMemberAssignmentMapping mapping);

void AddMemberMapping(IMemberAssignmentMapping mapping);
Expand Down
Expand Up @@ -42,8 +42,6 @@ public void AddMemberMapping(IMemberAssignmentMapping mapping)
}
}

public bool HasMemberMappings() => _delegateMappings.Any() || _childContainers.Any(x => x.HasMemberMappings());

public bool HasMemberMapping(IMemberAssignmentMapping mapping) =>
_delegateMappings.Contains(mapping) || _parent?.HasMemberMapping(mapping) == true;
}
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs
Expand Up @@ -63,7 +63,7 @@ ITypeSymbol targetType
public override ExpressionSyntax Build(TypeMappingBuildContext ctx) =>
Invocation(MethodName, SourceParameter.WithArgument(ctx.Source), ReferenceHandlerParameter?.WithArgument(ctx.ReferenceHandler));

public virtual MethodDeclarationSyntax? BuildMethod(SourceEmitterContext ctx)
public virtual MethodDeclarationSyntax BuildMethod(SourceEmitterContext ctx)
{
var returnType = FullyQualifiedIdentifier(_returnType);

Expand Down
@@ -1,7 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Descriptors.Mappings.MemberMappings;
using Riok.Mapperly.Emit;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.SyntaxFactoryHelper;

Expand All @@ -27,12 +26,6 @@ public NewValueTupleExpressionMapping(ITypeSymbol sourceType, ITypeSymbol target

public void AddConstructorParameterMapping(ValueTupleConstructorParameterMapping mapping) => _constructorPropertyMappings.Add(mapping);

public override MethodDeclarationSyntax? BuildMethod(SourceEmitterContext ctx)
{
// generate method body if complex mapping otherwise return null
return HasMemberMappings() ? base.BuildMethod(ctx) : null;
}

public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
// generate error if constructor argument don't match
Expand All @@ -41,15 +34,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
return ThrowNotImplementedException().WithLeadingTrivia(TriviaList(Comment(NoMappingComment)));

Check warning on line 34 in src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleExpressionMapping.cs

View check run for this annotation

Codecov / codecov/patch

src/Riok.Mapperly/Descriptors/Mappings/NewValueTupleExpressionMapping.cs#L33-L34

Added lines #L33 - L34 were not covered by tests
}

// generate method call if it's a complex mapping
if (HasMemberMappings())
{
return base.Build(ctx);
}

// (Name:.. ,..);
var ctorArgs = _constructorPropertyMappings.Select(x => x.BuildArgument(ctx, emitFieldName: true));
return TupleExpression(SeparatedList(ctorArgs));
return base.Build(ctx);
}

public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext ctx)
Expand All @@ -65,21 +50,12 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c
var ctorArgs = _constructorPropertyMappings.Select(x => x.BuildArgument(ctx, emitFieldName: true));
var tupleCreationExpression = TupleExpression(SeparatedList(ctorArgs));

// return (Name:.. ,..); if not a complex mapping
if (!HasMemberMappings())
{
yield return ReturnStatement(tupleCreationExpression);
yield break;
}

// generate
// var target = (Name:.. ,..);
// target.Name.Child = ...
// return target
var targetVariableName = ctx.NameBuilder.New(TargetVariableName);
yield return DeclareLocalVariable(targetVariableName, tupleCreationExpression);

// map properties
// target.Name.Child = ...
foreach (var expression in BuildBody(ctx, IdentifierName(targetVariableName)))
{
yield return expression;
Expand Down
Expand Up @@ -19,8 +19,6 @@ protected ObjectMemberMethodMapping(ITypeSymbol sourceType, ITypeSymbol targetTy
_mapping = new ObjectMemberExistingTargetMapping(sourceType, targetType);
}

public bool HasMemberMappings() => _mapping.HasMemberMappings();

public bool HasMemberMapping(IMemberAssignmentMapping mapping) => _mapping.HasMemberMapping(mapping);

public void AddMemberMapping(IMemberAssignmentMapping mapping) => _mapping.AddMemberMapping(mapping);
Expand Down
Expand Up @@ -30,8 +30,8 @@ ITypeSymbol objectType

public GenericMappingTypeParameters TypeParameters { get; }

public override MethodDeclarationSyntax? BuildMethod(SourceEmitterContext ctx) =>
base.BuildMethod(ctx)?.WithTypeParameterList(TypeParameterList(TypeParameters.SourceType, TypeParameters.TargetType));
public override MethodDeclarationSyntax BuildMethod(SourceEmitterContext ctx) =>
base.BuildMethod(ctx).WithTypeParameterList(TypeParameterList(TypeParameters.SourceType, TypeParameters.TargetType));

protected override ExpressionSyntax BuildTargetType()
{
Expand Down
3 changes: 1 addition & 2 deletions src/Riok.Mapperly/Emit/SourceEmitter.cs
@@ -1,7 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Descriptors;
using Riok.Mapperly.Helpers;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.SyntaxFactoryHelper;

Expand All @@ -24,7 +23,7 @@ public static CompilationUnitSyntax Build(MapperDescriptor descriptor)

private static IEnumerable<MemberDeclarationSyntax> BuildMembers(MapperDescriptor descriptor, SourceEmitterContext sourceEmitterContext)
{
return descriptor.MethodTypeMappings.Select(mapping => mapping.BuildMethod(sourceEmitterContext)).WhereNotNull();
return descriptor.MethodTypeMappings.Select(mapping => mapping.BuildMethod(sourceEmitterContext));
}

private static MemberDeclarationSyntax WrapInClassesAsNeeded(INamedTypeSymbol symbol, MemberDeclarationSyntax syntax)
Expand Down
Expand Up @@ -35,7 +35,7 @@ public static partial class DeepCloningMapper

if (src.TupleValue != null)
{
target.TupleValue = (A: src.TupleValue.Value.A, src.TupleValue.Value.Item2);
target.TupleValue = MapToValueTuple(src.TupleValue.Value);
}

if (src.RecursiveObject != null)
Expand Down Expand Up @@ -111,6 +111,12 @@ public static partial class DeepCloningMapper
return target;
}

private static (string A, string) MapToValueTuple((string A, string) source)
{
var target = (A: source.A, source.Item2);
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();
Expand All @@ -119,4 +125,4 @@ public static partial class DeepCloningMapper
return target;
}
}
}
}
Expand Up @@ -80,7 +80,7 @@ public partial class TestMapper

if (testObject.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(testObject.TupleValue.Value.A), ParseableInt(testObject.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(testObject.TupleValue.Value);
}

if (testObject.RecursiveObject != null)
Expand Down Expand Up @@ -164,7 +164,7 @@ public partial class TestMapper

if (dto.TupleValue != null)
{
target.TupleValue = (A: dto.TupleValue.Value.A.ToString(), dto.TupleValue.Value.Item2.ToString());
target.TupleValue = MapToValueTuple1(dto.TupleValue.Value);
}

if (dto.RecursiveObject != null)
Expand Down Expand Up @@ -255,7 +255,7 @@ public partial class TestMapper

if (source.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(source.TupleValue.Value.A), ParseableInt(source.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(source.TupleValue.Value);
}

if (source.RecursiveObject != null)
Expand Down Expand Up @@ -342,6 +342,12 @@ public partial class TestMapper
return target;
}

private (int A, int) MapToValueTuple((string A, string) source)
{
var target = (A: ParseableInt(source.A), ParseableInt(source.Item2));
return target;
}

private int[] MapToInt32Array(global::System.Span<string> source)
{
var target = new int[source.Length];
Expand Down Expand Up @@ -401,6 +407,12 @@ private string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnu
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<global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested> MapToIReadOnlyCollection(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source)
{
var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length];
Expand Down Expand Up @@ -453,4 +465,4 @@ private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumD
return target;
}
}
}
}
Expand Up @@ -79,7 +79,7 @@ public static partial class StaticTestMapper

if (src.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(src.TupleValue.Value.A), ParseableInt(src.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(src.TupleValue.Value);
}

if (src.RecursiveObject != null)
Expand Down Expand Up @@ -178,7 +178,7 @@ public static partial class StaticTestMapper

if (testObject.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(testObject.TupleValue.Value.A), ParseableInt(testObject.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(testObject.TupleValue.Value);
}

if (testObject.RecursiveObject != null)
Expand Down Expand Up @@ -262,7 +262,7 @@ public static partial class StaticTestMapper

if (dto.TupleValue != null)
{
target.TupleValue = (A: dto.TupleValue.Value.A.ToString(), dto.TupleValue.Value.Item2.ToString());
target.TupleValue = MapToValueTuple1(dto.TupleValue.Value);
}

if (dto.RecursiveObject != null)
Expand Down Expand Up @@ -353,7 +353,7 @@ public static partial class StaticTestMapper

if (source.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(source.TupleValue.Value.A), ParseableInt(source.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(source.TupleValue.Value);
}

if (source.RecursiveObject != null)
Expand Down Expand Up @@ -579,6 +579,12 @@ public static partial class StaticTestMapper
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 int[] MapToInt32Array(global::System.Span<string> source)
{
var target = new int[source.Length];
Expand Down Expand Up @@ -638,6 +644,12 @@ private static string MapToString(global::Riok.Mapperly.IntegrationTests.Models.
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<global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested> MapToIReadOnlyCollection(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source)
{
var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length];
Expand Down Expand Up @@ -690,4 +702,4 @@ private static string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.Te
return target;
}
}
}
}
Expand Up @@ -32,7 +32,7 @@ public static partial class DeepCloningMapper

if (src.TupleValue != null)
{
target.TupleValue = (A: src.TupleValue.Value.A, src.TupleValue.Value.Item2);
target.TupleValue = MapToValueTuple(src.TupleValue.Value);
}

if (src.RecursiveObject != null)
Expand Down Expand Up @@ -110,6 +110,12 @@ public static partial class DeepCloningMapper
return target;
}

private static (string A, string) MapToValueTuple((string A, string) source)
{
var target = (A: source.A, source.Item2);
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();
Expand All @@ -118,4 +124,4 @@ public static partial class DeepCloningMapper
return target;
}
}
}
}
Expand Up @@ -77,7 +77,7 @@ public partial class TestMapper

if (testObject.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(testObject.TupleValue.Value.A), ParseableInt(testObject.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(testObject.TupleValue.Value);
}

if (testObject.RecursiveObject != null)
Expand Down Expand Up @@ -160,7 +160,7 @@ public partial class TestMapper

if (dto.TupleValue != null)
{
target.TupleValue = (A: dto.TupleValue.Value.A.ToString(), dto.TupleValue.Value.Item2.ToString());
target.TupleValue = MapToValueTuple1(dto.TupleValue.Value);
}

if (dto.RecursiveObject != null)
Expand Down Expand Up @@ -253,7 +253,7 @@ public partial class TestMapper

if (source.TupleValue != null)
{
target.TupleValue = (A: ParseableInt(source.TupleValue.Value.A), ParseableInt(source.TupleValue.Value.Item2));
target.TupleValue = MapToValueTuple(source.TupleValue.Value);
}

if (source.RecursiveObject != null)
Expand Down Expand Up @@ -342,6 +342,12 @@ public partial class TestMapper
return target;
}

private (int A, int) MapToValueTuple((string A, string) source)
{
var target = (A: ParseableInt(source.A), ParseableInt(source.Item2));
return target;
}

private int[] MapToInt32Array(global::System.Span<string> source)
{
var target = new int[source.Length];
Expand Down Expand Up @@ -401,6 +407,12 @@ private string MapToString(global::Riok.Mapperly.IntegrationTests.Models.TestEnu
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<global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested> MapToIReadOnlyCollection(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectNestedDto[] source)
{
var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObjectNested[source.Length];
Expand Down Expand Up @@ -453,4 +465,4 @@ private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumD
return target;
}
}
}
}

0 comments on commit d384b57

Please sign in to comment.