Skip to content
Permalink
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
  • Loading branch information
JacobStocklass committed Jul 14, 2021
1 parent 8bf328c commit aab33a55f3cb91003363ada1395f07868bc17ee5
Showing with 23 additions and 0 deletions.
  1. +23 −0 ...d-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java
@@ -144,6 +144,26 @@ private static void fillField(
if (val instanceof ByteString) {
protoMsg.setField(fieldDescriptor, ((ByteString) val).toByteArray());
return;
} else if (val instanceof JSONArray) {
try {
byte[] bytes = new byte[((JSONArray) val).length()];
for (int j = 0; j < ((JSONArray) val).length(); j++) {
bytes[j] = (byte) ((JSONArray) val).getInt(j);
if (bytes[j] != ((JSONArray) val).getInt(j)) {
throw new IllegalArgumentException(
String.format(
"Error: "
+ currentScope
+ "["
+ j
+ "] could not be converted to byte[]."));
}
}
protoMsg.setField(fieldDescriptor, bytes);
} catch (JSONException e) {
throw new IllegalArgumentException(
String.format("Error: " + currentScope + "could not be converted to byte[]."));
}
}
break;
case INT64:
@@ -261,6 +281,9 @@ private static void fillRepeatedField(
+ index
+ "] could not be converted to byte[]."));
}
} else if (val instanceof ByteString) {
protoMsg.addRepeatedField(fieldDescriptor, ((ByteString) val).toByteArray());
return;
} else {
fail = true;
}

0 comments on commit aab33a5

Please sign in to comment.