Skip to content

Commit

Permalink
option to tweak generated Register* function names
Browse files Browse the repository at this point in the history
  • Loading branch information
jhump authored and achew22 committed Jun 13, 2018
1 parent a5b66c1 commit 78a473d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 39 deletions.
8 changes: 4 additions & 4 deletions examples/proto/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/proto/examplepb/echo_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/proto/examplepb/flow_combination.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/proto/examplepb/stream.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/proto/examplepb/unannotated_echo_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/proto/examplepb/wrappers.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions protoc-gen-grpc-gateway/gengateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ var (
)

type generator struct {
reg *descriptor.Registry
baseImports []descriptor.GoPackage
useRequestContext bool
reg *descriptor.Registry
baseImports []descriptor.GoPackage
useRequestContext bool
registerFuncSuffix string
}

// New returns a new generator which generates grpc gateway files.
func New(reg *descriptor.Registry, useRequestContext bool) gen.Generator {
func New(reg *descriptor.Registry, useRequestContext bool, registerFuncSuffix string) gen.Generator {
var imports []descriptor.GoPackage
for _, pkgpath := range []string{
"io",
Expand Down Expand Up @@ -56,7 +57,12 @@ func New(reg *descriptor.Registry, useRequestContext bool) gen.Generator {
}
imports = append(imports, pkg)
}
return &generator{reg: reg, baseImports: imports, useRequestContext: useRequestContext}
return &generator{
reg: reg,
baseImports: imports,
useRequestContext: useRequestContext,
registerFuncSuffix: registerFuncSuffix,
}
}

func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGeneratorResponse_File, error) {
Expand Down Expand Up @@ -110,5 +116,11 @@ func (g *generator) generate(file *descriptor.File) (string, error) {
imports = append(imports, pkg)
}
}
return applyTemplate(param{File: file, Imports: imports, UseRequestContext: g.useRequestContext})
params := param{
File: file,
Imports: imports,
UseRequestContext: g.useRequestContext,
RegisterFuncSuffix: g.registerFuncSuffix,
}
return applyTemplate(params)
}
33 changes: 18 additions & 15 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

type param struct {
*descriptor.File
Imports []descriptor.GoPackage
UseRequestContext bool
Imports []descriptor.GoPackage
UseRequestContext bool
RegisterFuncSuffix string
}

type binding struct {
Expand Down Expand Up @@ -68,8 +69,9 @@ func (f queryParamFilter) String() string {
}

type trailerParams struct {
Services []*descriptor.Service
UseRequestContext bool
Services []*descriptor.Service
UseRequestContext bool
RegisterFuncSuffix string
}

func applyTemplate(p param) (string, error) {
Expand Down Expand Up @@ -102,8 +104,9 @@ func applyTemplate(p param) (string, error) {
}

tp := trailerParams{
Services: targetServices,
UseRequestContext: p.UseRequestContext,
Services: targetServices,
UseRequestContext: p.UseRequestContext,
RegisterFuncSuffix: p.RegisterFuncSuffix,
}
if err := trailerTemplate.Execute(w, tp); err != nil {
return "", err
Expand Down Expand Up @@ -312,9 +315,9 @@ var (
trailerTemplate = template.Must(template.New("trailer").Parse(`
{{$UseRequestContext := .UseRequestContext}}
{{range $svc := .Services}}
// Register{{$svc.GetName}}HandlerFromEndpoint is same as Register{{$svc.GetName}}Handler but
// Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}FromEndpoint is same as Register{{$svc.GetName}}{{$.RegisterFuncSuffix}} but
// automatically dials to "endpoint" and closes the connection when "ctx" gets done.
func Register{{$svc.GetName}}HandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) {
func Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}FromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) {
conn, err := grpc.Dial(endpoint, opts...)
if err != nil {
return err
Expand All @@ -334,21 +337,21 @@ func Register{{$svc.GetName}}HandlerFromEndpoint(ctx context.Context, mux *runti
}()
}()
return Register{{$svc.GetName}}Handler(ctx, mux, conn)
return Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}(ctx, mux, conn)
}
// Register{{$svc.GetName}}Handler registers the http handlers for service {{$svc.GetName}} to "mux".
// Register{{$svc.GetName}}{{$.RegisterFuncSuffix}} registers the http handlers for service {{$svc.GetName}} to "mux".
// The handlers forward requests to the grpc endpoint over "conn".
func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return Register{{$svc.GetName}}HandlerClient(ctx, mux, New{{$svc.GetName}}Client(conn))
func Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Client(ctx, mux, New{{$svc.GetName}}Client(conn))
}
// Register{{$svc.GetName}}Handler registers the http handlers for service {{$svc.GetName}} to "mux".
// The handlers forward requests to the grpc endpoint over the given implementation of "{{$svc.GetName}}Client".
// Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Client registers the http handlers for service {{$svc.GetName}}
// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "{{$svc.GetName}}Client".
// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "{{$svc.GetName}}Client"
// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in
// "{{$svc.GetName}}Client" to call the correct interceptors.
func Register{{$svc.GetName}}HandlerClient(ctx context.Context, mux *runtime.ServeMux, client {{$svc.GetName}}Client) error {
func Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Client(ctx context.Context, mux *runtime.ServeMux, client {{$svc.GetName}}Client) error {
{{range $m := $svc.Methods}}
{{range $b := $m.Bindings}}
mux.Handle({{$b.HTTPMethod | printf "%q"}}, pattern_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
Expand Down
6 changes: 3 additions & 3 deletions protoc-gen-grpc-gateway/gengateway/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestApplyTemplateHeader(t *testing.T) {
},
},
}
got, err := applyTemplate(param{File: crossLinkFixture(&file)})
got, err := applyTemplate(param{File: crossLinkFixture(&file), RegisterFuncSuffix: "Handler"})
if err != nil {
t.Errorf("applyTemplate(%#v) failed with %v; want success", file, err)
return
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestApplyTemplateRequestWithoutClientStreaming(t *testing.T) {
},
},
}
got, err := applyTemplate(param{File: crossLinkFixture(&file)})
got, err := applyTemplate(param{File: crossLinkFixture(&file), RegisterFuncSuffix: "Handler"})
if err != nil {
t.Errorf("applyTemplate(%#v) failed with %v; want success", file, err)
return
Expand Down Expand Up @@ -383,7 +383,7 @@ func TestApplyTemplateRequestWithClientStreaming(t *testing.T) {
},
},
}
got, err := applyTemplate(param{File: crossLinkFixture(&file)})
got, err := applyTemplate(param{File: crossLinkFixture(&file), RegisterFuncSuffix: "Handler"})
if err != nil {
t.Errorf("applyTemplate(%#v) failed with %v; want success", file, err)
return
Expand Down
3 changes: 2 additions & 1 deletion protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
var (
importPrefix = flag.String("import_prefix", "", "prefix to be added to go package paths for imported proto files")
importPath = flag.String("import_path", "", "used as the package if no input files declare go_package. If it contains slashes, everything up to the rightmost slash is ignored.")
registerFuncSuffix = flag.String("register_func_suffix", "Handler", "used to construct names of generated Register*<Suffix> methods.")
useRequestContext = flag.Bool("request_context", true, "determine whether to use http.Request's context or not")
allowDeleteBody = flag.Bool("allow_delete_body", false, "unless set, HTTP DELETE methods may not have a body")
grpcAPIConfiguration = flag.String("grpc_api_configuration", "", "path to gRPC API Configuration in YAML format")
Expand Down Expand Up @@ -61,7 +62,7 @@ func main() {
}
}

g := gengateway.New(reg, *useRequestContext)
g := gengateway.New(reg, *useRequestContext, *registerFuncSuffix)

if *grpcAPIConfiguration != "" {
if err := reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil {
Expand Down

0 comments on commit 78a473d

Please sign in to comment.