Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit aab33a5

Browse files
feat: Expand bytes field type handling (#1180)
* Expand Bytes Field type handling Add the ability for a single BYTES field to handle a JSONArray of bytes, and add the ability for a repeated BYTES field to handle a ByteString. * Fixing Typo Index to i * Fixing variable naming * Lint
1 parent 8bf328c commit aab33a5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ private static void fillField(
144144
if (val instanceof ByteString) {
145145
protoMsg.setField(fieldDescriptor, ((ByteString) val).toByteArray());
146146
return;
147+
} else if (val instanceof JSONArray) {
148+
try {
149+
byte[] bytes = new byte[((JSONArray) val).length()];
150+
for (int j = 0; j < ((JSONArray) val).length(); j++) {
151+
bytes[j] = (byte) ((JSONArray) val).getInt(j);
152+
if (bytes[j] != ((JSONArray) val).getInt(j)) {
153+
throw new IllegalArgumentException(
154+
String.format(
155+
"Error: "
156+
+ currentScope
157+
+ "["
158+
+ j
159+
+ "] could not be converted to byte[]."));
160+
}
161+
}
162+
protoMsg.setField(fieldDescriptor, bytes);
163+
} catch (JSONException e) {
164+
throw new IllegalArgumentException(
165+
String.format("Error: " + currentScope + "could not be converted to byte[]."));
166+
}
147167
}
148168
break;
149169
case INT64:
@@ -261,6 +281,9 @@ private static void fillRepeatedField(
261281
+ index
262282
+ "] could not be converted to byte[]."));
263283
}
284+
} else if (val instanceof ByteString) {
285+
protoMsg.addRepeatedField(fieldDescriptor, ((ByteString) val).toByteArray());
286+
return;
264287
} else {
265288
fail = true;
266289
}

0 commit comments

Comments
 (0)