Skip to content

Commit

Permalink
Picking up debruce's commits to support google.protobuf.any in the pa…
Browse files Browse the repository at this point in the history
…yload builder:

Add constructor to Payload. Add test case to PayloadBuilderTest to
exercise this constructor by creating an Any containing a uuri, passing
it to the constructor, building it, and then deserializing the Any, and
the contained uuri.
  • Loading branch information
lukas-he committed Feb 18, 2025
1 parent 2c101d7 commit 037856c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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 @@ -131,6 +132,13 @@ struct Payload {
/// 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 @@ -46,6 +46,13 @@ Payload::Payload(Serialized&& serialized) {
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

0 comments on commit 037856c

Please sign in to comment.