Skip to content

Commit

Permalink
Merge pull request #2 from tmc/support-optional
Browse files Browse the repository at this point in the history
Support proto3 optional
  • Loading branch information
tmc authored Jan 15, 2023
2 parents 440a8bd + fa0fc26 commit d786610
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 65 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/Masterminds/sprig v2.22.0+incompatible
google.golang.org/protobuf v1.27.1
google.golang.org/protobuf v1.28.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Masterminds/sprig"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/pluginpb"
)

func main() {
Expand All @@ -27,6 +28,7 @@ func main() {
ParamFunc: flags.Set,
}
opts.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
genOpts := GenOpts{
Format: *format,
TemplateDir: *templates,
Expand Down
4 changes: 2 additions & 2 deletions templates/hugo-markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Message template
| ----- | ---- | ----------- |
{{range .Fields}}{{ if (not .Desc.ContainingOneof) }}{{template "field" .}}{{end}}{{end}}
{{- end -}}
{{range .Oneofs}}{{template "oneof" .}}{{end}}
{{range .Oneofs}}{{ if .Desc.IsSynthetic }}{{template "field" (index .Fields 0) }}{{else}}{{template "oneof" .}}{{end}}{{end}}

{{if .Extensions}}
| Extension | Type | Base | Number | Description |
Expand All @@ -110,7 +110,7 @@ Message template
Field template
***************************************************************/}}
{{define "field" -}}
| {{.Desc.Name}}{{ if .Desc.IsList }}[]{{ end }} |
| {{.Desc.Name }}{{ if .Desc.IsList }}[]{{ end }}{{ if .Desc.HasOptionalKeyword }} (optional){{ end }} |
{{- if (or (is_primitive .) (is_google_type .)) -}}
{{ field_type . }}
{{- else -}}
Expand Down
5 changes: 3 additions & 2 deletions templates/markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ Message template
| ----- | ---- | ----------- |
{{range .Fields}}{{ if (not .Desc.ContainingOneof) }}{{template "field" .}}{{end}}{{end}}
{{- end -}}
{{range .Oneofs}}{{template "oneof" .}}{{end}}

{{range .Oneofs}}{{ if .Desc.IsSynthetic }}{{template "field" (index .Fields 0) }}{{else}}{{template "oneof" .}}{{end}}{{end}}

{{if .Extensions}}
| Extension | Type | Base | Number | Description |
Expand All @@ -110,7 +111,7 @@ Message template
Field template
***************************************************************/}}
{{define "field" -}}
| {{.Desc.Name}}{{ if .Desc.IsList }}[]{{ end }} |
| {{.Desc.Name }}{{ if .Desc.IsList }}[]{{ end }}{{ if .Desc.HasOptionalKeyword }} (optional){{ end }} |
{{- if (or (is_primitive .) (is_google_type .)) -}}
{{ field_type . }}
{{- else -}}
Expand Down
46 changes: 23 additions & 23 deletions testdata/example1/Booking.md → testdata/example1/booking.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Booking.proto

<a name="Booking-proto"></a><p align="right"><a href="#top">Top</a></p>
---
title: com.example.booking
description: API Specification for the com.example.booking package.
---

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

<!-- begin services -->


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




<!-- begin services -->



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



<!-- end nested messages -->




<!-- end nested enums -->



Expand All @@ -60,15 +62,14 @@ Represents the status of a vehicle booking.
| Field | Type | Description |
| ----- | ---- | ----------- |
| id |int32| Unique booking status ID. |
| description |string| Booking status description. E.g. &#34;Active&#34;. |

| description |string| Booking status description. E.g. "Active". |





<!-- end nested messages -->


<!-- end nested enums -->



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

Represents the booking of a vehicle.
//
// Vehicles are quite fun. But drive carefully!

Vehicles are quite fun. But drive carefully!



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



<!-- end nested messages -->




<!-- end nested enums -->



Expand All @@ -116,16 +116,16 @@ An empty message for testing







<!-- end nested messages -->


<!-- end nested enums -->



<!-- end messages -->

<!-- begin file-level enums -->
<!-- end file-level enums -->


<!-- begin file-level extensions -->
<!-- end file-level extensions -->

File renamed without changes.
42 changes: 42 additions & 0 deletions testdata/example1/field_presence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: com.example.proto3
description: API Specification for the com.example.proto3 package.
---

<a name="field_presence-proto"></a><p align="right"><a href="#top">Top</a></p>

<!-- begin services -->

<!-- begin services -->



<a name="com-example-proto3-MyMessage"></a>

### MyMessage





| Field | Type | Description |
| ----- | ---- | ----------- |
| not_tracked |int32| |
| tracked (optional) |int32| Explicit presence |




<!-- end nested messages -->

<!-- end nested enums -->


<!-- end messages -->

<!-- begin file-level enums -->
<!-- end file-level enums -->

<!-- begin file-level extensions -->
<!-- end file-level extensions -->

12 changes: 12 additions & 0 deletions testdata/example1/field_presence.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Encoding and show field presence.
syntax = "proto3";

package com.example.proto3;

option go_package = "example.com/vehicleproto3";

message MyMessage {
int32 not_tracked = 1;
// Explicit presence
optional int32 tracked = 2;
}
73 changes: 36 additions & 37 deletions testdata/example1/Vehicle.md → testdata/example1/vehicle.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Vehicle.proto

<a name="Vehicle-proto"></a><p align="right"><a href="#top">Top</a></p>

---
title: com.example
description: API Specification for the com.example package.
---

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

<!-- begin services -->

<!-- begin services -->



Expand All @@ -20,15 +23,14 @@ Represents a manufacturer of cars.
| Field | Type | Description |
| ----- | ---- | ----------- |
| id |int32| The unique manufacturer ID. |
| code |string| A manufacturer code, e.g. &#34;DKL4P&#34;. |
| details |string| Manufacturer details (minimum orders et.c.). |
| category |[Manufacturer.Category](#com-example-Manufacturer-Category)| Manufacturer category. |

| code |string| A manufacturer code, e.g. "DKL4P". |
| details (optional) |string| Manufacturer details (minimum orders et.c.). |
| category (optional) |[Manufacturer.Category](#com-example-Manufacturer-Category)| Manufacturer category. |





<!-- end nested messages -->



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



<!-- end nested enums -->



Expand All @@ -62,18 +64,17 @@ Represents a vehicle model.
| Field | Type | Description |
| ----- | ---- | ----------- |
| id |string| The unique model ID. |
| model_code |string| The car model code, e.g. &#34;PZ003&#34;. |
| model_name |string| The car model name, e.g. &#34;Z3&#34;. |
| model_code |string| The car model code, e.g. "PZ003". |
| model_name |string| The car model name, e.g. "Z3". |
| daily_hire_rate_dollars |sint32| Dollars per day. |
| daily_hire_rate_cents |sint32| Cents per day. |




<!-- end nested messages -->




<!-- end nested enums -->



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

| mileage (optional) |sint32| Current vehicle mileage, if known. |
| category (optional) |[Vehicle.Category](#com-example-Vehicle-Category)| Vehicle category. |
| daily_hire_rate_dollars (optional) |sint32| Dollars per day. |
| daily_hire_rate_cents (optional) |sint32| Cents per day. |



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

### Category

Represents a vehicle category. E.g. &#34;Sedan&#34; or &#34;Truck&#34;.
Represents a vehicle category. E.g. "Sedan" or "Truck".




| Field | Type | Description |
| ----- | ---- | ----------- |
| code |string| Category code. E.g. &#34;S&#34;. |
| description |string| Category name. E.g. &#34;Sedan&#34;. |
| code |string| Category code. E.g. "S". |
| description |string| Category name. E.g. "Sedan". |




<!-- end nested messages -->


<!-- end nested enums -->



<!-- end nested messages -->


<!-- end nested enums -->






<!-- end messages -->

<!-- begin file-level enums -->


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



<!-- end file-level enums -->

<!-- begin file-level extensions -->

<a name="vehicle-proto-extensions"></a>

<a name="Vehicle-proto-extensions"></a>

### File-level Extensions
| Extension | Type | Base | Number | Description |
### Extensions
| Extension | Type | Extension Point | Number | Description |
| --------- | ---- | ---- | ------ | ----------- |
| country | com.example.country | (none) | 100 | Manufacturer country. |
| country | com.example.country | Manufacturer | 100 | Manufacturer country. |


<!-- end file-level extensions -->

File renamed without changes.

0 comments on commit d786610

Please sign in to comment.