Skip to content

Commit 1003dff

Browse files
tanx16copybara-github
authored andcommitted
Array utils for textformat migration
PiperOrigin-RevId: 764903879
1 parent e09a771 commit 1003dff

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.google.protobuf;
22

3+
import static java.util.Arrays.stream;
4+
5+
import java.util.stream.Collectors;
6+
import java.util.stream.StreamSupport;
7+
38
/**
49
* The legacy APIs preserve the existing toString() behavior (output TextFormat), which allows us to
510
* migrate toString callers that expect TextFormat output off toString. Eventually, we will make
@@ -64,4 +69,33 @@ public static String legacyUnredactedStringValueOf(
6469
Object object) {
6570
return (object == null) ? String.valueOf(object) : legacyUnredactedToString(object);
6671
}
72+
73+
/**
74+
* Map each element in an {@code Iterable<T>} to {@code Iterable<String>} using
75+
* legacyUnredactedStringValueOf. This is a useful shortcut for callers that operate on an
76+
* Iterable of objects.
77+
*/
78+
@Deprecated
79+
public static Iterable<String> legacyUnredactedToStringList(
80+
Iterable<?> iterable) {
81+
return iterable == null
82+
? null
83+
: StreamSupport.stream(iterable.spliterator(), false)
84+
.map(LegacyUnredactedTextFormat::legacyUnredactedStringValueOf)
85+
.collect(Collectors.toList());
86+
}
87+
88+
/**
89+
* Map each element in an Object[] to String using legacyUnredactedStringValueOf. This is a useful
90+
* shortcut for callers that operate on an Object[] of objects.
91+
*/
92+
@Deprecated
93+
public static String[] legacyUnredactedToStringArray(
94+
Object[] objects) {
95+
return objects == null
96+
? null
97+
: stream(objects)
98+
.map(LegacyUnredactedTextFormat::legacyUnredactedStringValueOf)
99+
.toArray(String[]::new);
100+
}
67101
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,23 @@ public static String unredactedStringValueOf(
6060
Object object) {
6161
return LegacyUnredactedTextFormat.legacyUnredactedStringValueOf(object);
6262
}
63+
64+
/**
65+
* Map each element in an {@code Iterable<T>} to {@code Iterable<String>} using
66+
* unredactedStringValueOf. This is a useful shortcut for callers that operate on an Iterable of
67+
* objects.
68+
*/
69+
public static Iterable<String> unredactedToStringList(
70+
Iterable<?> iterable) {
71+
return LegacyUnredactedTextFormat.legacyUnredactedToStringList(iterable);
72+
}
73+
74+
/**
75+
* Map each element in an Object[] to String using unredactedStringValueOf. This is a useful
76+
* shortcut for callers that operate on an Object[] of objects.
77+
*/
78+
public static String[] unredactedToStringArray(
79+
Object[] objects) {
80+
return LegacyUnredactedTextFormat.legacyUnredactedToStringArray(objects);
81+
}
6382
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.google.common.truth.Truth.assertThat;
44

55
import proto2_unittest.UnittestProto;
6+
import java.util.Arrays;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
89
import org.junit.runners.JUnit4;
@@ -180,4 +181,70 @@ public void legacyUnredactedToString_returnsEmptyStringForNullObject() {
180181
assertThat(LegacyUnredactedTextFormat.legacyUnredactedStringValueOf(message))
181182
.isEqualTo(String.valueOf(message));
182183
}
184+
185+
@Test
186+
public void legacyUnredactedToStringArray_returnsTextFormat() {
187+
UnittestProto.RedactedFields message =
188+
UnittestProto.RedactedFields.newBuilder()
189+
.addRepeatedRedactedMessage(
190+
UnittestProto.TestNestedMessageRedaction.newBuilder()
191+
.setOptionalRedactedNestedString("123")
192+
.build())
193+
.addRepeatedRedactedMessage(
194+
UnittestProto.TestNestedMessageRedaction.newBuilder()
195+
.setOptionalUnredactedNestedString("456")
196+
.build())
197+
.build();
198+
assertThat(
199+
LegacyUnredactedTextFormat.legacyUnredactedToStringArray(
200+
new Message[] {message, message}))
201+
.isEqualTo(
202+
new String[] {
203+
"repeated_redacted_message {\n"
204+
+ " optional_redacted_nested_string: \"123\"\n"
205+
+ "}\n"
206+
+ "repeated_redacted_message {\n"
207+
+ " optional_unredacted_nested_string: \"456\"\n"
208+
+ "}\n",
209+
"repeated_redacted_message {\n"
210+
+ " optional_redacted_nested_string: \"123\"\n"
211+
+ "}\n"
212+
+ "repeated_redacted_message {\n"
213+
+ " optional_unredacted_nested_string: \"456\"\n"
214+
+ "}\n"
215+
});
216+
}
217+
218+
@Test
219+
public void legacyUnredactedToStringList_returnsTextFormat() {
220+
UnittestProto.RedactedFields message =
221+
UnittestProto.RedactedFields.newBuilder()
222+
.addRepeatedRedactedMessage(
223+
UnittestProto.TestNestedMessageRedaction.newBuilder()
224+
.setOptionalRedactedNestedString("123")
225+
.build())
226+
.addRepeatedRedactedMessage(
227+
UnittestProto.TestNestedMessageRedaction.newBuilder()
228+
.setOptionalUnredactedNestedString("456")
229+
.build())
230+
.build();
231+
assertThat(
232+
LegacyUnredactedTextFormat.legacyUnredactedToStringList(
233+
Arrays.asList(message, message)))
234+
.containsExactly(
235+
new String[] {
236+
"repeated_redacted_message {\n"
237+
+ " optional_redacted_nested_string: \"123\"\n"
238+
+ "}\n"
239+
+ "repeated_redacted_message {\n"
240+
+ " optional_unredacted_nested_string: \"456\"\n"
241+
+ "}\n",
242+
"repeated_redacted_message {\n"
243+
+ " optional_redacted_nested_string: \"123\"\n"
244+
+ "}\n"
245+
+ "repeated_redacted_message {\n"
246+
+ " optional_unredacted_nested_string: \"456\"\n"
247+
+ "}\n"
248+
});
249+
}
183250
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.google.common.truth.Truth.assertThat;
44

55
import proto2_unittest.UnittestProto;
6+
import java.util.Arrays;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
89
import org.junit.runners.JUnit4;
@@ -180,4 +181,67 @@ public void unredactedStringValueOf_returnsEmptyStringForNullObject() {
180181
assertThat(UnredactedDebugFormatForTest.unredactedStringValueOf(message))
181182
.isEqualTo(String.valueOf(message));
182183
}
184+
185+
@Test
186+
public void unredactedToStringArray_returnsTextFormat() {
187+
UnittestProto.RedactedFields message =
188+
UnittestProto.RedactedFields.newBuilder()
189+
.addRepeatedRedactedMessage(
190+
UnittestProto.TestNestedMessageRedaction.newBuilder()
191+
.setOptionalRedactedNestedString("123")
192+
.build())
193+
.addRepeatedRedactedMessage(
194+
UnittestProto.TestNestedMessageRedaction.newBuilder()
195+
.setOptionalUnredactedNestedString("456")
196+
.build())
197+
.build();
198+
assertThat(
199+
UnredactedDebugFormatForTest.unredactedToStringArray(new Message[] {message, message}))
200+
.isEqualTo(
201+
new String[] {
202+
"repeated_redacted_message {\n"
203+
+ " optional_redacted_nested_string: \"123\"\n"
204+
+ "}\n"
205+
+ "repeated_redacted_message {\n"
206+
+ " optional_unredacted_nested_string: \"456\"\n"
207+
+ "}\n",
208+
"repeated_redacted_message {\n"
209+
+ " optional_redacted_nested_string: \"123\"\n"
210+
+ "}\n"
211+
+ "repeated_redacted_message {\n"
212+
+ " optional_unredacted_nested_string: \"456\"\n"
213+
+ "}\n"
214+
});
215+
}
216+
217+
@Test
218+
public void unredactedToStringList_returnsTextFormat() {
219+
UnittestProto.RedactedFields message =
220+
UnittestProto.RedactedFields.newBuilder()
221+
.addRepeatedRedactedMessage(
222+
UnittestProto.TestNestedMessageRedaction.newBuilder()
223+
.setOptionalRedactedNestedString("123")
224+
.build())
225+
.addRepeatedRedactedMessage(
226+
UnittestProto.TestNestedMessageRedaction.newBuilder()
227+
.setOptionalUnredactedNestedString("456")
228+
.build())
229+
.build();
230+
assertThat(UnredactedDebugFormatForTest.unredactedToStringList(Arrays.asList(message, message)))
231+
.containsExactly(
232+
new String[] {
233+
"repeated_redacted_message {\n"
234+
+ " optional_redacted_nested_string: \"123\"\n"
235+
+ "}\n"
236+
+ "repeated_redacted_message {\n"
237+
+ " optional_unredacted_nested_string: \"456\"\n"
238+
+ "}\n",
239+
"repeated_redacted_message {\n"
240+
+ " optional_redacted_nested_string: \"123\"\n"
241+
+ "}\n"
242+
+ "repeated_redacted_message {\n"
243+
+ " optional_unredacted_nested_string: \"456\"\n"
244+
+ "}\n"
245+
});
246+
}
183247
}

0 commit comments

Comments
 (0)