Skip to content

Commit 2214d52

Browse files
authored
misc: Support Proto/Enum types in testproxy (#2671)
Also add schemaBundleId to SchemalessProto/Enum types Change-Id: I8878817ea7f194ce9d51939c1aae314304281b52
1 parent 70c05c9 commit 2214d52

File tree

9 files changed

+170
-76
lines changed

9 files changed

+170
-76
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/common/Type.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,16 +505,19 @@ public int hashCode() {
505505
abstract class SchemalessProto implements SqlType.Proto {
506506

507507
public static SchemalessProto fromProto(com.google.bigtable.v2.Type.Proto proto) {
508-
return create(proto.getMessageName());
508+
return create(proto.getMessageName(), proto.getSchemaBundleId());
509509
}
510510

511-
public static SchemalessProto create(java.lang.String messageName) {
512-
return new AutoValue_Type_SchemalessProto(messageName);
511+
public static SchemalessProto create(
512+
java.lang.String messageName, java.lang.String schemaBundleId) {
513+
return new AutoValue_Type_SchemalessProto(messageName, schemaBundleId);
513514
}
514515

515516
@Override
516517
public abstract java.lang.String getMessageName();
517518

519+
public abstract java.lang.String schemaBundleId();
520+
518521
@Override
519522
public Parser<AbstractMessage> getParserForType() {
520523
throw new UnsupportedOperationException(
@@ -529,7 +532,12 @@ public Code getCode() {
529532

530533
@Override
531534
public java.lang.String toString() {
532-
return getCode().name() + "{messageName=" + getMessageName() + "}";
535+
return getCode().name()
536+
+ "{messageName="
537+
+ getMessageName()
538+
+ ", schemaBundleId="
539+
+ schemaBundleId()
540+
+ "}";
533541
}
534542
}
535543

@@ -544,15 +552,18 @@ public java.lang.String toString() {
544552
abstract class SchemalessEnum implements SqlType.Enum {
545553

546554
public static SchemalessEnum fromProto(com.google.bigtable.v2.Type.Enum proto) {
547-
return create(proto.getEnumName());
555+
return create(proto.getEnumName(), proto.getSchemaBundleId());
548556
}
549557

550-
public static SchemalessEnum create(java.lang.String enumName) {
551-
return new AutoValue_Type_SchemalessEnum(enumName);
558+
public static SchemalessEnum create(
559+
java.lang.String enumName, java.lang.String schemaBundleId) {
560+
return new AutoValue_Type_SchemalessEnum(enumName, schemaBundleId);
552561
}
553562

554563
public abstract java.lang.String getEnumName();
555564

565+
public abstract java.lang.String schemaBundleId();
566+
556567
@Override
557568
public Function<Integer, ProtocolMessageEnum> getForNumber() {
558569
throw new UnsupportedOperationException(
@@ -567,7 +578,12 @@ public Code getCode() {
567578

568579
@Override
569580
public java.lang.String toString() {
570-
return getCode().name() + "{enumName=" + getEnumName() + "}";
581+
return getCode().name()
582+
+ "{enumName="
583+
+ getEnumName()
584+
+ ", schemaBundleId="
585+
+ schemaBundleId()
586+
+ "}";
571587
}
572588
}
573589

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/common/TypeTest.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public void simpleTypes_TypeToString() {
5757
assertThat(Type.Timestamp.create().toString()).isEqualTo("TIMESTAMP");
5858
assertThat(Type.Date.create().toString()).isEqualTo("DATE");
5959
assertThat(Type.SchemalessStruct.create().toString()).isEqualTo("STRUCT");
60-
assertThat(Type.SchemalessProto.create("MyMessage").toString())
61-
.isEqualTo("PROTO{messageName=MyMessage}");
62-
assertThat(Type.SchemalessEnum.create("MyEnum").toString()).isEqualTo("ENUM{enumName=MyEnum}");
60+
assertThat(Type.SchemalessProto.create("MyMessage", "my_bundle").toString())
61+
.isEqualTo("PROTO{messageName=MyMessage, schemaBundleId=my_bundle}");
62+
assertThat(Type.SchemalessEnum.create("MyEnum", "other_bundle").toString())
63+
.isEqualTo("ENUM{enumName=MyEnum, schemaBundleId=other_bundle}");
6364
}
6465

6566
@Test
@@ -123,37 +124,48 @@ public void map_equals() {
123124

124125
@Test
125126
public void proto_equals() {
126-
assertThat(Type.SchemalessProto.create("MyMessage"))
127-
.isEqualTo(Type.SchemalessProto.create("MyMessage"));
127+
assertThat(Type.SchemalessProto.create("MyMessage", "my_bundle"))
128+
.isEqualTo(Type.SchemalessProto.create("MyMessage", "my_bundle"));
128129
assertThat(Type.Proto.create(Singer.getDefaultInstance()))
129130
.isEqualTo(Type.Proto.create(Singer.getDefaultInstance()));
130131

131-
assertThat(Type.SchemalessProto.create("MyMessage"))
132-
.isNotEqualTo(Type.SchemalessProto.create("AnotherMessage"));
132+
assertThat(Type.SchemalessProto.create("MyMessage", "my_bundle"))
133+
.isNotEqualTo(Type.SchemalessProto.create("AnotherMessage", "my_bundle"));
134+
assertThat(Type.SchemalessProto.create("MyMessage", "my_bundle"))
135+
.isNotEqualTo(Type.SchemalessProto.create("MyMessage", "another_bundle"));
133136
assertThat(Type.Proto.create(Singer.getDefaultInstance()))
134137
.isNotEqualTo(Type.Proto.create(Album.getDefaultInstance()));
135138

136-
assertThat(Type.SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer"))
139+
assertThat(
140+
Type.SchemalessProto.create(
141+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"))
137142
.isNotEqualTo(Type.Proto.create(Singer.getDefaultInstance()));
138143
assertThat(Type.Proto.create(Singer.getDefaultInstance()))
139-
.isNotEqualTo(Type.SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer"));
144+
.isNotEqualTo(
145+
Type.SchemalessProto.create(
146+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"));
140147
}
141148

142149
@Test
143150
public void enum_equals() {
144-
assertThat(Type.SchemalessEnum.create("MyEnum"))
145-
.isEqualTo(Type.SchemalessEnum.create("MyEnum"));
151+
assertThat(Type.SchemalessEnum.create("MyEnum", "my_bundle"))
152+
.isEqualTo(Type.SchemalessEnum.create("MyEnum", "my_bundle"));
146153
assertThat(Type.Enum.create(Genre::forNumber)).isEqualTo(Type.Enum.create(Genre::forNumber));
147154

148-
assertThat(Type.SchemalessEnum.create("MyEnum"))
149-
.isNotEqualTo(Type.SchemalessEnum.create("AnotherEnum"));
155+
assertThat(Type.SchemalessEnum.create("MyEnum", "my_bundle"))
156+
.isNotEqualTo(Type.SchemalessEnum.create("AnotherEnum", "my_bundle"));
157+
assertThat(Type.SchemalessEnum.create("MyEnum", "my_bundle"))
158+
.isNotEqualTo(Type.SchemalessEnum.create("MyEnum", "another_bundle"));
150159
assertThat(Type.Enum.create(Genre::forNumber))
151160
.isNotEqualTo(Type.Enum.create(Format::forNumber));
152161

153-
assertThat(Type.SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"))
162+
assertThat(
163+
Type.SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))
154164
.isNotEqualTo(Type.Enum.create(Genre::forNumber));
155165
assertThat(Type.Enum.create(Genre::forNumber))
156-
.isNotEqualTo(Type.SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"));
166+
.isNotEqualTo(
167+
Type.SchemalessEnum.create(
168+
"com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"));
157169
}
158170

159171
@Test
@@ -230,13 +242,13 @@ public void schemalessStruct_throwsExceptionOnSchemaAccess() {
230242

231243
@Test
232244
public void schemalessProto_throwsExceptionOnGetParser() {
233-
SchemalessProto proto = Type.SchemalessProto.create("MyMessage");
245+
SchemalessProto proto = Type.SchemalessProto.create("MyMessage", "my_bundle");
234246
assertThrows(UnsupportedOperationException.class, proto::getParserForType);
235247
}
236248

237249
@Test
238250
public void schemalessEnum_throwsExceptionOnGetForNumber() {
239-
SchemalessEnum myEnum = Type.SchemalessEnum.create("MyEnum");
251+
SchemalessEnum myEnum = Type.SchemalessEnum.create("MyEnum", "my_bundle");
240252
assertThrows(UnsupportedOperationException.class, myEnum::getForNumber);
241253
}
242254

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/AbstractProtoStructReaderTest.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ public void mapField_accessingProto() {
257257
"testField",
258258
mapType(
259259
bytesType(),
260-
protoType("com.google.cloud.bigtable.data.v2.test.Singer"))))),
260+
protoType(
261+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"))))),
261262
Collections.singletonList(
262263
mapValue(mapElement(bytesValue("key"), bytesValue(singer.toByteArray())))));
263264
HashMap<ByteString, Singer> expectedMap = new HashMap<>();
@@ -280,15 +281,17 @@ public void mapField_accessingProto() {
280281
"testField",
281282
SqlType.mapOf(
282283
SqlType.bytes(),
283-
SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer"))));
284+
SchemalessProto.create(
285+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"))));
284286
assertThrows(
285287
UnsupportedOperationException.class,
286288
() ->
287289
structWithMap.getMap(
288290
0,
289291
SqlType.mapOf(
290292
SqlType.bytes(),
291-
SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer"))));
293+
SchemalessProto.create(
294+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"))));
292295
assertThrows(
293296
IllegalStateException.class,
294297
() ->
@@ -319,7 +322,8 @@ public void mapField_accessingEnum() {
319322
"testField",
320323
mapType(
321324
bytesType(),
322-
enumType("com.google.cloud.bigtable.data.v2.test.Genre"))))),
325+
enumType(
326+
"com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))))),
323327
Collections.singletonList(mapValue(mapElement(bytesValue("key"), int64Value(0)))));
324328
HashMap<ByteString, Genre> expectedMap = new HashMap<>();
325329
expectedMap.put(ByteString.copyFromUtf8("key"), Genre.POP);
@@ -340,31 +344,35 @@ public void mapField_accessingEnum() {
340344
"testField",
341345
SqlType.mapOf(
342346
SqlType.bytes(),
343-
SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"))));
347+
SchemalessEnum.create(
348+
"com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))));
344349
assertThrows(
345350
UnsupportedOperationException.class,
346351
() ->
347352
structWithMap.getMap(
348353
0,
349354
SqlType.mapOf(
350355
SqlType.bytes(),
351-
SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"))));
356+
SchemalessEnum.create(
357+
"com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))));
352358
assertThrows(
353359
UnsupportedOperationException.class,
354360
() ->
355361
structWithMap.getMap(
356362
"testField",
357363
SqlType.mapOf(
358364
SqlType.bytes(),
359-
SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"))));
365+
SchemalessEnum.create(
366+
"com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))));
360367
assertThrows(
361368
UnsupportedOperationException.class,
362369
() ->
363370
structWithMap.getMap(
364371
0,
365372
SqlType.mapOf(
366373
SqlType.bytes(),
367-
SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"))));
374+
SchemalessEnum.create(
375+
"com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))));
368376
assertThrows(
369377
IllegalStateException.class,
370378
() -> structWithMap.getMap("testField", SqlType.mapOf(SqlType.bytes(), SqlType.bytes())));
@@ -481,8 +489,8 @@ public static List<Object[]> parameters() {
481489
structField("stringField", stringType()),
482490
structField("intField", int64Type()),
483491
structField("listField", arrayType(stringType())),
484-
structField("protoField", protoType("MyMessage")),
485-
structField("enumField", enumType("MyEnum"))))),
492+
structField("protoField", protoType("MyMessage", "my_bundle")),
493+
structField("enumField", enumType("MyEnum", "other_bundle"))))),
486494
Collections.singletonList(
487495
arrayValue(
488496
stringValue("test"),
@@ -501,8 +509,8 @@ public static List<Object[]> parameters() {
501509
structField("stringField", stringType()),
502510
structField("intField", int64Type()),
503511
structField("listField", arrayType(stringType())),
504-
structField("protoField", protoType("MyMessage")),
505-
structField("enumField", enumType("MyEnum")))),
512+
structField("protoField", protoType("MyMessage", "my_bundle")),
513+
structField("enumField", enumType("MyEnum", "other_bundle")))),
506514
arrayValue(
507515
stringValue("test"),
508516
int64Value(100),
@@ -686,7 +694,8 @@ public static List<Object[]> parameters() {
686694
{
687695
Collections.singletonList(
688696
columnMetadata(
689-
"testField", protoType("com.google.cloud.bigtable.data.v2.test.Singer"))),
697+
"testField",
698+
protoType("com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"))),
690699
Collections.singletonList(
691700
bytesValue(
692701
Singer.newBuilder()
@@ -707,7 +716,9 @@ public static List<Object[]> parameters() {
707716
Collections.singletonList(
708717
columnMetadata(
709718
"testField",
710-
arrayType(protoType("com.google.cloud.bigtable.data.v2.test.Singer")))),
719+
arrayType(
720+
protoType(
721+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle")))),
711722
Collections.singletonList(
712723
arrayValue(
713724
bytesValue(
@@ -743,7 +754,8 @@ public static List<Object[]> parameters() {
743754
"testField",
744755
mapType(
745756
bytesType(),
746-
protoType("com.google.cloud.bigtable.data.v2.test.Singer")))),
757+
protoType(
758+
"com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle")))),
747759
Collections.singletonList(
748760
mapValue(
749761
mapElement(
@@ -791,7 +803,8 @@ public static List<Object[]> parameters() {
791803
{
792804
Collections.singletonList(
793805
columnMetadata(
794-
"testField", enumType("com.google.cloud.bigtable.data.v2.test.Genre"))),
806+
"testField",
807+
enumType("com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle"))),
795808
Collections.singletonList(int64Value(1)),
796809
0,
797810
"testField",
@@ -806,7 +819,8 @@ public static List<Object[]> parameters() {
806819
Collections.singletonList(
807820
columnMetadata(
808821
"testField",
809-
arrayType(enumType("com.google.cloud.bigtable.data.v2.test.Genre")))),
822+
arrayType(
823+
enumType("com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle")))),
810824
Collections.singletonList(arrayValue(nullValue(), int64Value(2), int64Value(100))),
811825
0,
812826
"testField",
@@ -824,7 +838,8 @@ public static List<Object[]> parameters() {
824838
columnMetadata(
825839
"testField",
826840
mapType(
827-
bytesType(), enumType("com.google.cloud.bigtable.data.v2.test.Genre")))),
841+
bytesType(),
842+
enumType("com.google.cloud.bigtable.data.v2.test.Genre", "my_bundle")))),
828843
Collections.singletonList(
829844
mapValue(
830845
mapElement(bytesValue("foo"), int64Value(1)),

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/ProtoStructTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ public class ProtoStructTest {
8585
structField("listField", arrayType(stringType())),
8686
structField("mapField", mapType(stringType(), stringType())),
8787
structField(
88-
"protoField", protoType("com.google.cloud.bigtable.data.v2.test.Singer")),
88+
"protoField",
89+
protoType("com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle")),
8990
structField(
90-
"enumField", enumType("com.google.cloud.bigtable.data.v2.test.Genre")))),
91+
"enumField",
92+
enumType(
93+
"com.google.cloud.bigtable.data.v2.test.Genre", "other_bundle")))),
9194
arrayValue(
9295
bytesValue("testBytes"),
9396
stringValue("testString"),
@@ -184,9 +187,11 @@ public void getColumnType_byName() {
184187
assertThat(struct.getColumnType("mapField"))
185188
.isEqualTo(SqlType.mapOf(SqlType.string(), SqlType.string()));
186189
assertThat(struct.getColumnType("protoField"))
187-
.isEqualTo(SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer"));
190+
.isEqualTo(
191+
SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"));
188192
assertThat(struct.getColumnType("enumField"))
189-
.isEqualTo(SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"));
193+
.isEqualTo(
194+
SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre", "other_bundle"));
190195
}
191196

192197
@Test
@@ -205,9 +210,11 @@ public void getColumnType_byIndex() {
205210
assertThat(struct.getColumnType(10))
206211
.isEqualTo(SqlType.mapOf(SqlType.string(), SqlType.string()));
207212
assertThat(struct.getColumnType(11))
208-
.isEqualTo(SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer"));
213+
.isEqualTo(
214+
SchemalessProto.create("com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle"));
209215
assertThat(struct.getColumnType(12))
210-
.isEqualTo(SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre"));
216+
.isEqualTo(
217+
SchemalessEnum.create("com.google.cloud.bigtable.data.v2.test.Genre", "other_bundle"));
211218
}
212219

213220
@Test

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/ResultSetImplTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ public void testSingleRow() throws ExecutionException, InterruptedException {
103103
columnMetadata("struct", structType(structField("string", stringType()))),
104104
columnMetadata("list", arrayType(stringType())),
105105
columnMetadata("map", mapType(stringType(), stringType())),
106-
columnMetadata("proto", protoType("com.google.cloud.bigtable.data.v2.test.Singer")),
107-
columnMetadata("enum", enumType("com.google.cloud.bigtable.data.v2.test.Genre")));
106+
columnMetadata(
107+
"proto", protoType("com.google.cloud.bigtable.data.v2.test.Singer", "my_bundle")),
108+
columnMetadata(
109+
"enum", enumType("com.google.cloud.bigtable.data.v2.test.Genre", "other_bundle")));
108110
ResultSetMetadata metadata = ProtoResultSetMetadata.fromProto(protoMetadata);
109111
ResultSet resultSet =
110112
resultSetWithFakeStream(

0 commit comments

Comments
 (0)