-
Notifications
You must be signed in to change notification settings - Fork 865
/
Copy pathStructureGenerator.tt
493 lines (452 loc) · 16.8 KB
/
StructureGenerator.tt
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<#@ template language="C#" inherits="BaseGenerator" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#
AddLicenseHeader();
#>
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.Net;
using Amazon.Runtime;
using Amazon.Runtime.Internal;
<#
bool structureIsNotEventStream = this.Structure != null && !this.Structure.IsEventStream;
bool structureIsEventStream = this.Structure != null && this.Structure.IsEventStream;
bool structureIsEvent = this.Structure != null && this.Structure.IsEvent;
#>
<#
if(structureIsEvent || structureIsEventStream)
{
#>
using Amazon.Runtime.EventStreams;
using Amazon.Runtime.EventStreams.Internal;
using <#=this.Config.Namespace#>.Model.Internal.MarshallTransformations;
using Amazon.Runtime.EventStreams.Utils;
using Amazon.Runtime.Internal.Util;
<#
}
#>
#pragma warning disable CS0612,CS0618,CS1570
namespace <#=this.Config.Namespace#>.Model
{
<#
if(this.StructureType == StructureType.Request)
this.FormatOperationRequestDocumentation(this.Operation);
else if (this.Operation != null && GeneratorHelpers.HasSuppressedResult(this.Operation))
this.FormatVoidResultDocumentation(this.Operation.Name);
else if(this.StructureType == StructureType.Response && (this.Structure == null || string.IsNullOrEmpty(this.Structure.Documentation)))
{
#>
/// <summary>
/// This is the response object from the <#=this.Operation.Name#> operation.
/// </summary>
<#
}
else
this.FormatClassDocumentation(this.Structure);
#>
<#
if(this.Structure != null && this.Structure.IsDeprecated)
{
#>
[Obsolete("<#=this.Structure.DeprecationMessage#>")]
<#
}
if(this.Structure is ExceptionShape)
{
#>
#if !NETSTANDARD
[Serializable]
#endif
<#
}
#>
<#
if(this.Structure != null && this.Structure.IsEventStream)
{
string EventStreamOutput = new ServiceClientGenerator.Generators.SourceFiles.EventStreamGenerator()
{
Structure = this.Structure,
Operation = this.Operation,
Config = this.Config
}.TransformText();
#>
<#=EventStreamOutput#>
<#
}
#>
<#
bool hasStreamingMember = this.Structure?.Members.Any(member => member.Shape.IsStreaming) ?? false;
bool structureContainsEventPayload = this.Structure?.Members.Any(member => member.IsEventPayload) ?? false;
Member payloadMember = this.Structure?.GetExplicitEventPayloadMember();
if(structureContainsEventPayload)
{
payloadMember = this.Structure?.Members.Single(member => member.IsEventPayload);
}
if (structureIsNotEventStream && this.StructureType == StructureType.Response && hasStreamingMember)
{
#>
public partial class <#=this.ClassName#><#=this.BaseClassString#>, IDisposable
{
<#
}
else
{
#>
<#
if( (this.Structure == null) || (structureIsNotEventStream))
{
#>
public partial class <#=this.ClassName#><#=this.BaseClassString#>
<#
if(structureIsEvent)
{
#>
: IEventStreamEvent
<#
}
#>
{
<#
}
#>
<#
}
#>
<#
if(structureIsNotEventStream)
{
if(this.IsWrapped)
{
#>
private <#=this.Structure.Name#> _response;
/// <summary>
/// Gets and sets the <#=this.Structure.Name#> property.
/// </summary>
public <#=this.Structure.Name#> <#=this.Structure.Name#>
{
get { return this._response; }
set { this._response = value; }
}
<#
}
else
{
foreach(var member in this.Structure.Members)
{
if (member.IsExcluded)
continue;
// If the shape is an exception then skip adding RequestId and ErrorCode because those properties comes from the base class.
if (this.Structure is ExceptionShape && (member.PropertyName == "RequestId" || member.PropertyName == "ErrorCode"))
continue;
// If the shape is a response then skip adding ContentLength because the property comes from the base class.
if (this.StructureType == StructureType.Response && member.PropertyName == "ContentLength")
continue;
#>
private <#=member.DetermineType()#> <#=member.VariableName#><#= member.IsCollection ? string.Format(" = AWSConfigs.InitializeCollections ? new {0}() : null;", member.DetermineType()) : ";"#>
<#
}
#>
<#
AddSimpleRequestConstructors(this.ClassName, this.Structure, this.Config.Namespace);
if(this.Structure is ExceptionShape)
{
var exceptionShape = (ExceptionShape)this.Structure;
if (exceptionShape.IsRetryable)
{
#>
private RetryableDetails _retryableDetails = new RetryableDetails(<#=exceptionShape.Throttling.ToString().ToLower()#>);
<#
#>
<#
}
#>
<#@ include file=".\Exceptions\ExceptionConstructors.t4" once="true" #>
<#@ include file=".\Exceptions\ExceptionSerialization.t4" once="true" #>
<#
}
foreach(var member in this.Structure.Members)
{
if (member.IsExcluded)
continue;
// If the shape is an exception then skip adding RequestId and ErrorCode because those properties come from the base class.
if (this.Structure is ExceptionShape && (member.PropertyName == "RequestId" || member.PropertyName == "ErrorCode"))
continue;
// If the shape is a response then skip adding ContentLength because the property comes from the base class.
if (this.StructureType == StructureType.Response && member.PropertyName == "ContentLength")
continue;
#>
<# this.FormatPropertyDocumentation(member); #>
<#
if(member.IsDeprecated)
{
#>
[Obsolete("<#=member.DeprecationMessage#>")]
<#
}
var propertyAttributes = new List<string>();
if(member.IsRequired && !member.IsIdempotent)
{
propertyAttributes.Add("Required=true");
}
if (member.Shape.Sensitive)
{
propertyAttributes.Add("Sensitive=true");
}
if (member.Shape.Min.HasValue)
{
propertyAttributes.Add("Min=" + member.Shape.Min);
}
if (member.Shape.Max.HasValue)
{
propertyAttributes.Add("Max=" + member.Shape.Max);
}
// Add PaginationRequestKey attribute to Request Pagination properties that will be used by PowerShell for Auto-Iteration
bool hasPaginatorInputToken = this.StructureType == StructureType.Request && this.Operation?.Paginators?.InputTokens.Any(x => x.PropertyName == member.PropertyName) == true;
bool hasPaginatorLimitKey = this.StructureType == StructureType.Request && this.Operation?.Paginators?.LimitKey?.PropertyName == member.PropertyName;
bool hasPaginatorOutputToken = this.StructureType == StructureType.Response && this.Operation?.Paginators?.OutputTokens.Any(x => x.PropertyName == member.PropertyName) == true;
if (hasPaginatorInputToken)
{
propertyAttributes.Add("PaginationInputToken=true");
}
else if (hasPaginatorLimitKey)
{
propertyAttributes.Add("PaginationLimitKey=true");
}
else if (hasPaginatorOutputToken)
{
propertyAttributes.Add("PaginationOutputToken=true");
}
if (propertyAttributes.Count > 0)
{
#>
[AWSProperty(<#=string.Join(", ", propertyAttributes)#>)]
<#
}
// Because some services have model properties named "Equals" which conflicts with .NET's Equals operation. Add the "new" keyword to override the behavior.
// The Retryable is there because the CloudHsmServiceException exception from CloudHSM models Retryable with a different return then what comes from the base class.
var memberModifier = string.Empty;
if (member.PropertyName.Equals("Equals") || (this.Structure is ExceptionShape && member.PropertyName.Equals("Retryable")))
{
memberModifier = "new ";
}
// Cognito and SecurityToken also have a modeled property named Expiration, which conflicts with the interface used in SRA.
else if (
member.PropertyName.Equals("Expiration") &&
this.ClassName.Equals("Credentials") &&
(this.Config.Namespace.Equals("Amazon.CognitoIdentity") || this.Config.Namespace.Equals("Amazon.SecurityToken")))
{
memberModifier = "override ";
}
#>
<#=member.AccessModifier#> <#=memberModifier#><#=member.DetermineType()#> <#=member.PropertyName#>
{
get { return this.<#=member.VariableName#>; }
set { this.<#=member.VariableName#> = value; }
}
<#
if (member.EmitIsSetProperties)
{
#>
/// <summary>
/// This property is set to true if the property <seealso cref="<#=member.PropertyName#>"/>
/// is set; false otherwise.
/// This property can be used to determine if the related property
/// was returned by a service response or if the related property
/// should be sent to the service during a service call.
/// </summary>
/// <returns>
/// True if the related property was set or will be sent to a service; false otherwise.
/// </returns>
<#
if(member.IsDeprecated)
{
#>
[Obsolete("<#=member.DeprecationMessage#>")]
<#
}
#>
public bool Is<#=member.PropertyName#>Set
{
get
{
return Amazon.Util.Internal.InternalSDKUtils.GetIsSet(this.<#=member.VariableName#>);
}
set
{
Amazon.Util.Internal.InternalSDKUtils.SetIsSet(value, ref this.<#=member.VariableName#>);
}
}
<#
}
#>
// Check to see if <#=member.PropertyName#> property is set
internal bool IsSet<#=member.PropertyName#>()
{
<#
if (member.EmitIsSetProperties)
{
#>
return this.Is<#=member.PropertyName#>Set;
<#
}
else if (member.IsNullable)
{
#>
return this.<#=member.VariableName#>.HasValue;
<#
}
else if (member.IsList && (member.MarshallLocation == MarshallLocation.Header || member.MarshallLocation == MarshallLocation.Headers))
{
// restxml and restjson is the only protocol where we send empty lists in headers. See following protocol tests.
// https://github.com/smithy-lang/smithy/blob/de486fa42c7bce4afc802bef95990795eeeed25a/smithy-aws-protocol-tests/model/restJson1/http-headers.smithy#L427
// https://github.com/smithy-lang/smithy/blob/de486fa42c7bce4afc802bef95990795eeeed25a/smithy-aws-protocol-tests/model/restXml/http-headers.smithy#L370
if (this.Config.ServiceModel.Type == ServiceType.Rest_Xml || this.Config.ServiceModel.Type == ServiceType.Rest_Json)
{
#>
return this.<#=member.VariableName#> != null;
<#
}
else
{
#>
return this.<#=member.VariableName#> != null && this.<#=member.VariableName#>.Count > 0;
<#
}
}
else if (member.IsMap || member.IsList)
{
#>
return this.<#=member.VariableName#> != null && (this.<#=member.VariableName#>.Count > 0 || !AWSConfigs.InitializeCollections);
<#
}
else if (member.IsDocument)
{
#>
return !this.<#=member.VariableName#>.IsNull();
<#
}
else
{
#>
return this.<#=member.VariableName#> != null;
<#
}
#>
}
<#
}
// Flexible checksum overrides to allow response validation configuration on the request
if (this.StructureType == StructureType.Request && !string.IsNullOrEmpty(this.Operation.ChecksumConfiguration?.RequestValidationModeMember))
{
#>
#region Flexible checksum properties
/// <inheritdoc/>
protected override CoreChecksumResponseBehavior CoreChecksumMode
{
get
{
if (IsSet<#=this.Operation.ChecksumConfiguration.RequestValidationModeMember#>())
{
return (CoreChecksumResponseBehavior)Enum.Parse(typeof(CoreChecksumResponseBehavior), this.<#=this.Operation.ChecksumConfiguration.RequestValidationModeMember#>);
}
return CoreChecksumResponseBehavior.DISABLED;
}
set { this.<#=this.Operation.ChecksumConfiguration.RequestValidationModeMember#> = value.ToString(); }
}
private static readonly List<CoreChecksumAlgorithm> _supportedChecksumAlgorithms = new List<CoreChecksumAlgorithm>
{
<#= string.Join(", ", Operation.ChecksumConfiguration?.ResponseAlgorithms?.Select(s => $"CoreChecksumAlgorithm.{s}").ToArray()) #>
};
/// <inheritdoc/>
protected override System.Collections.ObjectModel.ReadOnlyCollection<CoreChecksumAlgorithm> ChecksumResponseAlgorithms => _supportedChecksumAlgorithms.AsReadOnly();
#endregion
<#
}
if (this.Structure is ExceptionShape)
{
var exceptionShape = (ExceptionShape)this.Structure;
if (exceptionShape.IsRetryable)
{
#>
/// <summary>
/// Flag indicating if the exception is retryable and the associated retry
/// details. A null value indicates that the exception is not retryable.
/// </summary>
public override RetryableDetails Retryable
{
get
{
return _retryableDetails;
}
}
<#
}
}
}
}
else //TODO: remove this else clause. It was added just to preserve whitespaces
{
#>
<#
}
if (this.StructureType == StructureType.Response && hasStreamingMember)
{
#>
#region Dispose Pattern
private bool _disposed;
/// <summary>
/// Disposes of all managed and unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes of all managed and unmanaged resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
<#
if (this.Structure != null)
{
foreach (var member in this.Structure.Members)
{
if (member.Shape.IsStreaming)
{
#>
this.<#=member.VariableName#>?.Dispose();
this.<#=member.VariableName#> = null;
<#
}
}
}
#>
}
this._disposed = true;
}
#endregion
<#
}
#>
}
}
<#+
// Set to true when the service model specifies a shape that should be wrapped in a response. ElastiCache CreateCacheCluster is an example of this.
public bool IsWrapped { get; set; }
public Operation Operation { get; set; }
public string ClassName { get; set; }
public string BaseClass { get; set; }
public Shape Structure { get; set; }
public StructureType StructureType { get; set; }
#>