Skip to content

Commit

Permalink
fix: try refactor and break tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Sep 27, 2023
1 parent 757dcd0 commit 99afa22
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void AddNullDelegateMemberAssignmentMapping(IMemberAssignmentMapping memb

private void AddMemberAssignmentMapping(IMemberAssignmentMappingContainer container, IMemberAssignmentMapping mapping)
{
SetSourceMemberMapped(mapping.SourcePath);
SetMembersMapped(mapping);
AddNullMemberInitializers(container, mapping.TargetPath);
container.AddMemberMapping(mapping);
}
Expand All @@ -43,15 +43,8 @@ private void AddNullMemberInitializers(IMemberAssignmentMappingContainer contain
continue;

// if member is initialised with a non null value then skip
if (
container.TryGetMemberMapping(nullablePath) is MemberAssignmentMapping
{
Mapping: MemberMapping { NullConditionalAccess: false }
}
)
{
if (NotNullInitializedTargetMemberNames.Contains(path.Path.First().Name))
continue;
}

if (!BuilderContext.SymbolAccessor.HasAccessibleParameterlessConstructor(type))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Configuration;
using Riok.Mapperly.Descriptors.Mappings;
using Riok.Mapperly.Descriptors.Mappings.MemberMappings;
using Riok.Mapperly.Diagnostics;
using Riok.Mapperly.Helpers;
using Riok.Mapperly.Symbols;
Expand Down Expand Up @@ -72,6 +73,8 @@ protected MembersMappingBuilderContext(MappingBuilderContext builderContext, T m

public Dictionary<string, List<PropertyMappingConfiguration>> MemberConfigsByRootTargetName { get; }

public readonly HashSet<string> NotNullInitializedTargetMemberNames = new();

public void AddDiagnostics()
{
AddUnmatchedIgnoredTargetMembersDiagnostics();
Expand All @@ -82,7 +85,19 @@ public void AddDiagnostics()
AddMappedAndIgnoredTargetMembersDiagnostics();
}

protected void SetSourceMemberMapped(MemberPath sourcePath) => _unmappedSourceMemberNames.Remove(sourcePath.Path.First().Name);
protected void SetMembersMapped(IMemberMapping mapping)
{
_unmappedSourceMemberNames.Remove(mapping.SourcePath.Path.First().Name);
}

protected void SetMembersMapped(IMemberAssignmentMapping assignmentMapping)
{
_unmappedSourceMemberNames.Remove(assignmentMapping.SourcePath.Path.First().Name);
if (!assignmentMapping.TargetType.IsNullable())
{
NotNullInitializedTargetMemberNames.Add(assignmentMapping.TargetPath.Path.First().Name);
}
}

private HashSet<string> InitIgnoredUnmatchedProperties(IEnumerable<string> allProperties, IEnumerable<string> mappedProperties)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public NewInstanceBuilderContext(MappingBuilderContext builderContext, T mapping

public void AddInitMemberMapping(MemberAssignmentMapping mapping)
{
SetSourceMemberMapped(mapping.SourcePath);
SetMembersMapped(mapping);
Mapping.AddInitMemberMapping(mapping);
}

public void AddConstructorParameterMapping(ConstructorParameterMapping mapping)
{
MemberConfigsByRootTargetName.Remove(mapping.Parameter.Name);
SetSourceMemberMapped(mapping.DelegateMapping.SourcePath);
SetMembersMapped(mapping.DelegateMapping);
Mapping.AddConstructorParameterMapping(mapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public NewInstanceContainerBuilderContext(MappingBuilderContext builderContext,

public void AddInitMemberMapping(MemberAssignmentMapping mapping)
{
SetSourceMemberMapped(mapping.SourcePath);
SetMembersMapped(mapping);
Mapping.AddInitMemberMapping(mapping);
}

public void AddConstructorParameterMapping(ConstructorParameterMapping mapping)
{
MemberConfigsByRootTargetName.Remove(mapping.Parameter.Name);
SetSourceMemberMapped(mapping.DelegateMapping.SourcePath);
SetMembersMapped(mapping.DelegateMapping);
Mapping.AddConstructorParameterMapping(mapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public NewValueTupleConstructorBuilderContext(MappingBuilderContext builderConte
public void AddTupleConstructorParameterMapping(ValueTupleConstructorParameterMapping mapping)
{
MemberConfigsByRootTargetName.Remove(mapping.Parameter.Name);
SetSourceMemberMapped(mapping.DelegateMapping.SourcePath);
SetMembersMapped(mapping.DelegateMapping);
Mapping.AddConstructorParameterMapping(mapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void AddTupleConstructorParameterMapping(ValueTupleConstructorParameterMa
}
}

SetSourceMemberMapped(mapping.DelegateMapping.SourcePath);
SetMembersMapped(mapping.DelegateMapping);
Mapping.AddConstructorParameterMapping(mapping);
}
}
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Descriptors/MappingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class MappingCollection
return mapping;
}

public void EnqueueToBuildBody(IMapping mapping, MappingBuilderContext ctx) =>
public void EnqueueToBuildBody(ITypeMapping mapping, MappingBuilderContext ctx) =>
_mappingsToBuildBody.Enqueue((mapping, ctx), mapping.BodyBuildingPriority);

public void Add(ITypeMapping mapping)
Expand Down
2 changes: 0 additions & 2 deletions src/Riok.Mapperly/Descriptors/Mappings/IMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ public interface IMapping
ITypeSymbol SourceType { get; }

ITypeSymbol TargetType { get; }

MappingBodyBuildingPriority BodyBuildingPriority { get; }
}
2 changes: 2 additions & 0 deletions src/Riok.Mapperly/Descriptors/Mappings/ITypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface ITypeMapping : IMapping
/// Gets a value indicating whether this mapping produces any code or can be omitted completely (eg. direct assignments or delegate mappings).
/// </summary>
bool IsSynthetic { get; }

MappingBodyBuildingPriority BodyBuildingPriority { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings;
/// <summary>
/// Represents a member assignment mapping or a container of member assignment mappings.
/// </summary>
public interface IMemberAssignmentMapping
public interface IMemberAssignmentMapping : IMapping
{
MemberPath SourcePath { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings;
/// Represents a member mapping which accesses a source member and maps it to a certain type.
/// (eg. <c>MapToC(source.A.B)</c>)
/// </summary>
public interface IMemberMapping
public interface IMemberMapping : IMapping
{
MemberPath SourcePath { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Symbols;
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper;
Expand All @@ -12,25 +13,29 @@ namespace Riok.Mapperly.Descriptors.Mappings.MemberMappings;
[DebuggerDisplay("MemberAssignmentMapping({SourcePath.FullName} => {TargetPath.FullName})")]
public class MemberAssignmentMapping : IMemberAssignmentMapping
{
private readonly IMemberMapping _mapping;

public MemberAssignmentMapping(MemberPath targetPath, IMemberMapping mapping)
{
TargetPath = targetPath;
Mapping = mapping;
_mapping = mapping;
}

public MemberPath SourcePath => Mapping.SourcePath;

public IMemberMapping Mapping { get; }
public MemberPath SourcePath => _mapping.SourcePath;

public MemberPath TargetPath { get; }

public ITypeSymbol SourceType => _mapping.SourceType;

public ITypeSymbol TargetType => _mapping.TargetType;

public IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx, ExpressionSyntax targetAccess) =>
ctx.SyntaxFactory.SingleStatement(BuildExpression(ctx, targetAccess));

public ExpressionSyntax BuildExpression(TypeMappingBuildContext ctx, ExpressionSyntax? targetAccess)
{
var targetMemberAccess = TargetPath.BuildAccess(targetAccess);
var mappedValue = Mapping.Build(ctx);
var mappedValue = _mapping.Build(ctx);

// target.Member = mappedValue;
return Assignment(targetMemberAccess, mappedValue);
Expand All @@ -54,7 +59,7 @@ public override int GetHashCode()
{
unchecked
{
var hashCode = Mapping.GetHashCode();
var hashCode = _mapping.GetHashCode();
hashCode = (hashCode * 397) ^ SourcePath.GetHashCode();
hashCode = (hashCode * 397) ^ TargetPath.GetHashCode();
return hashCode;
Expand All @@ -67,6 +72,6 @@ public override int GetHashCode()

protected bool Equals(MemberAssignmentMapping other)
{
return Mapping.Equals(other.Mapping) && SourcePath.Equals(other.SourcePath) && TargetPath.Equals(other.TargetPath);
return _mapping.Equals(other._mapping) && SourcePath.Equals(other.SourcePath) && TargetPath.Equals(other.TargetPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
using Riok.Mapperly.Symbols;
Expand All @@ -22,6 +23,10 @@ public MemberExistingTargetMapping(IExistingTargetMapping delegateMapping, Membe

public MemberPath TargetPath { get; }

public ITypeSymbol SourceType => _delegateMapping.SourceType;

public ITypeSymbol TargetType => _delegateMapping.TargetType;

public IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx, ExpressionSyntax targetAccess)
{
var source = SourcePath.BuildAccess(ctx.Source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Symbols;

Expand Down Expand Up @@ -29,6 +30,10 @@ bool addValuePropertyOnNullable

public MemberPath SourcePath { get; }

public ITypeSymbol SourceType => _delegateMapping.SourceType;

public ITypeSymbol TargetType => _delegateMapping.TargetType;

public ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
ctx = ctx.WithSource(SourcePath.BuildAccess(ctx.Source, _addValuePropertyOnNullable, NullConditionalAccess));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ bool useNullConditionalAccess

public MemberPath SourcePath { get; }

public ITypeSymbol SourceType => _delegateMapping.SourceType;

public ITypeSymbol TargetType => _delegateMapping.TargetType;

public ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
// the source type of the delegate mapping is nullable or the source path is not nullable
Expand Down

0 comments on commit 99afa22

Please sign in to comment.