From fe5216f48868336b878efb7df8748e25c3e047c2 Mon Sep 17 00:00:00 2001 From: JacobStocklass <35313153+JacobStocklass@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:08:46 -0700 Subject: [PATCH 1/4] 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. --- .../storage/v1beta2/JsonToProtoMessage.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java index c316df0eef..538a559128 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java @@ -144,7 +144,32 @@ 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 + + "[" + + index + + "] could not be converted to byte[].")); + } + } + protoMsg.setField(fieldDescriptor, bytes); + } catch (JSONException e) { + throw new IllegalArgumentException( + String.format( + "Error: " + + currentScope + + "[" + + index + + "] could not be converted to byte[].")); + } + } break; case INT64: if (val instanceof Integer) { @@ -261,6 +286,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; } From 38006409348b10fdae86134e1bea1b63fd3394e7 Mon Sep 17 00:00:00 2001 From: JacobStocklass <35313153+JacobStocklass@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:27:55 -0700 Subject: [PATCH 2/4] Fixing Typo Index to i --- .../cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java index 538a559128..e5626b5c88 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java @@ -155,7 +155,7 @@ private static void fillField( "Error: " + currentScope + "[" - + index + + i + "] could not be converted to byte[].")); } } @@ -166,7 +166,7 @@ private static void fillField( "Error: " + currentScope + "[" - + index + + i + "] could not be converted to byte[].")); } } From da2a88c5bc20cfb8e35f3c6bc501af2eddf11816 Mon Sep 17 00:00:00 2001 From: jacobstocklass Date: Thu, 8 Jul 2021 20:56:46 +0000 Subject: [PATCH 3/4] Fixing variable naming --- .../cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java index e5626b5c88..6f6b1edbac 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java @@ -155,7 +155,7 @@ private static void fillField( "Error: " + currentScope + "[" - + i + + j + "] could not be converted to byte[].")); } } @@ -165,9 +165,7 @@ private static void fillField( String.format( "Error: " + currentScope - + "[" - + i - + "] could not be converted to byte[].")); + + "could not be converted to byte[].")); } } break; From 7bcb41059165f511304e2960be6e56ae5fb1ffb8 Mon Sep 17 00:00:00 2001 From: jacobstocklass Date: Thu, 8 Jul 2021 21:35:17 +0000 Subject: [PATCH 4/4] Lint --- .../storage/v1beta2/JsonToProtoMessage.java | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java index 6f6b1edbac..9c6d831034 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonToProtoMessage.java @@ -145,29 +145,26 @@ private static void fillField( 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[].")); - } + 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[].")); } + protoMsg.setField(fieldDescriptor, bytes); + } catch (JSONException e) { + throw new IllegalArgumentException( + String.format("Error: " + currentScope + "could not be converted to byte[].")); } + } break; case INT64: if (val instanceof Integer) { @@ -284,7 +281,7 @@ private static void fillRepeatedField( + index + "] could not be converted to byte[].")); } - } else if (val instanceof ByteString){ + } else if (val instanceof ByteString) { protoMsg.addRepeatedField(fieldDescriptor, ((ByteString) val).toByteArray()); return; } else {