Skip to content
Merged
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>
#include <uprotocol/v1/uattributes.pb.h>

#include <cstdint>
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: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
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation

Check notice on line 1 in src/datamodel/builder/Payload.cpp

View workflow job for this annotation

GitHub Actions / Lint C++ sources

Run clang-format on src/datamodel/builder/Payload.cpp

File src/datamodel/builder/Payload.cpp does not conform to Custom style guidelines. (lines 51, 52, 53)
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
Expand Down Expand Up @@ -27,7 +27,7 @@
if (!UPayloadFormat_IsValid(format)) {
throw std::out_of_range("Invalid String payload format");
}
payload_ = std::make_tuple(std::move(value), format);

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

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:30:29 [performance-move-const-arg]

std::move of the const variable 'value' has no effect; remove std::move() or make the variable non-const
}

// Move string constructor
Expand All @@ -46,23 +46,30 @@
payload_ = std::move(serialized);
}

// google::protobuf::Any constructor
Payload::Payload(const google::protobuf::Any& any) {
payload_ = std::make_tuple(
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_)) {}

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

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:58:51 [performance-move-const-arg]

std::move of the expression of the trivially-copyable type 'bool' has no effect; remove std::move()

// Copy constructor
Payload::Payload(const Payload& other)

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

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:61:10 [modernize-use-equals-default]

use '= default' to define a trivial copy constructor
: payload_(other.payload_), moved_(other.moved_) {}

// Move assignment operator
Payload& Payload::operator=(Payload&& other) noexcept {
payload_ = std::move(other.payload_);
moved_ = std::move(other.moved_);

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

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:67:11 [performance-move-const-arg]

std::move of the expression of the trivially-copyable type 'bool' has no effect; remove std::move()
return *this;
}

// Copy assignment operator
Payload& Payload::operator=(const Payload& other) {

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

View workflow job for this annotation

GitHub Actions / Lint C++ sources

src/datamodel/builder/Payload.cpp:72:19 [cert-oop54-cpp]

operator=() does not handle self-assignment properly
payload_ = other.payload_;
moved_ = other.moved_;
return *this;
Expand Down
33 changes: 30 additions & 3 deletions test/coverage/datamodel/PayloadBuilderTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation

Check notice on line 1 in test/coverage/datamodel/PayloadBuilderTest.cpp

View workflow job for this annotation

GitHub Actions / Lint C++ sources

Run clang-format on test/coverage/datamodel/PayloadBuilderTest.cpp

File test/coverage/datamodel/PayloadBuilderTest.cpp does not conform to Custom style guidelines. (lines 133, 134, 135)
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
Expand Down Expand Up @@ -101,7 +101,7 @@

// Act
Payload payload(uriObject);
auto& [payload_reference, _] = payload.buildCopy();
auto& [payload_reference, payload_format] = payload.buildCopy();
const void* original_address = payload_reference.data();

// Assert
Expand All @@ -110,7 +110,7 @@
uprotocol::v1::UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF);
EXPECT_EQ(payloadData, expectedPayloadData);

EXPECT_THROW(auto _ = payload.buildCopy(), Payload::PayloadMoved);
EXPECT_THROW(auto result = payload.buildCopy(), Payload::PayloadMoved);
EXPECT_EQ(original_address, payloadData.data());
}

Expand All @@ -130,7 +130,9 @@
uprotocol::v1::UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF);
EXPECT_EQ(payloadData, uriObject.SerializeAsString());

EXPECT_THROW(std::move(payload).buildMove(), Payload::PayloadMoved);
EXPECT_THROW({
auto _ = std::move(payload).buildMove();
}, Payload::PayloadMoved);
}

// Create serialized protobuf payload. Call build after move.
Expand Down Expand Up @@ -340,6 +342,31 @@
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