Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payload Builder supporting google.protobuf.Any #306

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions include/up-cpp/datamodel/builder/Payload.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef UP_CPP_DATAMODEL_BUILDER_PAYLOAD_H
#define UP_CPP_DATAMODEL_BUILDER_PAYLOAD_H

#include <google/protobuf/any.pb.h>

Check failure on line 15 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:15:10 [clang-diagnostic-error]

'google/protobuf/any.pb.h' file not found
#include <uprotocol/v1/uattributes.pb.h>

#include <cstdint>
Expand Down Expand Up @@ -46,8 +47,8 @@
/// @remarks The UPayloadFormat will automatically be set to
/// UPAYLOAD_FORMAT_PROTOBUF
template <typename ProtobufT>
explicit Payload(const ProtobufT& message) {

Check warning on line 50 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:50:2 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: payload_
std::string serialized;

Check warning on line 51 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:51:15 [cppcoreguidelines-init-variables]

variable 'serialized' is not initialized
message.SerializeToString(&serialized);
payload_ =
std::make_tuple(std::move(serialized),
Expand Down Expand Up @@ -79,8 +80,8 @@
/// will compile out.
/// @param data Data to be serialized and stored.
template <typename Serializer, typename ValueT>
Payload(Serializer s, const ValueT& data) {

Check warning on line 83 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:83:2 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: payload_

Check warning on line 83 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:83:21 [misc-unused-parameters]

parameter 's' is unused
auto serializedData = Serializer::serialize(data);

Check warning on line 84 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:84:8 [readability-identifier-naming]

invalid case style for variable 'serializedData'
if (!UPayloadFormat_IsValid(
std::get<PayloadType::Format>(serializedData))) {
throw std::out_of_range("Invalid Serializer payload format");
Expand All @@ -95,7 +96,7 @@
///
/// @throws std::out_of_range If format is not valid for v1::UPayloadFormat
Payload(const std::vector<uint8_t>& value_bytes,
const v1::UPayloadFormat format);

Check warning on line 99 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:99:10 [readability-avoid-const-params-in-decls]

parameter 'format' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions

/// @brief Creates a Payload builder with a provided pre-serialized data.
///
Expand All @@ -106,7 +107,7 @@
///
/// @note This would typically be used for UPAYLOAD_FORMAT_TEXT or
/// UPAYLOAD_FORMAT_JSON, but can be used for other payload formats.
Payload(const std::string& value, const v1::UPayloadFormat format);

Check warning on line 110 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:110:36 [readability-avoid-const-params-in-decls]

parameter 'format' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions

/// @brief Creates a Payload builder with a provided pre-serialized data.
///
Expand All @@ -119,7 +120,7 @@
///
/// @note This would typically be used for UPAYLOAD_FORMAT_TEXT or
/// UPAYLOAD_FORMAT_JSON, but can be used for other payload formats.
Payload(std::string&& value, const v1::UPayloadFormat format);

Check warning on line 123 in include/up-cpp/datamodel/builder/Payload.h

View workflow job for this annotation

GitHub Actions / Lint C++ sources

include/up-cpp/datamodel/builder/Payload.h:123:31 [readability-avoid-const-params-in-decls]

parameter 'format' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions

/// @brief Creates a Payload builder with a provided pre-serialized data.
///
Expand All @@ -131,6 +132,13 @@
/// for v1::UPayloadFormat
explicit Payload(Serialized&&);

/// @brief Creates a Payload builder with a provided protobuf::Any.
///
/// The contents of value will be moved into the Payload object.
///
/// @param An initialized google::protobuf::Any object..
explicit Payload(const google::protobuf::Any&);

/// @brief Move constructor.
Payload(Payload&&) noexcept;

Expand Down
7 changes: 7 additions & 0 deletions src/datamodel/builder/Payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace uprotocol::datamodel::builder {

// Byte vector constructor
Payload::Payload(const std::vector<uint8_t>& value_bytes,

Check warning on line 16 in src/datamodel/builder/Payload.cpp

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:16:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: payload_

Check warning on line 16 in src/datamodel/builder/Payload.cpp

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:16:46 [misc-unused-parameters]

parameter 'value_bytes' is unused
const v1::UPayloadFormat format) {
if (!UPayloadFormat_IsValid(format)) {
throw std::out_of_range("Invalid Byte vector payload format");
Expand Down Expand Up @@ -46,6 +46,13 @@
payload_ = std::move(serialized);
}

// google::protobuf::Any constructor
Payload::Payload(const google::protobuf::Any& any) {
payload_ = std::make_tuple(
std::move(any.SerializeAsString()),
uprotocol::v1::UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY);
}

// Move constructor
Payload::Payload(Payload&& other) noexcept
: payload_(std::move(other.payload_)), moved_(std::move(other.moved_)) {}
Expand Down
25 changes: 25 additions & 0 deletions test/coverage/datamodel/PayloadBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,31 @@ TEST_F(PayloadTest, StringMovePayloadTest) {
EXPECT_THROW(auto _ = payload.buildCopy(), Payload::PayloadMoved);
}

// Create Any and move payload object test
TEST_F(PayloadTest, AnyMovePayloadTest) { // NOLINT
// Arrange
uprotocol::v1::UUri uri_object; // NOLINT
uri_object.set_authority_name(testStringPayload_);
google::protobuf::Any any;
any.PackFrom(uri_object, "hello_world");

// Act
Payload payload(any);
auto [serialized_data, payload_format] = std::move(payload).buildMove();

// Assert
EXPECT_EQ(
payload_format,
uprotocol::v1::UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY);
google::protobuf::Any parsed_any;
EXPECT_TRUE(parsed_any.ParseFromString(serialized_data));
EXPECT_EQ(parsed_any.type_url(), "hello_world/uprotocol.v1.UUri");

uprotocol::v1::UUri parsed_uri_object;
EXPECT_TRUE(parsed_uri_object.ParseFromString(parsed_any.value()));
EXPECT_EQ(parsed_uri_object.authority_name(), testStringPayload_);
}

/////////////////////RValue String Payload Tests/////////////////////

// Create RValue String Payload
Expand Down
Loading