Skip to content

Commit 5ccd88c

Browse files
authored
chore(internal/protoveneer): support Duration (#10293)
Automatically convert the durationpb.Duration protobuf into a time.Duration.
1 parent 7b073ca commit 5ccd88c

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

internal/protoveneer/cmd/protoveneer/protoveneer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,13 @@ var externalTypes = []*externalType{
849849
convertTo: "timestamppb.New",
850850
convertFrom: "support.TimeFromProto",
851851
},
852+
{
853+
qualifiedName: "time.Duration",
854+
replaces: "*durationpb.Duration",
855+
importPaths: []string{"time", "google.golang.org/protobuf/types/known/durationpb"},
856+
convertTo: "durationpb.New",
857+
convertFrom: "support.DurationFromProto",
858+
},
852859
{
853860
qualifiedName: "*apierror.APIError",
854861
replaces: "*status.Status",

internal/protoveneer/cmd/protoveneer/testdata/basic/basic.pb.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
status "google.golang.org/genproto/googleapis/rpc/status"
2020
date "google.golang.org/genproto/googleapis/type/date"
2121
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
22+
durationpb "google.golang.org/protobuf/types/known/durationpb"
2223
"google.golang.org/protobuf/types/known/structpb"
2324
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
2425
)
@@ -135,8 +136,10 @@ type Pop struct {
135136
Y unexported
136137
}
137138

138-
// Status converts to itself.
139+
// status.Status converts to apierror.APIError.
140+
// durationpb.Duration converts to time.Duration.
139141

140142
type File struct {
141143
Error *status.Status
144+
Dur *durationpb.Duration
142145
}

internal/protoveneer/cmd/protoveneer/testdata/basic/golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
pb "example.com/basic"
1010
"example.com/protoveneer/support"
1111
"github.com/googleapis/gax-go/v2/apierror"
12+
"google.golang.org/protobuf/types/known/durationpb"
1213
"google.golang.org/protobuf/types/known/timestamppb"
1314
"time"
1415
)
@@ -106,6 +107,7 @@ func (CitationMetadata) fromProto(p *pb.CitationMetadata) *CitationMetadata {
106107

107108
type File struct {
108109
Error *apierror.APIError
110+
Dur time.Duration
109111
}
110112

111113
func (v *File) toProto() *pb.File {
@@ -114,6 +116,7 @@ func (v *File) toProto() *pb.File {
114116
}
115117
return &pb.File{
116118
Error: support.APIErrorToProto(v.Error),
119+
Dur: durationpb.New(v.Dur),
117120
}
118121
}
119122

@@ -123,6 +126,7 @@ func (File) fromProto(p *pb.File) *File {
123126
}
124127
return &File{
125128
Error: support.APIErrorFromProto(p.Error),
129+
Dur: support.DurationFromProto(p.Dur),
126130
}
127131
}
128132

internal/protoveneer/support/support.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
spb "google.golang.org/genproto/googleapis/rpc/status"
2525
"google.golang.org/genproto/googleapis/type/date"
2626
gstatus "google.golang.org/grpc/status"
27+
"google.golang.org/protobuf/types/known/durationpb"
2728
"google.golang.org/protobuf/types/known/structpb"
2829
"google.golang.org/protobuf/types/known/timestamppb"
2930
)
@@ -141,3 +142,11 @@ func APIErrorFromProto(s *spb.Status) *apierror.APIError {
141142
}
142143
return aerr
143144
}
145+
146+
// DurationFromProto converts a Duration proto to a time.Duration.
147+
func DurationFromProto(d *durationpb.Duration) time.Duration {
148+
if d == nil {
149+
return 0
150+
}
151+
return d.AsDuration()
152+
}

0 commit comments

Comments
 (0)