Skip to content

Commit abc1cf9

Browse files
authored
Support PROTO, ENUM (#182)
* Support PROTOs * add comment * add testdata * Fix typo * Use non-deprecated package * Change function order * Use Enum() in test * Add TestDecodeColumnProto and TestDecodeColumnProtoArray * Rewrite TestDecodeColumnProto and TestDecodeColumnProtoArray * Some cleanup * Tidy imports * Replace PROTO tests to TestDecodeColumnGCV * Add comments * Fix test case * Remove testdata/protos * Update comments * Reorder test cases * Remove useless use of NullProtoEnum
1 parent 82b9d36 commit abc1cf9

File tree

4 files changed

+100
-61
lines changed

4 files changed

+100
-61
lines changed

decoder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
5959
for _, v := range vs {
6060
decoded = append(decoded, nullBoolToString(v))
6161
}
62-
case sppb.TypeCode_BYTES:
62+
case sppb.TypeCode_BYTES, sppb.TypeCode_PROTO:
6363
var vs [][]byte
6464
if err := column.Decode(&vs); err != nil {
6565
return "", err
@@ -92,7 +92,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
9292
for _, v := range vs {
9393
decoded = append(decoded, nullFloat64ToString(v))
9494
}
95-
case sppb.TypeCode_INT64:
95+
case sppb.TypeCode_INT64, sppb.TypeCode_ENUM:
9696
var vs []spanner.NullInt64
9797
if err := column.Decode(&vs); err != nil {
9898
return "", err
@@ -183,7 +183,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
183183
return "", err
184184
}
185185
return nullBoolToString(v), nil
186-
case sppb.TypeCode_BYTES:
186+
case sppb.TypeCode_BYTES, sppb.TypeCode_PROTO:
187187
var v []byte
188188
if err := column.Decode(&v); err != nil {
189189
return "", err
@@ -201,7 +201,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
201201
return "", err
202202
}
203203
return nullFloat64ToString(v), nil
204-
case sppb.TypeCode_INT64:
204+
case sppb.TypeCode_INT64, sppb.TypeCode_ENUM:
205205
var v spanner.NullInt64
206206
if err := column.Decode(&v); err != nil {
207207
return "", err

decoder_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"testing"
2222
"time"
2323

24+
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
25+
"google.golang.org/protobuf/types/known/structpb"
26+
"google.golang.org/protobuf/types/known/typepb"
27+
2428
"cloud.google.com/go/civil"
2529
"cloud.google.com/go/spanner"
2630
)
@@ -315,6 +319,84 @@ func TestDecodeColumn(t *testing.T) {
315319
value: []spanner.NullJSON(nil),
316320
want: "NULL",
317321
},
322+
323+
// PROTO
324+
// This table tests uses spanner.GenericColumnValue because of non-stability
325+
{
326+
desc: "proto",
327+
value: spanner.GenericColumnValue{
328+
Type: &sppb.Type{
329+
Code: sppb.TypeCode_PROTO,
330+
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
331+
},
332+
Value: structpb.NewStringValue("YWJjZA=="),
333+
},
334+
want: "YWJjZA==",
335+
},
336+
{
337+
desc: "null proto",
338+
value: spanner.GenericColumnValue{
339+
Type: &sppb.Type{
340+
Code: sppb.TypeCode_PROTO,
341+
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
342+
},
343+
Value: structpb.NewNullValue(),
344+
},
345+
want: "NULL",
346+
},
347+
{
348+
desc: "array proto",
349+
value: spanner.GenericColumnValue{
350+
Type: &sppb.Type{
351+
Code: sppb.TypeCode_ARRAY,
352+
ArrayElementType: &sppb.Type{
353+
Code: sppb.TypeCode_PROTO,
354+
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
355+
},
356+
},
357+
Value: structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{
358+
structpb.NewStringValue("YWJjZA=="),
359+
structpb.NewStringValue("ZWZnaA=="),
360+
}}),
361+
},
362+
want: "[YWJjZA==, ZWZnaA==]",
363+
},
364+
{
365+
desc: "null array proto",
366+
value: spanner.GenericColumnValue{
367+
Type: &sppb.Type{
368+
Code: sppb.TypeCode_ARRAY,
369+
ArrayElementType: &sppb.Type{
370+
Code: sppb.TypeCode_PROTO,
371+
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
372+
},
373+
},
374+
Value: structpb.NewNullValue(),
375+
},
376+
want: "NULL",
377+
},
378+
379+
// ENUM
380+
{
381+
desc: "enum",
382+
value: typepb.Syntax_SYNTAX_PROTO3,
383+
want: "1",
384+
},
385+
{
386+
desc: "null enum",
387+
value: (*typepb.Syntax)(nil),
388+
want: "NULL",
389+
},
390+
{
391+
desc: "array enum",
392+
value: []*typepb.Syntax{typepb.Syntax_SYNTAX_PROTO2.Enum(), typepb.Syntax_SYNTAX_PROTO3.Enum()},
393+
want: "[0, 1]",
394+
},
395+
{
396+
desc: "null array enum",
397+
value: []*typepb.Syntax(nil),
398+
want: "NULL",
399+
},
318400
}
319401

320402
for _, test := range tests {

go.mod

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ module github.com/cloudspannerecosystem/spanner-cli
33
go 1.19
44

55
require (
6-
cloud.google.com/go v0.112.2
7-
cloud.google.com/go/spanner v1.59.0
6+
cloud.google.com/go v0.113.0
7+
cloud.google.com/go/spanner v1.62.0
88
github.com/apstndb/gsqlsep v0.0.0-20230324124551-0e8335710080
99
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
1010
github.com/google/go-cmp v0.6.0
1111
github.com/jessevdk/go-flags v1.4.0
1212
github.com/olekukonko/tablewriter v0.0.4
1313
github.com/xlab/treeprint v1.0.1-0.20200715141336-10e0bc383e01
14-
google.golang.org/api v0.173.0
14+
google.golang.org/api v0.180.0
15+
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda
1516
google.golang.org/grpc v1.65.0
1617
google.golang.org/protobuf v1.34.2
1718
)
1819

1920
require (
2021
cel.dev/expr v0.15.0 // indirect
2122
cloud.google.com/go/auth v0.6.1 // indirect
22-
cloud.google.com/go/auth/oauth2adapt v0.1.0 // indirect
23+
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
2324
cloud.google.com/go/compute/metadata v0.4.0 // indirect
24-
cloud.google.com/go/iam v1.1.7 // indirect
25-
cloud.google.com/go/longrunning v0.5.6 // indirect
25+
cloud.google.com/go/iam v1.1.8 // indirect
26+
cloud.google.com/go/longrunning v0.5.7 // indirect
2627
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.0 // indirect
2728
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
2829
github.com/cespare/xxhash/v2 v2.3.0 // indirect
@@ -36,11 +37,8 @@ require (
3637
github.com/golang/protobuf v1.5.4 // indirect
3738
github.com/google/s2a-go v0.1.7 // indirect
3839
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
39-
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
40-
github.com/json-iterator/go v1.1.12 // indirect
40+
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
4141
github.com/mattn/go-runewidth v0.0.8 // indirect
42-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
43-
github.com/modern-go/reflect2 v1.0.2 // indirect
4442
go.opencensus.io v0.24.0 // indirect
4543
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
4644
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
@@ -56,7 +54,6 @@ require (
5654
golang.org/x/text v0.16.0 // indirect
5755
golang.org/x/time v0.5.0 // indirect
5856
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
59-
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
6057
google.golang.org/genproto/googleapis/api v0.0.0-20240708141625-4ad9e859172b // indirect
6158
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
6259
)

0 commit comments

Comments
 (0)