Skip to content

Commit 4f8a18e

Browse files
committed
feat: generate behavior comments for fields
`google.api.field_behavior` is used for describing field behaviors for fields programmatically, instead of in comments. Because they are not in comments, they wont be reflected in the generated Typescript code. This change includes an appended behavior comment for all fields that are annotated with field_behavior. Proto3 `optional` do not have this comment appended, because the generated types already sufficiently signals this.
1 parent 2a0fffe commit 4f8a18e

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

examples/proto/gen/typescript/einride/example/freight/v1/index.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,43 @@ export type Shipment = {
88
// The resource name of the shipment.
99
name: string | undefined;
1010
// The creation timestamp of the shipment.
11+
//
12+
// Behaviors: OUTPUT_ONLY
1113
createTime: wellKnownTimestamp | undefined;
1214
// The last update timestamp of the shipment.
1315
// Updated when create/update/delete operation is shipment.
16+
//
17+
// Behaviors: OUTPUT_ONLY
1418
updateTime: wellKnownTimestamp | undefined;
1519
// The deletion timestamp of the shipment.
20+
//
21+
// Behaviors: OUTPUT_ONLY
1622
deleteTime: wellKnownTimestamp | undefined;
1723
// The resource name of the origin site of the shipment.
1824
// Format: shippers/{shipper}/sites/{site}
25+
//
26+
// Behaviors: REQUIRED
1927
originSite: string | undefined;
2028
// The resource name of the destination site of the shipment.
2129
// Format: shippers/{shipper}/sites/{site}
30+
//
31+
// Behaviors: REQUIRED
2232
destinationSite: string | undefined;
2333
// The earliest pickup time of the shipment at the origin site.
34+
//
35+
// Behaviors: REQUIRED
2436
pickupEarliestTime: wellKnownTimestamp | undefined;
2537
// The latest pickup time of the shipment at the origin site.
38+
//
39+
// Behaviors: REQUIRED
2640
pickupLatestTime: wellKnownTimestamp | undefined;
2741
// The earliest delivery time of the shipment at the destination site.
42+
//
43+
// Behaviors: REQUIRED
2844
deliveryEarliestTime: wellKnownTimestamp | undefined;
2945
// The latest delivery time of the shipment at the destination site.
46+
//
47+
// Behaviors: REQUIRED
3048
deliveryLatestTime: wellKnownTimestamp | undefined;
3149
// The line items of the shipment.
3250
lineItems: LineItem[] | undefined;
@@ -56,13 +74,21 @@ export type Shipper = {
5674
// The resource name of the shipper.
5775
name: string | undefined;
5876
// The creation timestamp of the shipper.
77+
//
78+
// Behaviors: OUTPUT_ONLY
5979
createTime: wellKnownTimestamp | undefined;
6080
// The last update timestamp of the shipper.
6181
// Updated when create/update/delete operation is performed.
82+
//
83+
// Behaviors: OUTPUT_ONLY
6284
updateTime: wellKnownTimestamp | undefined;
6385
// The deletion timestamp of the shipper.
86+
//
87+
// Behaviors: OUTPUT_ONLY
6488
deleteTime: wellKnownTimestamp | undefined;
6589
// The display name of the shipper.
90+
//
91+
// Behaviors: REQUIRED
6692
displayName: string | undefined;
6793
};
6894

@@ -72,13 +98,21 @@ export type Site = {
7298
// The resource name of the site.
7399
name: string | undefined;
74100
// The creation timestamp of the site.
101+
//
102+
// Behaviors: OUTPUT_ONLY
75103
createTime: wellKnownTimestamp | undefined;
76104
// The last update timestamp of the site.
77105
// Updated when create/update/delete operation is performed.
106+
//
107+
// Behaviors: OUTPUT_ONLY
78108
updateTime: wellKnownTimestamp | undefined;
79109
// The deletion timestamp of the site.
110+
//
111+
// Behaviors: OUTPUT_ONLY
80112
deleteTime: wellKnownTimestamp | undefined;
81113
// The display name of the site.
114+
//
115+
// Behaviors: REQUIRED
82116
displayName: string | undefined;
83117
// The geographic location of the site.
84118
latLng: googletype_LatLng | undefined;
@@ -100,6 +134,8 @@ export type googletype_LatLng = {
100134
export type GetShipperRequest = {
101135
// The resource name of the shipper to retrieve.
102136
// Format: shippers/{shipper}
137+
//
138+
// Behaviors: REQUIRED
103139
name: string | undefined;
104140
};
105141

@@ -129,6 +165,8 @@ export type ListShippersResponse = {
129165
// Request message for FreightService.CreateShipper.
130166
export type CreateShipperRequest = {
131167
// The shipper to create.
168+
//
169+
// Behaviors: REQUIRED
132170
shipper: Shipper | undefined;
133171
};
134172

@@ -137,6 +175,8 @@ export type UpdateShipperRequest = {
137175
// The shipper to update with. The name must match or be empty.
138176
// The shipper's `name` field is used to identify the shipper to be updated.
139177
// Format: shippers/{shipper}
178+
//
179+
// Behaviors: REQUIRED
140180
shipper: Shipper | undefined;
141181
// The list of fields to be updated.
142182
updateMask: wellKnownFieldMask | undefined;
@@ -174,20 +214,26 @@ type wellKnownFieldMask = string;
174214
export type DeleteShipperRequest = {
175215
// The resource name of the shipper to delete.
176216
// Format: shippers/{shipper}
217+
//
218+
// Behaviors: REQUIRED
177219
name: string | undefined;
178220
};
179221

180222
// Request message for FreightService.GetSite.
181223
export type GetSiteRequest = {
182224
// The resource name of the site to retrieve.
183225
// Format: shippers/{shipper}/sites/{site}
226+
//
227+
// Behaviors: REQUIRED
184228
name: string | undefined;
185229
};
186230

187231
// Request message for FreightService.ListSites.
188232
export type ListSitesRequest = {
189233
// The resource name of the parent, which owns this collection of sites.
190234
// Format: shippers/{shipper}
235+
//
236+
// Behaviors: REQUIRED
191237
parent: string | undefined;
192238
// Requested page size. Server may return fewer sites than requested.
193239
// If unspecified, server will pick an appropriate default.
@@ -214,8 +260,12 @@ export type ListSitesResponse = {
214260
export type CreateSiteRequest = {
215261
// The resource name of the parent shipper for which this site will be created.
216262
// Format: shippers/{shipper}
263+
//
264+
// Behaviors: REQUIRED
217265
parent: string | undefined;
218266
// The site to create.
267+
//
268+
// Behaviors: REQUIRED
219269
site: Site | undefined;
220270
};
221271

@@ -224,6 +274,8 @@ export type UpdateSiteRequest = {
224274
// The site to update with. The name must match or be empty.
225275
// The site's `name` field is used to identify the site to be updated.
226276
// Format: shippers/{shipper}/sites/{site}
277+
//
278+
// Behaviors: REQUIRED
227279
site: Site | undefined;
228280
// The list of fields to be updated.
229281
updateMask: wellKnownFieldMask | undefined;
@@ -233,20 +285,26 @@ export type UpdateSiteRequest = {
233285
export type DeleteSiteRequest = {
234286
// The resource name of the site to delete.
235287
// Format: shippers/{shipper}/sites/{site}
288+
//
289+
// Behaviors: REQUIRED
236290
name: string | undefined;
237291
};
238292

239293
// Request message for FreightService.GetShipment.
240294
export type GetShipmentRequest = {
241295
// The resource name of the shipment to retrieve.
242296
// Format: shippers/{shipper}/shipments/{shipment}
297+
//
298+
// Behaviors: REQUIRED
243299
name: string | undefined;
244300
};
245301

246302
// Request message for FreightService.ListShipments.
247303
export type ListShipmentsRequest = {
248304
// The resource name of the parent, which owns this collection of shipments.
249305
// Format: shippers/{shipper}
306+
//
307+
// Behaviors: REQUIRED
250308
parent: string | undefined;
251309
// Requested page size. Server may return fewer shipments than requested.
252310
// If unspecified, server will pick an appropriate default.
@@ -273,8 +331,12 @@ export type ListShipmentsResponse = {
273331
export type CreateShipmentRequest = {
274332
// The resource name of the parent shipper for which this shipment will be created.
275333
// Format: shippers/{shipper}
334+
//
335+
// Behaviors: REQUIRED
276336
parent: string | undefined;
277337
// The shipment to create.
338+
//
339+
// Behaviors: REQUIRED
278340
shipment: Shipment | undefined;
279341
};
280342

@@ -283,6 +345,8 @@ export type UpdateShipmentRequest = {
283345
// The shipment to update with. The name must match or be empty.
284346
// The shipment's `name` field is used to identify the shipment to be updated.
285347
// Format: shippers/{shipper}/shipments/{shipment}
348+
//
349+
// Behaviors: REQUIRED
286350
shipment: Shipment | undefined;
287351
// The list of fields to be updated.
288352
updateMask: wellKnownFieldMask | undefined;
@@ -292,6 +356,8 @@ export type UpdateShipmentRequest = {
292356
export type DeleteShipmentRequest = {
293357
// The resource name of the shipment to delete.
294358
// Format: shippers/{shipper}/shipments/{shipment}
359+
//
360+
// Behaviors: REQUIRED
295361
name: string | undefined;
296362
};
297363

internal/plugin/commentgen.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"strings"
55

66
"github.com/einride/protoc-gen-typescript-http/internal/codegen"
7+
"google.golang.org/genproto/googleapis/api/annotations"
8+
"google.golang.org/protobuf/proto"
79
"google.golang.org/protobuf/reflect/protoreflect"
810
)
911

@@ -20,4 +22,32 @@ func (c commentGenerator) generateLeading(f *codegen.File, indent int) {
2022
}
2123
f.P(t(indent), "// ", strings.TrimSpace(line))
2224
}
25+
if field, ok := c.descriptor.(protoreflect.FieldDescriptor); ok {
26+
if behaviorComment := fieldBehaviorComment(field); len(behaviorComment) > 0 {
27+
f.P(t(indent), "//")
28+
f.P(t(indent), "// ", behaviorComment)
29+
}
30+
}
31+
}
32+
33+
func fieldBehaviorComment(field protoreflect.FieldDescriptor) string {
34+
behaviors := getFieldBehaviors(field)
35+
if len(behaviors) == 0 {
36+
return ""
37+
}
38+
39+
behaviorStrings := make([]string, 0, len(behaviors))
40+
for _, b := range behaviors {
41+
behaviorStrings = append(behaviorStrings, b.String())
42+
}
43+
return "Behaviors: " + strings.Join(behaviorStrings, ", ")
44+
}
45+
46+
func getFieldBehaviors(field protoreflect.FieldDescriptor) []annotations.FieldBehavior {
47+
if behaviors, ok := proto.GetExtension(
48+
field.Options(), annotations.E_FieldBehavior,
49+
).([]annotations.FieldBehavior); ok {
50+
return behaviors
51+
}
52+
return nil
2353
}

0 commit comments

Comments
 (0)