diff --git a/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs b/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
index 5e1ca3cbe9..afcfd9064a 100644
--- a/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
+++ b/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
@@ -8,18 +8,11 @@ namespace Riok.Mapperly.Configuration;
///
/// Creates instances by resolving attribute data from provided symbols.
///
-public class AttributeDataAccessor
+public class AttributeDataAccessor(SymbolAccessor symbolAccessor)
{
private const string NameOfOperatorName = "nameof";
private const char FullNameOfPrefix = '@';
- private readonly SymbolAccessor _symbolAccessor;
-
- public AttributeDataAccessor(SymbolAccessor symbolAccessor)
- {
- _symbolAccessor = symbolAccessor;
- }
-
public TData AccessSingle(ISymbol symbol)
where TAttribute : Attribute
where TData : notnull => Access(symbol).Single();
@@ -29,7 +22,7 @@ public TData AccessSingle(ISymbol symbol)
where TData : notnull => Access(symbol).FirstOrDefault();
public bool HasAttribute(ISymbol symbol)
- where TAttribute : Attribute => _symbolAccessor.HasAttribute(symbol);
+ where TAttribute : Attribute => symbolAccessor.HasAttribute(symbol);
public IEnumerable Access(ISymbol symbol)
where TAttribute : Attribute => Access(symbol);
@@ -49,7 +42,7 @@ public IEnumerable Access(ISymbol symbol)
where TAttribute : Attribute
where TData : notnull
{
- var attrDatas = _symbolAccessor.GetAttributes(symbol);
+ var attrDatas = symbolAccessor.GetAttributes(symbol);
foreach (var attrData in attrDatas)
{
yield return Access(attrData);
diff --git a/src/Riok.Mapperly/Configuration/EnumConfiguration.cs b/src/Riok.Mapperly/Configuration/EnumConfiguration.cs
index 054751b447..8ea51ae569 100644
--- a/src/Riok.Mapperly/Configuration/EnumConfiguration.cs
+++ b/src/Riok.Mapperly/Configuration/EnumConfiguration.cs
@@ -7,17 +7,12 @@ namespace Riok.Mapperly.Configuration;
/// Represents the
/// with enum values as typed constants.
///
-public class EnumConfiguration
+public class EnumConfiguration(EnumMappingStrategy strategy)
{
- public EnumConfiguration(EnumMappingStrategy strategy)
- {
- Strategy = strategy;
- }
-
///
/// The strategy to be used to map enums.
///
- public EnumMappingStrategy Strategy { get; }
+ public EnumMappingStrategy Strategy { get; } = strategy;
///
/// Whether the case should be ignored during mappings.
diff --git a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs
index cf4452daf6..7cbbd6bfc1 100644
--- a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs
+++ b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs
@@ -12,24 +12,15 @@ namespace Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity;
/// target.EnsureCapacity(source.Length + target.Count);
///
///
-public class EnsureCapacityMember : EnsureCapacityInfo
+public class EnsureCapacityMember(string targetAccessor, string sourceAccessor) : EnsureCapacityInfo
{
- private readonly string _targetAccessor;
- private readonly string _sourceAccessor;
-
- public EnsureCapacityMember(string targetAccessor, string sourceAccessor)
- {
- _targetAccessor = targetAccessor;
- _sourceAccessor = sourceAccessor;
- }
-
public override StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyntax target)
{
return EnsureCapacityStatement(
ctx.SyntaxFactory,
target,
- MemberAccess(ctx.Source, _sourceAccessor),
- MemberAccess(target, _targetAccessor)
+ 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 091c6449d3..c7c09e33d2 100644
--- a/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityNonEnumerated.cs
+++ b/src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityNonEnumerated.cs
@@ -18,21 +18,13 @@ namespace Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity;
/// target.EnsureCapacity(sourceCount + target.Count);
///
///
-public class EnsureCapacityNonEnumerated : EnsureCapacityInfo
+public class EnsureCapacityNonEnumerated(string targetAccessor, IMethodSymbol getNonEnumeratedMethod) : EnsureCapacityInfo
{
private const string SourceCountVariableName = "sourceCount";
- private readonly string _targetAccessor;
- private readonly IMethodSymbol _getNonEnumeratedMethod;
-
- public EnsureCapacityNonEnumerated(string targetAccessor, IMethodSymbol getNonEnumeratedMethod)
- {
- _targetAccessor = targetAccessor;
- _getNonEnumeratedMethod = getNonEnumeratedMethod;
- }
public override StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyntax target)
{
- var targetCount = MemberAccess(target, _targetAccessor);
+ var targetCount = MemberAccess(target, targetAccessor);
var countIdentifier = Identifier(ctx.NameBuilder.New(SourceCountVariableName));
var countIdentifierName = IdentifierName(countIdentifier);
@@ -42,7 +34,7 @@ public override StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyn
var outVarArgument = Argument(DeclarationExpression(VarIdentifier, SingleVariableDesignation(countIdentifier)))
.WithRefOrOutKeyword(TrailingSpacedToken(SyntaxKind.OutKeyword));
- var getNonEnumeratedInvocation = StaticInvocation(_getNonEnumeratedMethod, enumerableArgument, outVarArgument);
+ var getNonEnumeratedInvocation = StaticInvocation(getNonEnumeratedMethod, enumerableArgument, outVarArgument);
var ensureCapacity = EnsureCapacityStatement(ctx.SyntaxFactory.AddIndentation(), target, countIdentifierName, targetCount);
return ctx.SyntaxFactory.If(getNonEnumeratedInvocation, ensureCapacity);
}
diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersContainerBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersContainerBuilderContext.cs
index da1b86e280..ee775e6b9d 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersContainerBuilderContext.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersContainerBuilderContext.cs
@@ -8,14 +8,13 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;
/// An implementation.
///
/// The type of mapping.
-public class MembersContainerBuilderContext : MembersMappingBuilderContext, IMembersContainerBuilderContext
+public class MembersContainerBuilderContext(MappingBuilderContext builderContext, T mapping)
+ : MembersMappingBuilderContext(builderContext, mapping),
+ IMembersContainerBuilderContext
where T : IMemberAssignmentTypeMapping
{
private readonly Dictionary _nullDelegateMappings = new();
- public MembersContainerBuilderContext(MappingBuilderContext builderContext, T mapping)
- : base(builderContext, mapping) { }
-
public void AddMemberAssignmentMapping(IMemberAssignmentMapping memberMapping) => AddMemberAssignmentMapping(Mapping, memberMapping);
public void AddNullDelegateMemberAssignmentMapping(IMemberAssignmentMapping memberMapping)
diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceBuilderContext.cs
index f906fdce1b..cf34999962 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceBuilderContext.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceBuilderContext.cs
@@ -7,12 +7,11 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;
/// An implementation of .
///
/// The type of the mapping.
-public class NewInstanceBuilderContext : MembersMappingBuilderContext, INewInstanceBuilderContext
+public class NewInstanceBuilderContext(MappingBuilderContext builderContext, T mapping)
+ : MembersMappingBuilderContext(builderContext, mapping),
+ INewInstanceBuilderContext
where T : INewInstanceObjectMemberMapping
{
- public NewInstanceBuilderContext(MappingBuilderContext builderContext, T mapping)
- : base(builderContext, mapping) { }
-
public void AddInitMemberMapping(MemberAssignmentMapping mapping)
{
SetSourceMemberMapped(mapping.SourcePath);
diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceContainerBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceContainerBuilderContext.cs
index 349fb8fcaa..725b809782 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceContainerBuilderContext.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewInstanceContainerBuilderContext.cs
@@ -8,12 +8,11 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;
/// which supports containers ().
///
///
-public class NewInstanceContainerBuilderContext : MembersContainerBuilderContext, INewInstanceBuilderContext
+public class NewInstanceContainerBuilderContext(MappingBuilderContext builderContext, T mapping)
+ : MembersContainerBuilderContext(builderContext, mapping),
+ INewInstanceBuilderContext
where T : INewInstanceObjectMemberMapping, IMemberAssignmentTypeMapping
{
- public NewInstanceContainerBuilderContext(MappingBuilderContext builderContext, T mapping)
- : base(builderContext, mapping) { }
-
public void AddInitMemberMapping(MemberAssignmentMapping mapping)
{
SetSourceMemberMapped(mapping.SourcePath);
diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleConstructorBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleConstructorBuilderContext.cs
index 1d93bddf1b..6293643c64 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleConstructorBuilderContext.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleConstructorBuilderContext.cs
@@ -7,12 +7,11 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;
/// An implementation of .
///
/// The type of the mapping.
-public class NewValueTupleConstructorBuilderContext : MembersMappingBuilderContext, INewValueTupleBuilderContext
+public class NewValueTupleConstructorBuilderContext(MappingBuilderContext builderContext, T mapping)
+ : MembersMappingBuilderContext(builderContext, mapping),
+ INewValueTupleBuilderContext
where T : INewValueTupleMapping
{
- public NewValueTupleConstructorBuilderContext(MappingBuilderContext builderContext, T mapping)
- : base(builderContext, mapping) { }
-
public void AddTupleConstructorParameterMapping(ValueTupleConstructorParameterMapping mapping)
{
MemberConfigsByRootTargetName.Remove(mapping.Parameter.Name);
diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleExpressionBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleExpressionBuilderContext.cs
index 4fc59ba51e..2aca5007c4 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleExpressionBuilderContext.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/NewValueTupleExpressionBuilderContext.cs
@@ -7,12 +7,11 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;
/// An implementation of .
///
/// The type of the mapping.
-public class NewValueTupleExpressionBuilderContext : MembersContainerBuilderContext, INewValueTupleBuilderContext
+public class NewValueTupleExpressionBuilderContext(MappingBuilderContext builderContext, T mapping)
+ : MembersContainerBuilderContext(builderContext, mapping),
+ INewValueTupleBuilderContext
where T : INewValueTupleMapping, IMemberAssignmentTypeMapping
{
- public NewValueTupleExpressionBuilderContext(MappingBuilderContext builderContext, T mapping)
- : base(builderContext, mapping) { }
-
public void AddTupleConstructorParameterMapping(ValueTupleConstructorParameterMapping mapping)
{
if (MemberConfigsByRootTargetName.TryGetValue(mapping.Parameter.Name, out var value))
diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MappingBodyBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MappingBodyBuilder.cs
index 5f01f5bff2..9648885876 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MappingBodyBuilder.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MappingBodyBuilder.cs
@@ -7,18 +7,11 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders;
///
/// Builds bodies mappings (the body of the mapping methods).
///
-public class MappingBodyBuilder
+public class MappingBodyBuilder(MappingCollection mappings)
{
- private readonly MappingCollection _mappings;
-
- public MappingBodyBuilder(MappingCollection mappings)
- {
- _mappings = mappings;
- }
-
public void BuildMappingBodies(CancellationToken cancellationToken)
{
- foreach (var (typeMapping, ctx) in _mappings.DequeueMappingsToBuildBody())
+ foreach (var (typeMapping, ctx) in mappings.DequeueMappingsToBuildBody())
{
cancellationToken.ThrowIfCancellationRequested();
diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/ExistingTargetMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/ExistingTargetMappingBuilder.cs
index 3e5010dcf3..b7ec867ac2 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBuilders/ExistingTargetMappingBuilder.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/ExistingTargetMappingBuilder.cs
@@ -2,7 +2,7 @@
namespace Riok.Mapperly.Descriptors.MappingBuilders;
-public class ExistingTargetMappingBuilder
+public class ExistingTargetMappingBuilder(MappingCollection mappings)
{
private delegate IExistingTargetMapping? BuildExistingTargetMapping(MappingBuilderContext context);
@@ -16,16 +16,9 @@ public class ExistingTargetMappingBuilder
NewInstanceObjectPropertyMappingBuilder.TryBuildExistingTargetMapping,
};
- private readonly MappingCollection _mappings;
-
- public ExistingTargetMappingBuilder(MappingCollection mappings)
- {
- _mappings = mappings;
- }
-
public IExistingTargetMapping? Find(TypeMappingKey mappingKey)
{
- return _mappings.FindExistingInstanceMapping(mappingKey);
+ return mappings.FindExistingInstanceMapping(mappingKey);
}
public IExistingTargetMapping? Build(MappingBuilderContext ctx, bool resultIsReusable)
@@ -37,10 +30,10 @@ public ExistingTargetMappingBuilder(MappingCollection mappings)
if (resultIsReusable)
{
- _mappings.AddExistingTargetMapping(mapping, ctx.MappingKey.Configuration);
+ mappings.AddExistingTargetMapping(mapping, ctx.MappingKey.Configuration);
}
- _mappings.EnqueueToBuildBody(mapping, ctx);
+ mappings.EnqueueToBuildBody(mapping, ctx);
return mapping;
}
diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs
index 1f159c7107..82fc8a4d89 100644
--- a/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs
+++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs
@@ -3,7 +3,7 @@
namespace Riok.Mapperly.Descriptors.MappingBuilders;
-public class MappingBuilder
+public class MappingBuilder(MappingCollection mappings)
{
private delegate INewInstanceMapping? BuildMapping(MappingBuilderContext context);
@@ -31,17 +31,10 @@ public class MappingBuilder
NewInstanceObjectPropertyMappingBuilder.TryBuildMapping,
};
- private readonly MappingCollection _mappings;
-
- public MappingBuilder(MappingCollection mappings)
- {
- _mappings = mappings;
- }
-
///
- public IReadOnlyCollection UserMappings => _mappings.UserMappings;
+ public IReadOnlyCollection UserMappings => mappings.UserMappings;
- public INewInstanceMapping? Find(TypeMappingKey mapping) => _mappings.Find(mapping);
+ public INewInstanceMapping? Find(TypeMappingKey mapping) => mappings.Find(mapping);
public INewInstanceMapping? Build(MappingBuilderContext ctx, bool resultIsReusable)
{
@@ -52,10 +45,10 @@ public MappingBuilder(MappingCollection mappings)
if (resultIsReusable)
{
- _mappings.AddNewInstanceMapping(mapping, ctx.MappingKey.Configuration);
+ mappings.AddNewInstanceMapping(mapping, ctx.MappingKey.Configuration);
}
- _mappings.EnqueueToBuildBody(mapping, ctx);
+ mappings.EnqueueToBuildBody(mapping, ctx);
return mapping;
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs
index 86613f96bf..d213e3e43c 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs
@@ -8,13 +8,10 @@ namespace Riok.Mapperly.Descriptors.Mappings;
///
/// Represents a mapping from an array to an array of the same type by using Array.Clone.
///
-public class ArrayCloneMapping : NewInstanceMapping
+public class ArrayCloneMapping(ITypeSymbol sourceType, ITypeSymbol targetType) : NewInstanceMapping(sourceType, targetType)
{
private const string CloneMethodName = nameof(Array.Clone);
- public ArrayCloneMapping(ITypeSymbol sourceType, ITypeSymbol targetType)
- : base(sourceType, targetType) { }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
return CastExpression(FullyQualifiedIdentifier(TargetType), InvocationExpression(MemberAccess(ctx.Source, CloneMethodName)));
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs
index 575702c6bb..7e5801bc29 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForEachMapping.cs
@@ -10,30 +10,18 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// Represents an enumerable to array mapping which works by initialising an array, looping through the source,
/// mapping each element and adding it to the target array.
///
-public class ArrayForEachMapping : MethodMapping
+public class ArrayForEachMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ INewInstanceMapping elementMapping,
+ ITypeSymbol targetArrayElementType,
+ string countPropertyName
+) : MethodMapping(sourceType, targetType)
{
private const string TargetVariableName = "target";
private const string LoopItemVariableName = "item";
private const string LoopCounterName = "i";
- private readonly INewInstanceMapping _elementMapping;
- private readonly ITypeSymbol _targetArrayElementType;
- private readonly string _countPropertyName;
-
- public ArrayForEachMapping(
- ITypeSymbol sourceType,
- ITypeSymbol targetType,
- INewInstanceMapping elementMapping,
- ITypeSymbol targetArrayElementType,
- string countPropertyName
- )
- : base(sourceType, targetType)
- {
- _elementMapping = elementMapping;
- _targetArrayElementType = targetArrayElementType;
- _countPropertyName = countPropertyName;
- }
-
public override IEnumerable BuildBody(TypeMappingBuildContext ctx)
{
var targetVariableName = ctx.NameBuilder.New(TargetVariableName);
@@ -41,10 +29,10 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c
// var target = new T[source.Count];
var sourceLengthArrayRank = ArrayRankSpecifier(
- SingletonSeparatedList(MemberAccess(ctx.Source, _countPropertyName))
+ SingletonSeparatedList(MemberAccess(ctx.Source, countPropertyName))
);
var targetInitializationValue = CreateArray(
- ArrayType(FullyQualifiedIdentifier(_targetArrayElementType)).WithRankSpecifiers(SingletonList(sourceLengthArrayRank))
+ ArrayType(FullyQualifiedIdentifier(targetArrayElementType)).WithRankSpecifiers(SingletonList(sourceLengthArrayRank))
);
yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, targetInitializationValue);
@@ -53,7 +41,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c
// target[i] = Map(item);
var (loopItemCtx, loopItemVariableName) = ctx.WithNewSource(LoopItemVariableName);
- var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx.AddIndentation());
+ var convertedSourceItemExpression = elementMapping.Build(loopItemCtx.AddIndentation());
var assignment = Assignment(
ElementAccess(IdentifierName(targetVariableName), IdentifierName(loopCounterVariableName)),
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs
index 064d1196e2..ea75711b2b 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs
@@ -5,27 +5,17 @@
namespace Riok.Mapperly.Descriptors.Mappings;
-public class ArrayForMapping : MethodMapping
+public class ArrayForMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ INewInstanceMapping elementMapping,
+ ITypeSymbol targetArrayElementType
+) : MethodMapping(sourceType, targetType)
{
private const string TargetVariableName = "target";
private const string LoopCounterName = "i";
private const string ArrayLengthProperty = nameof(Array.Length);
- private readonly INewInstanceMapping _elementMapping;
- private readonly ITypeSymbol _targetArrayElementType;
-
- public ArrayForMapping(
- ITypeSymbol sourceType,
- ITypeSymbol targetType,
- INewInstanceMapping elementMapping,
- ITypeSymbol targetArrayElementType
- )
- : base(sourceType, targetType)
- {
- _elementMapping = elementMapping;
- _targetArrayElementType = targetArrayElementType;
- }
-
public override IEnumerable BuildBody(TypeMappingBuildContext ctx)
{
var targetVariableName = ctx.NameBuilder.New(TargetVariableName);
@@ -36,13 +26,13 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c
SingletonSeparatedList(MemberAccess(ctx.Source, ArrayLengthProperty))
);
var targetInitializationValue = CreateArray(
- ArrayType(FullyQualifiedIdentifier(_targetArrayElementType)).WithRankSpecifiers(SingletonList(sourceLengthArrayRank))
+ ArrayType(FullyQualifiedIdentifier(targetArrayElementType)).WithRankSpecifiers(SingletonList(sourceLengthArrayRank))
);
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.AddIndentation());
+ var mappedIndexedSourceValue = elementMapping.Build(forLoopBuilderCtx.AddIndentation());
var assignment = Assignment(
ElementAccess(IdentifierName(targetVariableName), IdentifierName(loopCounterVariableName)),
mappedIndexedSourceValue
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs
index d6ede730c3..5f9e24c646 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs
@@ -8,19 +8,12 @@ namespace Riok.Mapperly.Descriptors.Mappings;
///
/// Represents a cast mapping.
///
-public class CastMapping : NewInstanceMapping
+public class CastMapping(ITypeSymbol sourceType, ITypeSymbol targetType, INewInstanceMapping? delegateMapping = null)
+ : NewInstanceMapping(sourceType, targetType)
{
- private readonly INewInstanceMapping? _delegateMapping;
-
- public CastMapping(ITypeSymbol sourceType, ITypeSymbol targetType, INewInstanceMapping? delegateMapping = null)
- : base(sourceType, targetType)
- {
- _delegateMapping = delegateMapping;
- }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
- var objToCast = _delegateMapping != null ? _delegateMapping.Build(ctx) : ctx.Source;
+ var objToCast = delegateMapping != null ? delegateMapping.Build(ctx) : ctx.Source;
return CastExpression(FullyQualifiedIdentifier(TargetType), objToCast);
}
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs
index 79256ab9e7..1773dba296 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs
@@ -7,10 +7,7 @@ namespace Riok.Mapperly.Descriptors.Mappings;
///
/// Represents a mapping where the target type has the source as single ctor argument.
///
-public class CtorMapping : NewInstanceMapping
+public class CtorMapping(ITypeSymbol sourceType, ITypeSymbol targetType) : NewInstanceMapping(sourceType, targetType)
{
- public CtorMapping(ITypeSymbol sourceType, ITypeSymbol targetType)
- : base(sourceType, targetType) { }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx) => CreateInstance(TargetType, ctx.Source);
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DelegateMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DelegateMapping.cs
index d8c1acbcea..ba6a57a382 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/DelegateMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/DelegateMapping.cs
@@ -9,15 +9,8 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// target = Map(source);
///
///
-public class DelegateMapping : NewInstanceMapping
+public class DelegateMapping(ITypeSymbol sourceType, ITypeSymbol targetType, INewInstanceMapping delegateMapping)
+ : NewInstanceMapping(sourceType, targetType)
{
- private readonly INewInstanceMapping _delegateMapping;
-
- public DelegateMapping(ITypeSymbol sourceType, ITypeSymbol targetType, INewInstanceMapping delegateMapping)
- : base(sourceType, targetType)
- {
- _delegateMapping = delegateMapping;
- }
-
- public override ExpressionSyntax Build(TypeMappingBuildContext ctx) => _delegateMapping.Build(ctx);
+ public override ExpressionSyntax Build(TypeMappingBuildContext ctx) => delegateMapping.Build(ctx);
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs
index 0fb5e0ee7a..4b8fdbba52 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs
@@ -9,24 +9,16 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// A derived type mapping maps one base type or interface to another
/// by implementing a if with instance checks over known types and performs the provided mapping for each type.
///
-public class DerivedTypeIfExpressionMapping : NewInstanceMapping
+public class DerivedTypeIfExpressionMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ IReadOnlyCollection typeMappings
+) : NewInstanceMapping(sourceType, targetType)
{
- private readonly IReadOnlyCollection _typeMappings;
-
- public DerivedTypeIfExpressionMapping(
- ITypeSymbol sourceType,
- ITypeSymbol targetType,
- IReadOnlyCollection typeMappings
- )
- : base(sourceType, targetType)
- {
- _typeMappings = typeMappings;
- }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
// source is A x ? MapToA(x) :
- var typeExpressions = _typeMappings
+ var typeExpressions = typeMappings
.Reverse()
.Aggregate(
DefaultLiteral(),
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs
index bc4a55aa2a..cbe11bf1ab 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeSwitchMapping.cs
@@ -10,18 +10,11 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// A derived type mapping maps one base type or interface to another
/// by implementing a type switch over known types and performs the provided mapping for each type.
///
-public class DerivedTypeSwitchMapping : NewInstanceMapping
+public class DerivedTypeSwitchMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IReadOnlyCollection typeMappings)
+ : NewInstanceMapping(sourceType, targetType)
{
private const string GetTypeMethodName = nameof(GetType);
- private readonly IReadOnlyCollection _typeMappings;
-
- public DerivedTypeSwitchMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IReadOnlyCollection typeMappings)
- : base(sourceType, targetType)
- {
- _typeMappings = typeMappings;
- }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
// _ => throw new ArgumentException(msg, nameof(ctx.Source)),
@@ -36,9 +29,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
// source switch { A x => MapToADto(x), B x => MapToBDto(x) }
var (typeArmContext, typeArmVariableName) = ctx.WithNewSource();
- var arms = _typeMappings
- .Select(x => BuildSwitchArm(typeArmVariableName, x.SourceType, x.Build(typeArmContext)))
- .Append(fallbackArm);
+ var arms = typeMappings.Select(x => BuildSwitchArm(typeArmVariableName, x.SourceType, x.Build(typeArmContext))).Append(fallbackArm);
return ctx.SyntaxFactory.Switch(ctx.Source, arms);
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DirectAssignmentMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DirectAssignmentMapping.cs
index d628563250..3cbf793fda 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/DirectAssignmentMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/DirectAssignmentMapping.cs
@@ -7,11 +7,8 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// Represents a direct assignment mapping.
/// Source and target types need to be the same types.
///
-public class DirectAssignmentMapping : NewInstanceMapping
+public class DirectAssignmentMapping(ITypeSymbol type) : NewInstanceMapping(type, type)
{
- public DirectAssignmentMapping(ITypeSymbol type)
- : base(type, type) { }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx) => ctx.Source;
public override bool IsSynthetic => true;
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs
index cdcfb073e0..b6a1a9042a 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumCastMapping.cs
@@ -9,12 +9,14 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums;
/// An enum cast mapping which casts the source to the target type and optionally checks whether the target is defined.
/// If it is not defined an optional fallback value is used.
///
-public class EnumCastMapping : CastMapping
+public class EnumCastMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ EnumCastMapping.CheckDefinedMode checkDefinedMode,
+ IReadOnlyCollection targetEnumMembers,
+ EnumFallbackValueMapping fallback
+) : CastMapping(sourceType, targetType)
{
- private readonly CheckDefinedMode _checkDefinedMode;
- private readonly IReadOnlyCollection _targetEnumMembers;
- private readonly EnumFallbackValueMapping _fallback;
-
public enum CheckDefinedMode
{
///
@@ -33,34 +35,20 @@ public enum CheckDefinedMode
Flags
}
- public EnumCastMapping(
- ITypeSymbol sourceType,
- ITypeSymbol targetType,
- CheckDefinedMode checkDefinedMode,
- IReadOnlyCollection targetEnumMembers,
- EnumFallbackValueMapping fallback
- )
- : base(sourceType, targetType)
- {
- _checkDefinedMode = checkDefinedMode;
- _targetEnumMembers = targetEnumMembers;
- _fallback = fallback;
- }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
var casted = base.Build(ctx);
- if (_checkDefinedMode == CheckDefinedMode.NoCheck)
+ if (checkDefinedMode == CheckDefinedMode.NoCheck)
return casted;
var valueDefinedCondition = BuildIsDefinedCondition(casted);
- return Conditional(valueDefinedCondition, casted, _fallback.Build(ctx));
+ return Conditional(valueDefinedCondition, casted, fallback.Build(ctx));
}
private ExpressionSyntax BuildIsDefinedCondition(ExpressionSyntax convertedSourceValue)
{
- var allEnumMembers = _targetEnumMembers.Select(x => MemberAccess(FullyQualifiedIdentifier(TargetType), x.Name));
- return _checkDefinedMode switch
+ var allEnumMembers = targetEnumMembers.Select(x => MemberAccess(FullyQualifiedIdentifier(TargetType), x.Name));
+ return checkDefinedMode switch
{
// (TargetEnum)v is TargetEnum.A or TargetEnum.B or ...
CheckDefinedMode.Value
@@ -72,7 +60,7 @@ private ExpressionSyntax BuildIsDefinedCondition(ExpressionSyntax convertedSourc
convertedSourceValue,
ParenthesizedExpression(BitwiseAnd(convertedSourceValue, ParenthesizedExpression(BitwiseOr(allEnumMembers))))
),
- _ => throw new ArgumentOutOfRangeException($"{nameof(_checkDefinedMode)} has an unknown value {_checkDefinedMode}")
+ _ => throw new ArgumentOutOfRangeException($"{nameof(checkDefinedMode)} has an unknown value {checkDefinedMode}")
};
}
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs
index cc415523b0..3d95e85d55 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFallbackValueMapping.cs
@@ -9,30 +9,21 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums;
/// Enum fallback value which is used if no enum value matched.
/// Either throws or returns the fallback value.
///
-public class EnumFallbackValueMapping : NewInstanceMapping
+public class EnumFallbackValueMapping(
+ ITypeSymbol source,
+ ITypeSymbol target,
+ INewInstanceMapping? fallbackMapping = null,
+ IFieldSymbol? fallbackMember = null
+) : NewInstanceMapping(source, target)
{
- private readonly INewInstanceMapping? _fallbackMapping;
-
- public EnumFallbackValueMapping(
- ITypeSymbol source,
- ITypeSymbol target,
- INewInstanceMapping? fallbackMapping = null,
- IFieldSymbol? fallbackMember = null
- )
- : base(source, target)
- {
- _fallbackMapping = fallbackMapping;
- FallbackMember = fallbackMember;
- }
-
- public IFieldSymbol? FallbackMember { get; }
+ public IFieldSymbol? FallbackMember { get; } = fallbackMember;
public SwitchExpressionArmSyntax BuildDiscardArm(TypeMappingBuildContext ctx) => SwitchArm(DiscardPattern(), Build(ctx));
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
- if (_fallbackMapping != null)
- return _fallbackMapping.Build(ctx);
+ if (fallbackMapping != null)
+ return fallbackMapping.Build(ctx);
if (FallbackMember == null)
return ThrowArgumentOutOfRangeException(ctx.Source, $"The value of enum {SourceType.Name} is not supported");
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs
index d038a40952..6064060ecc 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringParseMapping.cs
@@ -11,32 +11,23 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums;
/// Less efficient than
/// but works in .
///
-public class EnumFromStringParseMapping : NewInstanceMapping
+public class EnumFromStringParseMapping(ITypeSymbol sourceType, ITypeSymbol targetType, bool genericParseMethodSupported, bool ignoreCase)
+ : NewInstanceMapping(sourceType, targetType)
{
private const string EnumClassName = "System.Enum";
private const string ParseMethodName = "Parse";
- private readonly bool _genericParseMethodSupported;
- private readonly bool _ignoreCase;
-
- public EnumFromStringParseMapping(ITypeSymbol sourceType, ITypeSymbol targetType, bool genericParseMethodSupported, bool ignoreCase)
- : base(sourceType, targetType)
- {
- _genericParseMethodSupported = genericParseMethodSupported;
- _ignoreCase = ignoreCase;
- }
-
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
// System.Enum.Parse(source, ignoreCase)
- if (_genericParseMethodSupported)
+ if (genericParseMethodSupported)
{
return GenericInvocation(
EnumClassName,
ParseMethodName,
new[] { FullyQualifiedIdentifier(TargetType) },
ctx.Source,
- BooleanLiteral(_ignoreCase)
+ BooleanLiteral(ignoreCase)
);
}
@@ -45,7 +36,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
MemberAccess(EnumClassName, ParseMethodName),
TypeOfExpression(FullyQualifiedIdentifier(TargetType)),
ctx.Source,
- BooleanLiteral(_ignoreCase)
+ BooleanLiteral(ignoreCase)
);
return CastExpression(FullyQualifiedIdentifier(TargetType), enumParseInvocation);
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs
index b9868c3689..3e94637792 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs
@@ -12,35 +12,23 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums;
/// Uses a switch expression for performance reasons (in comparison to ).
/// Optimized version of .
///
-public class EnumFromStringSwitchMapping : MethodMapping
+public class EnumFromStringSwitchMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ IEnumerable enumMembers,
+ bool ignoreCase,
+ EnumFallbackValueMapping fallbackMapping
+) : MethodMapping(sourceType, targetType)
{
private const string IgnoreCaseSwitchDesignatedVariableName = "s";
private const string StringEqualsMethodName = nameof(string.Equals);
private const string StringComparisonFullName = "System.StringComparison.OrdinalIgnoreCase";
- private readonly IEnumerable _enumMembers;
- private readonly bool _ignoreCase;
- private readonly EnumFallbackValueMapping _fallbackMapping;
-
- public EnumFromStringSwitchMapping(
- ITypeSymbol sourceType,
- ITypeSymbol targetType,
- IEnumerable enumMembers,
- bool ignoreCase,
- EnumFallbackValueMapping fallbackMapping
- )
- : base(sourceType, targetType)
- {
- _enumMembers = enumMembers;
- _ignoreCase = ignoreCase;
- _fallbackMapping = fallbackMapping;
- }
-
public override IEnumerable BuildBody(TypeMappingBuildContext ctx)
{
// switch for each name to the enum value
- var arms = _ignoreCase ? BuildArmsIgnoreCase(ctx) : _enumMembers.Select(BuildArm);
- arms = arms.Append(_fallbackMapping.BuildDiscardArm(ctx));
+ var arms = ignoreCase ? BuildArmsIgnoreCase(ctx) : enumMembers.Select(BuildArm);
+ arms = arms.Append(fallbackMapping.BuildDiscardArm(ctx));
var switchExpr = ctx.SyntaxFactory.Switch(ctx.Source, arms);
yield return ctx.SyntaxFactory.Return(switchExpr);
@@ -49,7 +37,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c
private IEnumerable BuildArmsIgnoreCase(TypeMappingBuildContext ctx)
{
var ignoreCaseSwitchDesignatedVariableName = ctx.NameBuilder.New(IgnoreCaseSwitchDesignatedVariableName);
- return _enumMembers.Select(f => BuildArmIgnoreCase(ignoreCaseSwitchDesignatedVariableName, f));
+ return enumMembers.Select(f => BuildArmIgnoreCase(ignoreCaseSwitchDesignatedVariableName, f));
}
private SwitchExpressionArmSyntax BuildArmIgnoreCase(string ignoreCaseSwitchDesignatedVariableName, IFieldSymbol field)
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs
index 8f48d61e75..1c642c3357 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumNameMapping.cs
@@ -10,28 +10,18 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums;
/// Uses a switch expression. Only supports defined enum values and no flags.
/// The name has to match exactly.
///
-public class EnumNameMapping : MethodMapping
+public class EnumNameMapping(
+ ITypeSymbol source,
+ ITypeSymbol target,
+ IReadOnlyDictionary enumMemberMappings,
+ EnumFallbackValueMapping fallback
+) : MethodMapping(source, target)
{
- private readonly IReadOnlyDictionary _enumMemberMappings;
- private readonly EnumFallbackValueMapping _fallback;
-
- public EnumNameMapping(
- ITypeSymbol source,
- ITypeSymbol target,
- IReadOnlyDictionary enumMemberMappings,
- EnumFallbackValueMapping fallback
- )
- : base(source, target)
- {
- _enumMemberMappings = enumMemberMappings;
- _fallback = fallback;
- }
-
public override IEnumerable BuildBody(TypeMappingBuildContext ctx)
{
// 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 arms = enumMemberMappings.Select(x => BuildArm(x.Key, x.Value)).Append(fallback.BuildDiscardArm(ctx));
var switchExpr = ctx.SyntaxFactory.Switch(ctx.Source, arms);
yield return ctx.SyntaxFactory.Return(switchExpr);
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs
index 705b0d13bf..d001231b32 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumToStringMapping.cs
@@ -11,18 +11,11 @@ namespace Riok.Mapperly.Descriptors.Mappings.Enums;
/// Uses a switch expression for performance reasons (in comparison to ).
/// Only supports defined enum values and no flags.
///
-public class EnumToStringMapping : MethodMapping
+public class EnumToStringMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IEnumerable enumMembers)
+ : MethodMapping(sourceType, targetType)
{
private const string ToStringMethodName = nameof(Enum.ToString);
- private readonly IEnumerable _enumMembers;
-
- public EnumToStringMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IEnumerable enumMembers)
- : base(sourceType, targetType)
- {
- _enumMembers = enumMembers;
- }
-
public override IEnumerable BuildBody(TypeMappingBuildContext ctx)
{
// fallback switch arm: _ => source.ToString()
@@ -30,7 +23,7 @@ public override IEnumerable BuildBody(TypeMappingBuildContext c
// switch for each name to the enum value
// eg: Enum1.Value1 => "Value1"
- var arms = _enumMembers.Select(BuildArm).Append(fallbackArm);
+ var arms = enumMembers.Select(BuildArm).Append(fallbackArm);
var switchExpr = ctx.SyntaxFactory.Switch(ctx.Source, arms);
yield return ctx.SyntaxFactory.Return(switchExpr);
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMapping.cs
index 6446f67d68..fb4265b42d 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMapping.cs
@@ -6,17 +6,11 @@ namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
///
/// A default implementation of .
///
-public abstract class ExistingTargetMapping : IExistingTargetMapping
+public abstract class ExistingTargetMapping(ITypeSymbol sourceType, ITypeSymbol targetType) : IExistingTargetMapping
{
- protected ExistingTargetMapping(ITypeSymbol sourceType, ITypeSymbol targetType)
- {
- SourceType = sourceType;
- TargetType = targetType;
- }
+ public ITypeSymbol SourceType { get; } = sourceType;
- public ITypeSymbol SourceType { get; }
-
- public ITypeSymbol TargetType { get; }
+ public ITypeSymbol TargetType { get; } = targetType;
public virtual bool CallableByOtherMappings => true;
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs
index c6a59bec55..1fb93c2977 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ExistingTargetMappingMethodWrapper.cs
@@ -6,25 +6,18 @@ namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
///
/// Wraps an as .
///
-public abstract class ExistingTargetMappingMethodWrapper : MethodMapping
+public abstract class ExistingTargetMappingMethodWrapper(IExistingTargetMapping mapping)
+ : MethodMapping(mapping.SourceType, mapping.TargetType)
{
private const string TargetVariableName = "target";
- private readonly IExistingTargetMapping _mapping;
-
- protected ExistingTargetMappingMethodWrapper(IExistingTargetMapping mapping)
- : base(mapping.SourceType, mapping.TargetType)
- {
- _mapping = mapping;
- }
-
public override IEnumerable BuildBody(TypeMappingBuildContext ctx)
{
var targetVariableName = ctx.NameBuilder.New(TargetVariableName);
yield return ctx.SyntaxFactory.DeclareLocalVariable(targetVariableName, CreateTargetInstance(ctx));
- foreach (var statement in _mapping.Build(ctx, IdentifierName(targetVariableName)))
+ foreach (var statement in mapping.Build(ctx, IdentifierName(targetVariableName)))
{
yield return statement;
}
diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs
index aa5f7df5dc..46f01b304b 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachAddEnumerableExistingTargetMapping.cs
@@ -9,38 +9,26 @@ namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
/// Represents a foreach enumerable mapping which works by looping through the source,
/// mapping each element and adding it to the target collection.
///
-public class ForEachAddEnumerableExistingTargetMapping : ExistingTargetMapping
+public class ForEachAddEnumerableExistingTargetMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ INewInstanceMapping elementMapping,
+ string insertMethodName,
+ EnsureCapacityInfo? ensureCapacityBuilder
+) : ExistingTargetMapping(sourceType, targetType)
{
private const string LoopItemVariableName = "item";
- private readonly INewInstanceMapping _elementMapping;
- private readonly string _insertMethodName;
- private readonly EnsureCapacityInfo? _ensureCapacityBuilder;
-
- public ForEachAddEnumerableExistingTargetMapping(
- ITypeSymbol sourceType,
- ITypeSymbol targetType,
- INewInstanceMapping elementMapping,
- string insertMethodName,
- EnsureCapacityInfo? ensureCapacityBuilder
- )
- : base(sourceType, targetType)
- {
- _elementMapping = elementMapping;
- _insertMethodName = insertMethodName;
- _ensureCapacityBuilder = ensureCapacityBuilder;
- }
-
public override IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax target)
{
- if (_ensureCapacityBuilder != null)
+ if (ensureCapacityBuilder != null)
{
- yield return _ensureCapacityBuilder.Build(ctx, target);
+ yield return ensureCapacityBuilder.Build(ctx, target);
}
var (loopItemCtx, loopItemVariableName) = ctx.WithNewSource(LoopItemVariableName);
- var convertedSourceItemExpression = _elementMapping.Build(loopItemCtx);
- var addMethod = MemberAccess(target, _insertMethodName);
+ 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 390020ff40..9180945bcb 100644
--- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs
+++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs
@@ -10,39 +10,25 @@ namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
/// Represents a foreach dictionary mapping which works by looping through the source,
/// mapping each element and set it to the target collection.
///
-public class ForEachSetDictionaryExistingTargetMapping : ExistingTargetMapping
+public class ForEachSetDictionaryExistingTargetMapping(
+ ITypeSymbol sourceType,
+ ITypeSymbol targetType,
+ INewInstanceMapping keyMapping,
+ INewInstanceMapping valueMapping,
+ INamedTypeSymbol? explicitCast,
+ EnsureCapacityInfo? ensureCapacity
+) : ExistingTargetMapping(sourceType, targetType)
{
private const string LoopItemVariableName = "item";
private const string ExplicitCastVariableName = "targetDict";
private const string KeyPropertyName = nameof(KeyValuePair