Skip to content

Commit 70c9921

Browse files
authored
impl(pubsub): change GetAttribute to absl::string_view (#12736)
1 parent e840198 commit 70c9921

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

google/cloud/pubsub/message.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ void SetAttribute(std::string const& key, std::string value,
5454
(*m.proto_.mutable_attributes())[key] = std::move(value);
5555
}
5656

57-
std::string GetAttribute(std::string const& key, pubsub::Message& m) {
57+
absl::string_view GetAttribute(std::string const& key, pubsub::Message& m) {
5858
auto value = m.proto_.attributes().find(key);
5959
if (value != m.proto_.attributes().end()) {
60-
return value->second;
60+
return absl::string_view(value->second.data(), value->second.size());
6161
}
62-
return std::string{};
62+
return absl::string_view{};
6363
}
6464

6565
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

google/cloud/pubsub/message.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_MESSAGE_H
1717

1818
#include "google/cloud/pubsub/version.h"
19+
#include "absl/strings/string_view.h"
1920
#include <google/pubsub/v1/pubsub.pb.h>
2021
#include <chrono>
2122
#include <iosfwd>
@@ -41,9 +42,10 @@ std::size_t MessageSize(pubsub::Message const&);
4142
std::size_t MessageProtoSize(::google::pubsub::v1::PubsubMessage const& m);
4243
// For Open Telemetry tracing only. Inserts or sets an attribute on the message.
4344
void SetAttribute(std::string const& key, std::string value, pubsub::Message&);
44-
// For Open Telemetry tracing only. Gets the value for a given attribute key on
45-
// the message.
46-
std::string GetAttribute(std::string const& key, pubsub::Message& m);
45+
// For Open Telemetry tracing only. Returns the value for a given attribute key
46+
// on the message or the null string_view when not found. Note: the string_view
47+
// is only valid for the lifetime of the corresponding message.
48+
absl::string_view GetAttribute(std::string const& key, pubsub::Message& m);
4749

4850
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
4951
} // namespace pubsub_internal
@@ -133,8 +135,8 @@ class Message {
133135
friend void pubsub_internal::SetAttribute(std::string const& key,
134136
std::string value,
135137
pubsub::Message&);
136-
friend std::string pubsub_internal::GetAttribute(std::string const& key,
137-
pubsub::Message&);
138+
friend absl::string_view pubsub_internal::GetAttribute(std::string const& key,
139+
pubsub::Message&);
138140

139141
/// Construct `Message` objects.
140142
friend class MessageBuilder;

google/cloud/pubsub/message_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ TEST(Message, GetAttributeFriend) {
282282
auto const v0 = pubsub_internal::GetAttribute("k0", m0);
283283
auto const v1 = pubsub_internal::GetAttribute("k1", m0);
284284

285-
EXPECT_EQ(v0, "v0");
285+
EXPECT_EQ(std::string(v0.data(), v0.size()), "v0");
286286
EXPECT_THAT(v1, IsEmpty());
287287
}
288288

0 commit comments

Comments
 (0)