Internal change

PiperOrigin-RevId: 532274191
pull/12802/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent a1f7a8cd36
commit 759fd91388
  1. 34
      src/google/protobuf/text_format.cc
  2. 7
      src/google/protobuf/text_format.h

@ -2527,6 +2527,10 @@ void TextFormat::Printer::PrintField(const Message& message,
PrintFieldName(message, field_index, count, reflection, field, generator); PrintFieldName(message, field_index, count, reflection, field, generator);
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
if (TryRedactFieldValue(message, field, generator,
/*insert_value_separator=*/true)) {
return;
}
const FastFieldValuePrinter* printer = GetFieldPrinter(field); const FastFieldValuePrinter* printer = GetFieldPrinter(field);
const Message& sub_message = const Message& sub_message =
field->is_repeated() field->is_repeated()
@ -2607,9 +2611,8 @@ void TextFormat::Printer::PrintFieldValue(const Message& message,
<< "Index must be -1 for non-repeated fields"; << "Index must be -1 for non-repeated fields";
const FastFieldValuePrinter* printer = GetFieldPrinter(field); const FastFieldValuePrinter* printer = GetFieldPrinter(field);
if (redact_debug_string_ && field->options().debug_redact()) { if (TryRedactFieldValue(message, field, generator,
IncrementRedactedFieldCounter(); /*insert_value_separator=*/false)) {
generator->PrintString("[REDACTED]");
return; return;
} }
@ -2829,6 +2832,31 @@ void TextFormat::Printer::PrintUnknownFields(
} }
} }
bool TextFormat::Printer::TryRedactFieldValue(
const Message& message, const FieldDescriptor* field,
BaseTextGenerator* generator, bool insert_value_separator) const {
auto do_redact = [&](absl::string_view replacement) {
IncrementRedactedFieldCounter();
if (insert_value_separator) {
generator->PrintMaybeWithMarker(MarkerToken(), ": ");
}
generator->PrintString(replacement);
if (insert_value_separator) {
if (single_line_mode_) {
generator->PrintLiteral(" ");
} else {
generator->PrintLiteral("\n");
}
}
};
if (redact_debug_string_ && field->options().debug_redact()) {
do_redact("[REDACTED]");
return true;
}
return false;
}
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google

@ -492,6 +492,13 @@ class PROTOBUF_EXPORT TextFormat {
bool PrintAny(const Message& message, BaseTextGenerator* generator) const; bool PrintAny(const Message& message, BaseTextGenerator* generator) const;
// Try to redact a field value based on the annotations associated with
// the field. This function returns true if it redacts the field value.
bool TryRedactFieldValue(const Message& message,
const FieldDescriptor* field,
BaseTextGenerator* generator,
bool insert_value_separator) const;
const FastFieldValuePrinter* GetFieldPrinter( const FastFieldValuePrinter* GetFieldPrinter(
const FieldDescriptor* field) const { const FieldDescriptor* field) const {
auto it = custom_printers_.find(field); auto it = custom_printers_.find(field);

Loading…
Cancel
Save