diff --git a/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Key.java b/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Key.java index 183092f48b42..bbe07f54f957 100644 --- a/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Key.java +++ b/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Key.java @@ -281,7 +281,9 @@ ListValue toProto() { } else if (part instanceof ByteArray) { builder.addValuesBuilder().setStringValue(((ByteArray) part).toBase64()); } else if (part instanceof Timestamp) { - builder.addValuesBuilder().setStringValue(((Timestamp) part).toString()); + builder.addValuesBuilder().setStringValue(part.toString()); + } else if (part instanceof Date) { + builder.addValuesBuilder().setStringValue(part.toString()); } else { throw new AssertionError("Illegal key part: " + part.getClass()); } diff --git a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/KeyTest.java b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/KeyTest.java index 78012912fefa..81f3957c286a 100644 --- a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/KeyTest.java +++ b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/KeyTest.java @@ -23,6 +23,8 @@ import com.google.cloud.Date; import com.google.cloud.Timestamp; import com.google.common.testing.EqualsTester; +import com.google.protobuf.ListValue; +import com.google.protobuf.NullValue; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -189,4 +191,35 @@ public void serialization() throws Exception { reserializeAndAssert(Key.of(Date.parseDate("2015-09-15"))); reserializeAndAssert(Key.of(1, 2, 3)); } + + @Test + public void toProto() { + String timestamp = "2015-09-15T00:00:00Z"; + String date = "2015-09-15"; + Key k = + Key.newBuilder() + .append((Boolean) null) + .append(true) + .append(32) + .append(64L) + .append(2.0f) + .append(4.0d) + .append("x") + .append(ByteArray.copyFrom("y")) + .append(Timestamp.parseTimestamp(timestamp)) + .append(Date.parseDate(date)) + .build(); + ListValue.Builder builder = ListValue.newBuilder(); + builder.addValuesBuilder().setNullValue(NullValue.NULL_VALUE); + builder.addValuesBuilder().setBoolValue(true); + builder.addValuesBuilder().setStringValue("32"); + builder.addValuesBuilder().setStringValue("64"); + builder.addValuesBuilder().setNumberValue(2.0f); + builder.addValuesBuilder().setNumberValue(4.0d); + builder.addValuesBuilder().setStringValue("x"); + builder.addValuesBuilder().setStringValue("eQ=="); + builder.addValuesBuilder().setStringValue(timestamp); + builder.addValuesBuilder().setStringValue(date); + assertThat(k.toProto()).isEqualTo(builder.build()); + } }