Skip to content

Commit

Permalink
feat: Add short, integer, long, boolean conversions into string (#2437)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Mar 15, 2024
1 parent fe9c3ae commit 4f4216e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ private void fillField(
if (val instanceof String) {
protoMsg.setField(fieldDescriptor, val);
return;
} else if (val instanceof Short
|| val instanceof Integer
|| val instanceof Long
|| val instanceof Boolean) {
protoMsg.setField(fieldDescriptor, String.valueOf(val));
return;
}
break;
case DOUBLE:
Expand Down Expand Up @@ -910,6 +916,12 @@ private void fillRepeatedField(
case STRING:
if (val instanceof String) {
protoMsg.addRepeatedField(fieldDescriptor, val);
} else if (val instanceof Short
|| val instanceof Integer
|| val instanceof Long
|| val instanceof Boolean) {
protoMsg.addRepeatedField(fieldDescriptor, String.valueOf(val));
return;
} else {
throwWrongFieldType(fieldDescriptor, currentScope, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.cloud.bigquery.storage.v1.ConnectionWorkerPool.Settings;
import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializationError;
import com.google.cloud.bigquery.storage.v1.TableFieldSchema.Mode;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors.DescriptorValidationException;
import com.google.protobuf.Int64Value;
Expand Down Expand Up @@ -1415,8 +1416,8 @@ public void testMultipleAppendSerializationErrors()
// put a vaild value into the field
foo1.put("foo", "allen");
JSONObject foo2 = new JSONObject();
// put a number into a string field
foo2.put("foo", 666);
// put a field which is not part of the expected schema
foo2.put("not_bar", "woody");
JSONArray jsonArr = new JSONArray();
jsonArr.put(foo);
jsonArr.put(foo1);
Expand All @@ -1434,14 +1435,11 @@ public void testMultipleAppendSerializationErrors()
} catch (AppendSerializationError appendSerializationError) {
Map<Integer, String> rowIndexToErrorMessage =
appendSerializationError.getRowIndexToErrorMessage();
assertEquals(2, rowIndexToErrorMessage.size());
assertEquals(
"The source object has fields unknown to BigQuery: root.not_foo.",
rowIndexToErrorMessage.get(0));
assertEquals(
"Field root.foo failed to convert to STRING. Error: JSONObject does not have a string"
+ " field at root.foo.",
rowIndexToErrorMessage.get(2));
ImmutableMap.of(
0, "The source object has fields unknown to BigQuery: root.not_foo.",
2, "The source object has fields unknown to BigQuery: root.not_bar."),
rowIndexToErrorMessage);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public class JsonToProtoMessageTest {
})
.put(
StringType.getDescriptor(),
new Message[] {StringType.newBuilder().setTestFieldType("test").build()})
new Message[] {
StringType.newBuilder().setTestFieldType("9223372036854775807").build(),
StringType.newBuilder().setTestFieldType("2147483647").build(),
StringType.newBuilder().setTestFieldType("true").build(),
StringType.newBuilder().setTestFieldType("test").build()
})
.put(
RepeatedType.getDescriptor(),
new Message[] {
Expand Down Expand Up @@ -147,6 +152,9 @@ public class JsonToProtoMessageTest {
.put(
RepeatedString.getDescriptor(),
new Message[] {
RepeatedString.newBuilder().addTestRepeated("9223372036854775807").build(),
RepeatedString.newBuilder().addTestRepeated("2147483647").build(),
RepeatedString.newBuilder().addTestRepeated("true").build(),
RepeatedString.newBuilder().addTestRepeated("hello").addTestRepeated("test").build()
})
.put(
Expand Down Expand Up @@ -925,6 +933,8 @@ public void testAllTypes() throws Exception {
} else if (entry.getKey() == Int64Type.getDescriptor()
|| entry.getKey() == BytesType.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 2, success);
} else if (entry.getKey() == StringType.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 4, success);
} else {
assertEquals(entry.getKey().getFullName(), 1, success);
}
Expand Down Expand Up @@ -962,6 +972,8 @@ public void testAllRepeatedTypesWithLimits() throws Exception {
assertEquals(entry.getKey().getFullName(), 4, success);
} else if (entry.getKey() == RepeatedInt64.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 2, success);
} else if (entry.getKey() == RepeatedString.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 4, success);
} else {
assertEquals(entry.getKey().getFullName(), 1, success);
}
Expand Down Expand Up @@ -1009,14 +1021,20 @@ public void testRequired() throws Exception {

@Test
public void testStructSimple() throws Exception {
structSimple("test", "test");
structSimple(true, "true");
structSimple(1, "1");
structSimple((short) 1, "1");
structSimple((long) 1, "1");
}

private void structSimple(Object value, String expected) throws Exception {
MessageType expectedProto =
MessageType.newBuilder()
.setTestFieldType(StringType.newBuilder().setTestFieldType("test").build())
.setTestFieldType(StringType.newBuilder().setTestFieldType(expected).build())
.build();
JSONObject stringType = new JSONObject();
stringType.put("test_field_type", "test");
JSONObject json = new JSONObject();
json.put("test_field_type", stringType);
JSONObject stringType = new JSONObject(ImmutableMap.of("test_field_type", value));
JSONObject json = new JSONObject(ImmutableMap.of("test_field_type", stringType));

DynamicMessage protoMsg =
JsonToProtoMessage.INSTANCE.convertToProtoMessage(MessageType.getDescriptor(), json);
Expand All @@ -1026,7 +1044,7 @@ public void testStructSimple() throws Exception {
@Test
public void testStructSimpleFail() throws Exception {
JSONObject stringType = new JSONObject();
stringType.put("test_field_type", 1);
stringType.put("test_field_type", new boolean[0]);
JSONObject json = new JSONObject();
json.put("test_field_type", stringType);
try {
Expand Down Expand Up @@ -1268,7 +1286,7 @@ public void testNestedRepeatedComplex() throws Exception {
@Test
public void testNestedRepeatedComplexFail() throws Exception {
double[] doubleArr = {1.1, 2.2, 3.3, 4.4, 5.5};
Boolean[] fakeStringArr = {true, false};
Boolean[][] fakeStringArr = {new Boolean[0], new Boolean[0]};
int[] intArr = {1, 2, 3, 4, 5};

JSONObject json = new JSONObject();
Expand Down

0 comments on commit 4f4216e

Please sign in to comment.