diff --git a/cmd/protoc-gen-doc/flags.go b/cmd/protoc-gen-doc/flags.go index 40aa8976..90d54c94 100644 --- a/cmd/protoc-gen-doc/flags.go +++ b/cmd/protoc-gen-doc/flags.go @@ -83,8 +83,8 @@ func (f *Flags) PrintVersion() { // ParseFlags parses the supplied options are returns a `Flags` object to the caller. // // Parameters: -// * `w` - the `io.Writer` to use for printing messages (help, version, etc.) -// * `args` - the set of args the program was invoked with (typically `os.Args`) +// - `w` - the `io.Writer` to use for printing messages (help, version, etc.) +// - `args` - the set of args the program was invoked with (typically `os.Args`) func ParseFlags(w io.Writer, args []string) *Flags { f := Flags{appName: args[0], writer: w} diff --git a/cmd/protoc-gen-doc/main.go b/cmd/protoc-gen-doc/main.go index 56e42ce4..fabfeebc 100644 --- a/cmd/protoc-gen-doc/main.go +++ b/cmd/protoc-gen-doc/main.go @@ -4,11 +4,11 @@ // // Example: generate HTML documentation // -// protoc --doc_out=. --doc_opt=html,index.html protos/*.proto +// protoc --doc_out=. --doc_opt=html,index.html protos/*.proto // // Example: use a custom template // -// protoc --doc_out=. --doc_opt=custom.tmpl,docs.txt protos/*.proto +// protoc --doc_out=. --doc_opt=custom.tmpl,docs.txt protos/*.proto // // For more details, check out the README at https://github.com/pseudomuto/protoc-gen-doc package main @@ -20,9 +20,10 @@ import ( "os" gendoc "github.com/pseudomuto/protoc-gen-doc" - _ "github.com/pseudomuto/protoc-gen-doc/extensions/google_api_http" // imported for side effects - _ "github.com/pseudomuto/protoc-gen-doc/extensions/lyft_validate" // imported for side effects - _ "github.com/pseudomuto/protoc-gen-doc/extensions/validator_field" // imported for side effects + _ "github.com/pseudomuto/protoc-gen-doc/extensions/google_api_field_behavior" // imported for side effects + _ "github.com/pseudomuto/protoc-gen-doc/extensions/google_api_http" // imported for side effects + _ "github.com/pseudomuto/protoc-gen-doc/extensions/lyft_validate" // imported for side effects + _ "github.com/pseudomuto/protoc-gen-doc/extensions/validator_field" // imported for side effects ) func main() { diff --git a/doc.go b/doc.go index e9bee30c..df9f6622 100644 --- a/doc.go +++ b/doc.go @@ -5,15 +5,15 @@ // // Example: generate HTML documentation // -// protoc --doc_out=. --doc_opt=html,index.html protos/*.proto +// protoc --doc_out=. --doc_opt=html,index.html protos/*.proto // // Example: exclude patterns // -// protoc --doc_out=. --doc_opt=html,index.html:google/*,somedir/* protos/*.proto +// protoc --doc_out=. --doc_opt=html,index.html:google/*,somedir/* protos/*.proto // // Example: use a custom template // -// protoc --doc_out=. --doc_opt=custom.tmpl,docs.txt protos/*.proto +// protoc --doc_out=. --doc_opt=custom.tmpl,docs.txt protos/*.proto // // For more details, check out the README at https://github.com/pseudomuto/protoc-gen-doc package gendoc diff --git a/extensions/google_api_field_behavior/google_api_field_behavior.go b/extensions/google_api_field_behavior/google_api_field_behavior.go new file mode 100644 index 00000000..fa3423c3 --- /dev/null +++ b/extensions/google_api_field_behavior/google_api_field_behavior.go @@ -0,0 +1,30 @@ +package extensions + +import ( + "github.com/pseudomuto/protoc-gen-doc/extensions" + "google.golang.org/genproto/googleapis/api/annotations" +) + +type FieldBehaviorExtension struct { + Options []string `json:"options"` +} + +func init() { + extensions.SetTransformer("google.api.field_behavior", func(payload interface{}) interface{} { + fb, ok := payload.([]annotations.FieldBehavior) + if !ok { + return nil + } + + if len(fb) == 0 { + return nil + } + + fbs := make([]string, len(fb)) + for i, behavior := range fb { + fbs[i] = behavior.String() + } + + return FieldBehaviorExtension{Options: fbs} + }) +} diff --git a/extensions/google_api_field_behavior/google_api_field_behavior_test.go b/extensions/google_api_field_behavior/google_api_field_behavior_test.go new file mode 100644 index 00000000..af346ec5 --- /dev/null +++ b/extensions/google_api_field_behavior/google_api_field_behavior_test.go @@ -0,0 +1,19 @@ +package extensions_test + +import ( + "testing" + + "github.com/pseudomuto/protoc-gen-doc/extensions" + "github.com/stretchr/testify/require" + "google.golang.org/genproto/googleapis/api/annotations" +) + +func TestTransform(t *testing.T) { + behavior := []annotations.FieldBehavior{annotations.FieldBehavior_REQUIRED} + + transformed := extensions.Transform(map[string]interface{}{"google.api.field_behavior": behavior}) + require.NotEmpty(t, transformed) + + options := transformed["google.api.field_behavior"].(FieldBehaviorExtension).Options() + require.Equal(t, options, []string{annotations.FieldBehavior_REQUIRED.String()}) +}