15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_MESSAGE_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_MESSAGE_H
18#include "google/cloud/pubsub/version.h"
19#include <google/pubsub/v1/pubsub.pb.h>
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
31GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
34namespace pubsub_internal {
35GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
42GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
46GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68using PubsubMessageDataType = std::decay<
69 decltype(std::declval<
google::pubsub::v1::PubsubMessage>().data())>::type;
72
73
74
75
76
77
82 PubsubMessageDataType
const&
data()
const& {
return proto_.data(); }
83 PubsubMessageDataType&&
data() && {
84 return std::move(*proto_.mutable_data());
86 std::string
const&
message_id()
const {
return proto_.message_id(); }
87 std::string
const&
ordering_key()
const {
return proto_.ordering_key(); }
88 std::chrono::system_clock::time_point
publish_time()
const;
89 std::map<std::string, std::string>
attributes()
const {
90 std::map<std::string, std::string> r;
91 for (
auto const& kv : proto_.attributes()) {
92 r.emplace(kv.first, kv.second);
117 friend std::ostream& operator<<(std::ostream& os,
Message const& rhs);
120 friend Message pubsub_internal::FromProto(
121 ::
google::pubsub::v1::PubsubMessage m);
122 friend ::
google::pubsub::v1::PubsubMessage
const& pubsub_internal::ToProto(
124 friend ::
google::pubsub::v1::PubsubMessage&& pubsub_internal::ToProto(
126 friend std::size_t pubsub_internal::MessageSize(
Message const&);
133 explicit Message(::
google::pubsub::v1::PubsubMessage m)
134 : proto_(std::move(m)) {}
136 std::size_t MessageSize()
const;
138 google::pubsub::v1::PubsubMessage proto_;
142
143
153 proto_.set_data(std::move(data));
160 return std::move(*
this);
165 proto_.set_ordering_key(std::move(key));
177 std::string
const& value) & {
179 google::protobuf::Map<std::string, std::string>::value_type;
180 proto_.mutable_attributes()->insert(value_type{key, value});
187 std::string
const& value) && {
193 (*proto_.mutable_attributes())[key] = std::move(value);
204 template <
typename Iterator>
206 google::protobuf::Map<std::string, std::string> tmp;
208 google::protobuf::Map<std::string, std::string>::value_type;
209 for (
auto kv = begin; kv != end; ++kv) {
211 tmp.insert(value_type(get<0>(*kv), get<1>(*kv)));
213 proto_.mutable_attributes()->swap(tmp);
219 template <
typename Iterator>
221 SetAttributes(std::move(begin), std::move(end));
222 return std::move(*
this);
228 std::vector<std::pair<std::string, std::string>> v) & {
230 google::protobuf::Map<std::string, std::string>::value_type;
231 google::protobuf::Map<std::string, std::string> tmp;
233 tmp.insert(value_type(std::move(kv.first), std::move(kv.second)));
235 proto_.mutable_attributes()->swap(tmp);
242 std::vector<std::pair<std::string, std::string>> v) && {
244 return std::move(*
this);
248 template <
typename Pair>
250 return SetAttributes(v.begin(), v.end());
254 template <
typename Pair>
256 SetAttributes(std::move(v));
257 return std::move(*
this);
261 google::pubsub::v1::PubsubMessage proto_;
264GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
267namespace pubsub_internal {
268GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
269inline ::
google::pubsub::v1::PubsubMessage
const& ToProto(
275 return std::move(m.proto_);
283 return m.MessageSize();
286std::size_t MessageProtoSize(::
google::pubsub::v1::PubsubMessage
const& m);
288GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Constructs Message objects.
Definition: message.h:144
Message Build() &&
Creates a new message.
Definition: message.h:149
MessageBuilder && SetOrderingKey(std::string key) &&
Sets the ordering key to key.
Definition: message.h:170
MessageBuilder & SetData(std::string data) &
Sets the message payload to data.
Definition: message.h:152
MessageBuilder && InsertAttribute(std::string const &key, std::string const &value) &&
Inserts an attribute to the message, leaving the message unchanged if key is already present.
Definition: message.h:186
MessageBuilder && SetAttributes(Iterator begin, Iterator end) &&
Sets the attributes in the message to the attributes from the range [begin, end)
Definition: message.h:220
MessageBuilder & SetAttributes(std::vector< Pair > v) &
Sets the attributes in the message to v.
Definition: message.h:249
MessageBuilder && SetData(std::string data) &&
Sets the message payload to data.
Definition: message.h:158
MessageBuilder & InsertAttribute(std::string const &key, std::string const &value) &
Inserts an attribute to the message, leaving the message unchanged if key is already present.
Definition: message.h:176
MessageBuilder && SetAttributes(std::vector< Pair > v) &&
Sets the attributes in the message to v.
Definition: message.h:255
MessageBuilder & SetAttribute(std::string const &key, std::string value) &
Inserts or sets an attribute on the message.
Definition: message.h:192
MessageBuilder && SetAttribute(std::string const &key, std::string value) &&
Inserts or sets an attribute on the message.
Definition: message.h:198
MessageBuilder && SetAttributes(std::vector< std::pair< std::string, std::string > > v) &&
Sets the attributes in the message to v.
Definition: message.h:240
MessageBuilder & SetOrderingKey(std::string key) &
Sets the ordering key to key.
Definition: message.h:164
MessageBuilder & SetAttributes(Iterator begin, Iterator end) &
Sets the attributes in the message to the attributes from the range [begin, end)
Definition: message.h:205
MessageBuilder & SetAttributes(std::vector< std::pair< std::string, std::string > > v) &
Sets the attributes in the message to v.
Definition: message.h:226
The C++ representation for a Cloud Pub/Sub messages.
Definition: message.h:78
std::string const & ordering_key() const
Definition: message.h:87
std::map< std::string, std::string > attributes() const
Definition: message.h:89
std::chrono::system_clock::time_point publish_time() const
PubsubMessageDataType && data() &&
Definition: message.h:83
friend bool operator!=(Message const &a, Message const &b)
Compares two messages.
Definition: message.h:111
PubsubMessageDataType const & data() const &
Definition: message.h:82
Message & operator=(Message &&)=default
Message & operator=(Message const &)=default
std::string const & message_id() const
Definition: message.h:86
Message(Message const &)=default
Message(Message &&)=default
friend bool operator==(Message const &a, Message const &b)
Compares two messages.
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.