Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add field behavior #514

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/protoc-gen-doc/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
11 changes: 6 additions & 5 deletions cmd/protoc-gen-doc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions extensions/google_api_field_behavior/google_api_field_behavior.go
Original file line number Diff line number Diff line change
@@ -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}
})
}
Original file line number Diff line number Diff line change
@@ -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()})
}