Skip to content

Commit ec1abde

Browse files
Add Cbor request and structure marshallers generators
1 parent 1c3f3aa commit ec1abde

File tree

7 files changed

+2363
-10
lines changed

7 files changed

+2363
-10
lines changed

extensions/src/AWSSDK.Extensions.CborProtocol/CborWriterExtensions.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,10 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15-
using Amazon.Runtime;
16-
using Amazon.Runtime.Internal;
17-
using Amazon.Runtime.Internal.Transform;
18-
using Amazon.Runtime.Internal.Util;
19-
using Amazon.Util;
15+
2016
using System;
21-
using System.Collections.Generic;
2217
using System.Formats.Cbor;
23-
using System.IO;
24-
using System.Text.RegularExpressions;
18+
using Amazon.Util;
2519

2620
namespace AWSSDK.Extensions.CborProtocol
2721
{
@@ -66,6 +60,14 @@ public static void WriteOptimizedNumber(this CborWriter writer, double value)
6660
}
6761
}
6862

63+
// Check if value can safely be represented as float32
64+
float floatCandidate = (float)value;
65+
if ((double)floatCandidate == value)
66+
{
67+
WriteOptimizedNumber(writer, floatCandidate);
68+
return;
69+
}
70+
6971
writer.WriteDouble(value);
7072
}
7173

@@ -97,7 +99,14 @@ public static void WriteOptimizedNumber(this CborWriter writer, float value)
9799
}
98100
}
99101

100-
writer.WriteSingle(value);
102+
// Manual encoding to avoid half-precision floats
103+
var bytes = new byte[5];
104+
bytes[0] = 0xFA; // CBOR float32 marker
105+
BitConverter.GetBytes(value).CopyTo(bytes, 1);
106+
if (BitConverter.IsLittleEndian)
107+
Array.Reverse(bytes, 1, 4);
108+
109+
writer.WriteEncodedValue(bytes);
101110
}
102111
}
103112
}

generator/ServiceClientGeneratorLib/GeneratorDriver.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ internal static bool WriteFile(string baseOutputDir,
13551355
/// Sets the marshaller of the generator based on the service type
13561356
/// </summary>
13571357
/// <param name="marshaller">The marshaller to be set</param>
1358-
/// <param name="normalizeMarshallers">If the service type is a type of json then normalizeMarshallers is set to true, false otherwise</param>
1358+
/// <param name="normalizeMarshallers">If the service type is using structure marshallers then normalizeMarshallers is set to true, false otherwise</param>
13591359
void GetRequestMarshaller(out BaseRequestMarshaller marshaller, out bool normalizeMarshallers)
13601360
{
13611361
normalizeMarshallers = false;
@@ -1366,6 +1366,10 @@ void GetRequestMarshaller(out BaseRequestMarshaller marshaller, out bool normali
13661366
marshaller = new JsonRPCRequestMarshaller();
13671367
normalizeMarshallers = true;
13681368
break;
1369+
case ServiceType.Cbor:
1370+
marshaller = new CborRequestMarshaller();
1371+
normalizeMarshallers = true;
1372+
break;
13691373
case ServiceType.Query:
13701374
marshaller = new AWSQueryRequestMarshaller();
13711375
break;
@@ -1388,6 +1392,8 @@ BaseRequestMarshaller GetStructureMarshaller()
13881392
case ServiceType.Rest_Json:
13891393
case ServiceType.Json:
13901394
return new JsonRPCStructureMarshaller();
1395+
case ServiceType.Cbor:
1396+
return new CborStructureMarshaller();
13911397
default:
13921398
throw new Exception("No structure marshaller for service type: " + this.Configuration.ServiceModel.Type);
13931399
}
@@ -1409,6 +1415,8 @@ BaseResponseUnmarshaller GetResponseUnmarshaller()
14091415
return new AWSQueryResponseUnmarshaller();
14101416
case ServiceType.Rest_Xml:
14111417
return new RestXmlResponseUnmarshaller();
1418+
case ServiceType.Cbor:
1419+
return new CborResponseUnmarshaller();
14121420
default:
14131421
throw new Exception("No response unmarshaller for service type: " + this.Configuration.ServiceModel.Type);
14141422
}
@@ -1429,6 +1437,8 @@ BaseResponseUnmarshaller GetStructureUnmarshaller()
14291437
return new AWSQueryStructureUnmarshaller();
14301438
case ServiceType.Rest_Xml:
14311439
return new RestXmlStructureUnmarshaller();
1440+
case ServiceType.Cbor:
1441+
return new CborStructureUnmarshaller();
14321442
default:
14331443
throw new Exception("No structure unmarshaller for service type: " + this.Configuration.ServiceModel.Type);
14341444
}
@@ -1448,6 +1458,8 @@ BaseResponseUnmarshaller GetExceptionUnmarshaller()
14481458
return new AWSQueryExceptionUnmarshaller();
14491459
case ServiceType.Rest_Xml:
14501460
return new RestXmlExceptionUnmarshaller();
1461+
case ServiceType.Cbor:
1462+
return new CborExceptionUnmarshaller();
14511463
default:
14521464
throw new Exception("No structure unmarshaller for service type: " + this.Configuration.ServiceModel.Type);
14531465
}

0 commit comments

Comments
 (0)