Skip to content

Commit

Permalink
Support PROTO, ENUM (#182)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
apstndb authored Sep 3, 2024
1 parent 82b9d36 commit abc1cf9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 61 deletions.
8 changes: 4 additions & 4 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
for _, v := range vs {
decoded = append(decoded, nullBoolToString(v))
}
case sppb.TypeCode_BYTES:
case sppb.TypeCode_BYTES, sppb.TypeCode_PROTO:
var vs [][]byte
if err := column.Decode(&vs); err != nil {
return "", err
Expand Down Expand Up @@ -92,7 +92,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
for _, v := range vs {
decoded = append(decoded, nullFloat64ToString(v))
}
case sppb.TypeCode_INT64:
case sppb.TypeCode_INT64, sppb.TypeCode_ENUM:
var vs []spanner.NullInt64
if err := column.Decode(&vs); err != nil {
return "", err
Expand Down Expand Up @@ -183,7 +183,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
return "", err
}
return nullBoolToString(v), nil
case sppb.TypeCode_BYTES:
case sppb.TypeCode_BYTES, sppb.TypeCode_PROTO:
var v []byte
if err := column.Decode(&v); err != nil {
return "", err
Expand All @@ -201,7 +201,7 @@ func DecodeColumn(column spanner.GenericColumnValue) (string, error) {
return "", err
}
return nullFloat64ToString(v), nil
case sppb.TypeCode_INT64:
case sppb.TypeCode_INT64, sppb.TypeCode_ENUM:
var v spanner.NullInt64
if err := column.Decode(&v); err != nil {
return "", err
Expand Down
82 changes: 82 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"testing"
"time"

sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/typepb"

"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
)
Expand Down Expand Up @@ -315,6 +319,84 @@ func TestDecodeColumn(t *testing.T) {
value: []spanner.NullJSON(nil),
want: "NULL",
},

// PROTO
// This table tests uses spanner.GenericColumnValue because of non-stability
{
desc: "proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
Value: structpb.NewStringValue("YWJjZA=="),
},
want: "YWJjZA==",
},
{
desc: "null proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
Value: structpb.NewNullValue(),
},
want: "NULL",
},
{
desc: "array proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_ARRAY,
ArrayElementType: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
},
Value: structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{
structpb.NewStringValue("YWJjZA=="),
structpb.NewStringValue("ZWZnaA=="),
}}),
},
want: "[YWJjZA==, ZWZnaA==]",
},
{
desc: "null array proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_ARRAY,
ArrayElementType: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
},
Value: structpb.NewNullValue(),
},
want: "NULL",
},

// ENUM
{
desc: "enum",
value: typepb.Syntax_SYNTAX_PROTO3,
want: "1",
},
{
desc: "null enum",
value: (*typepb.Syntax)(nil),
want: "NULL",
},
{
desc: "array enum",
value: []*typepb.Syntax{typepb.Syntax_SYNTAX_PROTO2.Enum(), typepb.Syntax_SYNTAX_PROTO3.Enum()},
want: "[0, 1]",
},
{
desc: "null array enum",
value: []*typepb.Syntax(nil),
want: "NULL",
},
}

for _, test := range tests {
Expand Down
19 changes: 8 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ module github.com/cloudspannerecosystem/spanner-cli
go 1.19

require (
cloud.google.com/go v0.112.2
cloud.google.com/go/spanner v1.59.0
cloud.google.com/go v0.113.0
cloud.google.com/go/spanner v1.62.0
github.com/apstndb/gsqlsep v0.0.0-20230324124551-0e8335710080
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/google/go-cmp v0.6.0
github.com/jessevdk/go-flags v1.4.0
github.com/olekukonko/tablewriter v0.0.4
github.com/xlab/treeprint v1.0.1-0.20200715141336-10e0bc383e01
google.golang.org/api v0.173.0
google.golang.org/api v0.180.0
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
cel.dev/expr v0.15.0 // indirect
cloud.google.com/go/auth v0.6.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.1.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
cloud.google.com/go/compute/metadata v0.4.0 // indirect
cloud.google.com/go/iam v1.1.7 // indirect
cloud.google.com/go/longrunning v0.5.6 // indirect
cloud.google.com/go/iam v1.1.8 // indirect
cloud.google.com/go/longrunning v0.5.7 // indirect
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.0 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand All @@ -36,11 +37,8 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
Expand All @@ -56,7 +54,6 @@ require (
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240708141625-4ad9e859172b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
)
Loading

0 comments on commit abc1cf9

Please sign in to comment.