Skip to content

Commit 4f4216e

Browse files
anthaviomartin-vanek-ckgcf-owl-bot[bot]
authored
feat: Add short, integer, long, boolean conversions into string (#2437)
* feat: Add short, integer, long, boolean conversions into string * chore: linting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Martin Vanek <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent fe9c3ae commit 4f4216e

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ private void fillField(
650650
if (val instanceof String) {
651651
protoMsg.setField(fieldDescriptor, val);
652652
return;
653+
} else if (val instanceof Short
654+
|| val instanceof Integer
655+
|| val instanceof Long
656+
|| val instanceof Boolean) {
657+
protoMsg.setField(fieldDescriptor, String.valueOf(val));
658+
return;
653659
}
654660
break;
655661
case DOUBLE:
@@ -910,6 +916,12 @@ private void fillRepeatedField(
910916
case STRING:
911917
if (val instanceof String) {
912918
protoMsg.addRepeatedField(fieldDescriptor, val);
919+
} else if (val instanceof Short
920+
|| val instanceof Integer
921+
|| val instanceof Long
922+
|| val instanceof Boolean) {
923+
protoMsg.addRepeatedField(fieldDescriptor, String.valueOf(val));
924+
return;
913925
} else {
914926
throwWrongFieldType(fieldDescriptor, currentScope, index);
915927
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.google.cloud.bigquery.storage.v1.ConnectionWorkerPool.Settings;
3838
import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializationError;
3939
import com.google.cloud.bigquery.storage.v1.TableFieldSchema.Mode;
40+
import com.google.common.collect.ImmutableMap;
4041
import com.google.protobuf.ByteString;
4142
import com.google.protobuf.Descriptors.DescriptorValidationException;
4243
import com.google.protobuf.Int64Value;
@@ -1415,8 +1416,8 @@ public void testMultipleAppendSerializationErrors()
14151416
// put a vaild value into the field
14161417
foo1.put("foo", "allen");
14171418
JSONObject foo2 = new JSONObject();
1418-
// put a number into a string field
1419-
foo2.put("foo", 666);
1419+
// put a field which is not part of the expected schema
1420+
foo2.put("not_bar", "woody");
14201421
JSONArray jsonArr = new JSONArray();
14211422
jsonArr.put(foo);
14221423
jsonArr.put(foo1);
@@ -1434,14 +1435,11 @@ public void testMultipleAppendSerializationErrors()
14341435
} catch (AppendSerializationError appendSerializationError) {
14351436
Map<Integer, String> rowIndexToErrorMessage =
14361437
appendSerializationError.getRowIndexToErrorMessage();
1437-
assertEquals(2, rowIndexToErrorMessage.size());
1438-
assertEquals(
1439-
"The source object has fields unknown to BigQuery: root.not_foo.",
1440-
rowIndexToErrorMessage.get(0));
14411438
assertEquals(
1442-
"Field root.foo failed to convert to STRING. Error: JSONObject does not have a string"
1443-
+ " field at root.foo.",
1444-
rowIndexToErrorMessage.get(2));
1439+
ImmutableMap.of(
1440+
0, "The source object has fields unknown to BigQuery: root.not_foo.",
1441+
2, "The source object has fields unknown to BigQuery: root.not_bar."),
1442+
rowIndexToErrorMessage);
14451443
}
14461444
}
14471445
}

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ public class JsonToProtoMessageTest {
8787
})
8888
.put(
8989
StringType.getDescriptor(),
90-
new Message[] {StringType.newBuilder().setTestFieldType("test").build()})
90+
new Message[] {
91+
StringType.newBuilder().setTestFieldType("9223372036854775807").build(),
92+
StringType.newBuilder().setTestFieldType("2147483647").build(),
93+
StringType.newBuilder().setTestFieldType("true").build(),
94+
StringType.newBuilder().setTestFieldType("test").build()
95+
})
9196
.put(
9297
RepeatedType.getDescriptor(),
9398
new Message[] {
@@ -147,6 +152,9 @@ public class JsonToProtoMessageTest {
147152
.put(
148153
RepeatedString.getDescriptor(),
149154
new Message[] {
155+
RepeatedString.newBuilder().addTestRepeated("9223372036854775807").build(),
156+
RepeatedString.newBuilder().addTestRepeated("2147483647").build(),
157+
RepeatedString.newBuilder().addTestRepeated("true").build(),
150158
RepeatedString.newBuilder().addTestRepeated("hello").addTestRepeated("test").build()
151159
})
152160
.put(
@@ -925,6 +933,8 @@ public void testAllTypes() throws Exception {
925933
} else if (entry.getKey() == Int64Type.getDescriptor()
926934
|| entry.getKey() == BytesType.getDescriptor()) {
927935
assertEquals(entry.getKey().getFullName(), 2, success);
936+
} else if (entry.getKey() == StringType.getDescriptor()) {
937+
assertEquals(entry.getKey().getFullName(), 4, success);
928938
} else {
929939
assertEquals(entry.getKey().getFullName(), 1, success);
930940
}
@@ -962,6 +972,8 @@ public void testAllRepeatedTypesWithLimits() throws Exception {
962972
assertEquals(entry.getKey().getFullName(), 4, success);
963973
} else if (entry.getKey() == RepeatedInt64.getDescriptor()) {
964974
assertEquals(entry.getKey().getFullName(), 2, success);
975+
} else if (entry.getKey() == RepeatedString.getDescriptor()) {
976+
assertEquals(entry.getKey().getFullName(), 4, success);
965977
} else {
966978
assertEquals(entry.getKey().getFullName(), 1, success);
967979
}
@@ -1009,14 +1021,20 @@ public void testRequired() throws Exception {
10091021

10101022
@Test
10111023
public void testStructSimple() throws Exception {
1024+
structSimple("test", "test");
1025+
structSimple(true, "true");
1026+
structSimple(1, "1");
1027+
structSimple((short) 1, "1");
1028+
structSimple((long) 1, "1");
1029+
}
1030+
1031+
private void structSimple(Object value, String expected) throws Exception {
10121032
MessageType expectedProto =
10131033
MessageType.newBuilder()
1014-
.setTestFieldType(StringType.newBuilder().setTestFieldType("test").build())
1034+
.setTestFieldType(StringType.newBuilder().setTestFieldType(expected).build())
10151035
.build();
1016-
JSONObject stringType = new JSONObject();
1017-
stringType.put("test_field_type", "test");
1018-
JSONObject json = new JSONObject();
1019-
json.put("test_field_type", stringType);
1036+
JSONObject stringType = new JSONObject(ImmutableMap.of("test_field_type", value));
1037+
JSONObject json = new JSONObject(ImmutableMap.of("test_field_type", stringType));
10201038

10211039
DynamicMessage protoMsg =
10221040
JsonToProtoMessage.INSTANCE.convertToProtoMessage(MessageType.getDescriptor(), json);
@@ -1026,7 +1044,7 @@ public void testStructSimple() throws Exception {
10261044
@Test
10271045
public void testStructSimpleFail() throws Exception {
10281046
JSONObject stringType = new JSONObject();
1029-
stringType.put("test_field_type", 1);
1047+
stringType.put("test_field_type", new boolean[0]);
10301048
JSONObject json = new JSONObject();
10311049
json.put("test_field_type", stringType);
10321050
try {
@@ -1268,7 +1286,7 @@ public void testNestedRepeatedComplex() throws Exception {
12681286
@Test
12691287
public void testNestedRepeatedComplexFail() throws Exception {
12701288
double[] doubleArr = {1.1, 2.2, 3.3, 4.4, 5.5};
1271-
Boolean[] fakeStringArr = {true, false};
1289+
Boolean[][] fakeStringArr = {new Boolean[0], new Boolean[0]};
12721290
int[] intArr = {1, 2, 3, 4, 5};
12731291

12741292
JSONObject json = new JSONObject();

0 commit comments

Comments
 (0)