Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigtable): Add MarshalJSON to allow clients to get a stringified version of the protobuf #10679

Merged
merged 22 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions bigtable/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,40 @@ limitations under the License.

package bigtable

import btapb "cloud.google.com/go/bigtable/admin/apiv2/adminpb"
import (
btapb "cloud.google.com/go/bigtable/admin/apiv2/adminpb"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

// Type wraps the protobuf representation of a type. See the protobuf definition
// for more details on types.
type Type interface {
proto() *btapb.Type
}

var marshalOptions = protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
var unmarshalOptions = protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we want DiscardUnknown? assuming that does what it sounds like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// MarshalJSON returns the string representation of the Type protobuf.
func MarshalJSON(t Type) ([]byte, error) {
return marshalOptions.Marshal(t.proto())
}

// UnmarshalJSON returns a Type object from json bytes.
func UnmarshalJSON(data []byte) (Type, error) {
result := &btapb.Type{}
if err := unmarshalOptions.Unmarshal(data, result); err != nil {
return nil, err
}
return ProtoToType(result), nil
}

// Equal compares Type objects.
func Equal(a, b Type) bool {
return proto.Equal(a.proto(), b.proto())
}

type unknown[T interface{}] struct {
wrapped *T
}
Expand Down Expand Up @@ -205,6 +231,8 @@ func ProtoToType(pb *btapb.Type) Type {
return int64ProtoToType(t.Int64Type)
case *btapb.Type_BytesType:
return bytesProtoToType(t.BytesType)
case *btapb.Type_StringType:
return stringProtoToType(t.StringType)
case *btapb.Type_AggregateType:
return aggregateProtoToType(t.AggregateType)
default:
Expand All @@ -229,6 +257,23 @@ func bytesProtoToType(b *btapb.Type_Bytes) BytesType {
return BytesType{Encoding: bytesEncodingProtoToType(b.Encoding)}
}

func stringEncodingProtoToType(se *btapb.Type_String_Encoding) StringEncoding {
if se == nil {
return unknown[btapb.Type_String_Encoding]{wrapped: se}
}

switch se.Encoding.(type) {
case *btapb.Type_String_Encoding_Utf8Raw_:
return StringUtf8Encoding{}
default:
return unknown[btapb.Type_String_Encoding]{wrapped: se}
}
}

func stringProtoToType(s *btapb.Type_String) StringType {
return StringType{Encoding: stringEncodingProtoToType(s.Encoding)}
}

func int64EncodingProtoToEncoding(ie *btapb.Type_Int64_Encoding) Int64Encoding {
if ie == nil {
return unknown[btapb.Type_Int64_Encoding]{wrapped: ie}
Expand All @@ -242,11 +287,11 @@ func int64EncodingProtoToEncoding(ie *btapb.Type_Int64_Encoding) Int64Encoding {
}
}

func int64ProtoToType(i *btapb.Type_Int64) Type {
func int64ProtoToType(i *btapb.Type_Int64) Int64Type {
return Int64Type{Encoding: int64EncodingProtoToEncoding(i.Encoding)}
}

func aggregateProtoToType(agg *btapb.Type_Aggregate) Type {
func aggregateProtoToType(agg *btapb.Type_Aggregate) AggregateType {
if agg == nil {
return AggregateType{Input: nil, Aggregator: unknownAggregator{wrapped: agg}}
}
Expand Down
Loading
Loading