Skip to content

Commit d786610

Browse files
authored
Merge pull request #2 from tmc/support-optional
Support proto3 optional
2 parents 440a8bd + fa0fc26 commit d786610

11 files changed

+123
-65
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.17
44

55
require (
66
github.com/Masterminds/sprig v2.22.0+incompatible
7-
google.golang.org/protobuf v1.27.1
7+
google.golang.org/protobuf v1.28.1
88
)
99

1010
require (

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
2424
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
2525
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
2626
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
27+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
28+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
2729
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2830
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/Masterminds/sprig"
1616
"google.golang.org/protobuf/compiler/protogen"
1717
"google.golang.org/protobuf/reflect/protoreflect"
18+
"google.golang.org/protobuf/types/pluginpb"
1819
)
1920

2021
func main() {
@@ -27,6 +28,7 @@ func main() {
2728
ParamFunc: flags.Set,
2829
}
2930
opts.Run(func(gen *protogen.Plugin) error {
31+
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
3032
genOpts := GenOpts{
3133
Format: *format,
3234
TemplateDir: *templates,

templates/hugo-markdown.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Message template
8686
| ----- | ---- | ----------- |
8787
{{range .Fields}}{{ if (not .Desc.ContainingOneof) }}{{template "field" .}}{{end}}{{end}}
8888
{{- end -}}
89-
{{range .Oneofs}}{{template "oneof" .}}{{end}}
89+
{{range .Oneofs}}{{ if .Desc.IsSynthetic }}{{template "field" (index .Fields 0) }}{{else}}{{template "oneof" .}}{{end}}{{end}}
9090

9191
{{if .Extensions}}
9292
| Extension | Type | Base | Number | Description |
@@ -110,7 +110,7 @@ Message template
110110
Field template
111111
***************************************************************/}}
112112
{{define "field" -}}
113-
| {{.Desc.Name}}{{ if .Desc.IsList }}[]{{ end }} |
113+
| {{.Desc.Name }}{{ if .Desc.IsList }}[]{{ end }}{{ if .Desc.HasOptionalKeyword }} (optional){{ end }} |
114114
{{- if (or (is_primitive .) (is_google_type .)) -}}
115115
{{ field_type . }}
116116
{{- else -}}

templates/markdown.tmpl

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ Message template
8686
| ----- | ---- | ----------- |
8787
{{range .Fields}}{{ if (not .Desc.ContainingOneof) }}{{template "field" .}}{{end}}{{end}}
8888
{{- end -}}
89-
{{range .Oneofs}}{{template "oneof" .}}{{end}}
89+
90+
{{range .Oneofs}}{{ if .Desc.IsSynthetic }}{{template "field" (index .Fields 0) }}{{else}}{{template "oneof" .}}{{end}}{{end}}
9091

9192
{{if .Extensions}}
9293
| Extension | Type | Base | Number | Description |
@@ -110,7 +111,7 @@ Message template
110111
Field template
111112
***************************************************************/}}
112113
{{define "field" -}}
113-
| {{.Desc.Name}}{{ if .Desc.IsList }}[]{{ end }} |
114+
| {{.Desc.Name }}{{ if .Desc.IsList }}[]{{ end }}{{ if .Desc.HasOptionalKeyword }} (optional){{ end }} |
114115
{{- if (or (is_primitive .) (is_google_type .)) -}}
115116
{{ field_type . }}
116117
{{- else -}}

testdata/example1/Booking.md renamed to testdata/example1/booking.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# Booking.proto
2-
3-
<a name="Booking-proto"></a><p align="right"><a href="#top">Top</a></p>
1+
---
2+
title: com.example.booking
3+
description: API Specification for the com.example.booking package.
4+
---
45

6+
<a name="booking-proto"></a><p align="right"><a href="#top">Top</a></p>
57

8+
<!-- begin services -->
69

710

811
<a name="com-example-booking-BookingService"></a>
@@ -20,7 +23,7 @@ Service for handling vehicle bookings.
2023

2124

2225

23-
26+
<!-- begin services -->
2427

2528

2629

@@ -40,10 +43,9 @@ Represents the booking status ID.
4043

4144

4245

46+
<!-- end nested messages -->
4347

44-
45-
46-
48+
<!-- end nested enums -->
4749

4850

4951

@@ -60,15 +62,14 @@ Represents the status of a vehicle booking.
6062
| Field | Type | Description |
6163
| ----- | ---- | ----------- |
6264
| id |int32| Unique booking status ID. |
63-
| description |string| Booking status description. E.g. &#34;Active&#34;. |
64-
65+
| description |string| Booking status description. E.g. "Active". |
6566

6667

6768

6869

69-
70+
<!-- end nested messages -->
7071

71-
72+
<!-- end nested enums -->
7273

7374

7475

@@ -78,8 +79,8 @@ Represents the status of a vehicle booking.
7879
### Booking
7980

8081
Represents the booking of a vehicle.
81-
//
82-
// Vehicles are quite fun. But drive carefully!
82+
83+
Vehicles are quite fun. But drive carefully!
8384

8485

8586

@@ -96,10 +97,9 @@ Represents the booking of a vehicle.
9697

9798

9899

100+
<!-- end nested messages -->
99101

100-
101-
102-
102+
<!-- end nested enums -->
103103

104104

105105

@@ -116,16 +116,16 @@ An empty message for testing
116116

117117

118118

119-
120-
121-
122-
119+
<!-- end nested messages -->
123120

124-
121+
<!-- end nested enums -->
125122

126123

127-
124+
<!-- end messages -->
128125

126+
<!-- begin file-level enums -->
127+
<!-- end file-level enums -->
129128

130-
129+
<!-- begin file-level extensions -->
130+
<!-- end file-level extensions -->
131131

File renamed without changes.

testdata/example1/field_presence.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: com.example.proto3
3+
description: API Specification for the com.example.proto3 package.
4+
---
5+
6+
<a name="field_presence-proto"></a><p align="right"><a href="#top">Top</a></p>
7+
8+
<!-- begin services -->
9+
10+
<!-- begin services -->
11+
12+
13+
14+
<a name="com-example-proto3-MyMessage"></a>
15+
16+
### MyMessage
17+
18+
19+
20+
21+
22+
| Field | Type | Description |
23+
| ----- | ---- | ----------- |
24+
| not_tracked |int32| |
25+
| tracked (optional) |int32| Explicit presence |
26+
27+
28+
29+
30+
<!-- end nested messages -->
31+
32+
<!-- end nested enums -->
33+
34+
35+
<!-- end messages -->
36+
37+
<!-- begin file-level enums -->
38+
<!-- end file-level enums -->
39+
40+
<!-- begin file-level extensions -->
41+
<!-- end file-level extensions -->
42+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Encoding and show field presence.
2+
syntax = "proto3";
3+
4+
package com.example.proto3;
5+
6+
option go_package = "example.com/vehicleproto3";
7+
8+
message MyMessage {
9+
int32 not_tracked = 1;
10+
// Explicit presence
11+
optional int32 tracked = 2;
12+
}

testdata/example1/Vehicle.md renamed to testdata/example1/vehicle.md

+36-37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# Vehicle.proto
2-
3-
<a name="Vehicle-proto"></a><p align="right"><a href="#top">Top</a></p>
4-
1+
---
2+
title: com.example
3+
description: API Specification for the com.example package.
4+
---
55

6+
<a name="vehicle-proto"></a><p align="right"><a href="#top">Top</a></p>
67

8+
<!-- begin services -->
79

10+
<!-- begin services -->
811

912

1013

@@ -20,15 +23,14 @@ Represents a manufacturer of cars.
2023
| Field | Type | Description |
2124
| ----- | ---- | ----------- |
2225
| id |int32| The unique manufacturer ID. |
23-
| code |string| A manufacturer code, e.g. &#34;DKL4P&#34;. |
24-
| details |string| Manufacturer details (minimum orders et.c.). |
25-
| category |[Manufacturer.Category](#com-example-Manufacturer-Category)| Manufacturer category. |
26-
26+
| code |string| A manufacturer code, e.g. "DKL4P". |
27+
| details (optional) |string| Manufacturer details (minimum orders et.c.). |
28+
| category (optional) |[Manufacturer.Category](#com-example-Manufacturer-Category)| Manufacturer category. |
2729

2830

2931

3032

31-
33+
<!-- end nested messages -->
3234

3335

3436

@@ -45,7 +47,7 @@ Manufacturer category. A manufacturer may be either inhouse or external.
4547
| CATEGORY_EXTERNAL | 1 | The manufacturer is external. |
4648

4749

48-
50+
<!-- end nested enums -->
4951

5052

5153

@@ -62,18 +64,17 @@ Represents a vehicle model.
6264
| Field | Type | Description |
6365
| ----- | ---- | ----------- |
6466
| id |string| The unique model ID. |
65-
| model_code |string| The car model code, e.g. &#34;PZ003&#34;. |
66-
| model_name |string| The car model name, e.g. &#34;Z3&#34;. |
67+
| model_code |string| The car model code, e.g. "PZ003". |
68+
| model_name |string| The car model name, e.g. "Z3". |
6769
| daily_hire_rate_dollars |sint32| Dollars per day. |
6870
| daily_hire_rate_cents |sint32| Cents per day. |
6971

7072

7173

7274

75+
<!-- end nested messages -->
7376

74-
75-
76-
77+
<!-- end nested enums -->
7778

7879

7980

@@ -92,11 +93,10 @@ Represents a vehicle that can be hired.
9293
| id |int32| Unique vehicle ID. |
9394
| model |[Model](#com-example-Model)| Vehicle model. |
9495
| reg_number |string| Vehicle registration number. |
95-
| mileage |sint32| Current vehicle mileage, if known. |
96-
| category |[Vehicle.Category](#com-example-Vehicle-Category)| Vehicle category. |
97-
| daily_hire_rate_dollars |sint32| Dollars per day. |
98-
| daily_hire_rate_cents |sint32| Cents per day. |
99-
96+
| mileage (optional) |sint32| Current vehicle mileage, if known. |
97+
| category (optional) |[Vehicle.Category](#com-example-Vehicle-Category)| Vehicle category. |
98+
| daily_hire_rate_dollars (optional) |sint32| Dollars per day. |
99+
| daily_hire_rate_cents (optional) |sint32| Cents per day. |
100100

101101

102102

@@ -112,33 +112,32 @@ Represents a vehicle that can be hired.
112112

113113
### Category
114114

115-
Represents a vehicle category. E.g. &#34;Sedan&#34; or &#34;Truck&#34;.
115+
Represents a vehicle category. E.g. "Sedan" or "Truck".
116116

117117

118118

119119

120120
| Field | Type | Description |
121121
| ----- | ---- | ----------- |
122-
| code |string| Category code. E.g. &#34;S&#34;. |
123-
| description |string| Category name. E.g. &#34;Sedan&#34;. |
122+
| code |string| Category code. E.g. "S". |
123+
| description |string| Category name. E.g. "Sedan". |
124124

125125

126126

127127

128+
<!-- end nested messages -->
128129

129-
130+
<!-- end nested enums -->
130131

131-
132132

133+
<!-- end nested messages -->
133134

134-
135+
<!-- end nested enums -->
135136

136-
137-
138-
139-
140137

138+
<!-- end messages -->
141139

140+
<!-- begin file-level enums -->
142141

143142

144143
<a name="com-example-Coolness"></a>
@@ -153,16 +152,16 @@ Represents a vehicle category. E.g. &#34;Sedan&#34; or &#34;Truck&#34;.
153152
| COOLNESS_MAX | 1 | The coolness is maximum. |
154153

155154

156-
155+
<!-- end file-level enums -->
157156

157+
<!-- begin file-level extensions -->
158158

159+
<a name="vehicle-proto-extensions"></a>
159160

160-
<a name="Vehicle-proto-extensions"></a>
161-
162-
### File-level Extensions
163-
| Extension | Type | Base | Number | Description |
161+
### Extensions
162+
| Extension | Type | Extension Point | Number | Description |
164163
| --------- | ---- | ---- | ------ | ----------- |
165-
| country | com.example.country | (none) | 100 | Manufacturer country. |
164+
| country | com.example.country | Manufacturer | 100 | Manufacturer country. |
166165

167-
166+
<!-- end file-level extensions -->
168167

File renamed without changes.

0 commit comments

Comments
 (0)