Skip to content

Commit

Permalink
feat: Expand bytes field type handling (#1180)
Browse files Browse the repository at this point in the history
* 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 authored Jul 14, 2021
1 parent 8bf328c commit aab33a5
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit aab33a5

Please sign in to comment.