From 653249fcdfadf3d187fee3cd056ecfcc3df2f3c6 Mon Sep 17 00:00:00 2001 From: Hoang Pham Date: Mon, 3 Jun 2024 14:39:00 -0400 Subject: [PATCH 1/3] Add string type support to bigtable type. --- bigtable/type.go | 29 +++++++++++++++++++++++++++++ bigtable/type_test.go | 17 +++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/bigtable/type.go b/bigtable/type.go index a390e8fd44b2..b8b9f8df05b8 100644 --- a/bigtable/type.go +++ b/bigtable/type.go @@ -54,6 +54,35 @@ func (bytes BytesType) proto() *btapb.Type { return &btapb.Type{Kind: &btapb.Type_BytesType{BytesType: &btapb.Type_Bytes{Encoding: encoding}}} } +// StringEncoding represents the encoding of a String. +type StringEncoding interface { + proto() *btapb.Type_String_Encoding +} + +// StringUtf8Encoding represents a string with UTF-8 encoding. +type StringUtf8Encoding struct { +} + +func (encoding StringUtf8Encoding) proto() *btapb.Type_String_Encoding { + return &btapb.Type_String_Encoding{ + Encoding: &btapb.Type_String_Encoding_Utf8Raw_{}, + } +} + +type StringType struct { + Encoding StringEncoding +} + +func (str StringType) proto() *btapb.Type { + var encoding *btapb.Type_String_Encoding + if str.Encoding != nil { + encoding = str.Encoding.proto() + } else { + encoding = StringUtf8Encoding{}.proto() + } + return &btapb.Type{Kind: &btapb.Type_StringType{StringType: &btapb.Type_String{Encoding: encoding}}} +} + // Int64Encoding represents the encoding of an Int64 type. type Int64Encoding interface { proto() *btapb.Type_Int64_Encoding diff --git a/bigtable/type_test.go b/bigtable/type_test.go index 4f688384ad3c..cafa7db18d91 100644 --- a/bigtable/type_test.go +++ b/bigtable/type_test.go @@ -50,6 +50,23 @@ func TestInt64Proto(t *testing.T) { } } +func TestStringProto(t *testing.T) { + want := &btapb.Type{ + Kind: &btapb.Type_StringType{ + StringType: &btapb.Type_String{ + Encoding: &btapb.Type_String_Encoding{ + Encoding: &btapb.Type_String_Encoding_Utf8Raw_{}, + }, + }, + }, + } + + got := StringType{}.proto() + if !proto.Equal(got, want) { + t.Errorf("got type %v, want: %v", got, want) + } +} + func TestAggregateProto(t *testing.T) { want := &btapb.Type{ Kind: &btapb.Type_AggregateType{ From c3e8b224aabf2cd88f28dffa486a440e7b2819a0 Mon Sep 17 00:00:00 2001 From: Hoang Pham Date: Mon, 3 Jun 2024 14:39:00 -0400 Subject: [PATCH 2/3] feat(bigtable): add string type to supported Bigtable type --- bigtable/type.go | 29 +++++++++++++++++++++++++++++ bigtable/type_test.go | 17 +++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/bigtable/type.go b/bigtable/type.go index a390e8fd44b2..b8b9f8df05b8 100644 --- a/bigtable/type.go +++ b/bigtable/type.go @@ -54,6 +54,35 @@ func (bytes BytesType) proto() *btapb.Type { return &btapb.Type{Kind: &btapb.Type_BytesType{BytesType: &btapb.Type_Bytes{Encoding: encoding}}} } +// StringEncoding represents the encoding of a String. +type StringEncoding interface { + proto() *btapb.Type_String_Encoding +} + +// StringUtf8Encoding represents a string with UTF-8 encoding. +type StringUtf8Encoding struct { +} + +func (encoding StringUtf8Encoding) proto() *btapb.Type_String_Encoding { + return &btapb.Type_String_Encoding{ + Encoding: &btapb.Type_String_Encoding_Utf8Raw_{}, + } +} + +type StringType struct { + Encoding StringEncoding +} + +func (str StringType) proto() *btapb.Type { + var encoding *btapb.Type_String_Encoding + if str.Encoding != nil { + encoding = str.Encoding.proto() + } else { + encoding = StringUtf8Encoding{}.proto() + } + return &btapb.Type{Kind: &btapb.Type_StringType{StringType: &btapb.Type_String{Encoding: encoding}}} +} + // Int64Encoding represents the encoding of an Int64 type. type Int64Encoding interface { proto() *btapb.Type_Int64_Encoding diff --git a/bigtable/type_test.go b/bigtable/type_test.go index 4f688384ad3c..cafa7db18d91 100644 --- a/bigtable/type_test.go +++ b/bigtable/type_test.go @@ -50,6 +50,23 @@ func TestInt64Proto(t *testing.T) { } } +func TestStringProto(t *testing.T) { + want := &btapb.Type{ + Kind: &btapb.Type_StringType{ + StringType: &btapb.Type_String{ + Encoding: &btapb.Type_String_Encoding{ + Encoding: &btapb.Type_String_Encoding_Utf8Raw_{}, + }, + }, + }, + } + + got := StringType{}.proto() + if !proto.Equal(got, want) { + t.Errorf("got type %v, want: %v", got, want) + } +} + func TestAggregateProto(t *testing.T) { want := &btapb.Type{ Kind: &btapb.Type_AggregateType{ From ece56853013f21702a344ad4d2bc0a868e5b169a Mon Sep 17 00:00:00 2001 From: Hoang Pham Date: Wed, 5 Jun 2024 10:23:53 -0400 Subject: [PATCH 3/3] Add comment for StringType. --- bigtable/type.go | 1 + 1 file changed, 1 insertion(+) diff --git a/bigtable/type.go b/bigtable/type.go index b8b9f8df05b8..6e35e982ce0a 100644 --- a/bigtable/type.go +++ b/bigtable/type.go @@ -69,6 +69,7 @@ func (encoding StringUtf8Encoding) proto() *btapb.Type_String_Encoding { } } +// StringType represents a string type StringType struct { Encoding StringEncoding }