summaryrefslogtreecommitdiffstats
path: root/contrib/libs/protobuf/patches/enable-debug-redact-for-message-fields-backport-ci-759fd91-c3f61a6.patch
blob: cb9d245a36e49a71c8b34c106c25b7b7387bf4a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
--- contrib/libs/protobuf/src/google/protobuf/text_format.cc	(7e573068bfd84adfb48ca975dd5bf3837a5bc0d1)
+++ contrib/libs/protobuf/src/google/protobuf/text_format.cc	(working tree)
@@ -2508,6 +2508,10 @@ void TextFormat::Printer::PrintField(const Message& message,
     PrintFieldName(message, field_index, count, reflection, field, generator);
 
     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+      if (TryRedactFieldValue(message, field, generator,
+                              /*insert_value_separator=*/true)) {
+        break;
+      }
       const FastFieldValuePrinter* printer = GetFieldPrinter(field);
       const Message& sub_message =
           field->is_repeated()
@@ -2582,9 +2586,8 @@ void TextFormat::Printer::PrintFieldValue(const Message& message,
       << "Index must be -1 for non-repeated fields";
 
   const FastFieldValuePrinter* printer = GetFieldPrinter(field);
-  if (redact_debug_string_ && field->options().debug_redact()) {
-    // TODO(b/258975650): Create OSS redaction documentation
-    generator->PrintString("[REDACTED]");
+  if (TryRedactFieldValue(message, field, generator,
+                          /*insert_value_separator=*/false)) {
     return;
   }
 
@@ -2799,6 +2802,30 @@ void TextFormat::Printer::PrintUnknownFields(
   }
 }
 
+bool TextFormat::Printer::TryRedactFieldValue(
+    const Message& message, const FieldDescriptor* field,
+    BaseTextGenerator* generator, bool insert_value_separator) const {
+  auto do_redact = [&](const TProtoStringType& replacement) {
+    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 google
 
--- contrib/libs/protobuf/src/google/protobuf/text_format.h	(7e573068bfd84adfb48ca975dd5bf3837a5bc0d1)
+++ contrib/libs/protobuf/src/google/protobuf/text_format.h	(working tree)
@@ -487,6 +487,13 @@ class PROTOBUF_EXPORT TextFormat {
 
     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 FieldDescriptor* field) const {
       auto it = custom_printers_.find(field);