Skip to content

Commit fb44b92

Browse files
committed
updating clang-format, glob CMake, switch-based escaping, Schema return by value
1 parent 4524ff2 commit fb44b92

9 files changed

Lines changed: 97 additions & 122 deletions

File tree

src/aws-cpp-sdk-core/CMakeLists.txt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ file(GLOB SMITHY_IDENTITY_SIGNER_HEADERS "include/smithy/identity/signer/*.h")
9797
file(GLOB SMITHY_IDENTITY_SIGNER_BUILTIN_HEADERS "include/smithy/identity/signer/built-in/*.h")
9898
file(GLOB SMITHY_INTERCEPTOR_HEADERS "include/smithy/interceptor/*.h")
9999
file(GLOB SMITHY_INTERCEPTOR_IMPL_HEADERS "include/smithy/interceptor/impl/*.h")
100+
file(GLOB SMITHY_CLIENT_SCHEMA_HEADERS "include/smithy/client/schema/*.h")
101+
file(GLOB SMITHY_CLIENT_SCHEMA_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/smithy/client/schema/*.cpp")
100102

101103
file(GLOB AWS_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
102104
file(GLOB AWS_TINYXML2_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/external/tinyxml2/*.cpp")
@@ -316,6 +318,7 @@ file(GLOB AWS_NATIVE_SDK_COMMON_HEADERS
316318
${OPTEL_HEADERS}
317319
${SMITHY_INTERCEPTOR_HEADERS}
318320
${SMITHY_INTERCEPTOR_IMPL_HEADERS}
321+
${SMITHY_CLIENT_SCHEMA_HEADERS}
319322
)
320323

321324
# misc platform-specific, not related to features (encryption/http clients)
@@ -378,6 +381,7 @@ file(GLOB AWS_NATIVE_SDK_NON_UNITY_SRC
378381
${UTILS_CHECKSUM_SOURCE}
379382
${UTILS_CRYPTO_SOURCE}
380383
${OPTEL_SOURCE}
384+
${SMITHY_CLIENT_SCHEMA_SOURCE}
381385
)
382386

383387
file(GLOB AWS_NATIVE_SDK_UNITY_SRC
@@ -462,6 +466,7 @@ if(MSVC)
462466
source_group("Header Files\\smithy\\identity\\signer\\built-in" FILES ${SMITHY_IDENTITY_SIGNER_BUILTIN_HEADERS})
463467
source_group("Header Files\\smithy\\interceptor" FILES ${SMITHY_INTERCEPTOR_HEADERS})
464468
source_group("Header Files\\smithy\\interceptor" FILES ${SMITHY_INTERCEPTOR_IMPL_HEADERS})
469+
source_group("Header Files\\smithy\\client\\schema" FILES ${SMITHY_CLIENT_SCHEMA_HEADERS})
465470

466471
# http client conditional headers
467472
if(ENABLE_CURL_CLIENT)
@@ -543,17 +548,7 @@ check_cxx_source_compiles("
543548
return 0;
544549
}" AWS_HAS_ALIGNED_ALLOC)
545550

546-
add_library(${PROJECT_NAME} ${AWS_NATIVE_SDK_SRC}
547-
include/smithy/client/schema/ShapeSerializer.h
548-
include/smithy/client/schema/CborShapeSerializer.h
549-
include/smithy/client/schema/Schema.h
550-
include/smithy/client/schema/JsonShapeSerializer.h
551-
include/smithy/client/schema/QueryShapeSerializer.h
552-
include/smithy/client/schema/XmlShapeSerializer.h
553-
source/smithy/client/schema/JsonShapeSerializer.cpp
554-
include/smithy/client/schema/JsonWriteUtils.h
555-
source/smithy/client/schema/JsonWriteUtils.cpp
556-
)
551+
add_library(${PROJECT_NAME} ${AWS_NATIVE_SDK_SRC})
557552
add_library(AWS::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
558553

559554
target_compile_definitions(${PROJECT_NAME} PUBLIC "AWS_SDK_VERSION_MAJOR=${AWSSDK_VERSION_MAJOR}")

src/aws-cpp-sdk-core/include/smithy/client/schema/CborShapeSerializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include <aws/core/Core_EXPORTS.h>
4-
#include <smithy/client/schema/ShapeSerializer.h>
54
#include <aws/core/utils/memory/AWSMemory.h>
5+
#include <smithy/client/schema/ShapeSerializer.h>
66

77
namespace smithy {
88
namespace schema {

src/aws-cpp-sdk-core/include/smithy/client/schema/JsonShapeSerializer.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#pragma once
22

3-
#include <smithy/client/schema/ShapeSerializer.h>
4-
3+
#include <aws/core/client/AWSError.h>
54
#include <aws/core/utils/memory/AWSMemory.h>
6-
7-
#include "aws/core/client/AWSError.h"
5+
#include <smithy/client/schema/ShapeSerializer.h>
86

97
namespace smithy {
108
namespace schema {
119

1210
class JsonShapeSerializer final : public ShapeSerializer {
1311
public:
14-
using SerializerOutcome = Aws::Utils::Outcome<Aws::String, Aws::Client::AWSError<Aws::Client::CoreErrors>>;
12+
using SerializerOutcome = Aws::Utils::Outcome<Aws::String, Aws::Client::AWSError<Aws::Client::CoreErrors>>;
1513
JsonShapeSerializer();
1614
~JsonShapeSerializer();
1715

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
#pragma once
2-
32
#include <aws/core/utils/memory/stl/AWSString.h>
43

5-
namespace smithy {
6-
namespace schema {
7-
class JsonWriteUtils {
8-
public:
9-
static void WriteQuotedString(Aws::String& buf, const Aws::String& value);
10-
private:
11-
static const bool NEEDS_ESCAPE[128];
12-
static const char ESCAPE_TABLE[128];
13-
static const char HEX[16];
14-
};
15-
}
16-
}
4+
namespace Aws {
5+
namespace Schema {
6+
7+
void WriteQuotedJsonString(Aws::String& buf, const Aws::String& value);
8+
9+
} // namespace Schema
10+
} // namespace Aws

src/aws-cpp-sdk-core/include/smithy/client/schema/QueryShapeSerializer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

3-
#include <smithy/client/schema/ShapeSerializer.h>
4-
53
#include <aws/core/utils/memory/AWSMemory.h>
4+
#include <smithy/client/schema/ShapeSerializer.h>
65

76
namespace smithy {
87
namespace schema {

src/aws-cpp-sdk-core/include/smithy/client/schema/Schema.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ enum class ShapeType : uint8_t {
3535
class Schema {
3636
public:
3737
Schema() = default;
38-
Schema(const Aws::String& memberName, ShapeType type)
39-
: m_type(type), m_memberName(memberName) {}
38+
Schema(const Aws::String& memberName, ShapeType type) : m_type(type), m_memberName(memberName) {}
4039

4140
ShapeType GetType() const { return m_type; }
4241
const char* GetId() const { return m_id; }
43-
const Aws::String& GetMemberName() const { return m_memberName; }
42+
Aws::String GetMemberName() const { return m_memberName; }
4443
int GetMemberIndex() const { return m_memberIndex; }
4544
bool IsMember() const { return !m_memberName.empty(); }
4645

src/aws-cpp-sdk-core/include/smithy/client/schema/XmlShapeSerializer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

3-
#include <smithy/client/schema/ShapeSerializer.h>
4-
53
#include <aws/core/utils/memory/AWSMemory.h>
4+
#include <smithy/client/schema/ShapeSerializer.h>
65

76
namespace smithy {
87
namespace schema {

src/aws-cpp-sdk-core/source/smithy/client/schema/JsonShapeSerializer.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ using SerializerOutcome = Aws::Utils::Outcome<Aws::String, Aws::Client::AWSError
2020
static constexpr int MAX_DEPTH = 1000;
2121

2222
class JsonShapeSerializer::Impl {
23-
24-
public:
25-
Impl() {m_buf.reserve(8192);}
23+
public:
24+
Impl() { m_buf.reserve(8192); }
2625

2726
bool BeginStructure(const Schema&) {
2827
if (m_depth + 1 >= MAX_DEPTH) {
@@ -66,7 +65,7 @@ class JsonShapeSerializer::Impl {
6665

6766
void WriteString(const Schema& schema, const Aws::String& value) {
6867
WriteFieldName(schema);
69-
JsonWriteUtils::WriteQuotedString(m_buf, value);
68+
Aws::Schema::WriteQuotedJsonString(m_buf, value);
7069
}
7170

7271
void WriteTimestamp(const Schema& schema, const DateTime& value) {
@@ -154,23 +153,21 @@ class JsonShapeSerializer::Impl {
154153
}
155154

156155
SerializerOutcome GetPayload() {
157-
if(m_finalized || !m_errorMessage.empty()) {
156+
if (m_finalized || !m_errorMessage.empty()) {
158157
return Aws::Client::AWSError<Aws::Client::CoreErrors>(
159-
Aws::Client::CoreErrors::INTERNAL_FAILURE,
160-
"SerializationException",
161-
!m_errorMessage.empty()?m_errorMessage:"Serializer has already been finalized",
162-
false);
158+
Aws::Client::CoreErrors::INTERNAL_FAILURE, "SerializationException",
159+
!m_errorMessage.empty() ? m_errorMessage : "Serializer has already been finalized", false);
163160
}
164161
m_finalized = true;
165162
return std::move(m_buf);
166163
}
167164

168-
private:
165+
private:
169166
Aws::String m_buf;
170167
int m_depth = 0;
171-
Aws::Array<bool, MAX_DEPTH> m_needsComma{};
172-
Aws::Array<bool, MAX_DEPTH> m_isMap{};
173-
Aws::Array<bool, MAX_DEPTH> m_isList{};
168+
Aws::Array<bool, MAX_DEPTH> m_needsComma{};
169+
Aws::Array<bool, MAX_DEPTH> m_isMap{};
170+
Aws::Array<bool, MAX_DEPTH> m_isList{};
174171
Aws::String m_currentMapKey;
175172
bool m_finalized = false;
176173
Aws::String m_errorMessage;
@@ -184,7 +181,7 @@ class JsonShapeSerializer::Impl {
184181
}
185182

186183
void WriteKey(const Aws::String& key) {
187-
JsonWriteUtils::WriteQuotedString(m_buf, key);
184+
Aws::Schema::WriteQuotedJsonString(m_buf, key);
188185
m_buf += ':';
189186
}
190187

@@ -199,7 +196,6 @@ class JsonShapeSerializer::Impl {
199196
WriteKey(schema.GetMemberName());
200197
}
201198
}
202-
203199
};
204200

205201
JsonShapeSerializer::JsonShapeSerializer() : m_impl(Aws::MakeUnique<Impl>("JsonShapeSerializer")) {}
Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,75 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
#include <aws/core/utils/memory/stl/AWSArray.h>
16
#include <smithy/client/schema/JsonWriteUtils.h>
27

3-
using namespace smithy::schema;
8+
namespace {
49

5-
const char JsonWriteUtils::HEX[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
10+
static const Aws::Array<char, 16> HEX = {{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}};
611

7-
const bool JsonWriteUtils::NEEDS_ESCAPE[128] = {
8-
// 0x00-0x1F: control chars
9-
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
10-
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
11-
// 0x20-0x2F: space ! " # $ % & ' ( ) * + , - . /
12-
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
13-
// 0x30-0x3F
14-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
15-
// 0x40-0x4F
16-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
17-
// 0x50-0x5F: P-Z [ \ ] ^ _
18-
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
19-
// 0x60-0x6F
20-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
21-
// 0x70-0x7F
22-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23-
};
12+
void EscapeChar(Aws::String& buf, unsigned char c) {
13+
switch (c) {
14+
case '\"':
15+
buf += "\\\"";
16+
break;
17+
case '\\':
18+
buf += "\\\\";
19+
break;
20+
case '\b':
21+
buf += "\\b";
22+
break;
23+
case '\f':
24+
buf += "\\f";
25+
break;
26+
case '\n':
27+
buf += "\\n";
28+
break;
29+
case '\r':
30+
buf += "\\r";
31+
break;
32+
case '\t':
33+
buf += "\\t";
34+
break;
35+
default:
36+
buf += "\\u00";
37+
buf += HEX[(c >> 4) & 0xF];
38+
buf += HEX[c & 0xF];
39+
break;
40+
}
41+
}
42+
43+
bool NeedsEscape(unsigned char c) { return c < 0x20 || c == '"' || c == '\\'; }
44+
45+
} // anonymous namespace
2446

25-
const char JsonWriteUtils::ESCAPE_TABLE[128] = {
26-
// 0x00-0x0F
27-
0,0,0,0,0,0,0,0,'b','t','n',0,'f','r',0,0,
28-
// 0x10-0x1F
29-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
30-
// 0x20-0x2F
31-
0,0,'"',0,0,0,0,0,0,0,0,0,0,0,0,0,
32-
// 0x30-0x3F
33-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34-
// 0x40-0x4F
35-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
36-
// 0x50-0x5F
37-
0,0,0,0,0,0,0,0,0,0,0,0,'\\',0,0,0,
38-
// 0x60-0x6F
39-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
40-
// 0x70-0x7F
41-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
42-
};
47+
namespace Aws {
48+
namespace Schema {
4349

44-
void JsonWriteUtils::WriteQuotedString(Aws::String& buf, const Aws::String& value) {
45-
buf += '"';
46-
const char* data = value.data();
47-
const size_t len = value.size();
50+
void WriteQuotedJsonString(Aws::String& buf, const Aws::String& value) {
51+
buf += '"';
52+
const char* data = value.data();
53+
const size_t len = value.size();
4854

49-
size_t i = 0;
55+
size_t i = 0;
56+
while (i < len) {
57+
size_t start = i;
5058
while (i < len) {
51-
// Fast scan: find next char that needs escaping
52-
size_t start = i;
53-
while (i < len) {
54-
unsigned char c = static_cast<unsigned char>(data[i]);
55-
if (c < 0x80 && NEEDS_ESCAPE[c]) break;
56-
i++;
57-
}
58-
// Bulk append safe run
59-
if (i > start) {
60-
buf.append(data + start, i - start);
61-
}
62-
// Escape one char
63-
if (i < len) {
64-
unsigned char c = static_cast<unsigned char>(data[i]);
65-
if (ESCAPE_TABLE[c] != 0) {
66-
buf += '\\';
67-
buf += ESCAPE_TABLE[c];
68-
} else {
69-
buf += '\\';
70-
buf += 'u';
71-
buf += '0';
72-
buf += '0';
73-
buf += HEX[(c >> 4) & 0xF];
74-
buf += HEX[c & 0xF];
75-
}
76-
i++;
77-
}
59+
unsigned char c = static_cast<unsigned char>(data[i]);
60+
if (c < 0x80 && NeedsEscape(c)) break;
61+
i++;
7862
}
79-
buf += '"';
63+
if (i > start) {
64+
buf.append(data + start, i - start);
65+
}
66+
if (i < len) {
67+
EscapeChar(buf, static_cast<unsigned char>(data[i]));
68+
i++;
69+
}
70+
}
71+
buf += '"';
8072
}
73+
74+
} // namespace Schema
75+
} // namespace Aws

0 commit comments

Comments
 (0)