Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/JsonTraits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once

#include <aws/core/utils/memory/stl/AWSString.h>
#include <smithy/client/schema/Trait.h>
#include <smithy/client/schema/TraitKey.h>

namespace smithy {
namespace schema {

class JsonNameTrait : public Trait {
public:
explicit JsonNameTrait(const Aws::String& value) : m_value(value) {}
const Aws::String& GetValue() const { return m_value; }
static const TraitKey<JsonNameTrait>& KEY() { return TraitKey<JsonNameTrait>::Instance(); }

private:
Aws::String m_value;
};

} // namespace schema
} // namespace smithy
14 changes: 14 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/Schema.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <aws/core/utils/memory/stl/AWSString.h>
#include <smithy/client/schema/TraitMap.h>

#include <cstdint>

Expand Down Expand Up @@ -48,13 +49,26 @@ class Schema {

uint16_t GetMemberCount() const { return m_memberCount; }

template <typename T>
const T* GetTrait(const TraitKey<T>& key) const {
return m_traits.Get(key);
}

bool HasTrait(const TraitKeyBase& key) const { return m_traits.Has(key); }

Schema& SetTrait(const TraitKeyBase& key, const Trait* trait) {
m_traits.Set(key, trait);
return *this;
}

private:
const char* m_id = nullptr;
ShapeType m_type = ShapeType::Structure;
Aws::String m_memberName;
int m_memberIndex = 0;
const Schema* m_members = nullptr;
uint16_t m_memberCount = 0;
TraitMap m_traits;
};

} // namespace schema
Expand Down
26 changes: 26 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/SerdeTraits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once

#include <smithy/client/schema/Trait.h>
#include <smithy/client/schema/TraitKey.h>

namespace smithy {
namespace schema {

class TimestampFormatTrait : public Trait {
public:
enum class Format { DATE_TIME, HTTP_DATE, EPOCH_SECONDS };

explicit TimestampFormatTrait(Format format) : m_format(format) {}
Format GetFormat() const { return m_format; }
static const TraitKey<TimestampFormatTrait>& KEY() { return TraitKey<TimestampFormatTrait>::Instance(); }

private:
Format m_format;
};

} // namespace schema
} // namespace smithy
12 changes: 12 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/Trait.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <smithy/Smithy_EXPORTS.h>

namespace smithy {
namespace schema {
class SMITHY_API Trait {
Comment thread
sbiscigl marked this conversation as resolved.
public:
virtual ~Trait() = default;
};
} // namespace schema
} // namespace smithy
32 changes: 32 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/TraitKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <smithy/Smithy_EXPORTS.h>

namespace smithy {
namespace schema {
class SMITHY_API TraitKeyBase {
public:
int GetId() const { return m_id; }

protected:
explicit TraitKeyBase(int id) : m_id(id) {}

private:
int m_id;
};

SMITHY_API int NextTraitKey();

template <typename T>
class TraitKey : public TraitKeyBase {
public:
static const TraitKey& Instance() {
static const TraitKey instance(NextTraitKey());
Comment thread
sbiscigl marked this conversation as resolved.
return instance;
}

private:
explicit TraitKey(int id) : TraitKeyBase(id) {}
};
} // namespace schema
} // namespace smithy
39 changes: 39 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/TraitMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <aws/core/utils/memory/stl/AWSVector.h>
#include <smithy/Smithy_EXPORTS.h>
#include <smithy/client/schema/Trait.h>
#include <smithy/client/schema/TraitKey.h>

namespace smithy {
namespace schema {
class SMITHY_API TraitMap {
public:
TraitMap() = default;

template <typename T>
const T* Get(const TraitKey<T>& key) const {
Comment thread
sbiscigl marked this conversation as resolved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets return by value and copy or shared pointer if we want to share lifetimes, raw pointer or ref will create lifetime issues

int idx = key.GetId();
if (idx >= static_cast<int>(m_value.size())) return nullptr;
return static_cast<const T*>(m_value[idx]);
}

bool Has(const TraitKeyBase& key) const {
int idx = key.GetId();
if (idx >= static_cast<int>(m_value.size())) return false;
return m_value[idx] != nullptr;
}

void Set(const TraitKeyBase& key, const Trait* trait) {
int idx = key.GetId();
if (idx >= static_cast<int>(m_value.size())) {
m_value.resize(idx + 1, nullptr);
}
m_value[idx] = trait;
}

private:
Aws::Vector<const Trait*> m_value;
};
} // namespace schema
} // namespace smithy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <aws/core/client/AWSError.h>
#include <aws/core/utils/memory/AWSMemory.h>
#include <smithy/Smithy_EXPORTS.h>
#include <smithy/client/schema/ShapeSerializer.h>
Expand All @@ -9,6 +10,7 @@ namespace schema {

class SMITHY_API XmlShapeSerializer final : public ShapeSerializer {
public:
using SerializerOutcome = Aws::Utils::Outcome<Aws::String, Aws::Client::AWSError<Aws::Client::CoreErrors>>;
XmlShapeSerializer();
~XmlShapeSerializer();

Expand All @@ -25,6 +27,8 @@ class SMITHY_API XmlShapeSerializer final : public ShapeSerializer {
void WriteEnum(const Schema& schema, int value) override;
void WriteNull(const Schema& schema) override;

void WriteAttribute(const Schema& schema, const Aws::String& value);

bool BeginList(const Schema& schema, size_t count) override;
void EndList() override;

Expand All @@ -35,7 +39,7 @@ class SMITHY_API XmlShapeSerializer final : public ShapeSerializer {
bool BeginNestedStructure(const Schema& schema) override;
void EndNestedStructure() override;

Aws::String GetPayload() const;
SerializerOutcome GetPayload();

private:
class Impl;
Expand Down
87 changes: 87 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/XmlTraits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once

#include <aws/core/utils/memory/stl/AWSString.h>
#include <smithy/client/schema/Trait.h>
#include <smithy/client/schema/TraitKey.h>

namespace smithy {
namespace schema {

class XmlNameTrait : public Trait {
public:
explicit XmlNameTrait(const Aws::String& value) : m_value(value) {}
const Aws::String& GetValue() const { return m_value; }
static const TraitKey<XmlNameTrait>& KEY() { return TraitKey<XmlNameTrait>::Instance(); }

private:
Aws::String m_value;
};

class XmlFlattenedTrait : public Trait {
public:
static const TraitKey<XmlFlattenedTrait>& KEY() { return TraitKey<XmlFlattenedTrait>::Instance(); }
};

class XmlAttributeTrait : public Trait {
public:
static const TraitKey<XmlAttributeTrait>& KEY() { return TraitKey<XmlAttributeTrait>::Instance(); }
};

class XmlNamespaceTrait : public Trait {
public:
XmlNamespaceTrait(const Aws::String& uri, const Aws::String& prefix = "") : m_uri(uri), m_prefix(prefix) {}
const Aws::String& GetUri() const { return m_uri; }
const Aws::String& GetPrefix() const { return m_prefix; }
static const TraitKey<XmlNamespaceTrait>& KEY() { return TraitKey<XmlNamespaceTrait>::Instance(); }

private:
Aws::String m_uri;
Aws::String m_prefix;
};

class XmlListItemNameTrait : public Trait {
public:
explicit XmlListItemNameTrait(const Aws::String& value) : m_value(value) {}
const Aws::String& GetValue() const { return m_value; }
static const TraitKey<XmlListItemNameTrait>& KEY() { return TraitKey<XmlListItemNameTrait>::Instance(); }

private:
Aws::String m_value;
};

class XmlMapEntryNameTrait : public Trait {
public:
explicit XmlMapEntryNameTrait(const Aws::String& value) : m_value(value) {}
const Aws::String& GetValue() const { return m_value; }
static const TraitKey<XmlMapEntryNameTrait>& KEY() { return TraitKey<XmlMapEntryNameTrait>::Instance(); }

private:
Aws::String m_value;
};

class XmlMapKeyNameTrait : public Trait {
public:
explicit XmlMapKeyNameTrait(const Aws::String& value) : m_value(value) {}
const Aws::String& GetValue() const { return m_value; }
static const TraitKey<XmlMapKeyNameTrait>& KEY() { return TraitKey<XmlMapKeyNameTrait>::Instance(); }

private:
Aws::String m_value;
};

class XmlMapValueNameTrait : public Trait {
public:
explicit XmlMapValueNameTrait(const Aws::String& value) : m_value(value) {}
const Aws::String& GetValue() const { return m_value; }
static const TraitKey<XmlMapValueNameTrait>& KEY() { return TraitKey<XmlMapValueNameTrait>::Instance(); }

private:
Aws::String m_value;
};

} // namespace schema
} // namespace smithy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/StringUtils.h>
#include <smithy/client/schema/JsonShapeSerializer.h>
#include <smithy/client/schema/JsonTraits.h>
#include <smithy/client/schema/JsonWriteUtils.h>

#include <array>
Expand Down Expand Up @@ -193,7 +194,8 @@ class JsonShapeSerializer::Impl {
if (m_depth > 0 && m_isMap[m_depth]) {
WriteKey(m_currentMapKey);
} else {
WriteKey(schema.GetMemberName());
const auto* jsonName = schema.GetTrait(JsonNameTrait::KEY());
WriteKey(jsonName ? jsonName->GetValue() : schema.GetMemberName());
}
}
};
Expand Down
10 changes: 10 additions & 0 deletions src/aws-cpp-sdk-core/source/smithy/client/schema/TraitKey.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <smithy/client/schema/TraitKey.h>

namespace smithy {
namespace schema {

static int s_traitIdCounter = 0;
Comment thread
sbiscigl marked this conversation as resolved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be std::atomic to match how java does it


int NextTraitKey() { return s_traitIdCounter++; }
} // namespace schema
} // namespace smithy
Loading
Loading