/
ForEachAddEnumerableExistingTargetMapping.cs
53 lines (46 loc) · 1.91 KB
/
ForEachAddEnumerableExistingTargetMapping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
namespace Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
/// <summary>
/// Represents a foreach enumerable mapping which works by looping through the source,
/// mapping each element and adding it to the target collection.
/// </summary>
public class ForEachAddEnumerableExistingTargetMapping : ExistingTargetMapping
{
private const string LoopItemVariableName = "item";
private readonly ITypeMapping _elementMapping;
private readonly string _insertMethodName;
private readonly EnsureCapacity? _ensureCapacityBuilder;
public ForEachAddEnumerableExistingTargetMapping(
ITypeSymbol sourceType,
ITypeSymbol targetType,
ITypeMapping elementMapping,
string insertMethodName,
EnsureCapacity? ensureCapacityBuilder
)
: base(sourceType, targetType)
{
_elementMapping = elementMapping;
_insertMethodName = insertMethodName;
_ensureCapacityBuilder = ensureCapacityBuilder;
}
public override IEnumerable<StatementSyntax> 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)))
);
}
}