Skip to content

Commit

Permalink
chore(internal/protoveneer): support Duration (#10293)
Browse files Browse the repository at this point in the history
Automatically convert the durationpb.Duration protobuf into a time.Duration.
  • Loading branch information
jba committed May 31, 2024
1 parent 7b073ca commit 5ccd88c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/protoveneer/cmd/protoveneer/protoveneer.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,13 @@ var externalTypes = []*externalType{
convertTo: "timestamppb.New",
convertFrom: "support.TimeFromProto",
},
{
qualifiedName: "time.Duration",
replaces: "*durationpb.Duration",
importPaths: []string{"time", "google.golang.org/protobuf/types/known/durationpb"},
convertTo: "durationpb.New",
convertFrom: "support.DurationFromProto",
},
{
qualifiedName: "*apierror.APIError",
replaces: "*status.Status",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
status "google.golang.org/genproto/googleapis/rpc/status"
date "google.golang.org/genproto/googleapis/type/date"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
durationpb "google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -135,8 +136,10 @@ type Pop struct {
Y unexported
}

// Status converts to itself.
// status.Status converts to apierror.APIError.
// durationpb.Duration converts to time.Duration.

type File struct {
Error *status.Status
Dur *durationpb.Duration
}
4 changes: 4 additions & 0 deletions internal/protoveneer/cmd/protoveneer/testdata/basic/golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
pb "example.com/basic"
"example.com/protoveneer/support"
"github.com/googleapis/gax-go/v2/apierror"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)
Expand Down Expand Up @@ -106,6 +107,7 @@ func (CitationMetadata) fromProto(p *pb.CitationMetadata) *CitationMetadata {

type File struct {
Error *apierror.APIError
Dur time.Duration
}

func (v *File) toProto() *pb.File {
Expand All @@ -114,6 +116,7 @@ func (v *File) toProto() *pb.File {
}
return &pb.File{
Error: support.APIErrorToProto(v.Error),
Dur: durationpb.New(v.Dur),
}
}

Expand All @@ -123,6 +126,7 @@ func (File) fromProto(p *pb.File) *File {
}
return &File{
Error: support.APIErrorFromProto(p.Error),
Dur: support.DurationFromProto(p.Dur),
}
}

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 @@ -24,6 +24,7 @@ import (
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/genproto/googleapis/type/date"
gstatus "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -141,3 +142,11 @@ func APIErrorFromProto(s *spb.Status) *apierror.APIError {
}
return aerr
}

// DurationFromProto converts a Duration proto to a time.Duration.
func DurationFromProto(d *durationpb.Duration) time.Duration {
if d == nil {
return 0
}
return d.AsDuration()
}

0 comments on commit 5ccd88c

Please sign in to comment.