From ad9f33cdc42894db4e96747704250b1e46586b9a Mon Sep 17 00:00:00 2001 From: Nia Weiss Date: Fri, 9 Oct 2020 12:01:46 -0400 Subject: [PATCH] fix handling of nil in grpc responses --- server/grpc.go | 17 +++++++++++++++++ server/grpc_test.go | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/server/grpc.go b/server/grpc.go index cac1d0853..72e49fe11 100644 --- a/server/grpc.go +++ b/server/grpc.go @@ -191,6 +191,19 @@ func (r ResultBool) ToRows(callback func(*pb.RowResponse) error) error { }}) } +// EmptyResult produces an empty result. +type EmptyResult struct{} + +// ToRows implements the ToRowser interface. +func (r EmptyResult) ToRows(callback func(*pb.RowResponse) error) error { + return nil +} + +// ToTable implements the ToTabler interface. +func (r EmptyResult) ToTable() (*pb.TableResponse, error) { + return &pb.TableResponse{}, nil +} + // Normally we wouldn't need this wrapper, but since pilosa returns // some concrete types for which we can't implement the ToTabler // interface, we have to check for those here and then wrap them @@ -199,6 +212,8 @@ func ToTablerWrapper(result interface{}) (pb.ToTabler, error) { toTabler, ok := result.(pb.ToTabler) if !ok { switch v := result.(type) { + case nil: + toTabler = EmptyResult{} case []pilosa.GroupCount: toTabler = pilosa.GroupCounts(v) case uint64: @@ -220,6 +235,8 @@ func ToRowserWrapper(result interface{}) (pb.ToRowser, error) { toRowser, ok := result.(pb.ToRowser) if !ok { switch v := result.(type) { + case nil: + toRowser = EmptyResult{} case []pilosa.GroupCount: toRowser = pilosa.GroupCounts(v) case uint64: diff --git a/server/grpc_test.go b/server/grpc_test.go index 2009bbfc6..6b2c3dd1e 100644 --- a/server/grpc_test.go +++ b/server/grpc_test.go @@ -272,6 +272,12 @@ func TestGRPC(t *testing.T) { {int64(12)}, }, }, + // nil + { + nil, + nil, + nil, + }, } for ti, test := range tests {