1515#include " google/cloud/bigtable/value.h"
1616#include " google/cloud/bigtable/timestamp.h"
1717#include " google/cloud/internal/throw_delegate.h"
18+ #include " absl/strings/cord.h"
1819#include < google/bigtable/v2/types.pb.h>
1920#include < google/protobuf/descriptor.h>
2021#include < google/protobuf/message.h>
@@ -25,6 +26,30 @@ namespace cloud {
2526namespace bigtable {
2627GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2728namespace {
29+
30+ // Some Bigtable proto fields use Cord internally and string externally.
31+ template <typename T, typename std::enable_if<
32+ std::is_same<T, std::string>::value>::type* = nullptr >
33+ std::string AsString (T const & s) {
34+ return s;
35+ }
36+ template <typename T, typename std::enable_if<
37+ std::is_same<T, std::string>::value>::type* = nullptr >
38+ std::string AsString (T&& s) {
39+ return std::move (s); // NOLINT(bugprone-move-forwarding-reference)
40+ }
41+ template <typename T, typename std::enable_if<
42+ std::is_same<T, absl::Cord>::value>::type* = nullptr >
43+ std::string AsString (T const & s) {
44+ return std::string (s);
45+ }
46+ template <typename T, typename std::enable_if<
47+ std::is_same<T, absl::Cord>::value>::type* = nullptr >
48+ std::string AsString (T&& s) {
49+ return std::string (
50+ std::move (s)); // NOLINT(bugprone-move-forwarding-reference)
51+ }
52+
2853// Compares two sets of Type and Value protos for equality. This method calls
2954// itself recursively to compare subtypes and subvalues.
3055bool Equal (google::bigtable::v2::Type const & pt1, // NOLINT(misc-no-recursion)
@@ -91,7 +116,7 @@ std::ostream& StreamHelper(std::ostream& os, // NOLINT(misc-no-recursion)
91116 return os << v.string_value ();
92117 }
93118 if (v.kind_case () == google::bigtable::v2::Value::kBytesValue ) {
94- return os << Bytes (v.bytes_value ());
119+ return os << Bytes (AsString ( v.bytes_value () ));
95120 }
96121 if (v.kind_case () == google::bigtable::v2::Value::kTimestampValue ) {
97122 auto ts = MakeTimestamp (v.timestamp_value ());
@@ -326,23 +351,23 @@ StatusOr<std::string> Value::GetValue(std::string const&,
326351 if (pv.kind_case () != google::bigtable::v2::Value::kStringValue ) {
327352 return internal::UnknownError (" missing STRING" , GCP_ERROR_INFO ());
328353 }
329- return pv.string_value ();
354+ return AsString ( pv.string_value () );
330355}
331356StatusOr<std::string> Value::GetValue (std::string const &,
332357 google::bigtable::v2::Value&& pv,
333358 google::bigtable::v2::Type const &) {
334359 if (pv.kind_case () != google::bigtable::v2::Value::kStringValue ) {
335360 return internal::UnknownError (" missing STRING" , GCP_ERROR_INFO ());
336361 }
337- return std::move (*pv.mutable_string_value ());
362+ return AsString ( std::move (*pv.mutable_string_value () ));
338363}
339364StatusOr<Bytes> Value::GetValue (Bytes const &,
340365 google::bigtable::v2::Value const & pv,
341366 google::bigtable::v2::Type const &) {
342367 if (pv.kind_case () != google::bigtable::v2::Value::kBytesValue ) {
343368 return internal::UnknownError (" missing BYTES" , GCP_ERROR_INFO ());
344369 }
345- return Bytes (pv.bytes_value ());
370+ return Bytes (AsString ( pv.bytes_value () ));
346371}
347372StatusOr<Timestamp> Value::GetValue (Timestamp const &,
348373 google::bigtable::v2::Value const & pv,
0 commit comments