Skip to content

Commit 50bb85d

Browse files
Update CBOR generators to support event streaming (#3954)
1 parent 7f81a1a commit 50bb85d

File tree

15 files changed

+599
-293
lines changed

15 files changed

+599
-293
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System.Formats.Cbor;
17+
using Amazon.Extensions.CborProtocol.Internal.Transform;
18+
using Amazon.Runtime.EventStreams;
19+
using Amazon.Runtime.Internal;
20+
21+
namespace Amazon.Extensions.CborProtocol.Internal.EventStreams
22+
{
23+
/// <summary>
24+
/// Base class for cbor event stream publishers.
25+
/// </summary>
26+
public abstract class CborEventStreamPublisher : EventStreamPublisher
27+
{
28+
/// <summary>
29+
/// Construct a CborMarshallerContext that subclasses can use to run the marshaller for the event type
30+
/// that should be sent.
31+
/// </summary>
32+
/// <param name="writer">The writer that the marshaller will use to write as it is marshalling the user's object into it's CBOR representation.</param>
33+
/// <returns></returns>
34+
protected static CborMarshallerContext CreateCborMarshallerContext(CborWriter writer)
35+
{
36+
// The original request and service are not needed for event serialization so placeholder values are used.
37+
IRequest request = new DefaultRequest(new EventStreamRequest(), "eventstream");
38+
return new CborMarshallerContext(request, writer);
39+
}
40+
}
41+
}

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/CborSimpleTypeUnmarshaller.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Formats.Cbor;
19+
using System.Globalization;
1920
using System.IO;
2021
using Amazon;
2122
using Amazon.Runtime;
@@ -378,6 +379,33 @@ public static CborNullableDateTimeUnmarshaller Instance
378379
}
379380
}
380381

382+
public class CborNullableDateTimeEpochLongMillisecondsUnmarshaller
383+
: ICborUnmarshaller<DateTime?, CborUnmarshallerContext>
384+
{
385+
386+
private CborNullableDateTimeEpochLongMillisecondsUnmarshaller() { }
387+
388+
private static CborNullableDateTimeEpochLongMillisecondsUnmarshaller _instance =
389+
new CborNullableDateTimeEpochLongMillisecondsUnmarshaller();
390+
391+
public static CborNullableDateTimeEpochLongMillisecondsUnmarshaller Instance
392+
{
393+
get { return _instance; }
394+
}
395+
396+
public DateTime? Unmarshall(CborUnmarshallerContext context)
397+
{
398+
if (context.Reader.PeekState() == CborReaderState.Null)
399+
{
400+
context.Reader.ReadNull();
401+
return null;
402+
}
403+
var millseconds = CborLongUnmarshaller.Instance.Unmarshall(context);
404+
var ret = Amazon.Util.AWSSDKUtils.EPOCH_START.AddMilliseconds(millseconds);
405+
return ret;
406+
}
407+
}
408+
381409
public class CborMemoryStreamUnmarshaller
382410
: ICborUnmarshaller<MemoryStream, CborUnmarshallerContext>
383411
{

generator/ServiceClientGeneratorLib/Generators/Marshallers/CborStructureUnmarshaller.cs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ public override string TransformText()
101101

102102
#line 32 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
103103

104+
Member eventPayloadMember = this.Structure.GetExplicitEventPayloadMember();
104105
//HasImplicitEventPayloadMembers means that the structure does not have a member with the EventPayload trait
105-
if(this.Structure != null && this.Structure.IsEvent && !this.Structure.HasImplicitEventPayloadMembers())
106+
if(this.Structure != null && this.Structure.IsEvent && !this.Structure.HasImplicitEventPayloadMembers() && eventPayloadMember != null)
106107
{
107-
Member eventPayloadMember = this.Structure.GetExplicitEventPayloadMember();
108108
if(eventPayloadMember.ModelShape.IsString)
109109
{
110110

@@ -185,16 +185,6 @@ public override string TransformText()
185185

186186
#line 71 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
187187

188-
}
189-
190-
191-
#line default
192-
#line hidden
193-
194-
#line 74 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
195-
196-
if(this.Structure != null && (!this.Structure.IsEvent || this.Structure.HasImplicitEventPayloadMembers()))
197-
{
198188
foreach (var member in this.Structure.Members)
199189
{
200190

@@ -203,68 +193,57 @@ public override string TransformText()
203193
#line hidden
204194
this.Write(" case \"");
205195

206-
#line 80 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
196+
#line 75 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
207197
this.Write(this.ToStringHelper.ToStringWithCulture(member.MarshallName));
208198

209199
#line default
210200
#line hidden
211201
this.Write("\":\r\n {\r\n context.AddPathSegment" +
212202
"(\"");
213203

214-
#line 82 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
204+
#line 77 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
215205
this.Write(this.ToStringHelper.ToStringWithCulture(member.PropertyName));
216206

217207
#line default
218208
#line hidden
219209
this.Write("\");\r\n var unmarshaller = ");
220210

221-
#line 83 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
211+
#line 78 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
222212
this.Write(this.ToStringHelper.ToStringWithCulture(member.DetermineTypeUnmarshallerInstantiate()));
223213

224214
#line default
225215
#line hidden
226216
this.Write(";\r\n unmarshalledObject.");
227217

228-
#line 84 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
218+
#line 79 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
229219
this.Write(this.ToStringHelper.ToStringWithCulture(member.PropertyName));
230220

231221
#line default
232222
#line hidden
233223
this.Write(" = unmarshaller.Unmarshall(context);\r\n context.PopPath" +
234224
"Segment();\r\n break;\r\n }\r\n");
235225

236-
#line 88 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
226+
#line 83 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
237227

238228
}
239-
}
240229

241230

242231
#line default
243232
#line hidden
244233
this.Write(" default:\r\n reader.SkipValue();\r\n " +
245-
" break;\r\n }\r\n");
246-
247-
#line 96 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
248-
249-
if(this.Structure != null && (!this.Structure.IsEvent || this.Structure.HasImplicitEventPayloadMembers()))
250-
{
251-
252-
253-
#line default
254-
#line hidden
255-
this.Write(" }\r\n");
234+
" break;\r\n }\r\n }\r\n reader.R" +
235+
"eadEndMap();\r\n");
256236

257-
#line 101 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
237+
#line 92 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
258238

259239
}
260240

261241

262242
#line default
263243
#line hidden
264-
this.Write(" reader.ReadEndMap();\r\n return unmarshalledObject;\r\n " +
265-
" }\r\n\r\n\r\n");
244+
this.Write(" return unmarshalledObject;\r\n }\r\n\r\n\r\n");
266245

267-
#line 109 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
246+
#line 99 "C:\repos\aws-sdk-net-v4\generator\ServiceClientGeneratorLib\Generators\Marshallers\CborStructureUnmarshaller.tt"
268247

269248
this.AddStructureSingletonMethod();
270249

generator/ServiceClientGeneratorLib/Generators/Marshallers/CborStructureUnmarshaller.tt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
3030
return null;
3131
var reader = context.Reader;
3232
<#
33+
Member eventPayloadMember = this.Structure.GetExplicitEventPayloadMember();
3334
//HasImplicitEventPayloadMembers means that the structure does not have a member with the EventPayload trait
34-
if(this.Structure != null && this.Structure.IsEvent && !this.Structure.HasImplicitEventPayloadMembers())
35+
if(this.Structure != null && this.Structure.IsEvent && !this.Structure.HasImplicitEventPayloadMembers() && eventPayloadMember != null)
3536
{
36-
Member eventPayloadMember = this.Structure.GetExplicitEventPayloadMember();
3737
if(eventPayloadMember.ModelShape.IsString)
3838
{
3939
#>
@@ -69,11 +69,6 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
6969
switch (propertyName)
7070
{
7171
<#
72-
}
73-
#>
74-
<#
75-
if(this.Structure != null && (!this.Structure.IsEvent || this.Structure.HasImplicitEventPayloadMembers()))
76-
{
7772
foreach (var member in this.Structure.Members)
7873
{
7974
#>
@@ -87,21 +82,16 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
8782
}
8883
<#
8984
}
90-
}
9185
#>
9286
default:
9387
reader.SkipValue();
9488
break;
9589
}
96-
<#
97-
if(this.Structure != null && (!this.Structure.IsEvent || this.Structure.HasImplicitEventPayloadMembers()))
98-
{
99-
#>
10090
}
91+
reader.ReadEndMap();
10192
<#
10293
}
10394
#>
104-
reader.ReadEndMap();
10595
return unmarshalledObject;
10696
}
10797

0 commit comments

Comments
 (0)