-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBuilderMethodFactory.cs
More file actions
110 lines (94 loc) · 4.33 KB
/
BuilderMethodFactory.cs
File metadata and controls
110 lines (94 loc) · 4.33 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using M31.FluentApi.Generator.CodeBuilding;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardElements;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardElements.FluentApiComments;
namespace M31.FluentApi.Generator.CodeGeneration.CodeBoardActors.Commons;
internal class BuilderMethodFactory
{
private readonly InnerBodyCreationDelegates innerBodyCreationDelegates;
private readonly TransformedComments transformedComments;
internal BuilderMethodFactory(InnerBodyCreationDelegates innerBodyCreationDelegates,
TransformedComments transformedComments)
{
this.innerBodyCreationDelegates = innerBodyCreationDelegates;
this.transformedComments = transformedComments;
}
internal BuilderMethod CreateEmptyBuilderMethod(MemberSymbolInfo memberInfo, string methodName)
{
MemberCommentKey key = new MemberCommentKey(memberInfo.Name, methodName);
Comments comments = transformedComments.GetMemberComments(key, Array.Empty<string>());
return new BuilderMethod(methodName, null, new List<Parameter>(), null, (_, _, _) => new List<string>(),
comments);
}
internal BuilderMethod CreateBuilderMethod(string methodName, ComputeValueCode computeValue)
{
List<Parameter> parameters = computeValue.Parameter != null
? new List<Parameter>() { computeValue.Parameter }
: new List<Parameter>();
List<string> BuildBodyCode(
string instancePrefix,
ReservedVariableNames reservedVariableNames,
string? returnType)
{
return new List<string>()
{
innerBodyCreationDelegates.GetSetMemberCode(computeValue.TargetMember)
.BuildCode(instancePrefix, computeValue.Code),
};
}
MemberCommentKey key = new MemberCommentKey(computeValue.TargetMember, methodName);
Comments comments = transformedComments.GetMemberComments(key, parameters.Select(p => p.Name).ToArray());
return new BuilderMethod(methodName, null, parameters, null, BuildBodyCode, comments);
}
internal BuilderMethod CreateBuilderMethod(string methodName, List<ComputeValueCode> computeValues)
{
List<Parameter> parameters = computeValues.Select(v => v.Parameter).OfType<Parameter>().ToList();
List<string> BuildBodyCode(
string instancePrefix,
ReservedVariableNames reservedVariableNames,
string? returnType)
{
return computeValues
.Select(v =>
innerBodyCreationDelegates.GetSetMemberCode(v.TargetMember).BuildCode(instancePrefix, v.Code))
.ToList();
}
Comments comments =
GetCompoundComments(methodName, parameters, computeValues.Select(v => v.TargetMember).ToArray());
return new BuilderMethod(methodName, null, parameters, null, BuildBodyCode, comments);
}
private Comments GetCompoundComments(
string methodName,
List<Parameter> parameters,
IReadOnlyCollection<string> memberNames)
{
string[] parameterNames = parameters.Select(p => p.Name).ToArray();
return new Comments(memberNames
.SelectMany(n =>
transformedComments.GetMemberComments(new MemberCommentKey(n, methodName), parameterNames).List)
.ToArray());
}
internal BuilderMethod CreateBuilderMethod(
MethodSymbolInfo methodSymbolInfo,
string methodName,
bool respectReturnType)
{
List<Parameter> parameters = CodeBuildingHelpers.CreateParameters(methodSymbolInfo.ParameterInfos);
string? returnTypeToRespect = respectReturnType ? methodSymbolInfo.ReturnType : null;
List<string> BuildBodyCode(
string instancePrefix,
ReservedVariableNames reservedVariableNames,
string? returnType)
{
return innerBodyCreationDelegates.GetCallMethodCode(methodSymbolInfo)
.BuildCode(instancePrefix, parameters, reservedVariableNames, returnType);
}
Comments comments = transformedComments.GetMethodComments(methodSymbolInfo);
return new BuilderMethod(
methodName,
methodSymbolInfo.GenericInfo,
parameters,
returnTypeToRespect,
BuildBodyCode,
comments);
}
}