Skip to content

Commit 98a714f

Browse files
authoredDec 5, 2024··
feat: add support for byte and short (#2789)
1 parent 333525a commit 98a714f

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed
 

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,9 @@ private FieldDescriptorAndFieldTableSchema computeDescriptorAndSchema(
486486
* Fills a non-repetaed protoField with the json data.
487487
*
488488
* @param protoMsg The protocol buffer message being constructed
489-
* @param fieldDescriptor
490-
* @param fieldSchema
489+
* @param fieldDescriptor Proto format to be transmitted over the wire (derived from table schema
490+
* via BQTableSchemaToProtoDescriptor.BQTableSchemaModeMap)
491+
* @param fieldSchema Actual table column schema type if available
491492
* @param json
492493
* @param exactJsonKeyName Exact key name in JSONObject instead of lowercased version
493494
* @param currentScope Debugging purposes
@@ -647,6 +648,12 @@ private void fillField(
647648
} else if (val instanceof Long) {
648649
protoMsg.setField(fieldDescriptor, val);
649650
return;
651+
} else if (val instanceof Byte) {
652+
protoMsg.setField(fieldDescriptor, Long.valueOf((Byte) val));
653+
return;
654+
} else if (val instanceof Short) {
655+
protoMsg.setField(fieldDescriptor, Long.valueOf((Short) val));
656+
return;
650657
}
651658
if (val instanceof String) {
652659
Long parsed = Longs.tryParse((String) val);
@@ -727,8 +734,9 @@ private void fillField(
727734
* Fills a repeated protoField with the json data.
728735
*
729736
* @param protoMsg The protocol buffer message being constructed
730-
* @param fieldDescriptor
731-
* @param fieldSchema
737+
* @param fieldDescriptor Proto format to be transmitted over the wire (derived from table schema
738+
* via BQTableSchemaToProtoDescriptor.BQTableSchemaModeMap)
739+
* @param fieldSchema Actual table column schema type if available
732740
* @param json If root level has no matching fields, throws exception.
733741
* @param exactJsonKeyName Exact key name in JSONObject instead of lowercased version
734742
* @param currentScope Debugging purposes
@@ -912,6 +920,10 @@ private void fillRepeatedField(
912920
protoMsg.addRepeatedField(fieldDescriptor, Long.valueOf((Integer) val));
913921
} else if (val instanceof Long) {
914922
protoMsg.addRepeatedField(fieldDescriptor, val);
923+
} else if (val instanceof Byte) {
924+
protoMsg.addRepeatedField(fieldDescriptor, Long.valueOf((Byte) val));
925+
} else if (val instanceof Short) {
926+
protoMsg.addRepeatedField(fieldDescriptor, Long.valueOf((Short) val));
915927
} else if (val instanceof String) {
916928
Long parsed = Longs.tryParse((String) val);
917929
if (parsed != null) {

‎google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java

+44-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import java.math.BigDecimal;
3131
import java.time.LocalTime;
3232
import java.util.ArrayList;
33+
import java.util.Collection;
3334
import java.util.Collections;
35+
import java.util.HashMap;
3436
import java.util.List;
3537
import java.util.Map;
3638
import java.util.logging.Logger;
@@ -579,8 +581,8 @@ public void testInt64() throws Exception {
579581
TestInt64 expectedProto =
580582
TestInt64.newBuilder().setByte(1).setShort(1).setInt(1).setLong(1).setString(1).build();
581583
JSONObject json = new JSONObject();
582-
json.put("byte", (byte) 1);
583-
json.put("short", (short) 1);
584+
json.put("byte", (byte) 1); // This does NOT actually verify byte as it is converted to int
585+
json.put("short", (short) 1); // This does NOT actually verify short as it is converted to int
584586
json.put("int", 1);
585587
json.put("long", 1L);
586588
json.put("string", "1");
@@ -589,6 +591,46 @@ public void testInt64() throws Exception {
589591
assertEquals(expectedProto, protoMsg);
590592
}
591593

594+
@Test
595+
public void testInt64Extended() throws Exception {
596+
TestInt64 expectedProto =
597+
TestInt64.newBuilder().setByte(1).setShort(1).setInt(1).setLong(1).setString(1).build();
598+
Map map = new HashMap();
599+
map.put("byte", (byte) 1);
600+
map.put("short", (short) 1);
601+
map.put("int", (int) 1);
602+
map.put("long", (long) 1);
603+
map.put("string", "1");
604+
JSONObject json = new JSONObject(map);
605+
DynamicMessage protoMsg =
606+
JsonToProtoMessage.INSTANCE.convertToProtoMessage(TestInt64.getDescriptor(), json);
607+
assertEquals(expectedProto, protoMsg);
608+
}
609+
610+
@Test
611+
public void testInt64Repeated() throws Exception {
612+
RepeatedInt64 expectedProto =
613+
RepeatedInt64.newBuilder()
614+
.addTestRepeated(1)
615+
.addTestRepeated(1)
616+
.addTestRepeated(1)
617+
.addTestRepeated(1)
618+
.addTestRepeated(1)
619+
.build();
620+
Collection collection = new ArrayList();
621+
collection.add((byte) 1);
622+
collection.add((short) 1);
623+
collection.add((int) 1);
624+
collection.add((long) 1);
625+
collection.add("1");
626+
JSONArray array = new JSONArray(collection);
627+
JSONObject json = new JSONObject();
628+
json.put("test_repeated", array);
629+
DynamicMessage protoMsg =
630+
JsonToProtoMessage.INSTANCE.convertToProtoMessage(RepeatedInt64.getDescriptor(), json);
631+
assertEquals(expectedProto, protoMsg);
632+
}
633+
592634
@Test
593635
public void testInt32() throws Exception {
594636
TestInt32 expectedProto =

0 commit comments

Comments
 (0)
Please sign in to comment.