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 20, 2023
1 parent 0d960a9 commit 3eee96d
Show file tree
Hide file tree
Showing 21 changed files with 332 additions and 110 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public partial int ParseableInt(string value)

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 int ParseableInt(string value)

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 void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.Test

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 @@ private partial int PrivateDirectInt(int value)
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static partial int ParseableInt(string value)

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 int ParseableInt(string value)

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 int ParseableInt(string value)

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 void UpdateDto(global::Riok.Mapperly.IntegrationTests.Mode

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 @@ object x when typeof(TTarget).IsAssignableFrom(typeof(object)) => (TTarget)(obje
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ 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);
Expand All @@ -32,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 @@ -110,6 +113,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 +127,4 @@ public static partial class DeepCloningMapper
return target;
}
}
}
}
Loading

0 comments on commit 3eee96d

Please sign in to comment.