Skip to content

Commit f5cb162

Browse files
Use ProtobufToStringOutput to control the output format of AbstractMessage.Builder.toString.
PiperOrigin-RevId: 744743041
1 parent 59e1f0f commit f5cb162

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

java/core/src/main/java/com/google/protobuf/AbstractMessage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,12 @@ public Message.Builder getRepeatedFieldBuilder(final FieldDescriptor field, int
452452

453453
@Override
454454
public String toString() {
455-
return TextFormat.printer().printToString(this);
455+
TextFormat.Printer printer =
456+
ProtobufToStringOutput.shouldOutputDebugFormat()
457+
? TextFormat.debugFormatPrinter()
458+
: TextFormat.printer();
459+
return printer.printToString(
460+
this, TextFormat.Printer.FieldReporterLevel.ABSTRACT_BUILDER_TO_STRING);
456461
}
457462

458463
/** Construct an UninitializedMessageException reporting missing fields in the given message. */

java/core/src/main/java/com/google/protobuf/TextFormat.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ static enum FieldReporterLevel {
159159
DEBUG_MULTILINE(9),
160160
DEBUG_SINGLE_LINE(10),
161161
ABSTRACT_TO_STRING(11),
162-
ABSTRACT_MUTABLE_TO_STRING(12),
163-
REPORT_NONE(13);
162+
ABSTRACT_BUILDER_TO_STRING(12),
163+
ABSTRACT_MUTABLE_TO_STRING(13),
164+
REPORT_NONE(14);
164165
private final int index;
165166

166167
FieldReporterLevel(int index) {

java/core/src/test/java/com/google/protobuf/ProtobufToStringOutputTest.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212

1313
@RunWith(JUnit4.class)
1414
public final class ProtobufToStringOutputTest extends DebugFormatTest {
15+
RedactedFields.Builder messageBuilder;
1516
RedactedFields message;
1617

1718
@Before
1819
public void setupTest() {
19-
message =
20+
messageBuilder =
2021
RedactedFields.newBuilder()
2122
.setOptionalUnredactedString("foo")
2223
.setOptionalRedactedString("bar")
2324
.setOptionalRedactedMessage(
24-
TestNestedMessageRedaction.newBuilder().setOptionalUnredactedNestedString("foobar"))
25-
.build();
25+
TestNestedMessageRedaction.newBuilder()
26+
.setOptionalUnredactedNestedString("foobar"));
27+
message = messageBuilder.build();
2628
}
2729

2830
@Test
@@ -100,4 +102,44 @@ public void toStringFormat_testProtoWrapperWithTextFormat() {
100102
+ "\\]");
101103
});
102104
}
105+
106+
@Test
107+
public void builderToStringFormat_defaultFormat() {
108+
assertThat(messageBuilder.toString())
109+
.matches(
110+
"optional_redacted_string: \"bar\"\n"
111+
+ "optional_unredacted_string: \"foo\"\n"
112+
+ "optional_redacted_message \\{\n"
113+
+ " optional_unredacted_nested_string: \"foobar\"\n"
114+
+ "\\}\n");
115+
}
116+
117+
@Test
118+
public void builderToStringFormat_testDebugFormat() {
119+
ProtobufToStringOutput.callWithDebugFormat(
120+
() ->
121+
assertThat(messageBuilder.toString())
122+
.matches(
123+
String.format(
124+
"%soptional_redacted_string: %s\n"
125+
+ "optional_unredacted_string: \"foo\"\n"
126+
+ "optional_redacted_message \\{\n"
127+
+ " %s\n"
128+
+ "\\}\n",
129+
UNSTABLE_PREFIX_MULTILINE, REDACTED_REGEX, REDACTED_REGEX)));
130+
}
131+
132+
@Test
133+
public void builderToStringFormat_testTextFormat() {
134+
ProtobufToStringOutput.callWithTextFormat(
135+
() -> {
136+
assertThat(messageBuilder.toString())
137+
.matches(
138+
"optional_redacted_string: \"bar\"\n"
139+
+ "optional_unredacted_string: \"foo\"\n"
140+
+ "optional_redacted_message \\{\n"
141+
+ " optional_unredacted_nested_string: \"foobar\"\n"
142+
+ "\\}\n");
143+
});
144+
}
103145
}

0 commit comments

Comments
 (0)