Skip to content

Commit

Permalink
fix: add more idiomatic way to insert ARRAY<BYTES> data (#1550)
Browse files Browse the repository at this point in the history
* fix: add more idiomatic way to insert ARRAY<BYTES> data

Towards b/219375453

* nit

* lint

* clean up
  • Loading branch information
stephaniewang526 authored Feb 28, 2022
1 parent 870cd93 commit 3ae4038
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ private static void fillRepeatedField(
}
}
if (!added) {
if (val instanceof JSONArray) {
if (val instanceof byte[]) {
protoMsg.addRepeatedField(fieldDescriptor, val);
} else if (val instanceof JSONArray) {
try {
byte[] bytes = new byte[((JSONArray) val).length()];
for (int j = 0; j < ((JSONArray) val).length(); j++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,24 @@ public void testJsonStreamWriterWithDefaultStream()
JSONArray jsonArr3 = new JSONArray();
jsonArr3.put(row4);

LOG.info("Sending two more messages");
JSONObject row5 = new JSONObject();
// Add another ARRAY<BYTES> using a more idiomatic way
JSONArray testArr = new JSONArray(); // create empty JSONArray
testArr.put(0, ByteString.copyFromUtf8("a").toByteArray()); // insert 1st bytes array
testArr.put(1, ByteString.copyFromUtf8("b").toByteArray()); // insert 2nd bytes array
row5.put("test_bytestring_repeated", testArr);
JSONArray jsonArr4 = new JSONArray();
jsonArr4.put(row5);

LOG.info("Sending three more messages");
ApiFuture<AppendRowsResponse> response2 = jsonStreamWriter.append(jsonArr2, -1);
LOG.info("Sending one more message");
LOG.info("Sending two more messages");
ApiFuture<AppendRowsResponse> response3 = jsonStreamWriter.append(jsonArr3, -1);
LOG.info("Sending one more message");
ApiFuture<AppendRowsResponse> response4 = jsonStreamWriter.append(jsonArr4, -1);
Assert.assertFalse(response2.get().getAppendResult().hasOffset());
Assert.assertFalse(response3.get().getAppendResult().hasOffset());
Assert.assertFalse(response4.get().getAppendResult().hasOffset());

TableResult result =
bigquery.listTableData(
Expand All @@ -430,6 +442,9 @@ public void testJsonStreamWriterWithDefaultStream()
assertEquals("bbb", iter.next().get(0).getStringValue());
assertEquals("ccc", iter.next().get(0).getStringValue());
assertEquals("ddd", iter.next().get(0).getStringValue());
FieldValueList currentRow2 = iter.next();
assertEquals("YQ==", currentRow2.get(3).getRepeatedValue().get(0).getStringValue());
assertEquals("Yg==", currentRow2.get(3).getRepeatedValue().get(1).getStringValue());
assertEquals(false, iter.hasNext());
}
}
Expand Down

0 comments on commit 3ae4038

Please sign in to comment.