Skip to content

Commit d9b8e62

Browse files
authored
chore: add java.util.UUID as key part (#3839)
1 parent 2ee7f97 commit d9b8e62

File tree

2 files changed

+33
-5
lines changed
  • google-cloud-spanner/src

2 files changed

+33
-5
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Key.java

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.ArrayList;
3232
import java.util.Collections;
3333
import java.util.List;
34+
import java.util.UUID;
3435
import javax.annotation.Nullable;
3536

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

183+
/** Appends a {@code UUID} value to the key */
184+
public Builder append(@Nullable UUID value) {
185+
buffer.add(value);
186+
return this;
187+
}
188+
181189
/**
182190
* Appends an object following the same conversion rules as {@link Key#of(Object...)}. When
183191
* using the {@code Builder}, most code should prefer using the strongly typed {@code
@@ -206,6 +214,8 @@ public Builder appendObject(@Nullable Object value) {
206214
append((Timestamp) value);
207215
} else if (value instanceof Date) {
208216
append((Date) value);
217+
} else if (value instanceof UUID) {
218+
append((UUID) value);
209219
} else if (value instanceof ProtocolMessageEnum) {
210220
append((ProtocolMessageEnum) value);
211221
} else {
@@ -316,6 +326,8 @@ ListValue toProto() {
316326
builder.addValuesBuilder().setStringValue(part.toString());
317327
} else if (part instanceof Date) {
318328
builder.addValuesBuilder().setStringValue(part.toString());
329+
} else if (part instanceof UUID) {
330+
builder.addValuesBuilder().setStringValue(part.toString());
319331
} else if (part instanceof ProtocolMessageEnum) {
320332
builder
321333
.addValuesBuilder()

google-cloud-spanner/src/test/java/com/google/cloud/spanner/KeyTest.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.protobuf.ListValue;
2727
import com.google.protobuf.NullValue;
2828
import java.math.BigDecimal;
29+
import java.util.UUID;
2930
import org.junit.Test;
3031
import org.junit.runner.RunWith;
3132
import org.junit.runners.JUnit4;
@@ -51,6 +52,7 @@ public void of() {
5152
String numeric = "3.141592";
5253
String timestamp = "2015-09-15T00:00:00Z";
5354
String date = "2015-09-15";
55+
String uuid = UUID.randomUUID().toString();
5456
String json = "{\"color\":\"red\",\"value\":\"#f00\"}";
5557
k =
5658
Key.of(
@@ -65,8 +67,9 @@ public void of() {
6567
json,
6668
ByteArray.copyFrom("y"),
6769
Timestamp.parseTimestamp(timestamp),
68-
Date.parseDate(date));
69-
assertThat(k.size()).isEqualTo(12);
70+
Date.parseDate(date),
71+
UUID.fromString(uuid));
72+
assertThat(k.size()).isEqualTo(13);
7073
assertThat(k.getParts())
7174
.containsExactly(
7275
null,
@@ -80,7 +83,8 @@ public void of() {
8083
json,
8184
ByteArray.copyFrom("y"),
8285
Timestamp.parseTimestamp(timestamp),
83-
Date.parseDate(date))
86+
Date.parseDate(date),
87+
UUID.fromString(uuid))
8488
.inOrder();
8589

8690
// Singleton null key.
@@ -94,6 +98,7 @@ public void builder() {
9498
String numeric = "3.141592";
9599
String timestamp = "2015-09-15T00:00:00Z";
96100
String date = "2015-09-15";
101+
String uuid = UUID.randomUUID().toString();
97102
String json = "{\"color\":\"red\",\"value\":\"#f00\"}";
98103
Key k =
99104
Key.newBuilder()
@@ -109,8 +114,9 @@ public void builder() {
109114
.append(ByteArray.copyFrom("y"))
110115
.append(Timestamp.parseTimestamp(timestamp))
111116
.append(Date.parseDate(date))
117+
.append(UUID.fromString(uuid))
112118
.build();
113-
assertThat(k.size()).isEqualTo(12);
119+
assertThat(k.size()).isEqualTo(13);
114120
assertThat(k.getParts())
115121
.containsExactly(
116122
null,
@@ -124,7 +130,8 @@ public void builder() {
124130
json,
125131
ByteArray.copyFrom("y"),
126132
Timestamp.parseTimestamp(timestamp),
127-
Date.parseDate(date))
133+
Date.parseDate(date),
134+
UUID.fromString(uuid))
128135
.inOrder();
129136
}
130137

@@ -153,6 +160,8 @@ public void testToString() {
153160
.isEqualTo("[" + timestamp + "]");
154161
String date = "2015-09-15";
155162
assertThat(Key.of(Date.parseDate(date)).toString()).isEqualTo("[" + date + "]");
163+
String uuid = UUID.randomUUID().toString();
164+
assertThat(Key.of(UUID.fromString(uuid)).toString()).isEqualTo("[" + uuid + "]");
156165
assertThat(Key.of(1, 2, 3).toString()).isEqualTo("[1,2,3]");
157166
}
158167

@@ -173,6 +182,7 @@ public void equalsAndHashCode() {
173182
Key.newBuilder().append((ByteArray) null).build(),
174183
Key.newBuilder().append((Timestamp) null).build(),
175184
Key.newBuilder().append((Date) null).build(),
185+
Key.newBuilder().append((UUID) null).build(),
176186
Key.newBuilder().appendObject(null).build());
177187

178188
tester.addEqualityGroup(Key.of(true), Key.newBuilder().append(true).build());
@@ -197,6 +207,8 @@ public void equalsAndHashCode() {
197207
tester.addEqualityGroup(Key.of(t), Key.newBuilder().append(t).build());
198208
Date d = Date.parseDate("2016-09-15");
199209
tester.addEqualityGroup(Key.of(d), Key.newBuilder().append(d).build());
210+
UUID uuid = UUID.randomUUID();
211+
tester.addEqualityGroup(Key.of(uuid), Key.newBuilder().append(uuid).build());
200212
tester.addEqualityGroup(Key.of("a", 2, null));
201213

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

221234
@Test
222235
public void toProto() {
223236
String timestamp = "2015-09-15T00:00:00Z";
224237
String date = "2015-09-15";
238+
String uuid = UUID.randomUUID().toString();
225239
Key k =
226240
Key.newBuilder()
227241
.append((Boolean) null)
@@ -236,6 +250,7 @@ public void toProto() {
236250
.append(ByteArray.copyFrom("y"))
237251
.append(Timestamp.parseTimestamp(timestamp))
238252
.append(Date.parseDate(date))
253+
.append(UUID.fromString(uuid))
239254
.build();
240255
ListValue.Builder builder = ListValue.newBuilder();
241256
builder.addValuesBuilder().setNullValue(NullValue.NULL_VALUE);
@@ -250,6 +265,7 @@ public void toProto() {
250265
builder.addValuesBuilder().setStringValue("eQ==");
251266
builder.addValuesBuilder().setStringValue(timestamp);
252267
builder.addValuesBuilder().setStringValue(date);
268+
builder.addValuesBuilder().setStringValue(uuid);
253269
assertThat(k.toProto()).isEqualTo(builder.build());
254270
}
255271
}

0 commit comments

Comments
 (0)