Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -70,6 +71,7 @@ private Key(List<Object> parts) {
* <li>{@link ByteArray} for the {@code BYTES} Cloud Spanner type
* <li>{@link Timestamp} for the {@code TIMESTAMP} Cloud Spanner type
* <li>{@link Date} for the {@code DATE} Cloud Spanner type
* <li>{@link java.util.UUID} for the {@code UUID} Cloud Spanner type
* </ul>
*
* @throws IllegalArgumentException if any member of {@code values} is not a supported type
Expand Down Expand Up @@ -178,6 +180,12 @@ public Builder append(@Nullable Date value) {
return this;
}

/** Appends a {@code UUID} value to the key */
public Builder append(@Nullable UUID value) {
buffer.add(value);
return this;
}

/**
* Appends an object following the same conversion rules as {@link Key#of(Object...)}. When
* using the {@code Builder}, most code should prefer using the strongly typed {@code
Expand Down Expand Up @@ -206,6 +214,8 @@ public Builder appendObject(@Nullable Object value) {
append((Timestamp) value);
} else if (value instanceof Date) {
append((Date) value);
} else if (value instanceof UUID) {
append((UUID) value);
} else if (value instanceof ProtocolMessageEnum) {
append((ProtocolMessageEnum) value);
} else {
Expand Down Expand Up @@ -316,6 +326,8 @@ ListValue toProto() {
builder.addValuesBuilder().setStringValue(part.toString());
} else if (part instanceof Date) {
builder.addValuesBuilder().setStringValue(part.toString());
} else if (part instanceof UUID) {
builder.addValuesBuilder().setStringValue(part.toString());
} else if (part instanceof ProtocolMessageEnum) {
builder
.addValuesBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import java.math.BigDecimal;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -51,6 +52,7 @@ public void of() {
String numeric = "3.141592";
String timestamp = "2015-09-15T00:00:00Z";
String date = "2015-09-15";
String uuid = UUID.randomUUID().toString();
String json = "{\"color\":\"red\",\"value\":\"#f00\"}";
k =
Key.of(
Expand All @@ -65,8 +67,9 @@ public void of() {
json,
ByteArray.copyFrom("y"),
Timestamp.parseTimestamp(timestamp),
Date.parseDate(date));
assertThat(k.size()).isEqualTo(12);
Date.parseDate(date),
UUID.fromString(uuid));
assertThat(k.size()).isEqualTo(13);
assertThat(k.getParts())
.containsExactly(
null,
Expand All @@ -80,7 +83,8 @@ public void of() {
json,
ByteArray.copyFrom("y"),
Timestamp.parseTimestamp(timestamp),
Date.parseDate(date))
Date.parseDate(date),
UUID.fromString(uuid))
.inOrder();

// Singleton null key.
Expand All @@ -94,6 +98,7 @@ public void builder() {
String numeric = "3.141592";
String timestamp = "2015-09-15T00:00:00Z";
String date = "2015-09-15";
String uuid = UUID.randomUUID().toString();
String json = "{\"color\":\"red\",\"value\":\"#f00\"}";
Key k =
Key.newBuilder()
Expand All @@ -109,8 +114,9 @@ public void builder() {
.append(ByteArray.copyFrom("y"))
.append(Timestamp.parseTimestamp(timestamp))
.append(Date.parseDate(date))
.append(UUID.fromString(uuid))
.build();
assertThat(k.size()).isEqualTo(12);
assertThat(k.size()).isEqualTo(13);
assertThat(k.getParts())
.containsExactly(
null,
Expand All @@ -124,7 +130,8 @@ public void builder() {
json,
ByteArray.copyFrom("y"),
Timestamp.parseTimestamp(timestamp),
Date.parseDate(date))
Date.parseDate(date),
UUID.fromString(uuid))
.inOrder();
}

Expand Down Expand Up @@ -153,6 +160,8 @@ public void testToString() {
.isEqualTo("[" + timestamp + "]");
String date = "2015-09-15";
assertThat(Key.of(Date.parseDate(date)).toString()).isEqualTo("[" + date + "]");
String uuid = UUID.randomUUID().toString();
assertThat(Key.of(UUID.fromString(uuid)).toString()).isEqualTo("[" + uuid + "]");
assertThat(Key.of(1, 2, 3).toString()).isEqualTo("[1,2,3]");
}

Expand All @@ -173,6 +182,7 @@ public void equalsAndHashCode() {
Key.newBuilder().append((ByteArray) null).build(),
Key.newBuilder().append((Timestamp) null).build(),
Key.newBuilder().append((Date) null).build(),
Key.newBuilder().append((UUID) null).build(),
Key.newBuilder().appendObject(null).build());

tester.addEqualityGroup(Key.of(true), Key.newBuilder().append(true).build());
Expand All @@ -197,6 +207,8 @@ public void equalsAndHashCode() {
tester.addEqualityGroup(Key.of(t), Key.newBuilder().append(t).build());
Date d = Date.parseDate("2016-09-15");
tester.addEqualityGroup(Key.of(d), Key.newBuilder().append(d).build());
UUID uuid = UUID.randomUUID();
tester.addEqualityGroup(Key.of(uuid), Key.newBuilder().append(uuid).build());
tester.addEqualityGroup(Key.of("a", 2, null));

tester.testEquals();
Expand All @@ -215,13 +227,15 @@ public void serialization() {
reserializeAndAssert(Key.of(ByteArray.copyFrom("xyz")));
reserializeAndAssert(Key.of(Timestamp.parseTimestamp("2015-09-15T00:00:00Z")));
reserializeAndAssert(Key.of(Date.parseDate("2015-09-15")));
reserializeAndAssert(Key.of(UUID.randomUUID()));
reserializeAndAssert(Key.of(1, 2, 3));
}

@Test
public void toProto() {
String timestamp = "2015-09-15T00:00:00Z";
String date = "2015-09-15";
String uuid = UUID.randomUUID().toString();
Key k =
Key.newBuilder()
.append((Boolean) null)
Expand All @@ -236,6 +250,7 @@ public void toProto() {
.append(ByteArray.copyFrom("y"))
.append(Timestamp.parseTimestamp(timestamp))
.append(Date.parseDate(date))
.append(UUID.fromString(uuid))
.build();
ListValue.Builder builder = ListValue.newBuilder();
builder.addValuesBuilder().setNullValue(NullValue.NULL_VALUE);
Expand All @@ -250,6 +265,7 @@ public void toProto() {
builder.addValuesBuilder().setStringValue("eQ==");
builder.addValuesBuilder().setStringValue(timestamp);
builder.addValuesBuilder().setStringValue(date);
builder.addValuesBuilder().setStringValue(uuid);
assertThat(k.toProto()).isEqualTo(builder.build());
}
}
Loading