-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathLambdaMethodModel.cs
More file actions
206 lines (176 loc) · 7.02 KB
/
LambdaMethodModel.cs
File metadata and controls
206 lines (176 loc) · 7.02 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes;
namespace Amazon.Lambda.Annotations.SourceGenerator.Models
{
/// <summary>
/// Represents original method model used for generating the code from the template.
/// </summary>
public class LambdaMethodModel
{
private AttributeModel<LambdaFunctionAttribute> _lambdaFunctionAttribute;
/// <summary>
/// Returns true if original method returns void
/// </summary>
public bool ReturnsVoid { get; set; }
/// <summary>
/// Returns true if original method returns <see cref="System.Threading.Tasks.Task"/>
/// </summary>
public bool ReturnsVoidTask { get; set; }
/// <summary>
/// Returns true if original method returns <see cref="System.Threading.Tasks.Task<T>"/>
/// </summary>
public bool ReturnsGenericTask { get; set; }
/// <summary>
/// Returns true if either the ReturnsVoidTask or ReturnsGenericTask are true
/// </summary>
public bool ReturnsVoidOrGenericTask => ReturnsVoidTask || ReturnsGenericTask;
/// <summary>
/// Gets or sets the return type of the method.
/// </summary>
public TypeModel ReturnType { get; set; }
/// <summary>
/// Returns true if the Lambda function returns either IHttpResult or Task<IHttpResult>
/// </summary>
public bool ReturnsIHttpResults
{
get
{
if(ReturnsVoid)
{
return false;
}
if(ReturnType.FullName == TypeFullNames.IHttpResult)
{
return true;
}
if(ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IHttpResult)
{
return true;
}
return false;
}
}
/// <summary>
/// Returns true if the Lambda function returns either IAuthorizerResult or Task<IAuthorizerResult>
/// </summary>
public bool ReturnsIAuthorizerResult
{
get
{
if (ReturnsVoid)
{
return false;
}
if (ReturnType.FullName == TypeFullNames.IAuthorizerResult)
{
return true;
}
if (ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IAuthorizerResult)
{
return true;
}
return false;
}
}
/// <summary>
/// Returns true if the Lambda function returns either ApplicationLoadBalancerResponse or Task<ApplicationLoadBalancerResponse>
/// </summary>
public bool ReturnsApplicationLoadBalancerResponse
{
get
{
if (ReturnsVoid)
{
return false;
}
if (ReturnType.FullName == TypeFullNames.ApplicationLoadBalancerResponse)
{
return true;
}
if (ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.ApplicationLoadBalancerResponse)
{
return true;
}
return false;
}
}
/// <summary>
/// Returns true if the Lambda function returns either void, Task, SQSBatchResponse or Task<SQSBatchResponse>
/// </summary>
public bool ReturnsVoidTaskOrSqsBatchResponse
{
get
{
if (ReturnsVoid || ReturnsVoidTask || ReturnType.FullName == TypeFullNames.SQSBatchResponse)
{
return true;
}
return ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.SQSBatchResponse;
}
}
/// <summary>
/// Gets or sets the parameters of original method. If this method has no parameters, returns
/// an empty list.
/// </summary>
public IList<ParameterModel> Parameters { get; set; }
/// <summary>
/// Gets or sets name of the original method.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets name of the original method.
/// </summary>
public string ExecutableAssemblyHandlerName => $"{this.Name.ToLower()}_handler";
/// <summary>
/// Returns true if original method uses dependency injection.
/// </summary>
public bool UsingDependencyInjection { get; set; }
public bool UsingHostBuilderForDependencyInjection { get; set; }
/// <summary>
/// Gets or sets the namespace for the nearest enclosing namespace. Returns null if the
/// symbol isn't contained in a namespace.
/// </summary>
public string ContainingNamespace { get; set; }
/// <summary>
/// Gets or sets the name of the assemblying containing the Lambda function.
/// </summary>
public string ContainingAssembly { get; set; }
/// <summary>
/// Gets or sets type of Lambda event
/// </summary>
public HashSet<EventType> Events { get; set; }
/// <summary>
/// Gets or sets the <see cref="TypeModel"/> for the containing type. Returns null if the
/// symbol is not contained within a type.
/// </summary>
public TypeModel ContainingType { get; set; }
/// <summary>
/// Gets or sets the attributes of original method. There always exist <see cref="Annotations.LambdaFunctionAttribute"/> in the list.
/// </summary>
public IList<AttributeModel> Attributes { get; set; }
/// <summary>
/// Gets <see cref="Annotations.LambdaFunctionAttribute"/> attribute.
/// </summary>
public AttributeModel<LambdaFunctionAttribute> LambdaFunctionAttribute
{
get
{
if (_lambdaFunctionAttribute == null)
{
var model = Attributes.First(att => att.Type.FullName == TypeFullNames.LambdaFunctionAttribute);
if (model is AttributeModel<LambdaFunctionAttribute> lambdaFunctionAttributeModel)
{
_lambdaFunctionAttribute = lambdaFunctionAttributeModel;
}
else
{
throw new InvalidOperationException($"Lambda method must has a {TypeFullNames.LambdaFunctionAttribute} attribute");
}
}
return _lambdaFunctionAttribute;
}
}
}
}