Skip to content

Commit

Permalink
feat(bigtable): add string type to supported Bigtable type (googleapi…
Browse files Browse the repository at this point in the history
…s#10306)

* Add string type support to bigtable type.

* feat(bigtable): add string type to supported Bigtable type

* Add comment for StringType.
  • Loading branch information
hoangpham95 authored Jun 20, 2024
1 parent d8bd2c1 commit 18fa7e4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bigtable/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions bigtable/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 18fa7e4

Please sign in to comment.