2828import static com .google .cloud .spanner .SpannerExceptionFactory .newSpannerException ;
2929import static com .google .common .base .Preconditions .checkArgument ;
3030
31+ /**
32+ * A gRPC/proto-based struct.
33+ */
3134class GrpcStruct extends Struct {
3235 private final Type type ;
3336 private final List <Object > rowData ;
@@ -37,23 +40,7 @@ class GrpcStruct extends Struct {
3740 this .rowData = rowData ;
3841 }
3942
40- static com .google .cloud .spanner .Value valueFromTypedProto (com .google .protobuf .Value proto ) {
41- if (proto .getKindCase () != Value .KindCase .LIST_VALUE && proto .getListValue ().getValuesCount ()
42- != 2 ) {
43- throw new AssertionError ("Expected a list of two elements: type and value" );
44- }
45- Type type ;
46- try {
47- type = Type .fromProto (
48- com .google .spanner .v1 .Type .parseFrom (
49- proto .getListValue ().getValues (0 ).getStringValueBytes ()));
50- } catch (InvalidProtocolBufferException e ) {
51- throw new AssertionError ("Expected serialized type." );
52- }
53- return asSpannerValue (type , proto .getListValue ().getValues (1 ));
54- }
55-
56- static Object fromTypedProto (com .google .protobuf .Value proto ) {
43+ static com .google .cloud .spanner .Value valueFromTypedProto (Value proto ) {
5744 if (proto .getKindCase () != Value .KindCase .LIST_VALUE && proto .getListValue ().getValuesCount ()
5845 != 2 ) {
5946 throw new AssertionError ("Expected a list of two elements: type and value" );
@@ -66,13 +53,8 @@ static Object fromTypedProto(com.google.protobuf.Value proto) {
6653 } catch (InvalidProtocolBufferException e ) {
6754 throw new AssertionError ("Expected serialized type." );
6855 }
69- return decodeValue (type , proto .getListValue ().getValues (1 ));
70- }
71-
72- static com .google .cloud .spanner .Value asSpannerValue (
73- Type fieldType , com .google .protobuf .Value proto ) {
74- Object value = decodeValue (fieldType , proto );
75- switch (fieldType .getCode ()) {
56+ Object value = decodeValue (type , proto .getListValue ().getValues (1 ));
57+ switch (type .getCode ()) {
7658 case BOOL :
7759 return com .google .cloud .spanner .Value .bool ((Boolean ) value );
7860 case INT64 :
@@ -88,9 +70,9 @@ static com.google.cloud.spanner.Value asSpannerValue(
8870 (com .google .cloud .Timestamp ) value );
8971 case DATE :
9072 return com .google .cloud .spanner .Value .date (
91- (com . google . cloud . Date ) value );
73+ (Date ) value );
9274 case ARRAY :
93- Type elementType = fieldType .getArrayElementType ();
75+ Type elementType = type .getArrayElementType ();
9476 switch (elementType .getCode ()) {
9577 case BOOL :
9678 return com .google .cloud .spanner .Value .boolArray ((Iterable <Boolean >) value );
@@ -107,7 +89,7 @@ static com.google.cloud.spanner.Value asSpannerValue(
10789 (Iterable <com .google .cloud .Timestamp >) value );
10890 case DATE :
10991 return com .google .cloud .spanner .Value .dateArray (
110- (Iterable <com . google . cloud . Date >) value );
92+ (Iterable <Date >) value );
11193
11294 case STRUCT :
11395 List <Type .StructField > fields = elementType .getStructFields ();
@@ -119,11 +101,27 @@ static com.google.cloud.spanner.Value asSpannerValue(
119101 }
120102 case STRUCT : // Not a legal top-level field type.
121103 default :
122- throw new AssertionError ("Unhandled type code: " + fieldType .getCode ());
104+ throw new AssertionError ("Unhandled type code: " + type .getCode ());
123105 }
124106 }
125107
126- static Object decodeValue (Type fieldType , com .google .protobuf .Value proto ) {
108+ static Object fromTypedProto (Value proto ) {
109+ if (proto .getKindCase () != Value .KindCase .LIST_VALUE && proto .getListValue ().getValuesCount ()
110+ != 2 ) {
111+ throw new AssertionError ("Expected a list of two elements: type and value" );
112+ }
113+ Type type ;
114+ try {
115+ type = Type .fromProto (
116+ com .google .spanner .v1 .Type .parseFrom (
117+ proto .getListValue ().getValues (0 ).getStringValueBytes ()));
118+ } catch (InvalidProtocolBufferException e ) {
119+ throw new AssertionError ("Expected serialized type." );
120+ }
121+ return decodeValue (type , proto .getListValue ().getValues (1 ));
122+ }
123+
124+ private static Object decodeValue (Type fieldType , com .google .protobuf .Value proto ) {
127125 if (proto .getKindCase () == Value .KindCase .NULL_VALUE ) {
128126 return null ;
129127 }
0 commit comments