diff --git a/go.mod b/go.mod index ce7fc11..8504b08 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index c6bf63e..2985f54 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index ea2e713..1fc100b 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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, diff --git a/templates/hugo-markdown.tmpl b/templates/hugo-markdown.tmpl index 2c146ef..bb7c1c7 100644 --- a/templates/hugo-markdown.tmpl +++ b/templates/hugo-markdown.tmpl @@ -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 | @@ -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 -}} diff --git a/templates/markdown.tmpl b/templates/markdown.tmpl index 432b8ec..78c9c49 100644 --- a/templates/markdown.tmpl +++ b/templates/markdown.tmpl @@ -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 | @@ -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 -}} diff --git a/testdata/example1/Booking.md b/testdata/example1/booking.md similarity index 71% rename from testdata/example1/Booking.md rename to testdata/example1/booking.md index 778de8c..3b3c944 100644 --- a/testdata/example1/Booking.md +++ b/testdata/example1/booking.md @@ -1,8 +1,11 @@ -# Booking.proto - -
+--- +title: com.example.booking +description: API Specification for the com.example.booking package. +--- + + @@ -20,7 +23,7 @@ Service for handling vehicle bookings. - + @@ -40,10 +43,9 @@ Represents the booking status ID. + - - - + @@ -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. "Active". | - +| description |string| Booking status description. E.g. "Active". | - + - + @@ -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! @@ -96,10 +97,9 @@ Represents the booking of a vehicle. + - - - + @@ -116,16 +116,16 @@ An empty message for testing - - - - + - + - + + + - + + diff --git a/testdata/example1/Booking.proto b/testdata/example1/booking.proto similarity index 100% rename from testdata/example1/Booking.proto rename to testdata/example1/booking.proto diff --git a/testdata/example1/field_presence.md b/testdata/example1/field_presence.md new file mode 100644 index 0000000..56e081d --- /dev/null +++ b/testdata/example1/field_presence.md @@ -0,0 +1,42 @@ +--- +title: com.example.proto3 +description: API Specification for the com.example.proto3 package. +--- + + + + + + + + + + + +### MyMessage + + + + + +| Field | Type | Description | +| ----- | ---- | ----------- | +| not_tracked |int32| | +| tracked (optional) |int32| Explicit presence | + + + + + + + + + + + + + + + + + diff --git a/testdata/example1/field_presence.proto b/testdata/example1/field_presence.proto new file mode 100644 index 0000000..6b945c4 --- /dev/null +++ b/testdata/example1/field_presence.proto @@ -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; +} diff --git a/testdata/example1/Vehicle.md b/testdata/example1/vehicle.md similarity index 52% rename from testdata/example1/Vehicle.md rename to testdata/example1/vehicle.md index f1e9b85..8120f99 100644 --- a/testdata/example1/Vehicle.md +++ b/testdata/example1/vehicle.md @@ -1,10 +1,13 @@ -# Vehicle.proto - - - +--- +title: com.example +description: API Specification for the com.example package. +--- + + + @@ -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. "DKL4P". | -| 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. | - + @@ -45,7 +47,7 @@ Manufacturer category. A manufacturer may be either inhouse or external. | CATEGORY_EXTERNAL | 1 | The manufacturer is external. | - + @@ -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. "PZ003". | -| model_name |string| The car model name, e.g. "Z3". | +| 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. | + - - - + @@ -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. | @@ -112,33 +112,32 @@ Represents a vehicle that can be hired. ### Category -Represents a vehicle category. E.g. "Sedan" or "Truck". +Represents a vehicle category. E.g. "Sedan" or "Truck". | Field | Type | Description | | ----- | ---- | ----------- | -| code |string| Category code. E.g. "S". | -| description |string| Category name. E.g. "Sedan". | +| code |string| Category code. E.g. "S". | +| description |string| Category name. E.g. "Sedan". | + - + - + - + - - - - + + @@ -153,16 +152,16 @@ Represents a vehicle category. E.g. "Sedan" or "Truck". | COOLNESS_MAX | 1 | The coolness is maximum. | - + + + - - -### 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. | - + diff --git a/testdata/example1/Vehicle.proto b/testdata/example1/vehicle.proto similarity index 100% rename from testdata/example1/Vehicle.proto rename to testdata/example1/vehicle.proto