Skip to content

Commit

Permalink
Fix #4466 support date data type in toproto of key (#4473)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaaym authored and sduskis committed Feb 8, 2019
1 parent 64f3b2b commit d6b2c21
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit d6b2c21

Please sign in to comment.