Skip to content

Commit

Permalink
fix: support DATETIME field that has a space between date and time an…
Browse files Browse the repository at this point in the history
…d has only date (#2176)

* fix: support DATETIME field that has a space between date and time
and has only date.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
agrawal-siddharth and gcf-owl-bot[bot] authored Jun 30, 2023
1 parent 721908d commit 494ce85
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ public class JsonToProtoMessage implements ToProtoConverter<Object> {
.toFormatter()
.withZone(ZoneOffset.UTC);

private static final DateTimeFormatter DATETIME_FORMATTER =
new DateTimeFormatterBuilder()
.parseLenient()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.optionalStart()
.optionalStart()
.parseCaseInsensitive()
.appendLiteral('T')
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.optionalEnd()
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.optionalEnd()
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter();

/** You can use {@link #INSTANCE} instead */
public JsonToProtoMessage() {}

Expand Down Expand Up @@ -402,7 +421,8 @@ private void fillField(
if (val instanceof String) {
protoMsg.setField(
fieldDescriptor,
CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.parse((String) val)));
CivilTimeEncoder.encodePacked64DatetimeMicros(
LocalDateTime.parse((String) val, DATETIME_FORMATTER)));
return;
} else if (val instanceof Long) {
protoMsg.setField(fieldDescriptor, val);
Expand Down Expand Up @@ -663,7 +683,8 @@ private void fillRepeatedField(
if (val instanceof String) {
protoMsg.addRepeatedField(
fieldDescriptor,
CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.parse((String) val)));
CivilTimeEncoder.encodePacked64DatetimeMicros(
LocalDateTime.parse((String) val, DATETIME_FORMATTER)));
} else if (val instanceof Long) {
protoMsg.addRepeatedField(fieldDescriptor, val);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public void testDateTimeMismatch() throws Exception {
TableFieldSchema.newBuilder()
.setName("datetime")
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.REPEATED)
.setMode(TableFieldSchema.Mode.NULLABLE)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
JSONObject json = new JSONObject();
Expand All @@ -623,6 +623,34 @@ public void testDateTimeMismatch() throws Exception {
}
}

private void dateTimeMatch_Internal(String jsonVal, Long expectedVal) throws Exception {
TableFieldSchema field =
TableFieldSchema.newBuilder()
.setName("datetime")
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.NULLABLE)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
TestDatetime expectedProto = TestDatetime.newBuilder().setDatetime(expectedVal).build();
JSONObject json = new JSONObject();
json.put("datetime", jsonVal);
DynamicMessage protoMsg =
JsonToProtoMessage.INSTANCE.convertToProtoMessage(
TestDatetime.getDescriptor(), tableSchema, json);
assertEquals(expectedProto, protoMsg);
}

@Test
public void testDateTimeMatch() throws Exception {
dateTimeMatch_Internal("2021-09-27T20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-09-27t20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-09-27 20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-9-27T20:51:10.752", 142258614586538368L);
dateTimeMatch_Internal("2021-09-27T00:00:00", 142258525253402624L);
dateTimeMatch_Internal("2021-09-27T00:0:00", 142258525253402624L);
dateTimeMatch_Internal("2021-09-27", 142258525253402624L);
}

@Test
public void testTimeMismatch() throws Exception {
TableFieldSchema field =
Expand Down Expand Up @@ -952,6 +980,9 @@ public void testStructComplex() throws Exception {
.setTestDate(1)
.setTestDatetime(1)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258525253402624L)
.addTestDatetimeStr(142258525253402624L)
.setComplexLvl1(
ComplexLvl1.newBuilder()
Expand Down Expand Up @@ -1020,7 +1051,14 @@ public void testStructComplex() throws Exception {
json.put("test_datetime", 1);
json.put(
"test_datetime_str",
new JSONArray(new String[] {"2021-09-27T20:51:10.752", "2021-09-27T00:00:00"}));
new JSONArray(
new String[] {
"2021-09-27T20:51:10.752",
"2021-09-27t20:51:10.752",
"2021-09-27 20:51:10.752",
"2021-09-27T00:00:00",
"2021-09-27"
}));
json.put("complex_lvl1", complex_lvl1);
json.put("complex_lvl2", complex_lvl2);
json.put(
Expand Down

0 comments on commit 494ce85

Please sign in to comment.