Skip to content

Commit

Permalink
chore(internal/protoveneer): support time (googleapis#10266)
Browse files Browse the repository at this point in the history
Protos represent time using the timestamppb.Timestamp type.
Support converting that to and from a time.Time.
  • Loading branch information
jba authored May 26, 2024
1 parent 0b28aac commit 0ceec32
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
21 changes: 17 additions & 4 deletions internal/protoveneer/cmd/protoveneer/protoveneer.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,17 @@ func write(typeInfos []*typeInfo, conf *config, fset *token.FileSet) ([]byte, er
return nil, errors.New("missing supportImportPath in config")
}
prn(` "%s"`, conf.SupportImportPath)
importPaths := map[string]bool{}
for _, et := range externalTypes {
if et.used && et.importPath != "" {
prn(` "%s"`, et.importPath)
if et.used {
for _, ip := range et.importPaths {
importPaths[ip] = true
}
}
}
for ip := range importPaths {
prn(` "%s"`, ip)
}
pr(")\n\n")

// Types.
Expand Down Expand Up @@ -775,7 +781,7 @@ func (ti *typeInfo) generateFromProto(pr func(string, ...any)) {
type externalType struct {
qualifiedName string
replaces string
importPath string
importPaths []string
convertTo string
convertFrom string

Expand All @@ -787,7 +793,7 @@ var externalTypes = []*externalType{
{
qualifiedName: "civil.Date",
replaces: "*date.Date",
importPath: "cloud.google.com/go/civil",
importPaths: []string{"cloud.google.com/go/civil"},
convertTo: "support.CivilDateToProto",
convertFrom: "support.CivilDateFromProto",
},
Expand All @@ -797,6 +803,13 @@ var externalTypes = []*externalType{
convertTo: "support.MapToStructPB",
convertFrom: "support.MapFromStructPB",
},
{
qualifiedName: "time.Time",
replaces: "*timestamppb.Timestamp",
importPaths: []string{"time", "google.golang.org/protobuf/types/known/timestamppb"},
convertTo: "timestamppb.New",
convertFrom: "support.TimeFromProto",
},
}

var protoTypeToExternalType = map[string]*externalType{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
date "google.golang.org/genproto/googleapis/type/date"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/types/known/structpb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

const (
Expand Down Expand Up @@ -120,4 +121,5 @@ type Citation struct {
// Output only. Publication date of the attribution.
PublicationDate *date.Date `protobuf:"bytes,6,opt,name=publication_date,json=publicationDate,proto3" json:"publication_date,omitempty"`
Struct *structpb.Struct
CreateTime *timestamppb.Timestamp
}
5 changes: 5 additions & 0 deletions internal/protoveneer/cmd/protoveneer/testdata/basic/golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"cloud.google.com/go/civil"
pb "example.com/basic"
"example.com/protoveneer/support"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)

// Blob contains raw media bytes.
Expand Down Expand Up @@ -47,6 +49,7 @@ type Citation struct {
// Output only. Publication date of the attribution.
PublicationDate civil.Date
Struct map[string]any
CreateTime time.Time
}

func (v *Citation) toProto() *pb.Citation {
Expand All @@ -57,6 +60,7 @@ func (v *Citation) toProto() *pb.Citation {
Uri: v.URI,
PublicationDate: support.CivilDateToProto(v.PublicationDate),
Struct: support.MapToStructPB(v.Struct),
CreateTime: timestamppb.New(v.CreateTime),
}
}

Expand All @@ -68,6 +72,7 @@ func (Citation) fromProto(p *pb.Citation) *Citation {
URI: p.Uri,
PublicationDate: support.CivilDateFromProto(p.PublicationDate),
Struct: support.MapFromStructPB(p.Struct),
CreateTime: support.TimeFromProto(p.CreateTime),
}
}

Expand Down
9 changes: 9 additions & 0 deletions internal/protoveneer/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"cloud.google.com/go/civil"
"google.golang.org/genproto/googleapis/type/date"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

// TransformSlice applies f to each element of from and returns
Expand Down Expand Up @@ -110,3 +111,11 @@ func MapFromStructPB(p *structpb.Struct) map[string]any {
}
return p.AsMap()
}

// TimeFromProto converts a Timestamp into a time.Time.
func TimeFromProto(ts *timestamppb.Timestamp) time.Time {
if ts == nil {
return time.Time{}
}
return ts.AsTime()
}

0 comments on commit 0ceec32

Please sign in to comment.