Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 702062087
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 2, 2024
1 parent 7c3b8b7 commit 3bbaa24
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.google.protobuf;

import java.util.Optional;

/**
* ProtobufToStringOutput controls the output format of {@link Message#toString()}. Specifically, for
* the Runnable object passed to `callWithDebugFormat` and `callWithTextFormat`, Message.toString()
* will always output the specified format unless ProtobufToStringOutput is used again to change the
* output format.
* ProtobufToStringOutput controls the output format of {@link Message#toString()}. Specifically,
* for the Runnable object passed to `callWithDebugFormat` and `callWithTextFormat`,
* Message.toString() will always output the specified format unless ProtobufToStringOutput is used
* again to change the output format.
*/
public final class ProtobufToStringOutput {
private enum OutputMode {
Expand All @@ -13,12 +15,14 @@ private enum OutputMode {
}

private static final ThreadLocal<OutputMode> outputMode =
new ThreadLocal<OutputMode>() {
@Override
protected OutputMode initialValue() {
return OutputMode.TEXT_FORMAT;
}
};
ThreadLocal.withInitial(() -> OutputMode.TEXT_FORMAT);

// Many tests are bound to the traditional Message.toString() output format (the text format). To
// accelerate the debug format adoption while avoiding breaking such tests, we introduce this
// test-only flag to disable the debug format in tests for certain libraries in which the output
// of Message.toString() is unlikely to be deserialized.
private static final ThreadLocal<Boolean> disableDebugFormatForTests =
ThreadLocal.withInitial(() -> false);

private ProtobufToStringOutput() {}

Expand Down Expand Up @@ -49,4 +53,8 @@ public static void callWithTextFormat(Runnable impl) {
public static boolean shouldOutputDebugFormat() {
return outputMode.get() == OutputMode.DEBUG_FORMAT;
}

static void setDisableDebugFormatForTests(boolean disable) {
disableDebugFormatForTests.set(disable);
}
}

0 comments on commit 3bbaa24

Please sign in to comment.