diff --git a/bigtable/type.go b/bigtable/type.go index a390e8fd44b2..6e35e982ce0a 100644 --- a/bigtable/type.go +++ b/bigtable/type.go @@ -54,6 +54,36 @@ 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_{}, + } +} + +// StringType represents a string +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{