Automated rollback of commit b55b834d5a.

PiperOrigin-RevId: 534168300
pull/12875/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent d5d7654efe
commit a6d895988d
  1. 1
      src/google/protobuf/lite_unittest.cc
  2. 12
      src/google/protobuf/message.h
  3. 8
      src/google/protobuf/message_lite.cc
  4. 4
      src/google/protobuf/message_lite.h
  5. 47
      src/google/protobuf/text_format.cc
  6. 14
      src/google/protobuf/text_format.h
  7. 100
      src/google/protobuf/text_format_unittest.cc
  8. 14
      src/google/protobuf/unittest.proto

@ -36,6 +36,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/strings/match.h" #include "absl/strings/match.h"

@ -182,10 +182,13 @@ class CelMapReflectionFriend; // field_backed_map_impl.cc
namespace internal { namespace internal {
class MapFieldPrinterHelper; // text_format.cc class MapFieldPrinterHelper; // text_format.cc
PROTOBUF_EXPORT void PerformAbslStringify( PROTOBUF_EXPORT std::string StringifyMessage(
const Message& message, const Message& message); // text_format.cc
absl::FunctionRef<void(absl::string_view)> append); // text_format.cc
} // namespace internal } // namespace internal
PROTOBUF_EXPORT std::string ShortFormat(
const Message& message); // text_format.cc
PROTOBUF_EXPORT std::string Utf8Format(
const Message& message); // text_format.cc
namespace util { namespace util {
class MessageDifferencer; class MessageDifferencer;
} }
@ -340,8 +343,7 @@ class PROTOBUF_EXPORT Message : public MessageLite {
// Do not rely on exact format. // Do not rely on exact format.
template <typename Sink> template <typename Sink>
friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) { friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) {
internal::PerformAbslStringify( sink.Append(internal::StringifyMessage(message));
message, [&](absl::string_view content) { sink.Append(content); });
} }
// Reflection-based methods ---------------------------------------- // Reflection-based methods ----------------------------------------

@ -657,6 +657,14 @@ void InternalMetadata::DoSwap<std::string>(std::string* other) {
} // namespace internal } // namespace internal
std::string ShortFormat(const MessageLite& message_lite) {
return message_lite.DebugString();
}
std::string Utf8Format(const MessageLite& message_lite) {
return message_lite.DebugString();
}
// =================================================================== // ===================================================================
// Shutdown support. // Shutdown support.

@ -617,6 +617,10 @@ T* OnShutdownDelete(T* p) {
} }
} // namespace internal } // namespace internal
std::string ShortFormat(const MessageLite& message_lite);
std::string Utf8Format(const MessageLite& message_lite);
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google

@ -99,6 +99,13 @@ inline void IncrementRedactedFieldCounter() {
num_redacted_field.fetch_add(1, std::memory_order_relaxed); num_redacted_field.fetch_add(1, std::memory_order_relaxed);
} }
inline void TrimTrailingSpace(std::string& debug_string) {
// Single line mode currently might have an extra space at the end.
if (!debug_string.empty() && debug_string.back() == ' ') {
debug_string.pop_back();
}
}
} // namespace } // namespace
namespace internal { namespace internal {
@ -140,10 +147,7 @@ std::string Message::ShortDebugString() const {
std::memory_order_relaxed)); std::memory_order_relaxed));
printer.PrintToString(*this, &debug_string); printer.PrintToString(*this, &debug_string);
// Single line mode currently might have an extra space at the end. TrimTrailingSpace(debug_string);
if (!debug_string.empty() && debug_string[debug_string.size() - 1] == ' ') {
debug_string.resize(debug_string.size() - 1);
}
return debug_string; return debug_string;
} }
@ -168,24 +172,51 @@ void Message::PrintDebugString() const { printf("%s", DebugString().c_str()); }
namespace internal { namespace internal {
PROTOBUF_EXPORT void PerformAbslStringify( enum class Option { kNone, kShort, kUTF8 };
const Message& message, absl::FunctionRef<void(absl::string_view)> append) {
std::string StringifyMessage(const Message& message, Option option) {
// Indicate all scoped reflection calls are from DebugString function. // Indicate all scoped reflection calls are from DebugString function.
ScopedReflectionMode scope(ReflectionMode::kDebugString); ScopedReflectionMode scope(ReflectionMode::kDebugString);
// TODO(b/249835002): consider using the single line version for short
TextFormat::Printer printer; TextFormat::Printer printer;
switch (option) {
case Option::kShort:
printer.SetSingleLineMode(true);
break;
case Option::kUTF8:
printer.SetUseUtf8StringEscaping(true);
break;
case Option::kNone:
break;
}
printer.SetExpandAny(true); printer.SetExpandAny(true);
printer.SetRedactDebugString(true); printer.SetRedactDebugString(true);
printer.SetRandomizeDebugString(true); printer.SetRandomizeDebugString(true);
printer.SetRootMessageFullName(message.GetDescriptor()->full_name()); printer.SetRootMessageFullName(message.GetDescriptor()->full_name());
std::string result; std::string result;
printer.PrintToString(message, &result); printer.PrintToString(message, &result);
append(result);
if (option == Option::kShort) {
TrimTrailingSpace(result);
}
return result;
}
PROTOBUF_EXPORT std::string StringifyMessage(const Message& message) {
return StringifyMessage(message, Option::kNone);
} }
} // namespace internal } // namespace internal
PROTOBUF_EXPORT std::string ShortFormat(const Message& message) {
return internal::StringifyMessage(message, internal::Option::kShort);
}
PROTOBUF_EXPORT std::string Utf8Format(const Message& message) {
return internal::StringifyMessage(message, internal::Option::kUTF8);
}
// =========================================================================== // ===========================================================================
// Implementation of the parse information tree class. // Implementation of the parse information tree class.

@ -76,6 +76,15 @@ namespace io {
class ErrorCollector; // tokenizer.h class ErrorCollector; // tokenizer.h
} }
namespace internal {
// Enum used to set printing options for StringifyMessage.
PROTOBUF_EXPORT enum class Option;
// Converts a protobuf message to a string with redaction enabled.
PROTOBUF_EXPORT std::string StringifyMessage(const Message& message,
Option option);
} // namespace internal
// This class implements protocol buffer text format, colloquially known as text // This class implements protocol buffer text format, colloquially known as text
// proto. Printing and parsing protocol messages in text format is useful for // proto. Printing and parsing protocol messages in text format is useful for
// debugging and human editing of messages. // debugging and human editing of messages.
@ -421,9 +430,8 @@ class PROTOBUF_EXPORT TextFormat {
friend std::string Message::DebugString() const; friend std::string Message::DebugString() const;
friend std::string Message::ShortDebugString() const; friend std::string Message::ShortDebugString() const;
friend std::string Message::Utf8DebugString() const; friend std::string Message::Utf8DebugString() const;
friend void internal::PerformAbslStringify( friend std::string internal::StringifyMessage(const Message& message,
const Message& message, internal::Option option);
absl::FunctionRef<void(absl::string_view)> append);
// Sets whether silent markers will be inserted. // Sets whether silent markers will be inserted.
void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; } void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; }

@ -60,6 +60,7 @@
#include "google/protobuf/io/tokenizer.h" #include "google/protobuf/io/tokenizer.h"
#include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/map_unittest.pb.h" #include "google/protobuf/map_unittest.pb.h"
#include "google/protobuf/message.h"
#include "google/protobuf/test_util.h" #include "google/protobuf/test_util.h"
#include "google/protobuf/test_util2.h" #include "google/protobuf/test_util2.h"
#include "google/protobuf/unittest.pb.h" #include "google/protobuf/unittest.pb.h"
@ -163,6 +164,105 @@ TEST_F(TextFormatTest, ShortDebugString) {
} }
TEST_F(TextFormatTest, ShortFormat) {
unittest::RedactedFields proto;
unittest::TestNestedMessageRedaction redacted_nested_proto;
unittest::TestNestedMessageRedaction unredacted_nested_proto;
redacted_nested_proto.set_optional_nested_string("hello");
unredacted_nested_proto.set_optional_nested_string("world");
proto.set_optional_redacted_string("foo");
proto.set_optional_unredacted_string("bar");
proto.add_repeated_redacted_string("1");
proto.add_repeated_redacted_string("2");
proto.add_repeated_unredacted_string("3");
proto.add_repeated_unredacted_string("4");
*proto.mutable_optional_redacted_message() = redacted_nested_proto;
*proto.mutable_optional_unredacted_message() = unredacted_nested_proto;
unittest::TestNestedMessageRedaction* redacted_message_1 =
proto.add_repeated_redacted_message();
unittest::TestNestedMessageRedaction* redacted_message_2 =
proto.add_repeated_redacted_message();
unittest::TestNestedMessageRedaction* unredacted_message_1 =
proto.add_repeated_unredacted_message();
unittest::TestNestedMessageRedaction* unredacted_message_2 =
proto.add_repeated_unredacted_message();
redacted_message_1->set_optional_nested_string("5");
redacted_message_2->set_optional_nested_string("6");
unredacted_message_1->set_optional_nested_string("7");
unredacted_message_2->set_optional_nested_string("8");
(*proto.mutable_map_redacted_string())["abc"] = "def";
(*proto.mutable_map_unredacted_string())["ghi"] = "jkl";
EXPECT_THAT(
google::protobuf::ShortFormat(proto),
testing::MatchesRegex(
"optional_redacted_string: \\[REDACTED\\] "
"optional_unredacted_string: \"bar\" "
"repeated_redacted_string: \\[REDACTED\\] "
"repeated_redacted_string: \\[REDACTED\\] "
"repeated_unredacted_string: \"3\" "
"repeated_unredacted_string: \"4\" "
"optional_redacted_message: \\[REDACTED\\] "
"optional_unredacted_message \\{ "
"optional_nested_string: \"world\" \\} "
"repeated_redacted_message: \\[REDACTED\\] "
"repeated_unredacted_message \\{ optional_nested_string: \"7\" \\} "
"repeated_unredacted_message \\{ optional_nested_string: \"8\" \\} "
"map_redacted_string: \\[REDACTED\\] "
"map_unredacted_string \\{ key: \"ghi\" value: \"jkl\" \\}"));
}
TEST_F(TextFormatTest, Utf8Format) {
unittest::RedactedFields proto;
unittest::TestNestedMessageRedaction redacted_nested_proto;
unittest::TestNestedMessageRedaction unredacted_nested_proto;
redacted_nested_proto.set_optional_nested_string("\350\260\267\346\255\214");
unredacted_nested_proto.set_optional_nested_string(
"\350\260\267\346\255\214");
proto.set_optional_redacted_string("foo");
proto.set_optional_unredacted_string("bar");
proto.add_repeated_redacted_string("1");
proto.add_repeated_redacted_string("2");
proto.add_repeated_unredacted_string("3");
proto.add_repeated_unredacted_string("4");
*proto.mutable_optional_redacted_message() = redacted_nested_proto;
*proto.mutable_optional_unredacted_message() = unredacted_nested_proto;
unittest::TestNestedMessageRedaction* redacted_message_1 =
proto.add_repeated_redacted_message();
unittest::TestNestedMessageRedaction* redacted_message_2 =
proto.add_repeated_redacted_message();
unittest::TestNestedMessageRedaction* unredacted_message_1 =
proto.add_repeated_unredacted_message();
unittest::TestNestedMessageRedaction* unredacted_message_2 =
proto.add_repeated_unredacted_message();
redacted_message_1->set_optional_nested_string("5");
redacted_message_2->set_optional_nested_string("6");
unredacted_message_1->set_optional_nested_string("7");
unredacted_message_2->set_optional_nested_string("8");
(*proto.mutable_map_redacted_string())["abc"] = "def";
(*proto.mutable_map_unredacted_string())["ghi"] = "jkl";
EXPECT_THAT(google::protobuf::Utf8Format(proto),
testing::MatchesRegex(
"optional_redacted_string: \\[REDACTED\\]\n"
"optional_unredacted_string: \"bar\"\n"
"repeated_redacted_string: \\[REDACTED\\]\n"
"repeated_redacted_string: \\[REDACTED\\]\n"
"repeated_unredacted_string: \"3\"\n"
"repeated_unredacted_string: \"4\"\n"
"optional_redacted_message: \\[REDACTED\\]\n"
"optional_unredacted_message \\{\n "
"optional_nested_string: \"\xE8\xB0\xB7\xE6\xAD\x8C\"\n\\}\n"
"repeated_redacted_message: \\[REDACTED\\]\n"
"repeated_unredacted_message \\{\n "
"optional_nested_string: \"7\"\n\\}\n"
"repeated_unredacted_message \\{\n "
"optional_nested_string: \"8\"\n\\}\n"
"map_redacted_string: \\[REDACTED\\]\n"
"map_unredacted_string \\{\n "
"key: \"ghi\"\n value: \"jkl\"\n\\}\n"));
}
TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { TEST_F(TextFormatTest, ShortPrimitiveRepeateds) {
proto_.set_optional_int32(123); proto_.set_optional_int32(123);
proto_.add_repeated_int32(456); proto_.add_repeated_int32(456);

@ -1663,8 +1663,22 @@ message BadFieldNames{
optional int32 for = 2; optional int32 for = 2;
} }
message TestNestedMessageRedaction {
optional string optional_nested_string = 1;
}
message RedactedFields { message RedactedFields {
optional string optional_redacted_string = 1 [debug_redact = true]; optional string optional_redacted_string = 1 [debug_redact = true];
optional string optional_unredacted_string = 2;
repeated string repeated_redacted_string = 3 [debug_redact = true];
repeated string repeated_unredacted_string = 4;
optional TestNestedMessageRedaction optional_redacted_message = 5 [debug_redact = true];
optional TestNestedMessageRedaction optional_unredacted_message = 6;
repeated TestNestedMessageRedaction repeated_redacted_message = 7
[debug_redact = true];
repeated TestNestedMessageRedaction repeated_unredacted_message = 8;
map<string, string> map_redacted_string = 9 [debug_redact = true];
map<string, string> map_unredacted_string = 10;
} }
message TestCord{ message TestCord{

Loading…
Cancel
Save