Skip to content

Commit

Permalink
Change google::protobuf::MapKey::GetStringValue to return `absl::st…
Browse files Browse the repository at this point in the history
…ring_view`

PiperOrigin-RevId: 674294662
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 13, 2024
1 parent 9b666c3 commit ea0ade3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/google/protobuf/map_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "google/protobuf/map_field.h"

#include <atomic>
#include <string>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -243,7 +244,8 @@ void MapFieldBase::SyncRepeatedFieldWithMapNoLock() {
const MapKey& map_key = it.GetKey();
switch (key_des->cpp_type()) {
case FieldDescriptor::CPPTYPE_STRING:
reflection->SetString(new_entry, key_des, map_key.GetStringValue());
reflection->SetString(new_entry, key_des,
std::string(map_key.GetStringValue()));
break;
case FieldDescriptor::CPPTYPE_INT64:
reflection->SetInt64(new_entry, key_des, map_key.GetInt64Value());
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/map_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
Expand Down Expand Up @@ -136,7 +137,7 @@ class PROTOBUF_EXPORT MapKey {
TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapKey::GetBoolValue");
return val_.bool_value;
}
const std::string& GetStringValue() const {
absl::string_view GetStringValue() const {
TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapKey::GetStringValue");
return val_.string_value.get();
}
Expand Down
5 changes: 3 additions & 2 deletions src/google/protobuf/map_field_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <type_traits>

#include "absl/base/casts.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/map.h"
#include "google/protobuf/map_field.h"
#include "google/protobuf/map_type_handler.h"
Expand Down Expand Up @@ -50,8 +51,8 @@ inline uint64_t UnwrapMapKeyImpl(const MapKey& map_key, const uint64_t*) {
inline bool UnwrapMapKeyImpl(const MapKey& map_key, const bool*) {
return map_key.GetBoolValue();
}
inline const std::string& UnwrapMapKeyImpl(const MapKey& map_key,
const std::string*) {
inline absl::string_view UnwrapMapKeyImpl(const MapKey& map_key,
const std::string*) {
return map_key.GetStringValue();
}
inline const MapKey& UnwrapMapKeyImpl(const MapKey& map_key, const MapKey*) {
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/reflection_visit_field_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ PROTOBUF_MAP_KEY_INFO(Int64, int64_t, INT64);
PROTOBUF_MAP_KEY_INFO(UInt32, uint32_t, UINT32);
PROTOBUF_MAP_KEY_INFO(UInt64, uint64_t, UINT64);
PROTOBUF_MAP_KEY_INFO(Bool, bool, BOOL);
PROTOBUF_MAP_KEY_INFO(String, const std::string&, STRING);
PROTOBUF_MAP_KEY_INFO(String, absl::string_view, STRING);

#undef PROTOBUF_MAP_KEY_INFO

Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/text_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,8 @@ void MapFieldPrinterHelper::CopyKey(const MapKey& key, Message* message,
ABSL_LOG(ERROR) << "Not supported.";
break;
case FieldDescriptor::CPPTYPE_STRING:
reflection->SetString(message, field_desc, key.GetStringValue());
reflection->SetString(message, field_desc,
std::string(key.GetStringValue()));
return;
case FieldDescriptor::CPPTYPE_INT64:
reflection->SetInt64(message, field_desc, key.GetInt64Value());
Expand Down

0 comments on commit ea0ade3

Please sign in to comment.