From 5130bb3364e3a6b22752b584e34eb684d63bfcb6 Mon Sep 17 00:00:00 2001 From: izumin5210 Date: Mon, 30 Jul 2018 09:53:33 +0900 Subject: [PATCH] Make support paths option Make protoc-gen-grpc-gateway support paths option such like golang/protobuf#515. --- .../gengateway/generator.go | 24 +++++++++++++++++-- .../gengateway/generator_test.go | 24 ++++++++++++++++++- protoc-gen-grpc-gateway/main.go | 3 ++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/protoc-gen-grpc-gateway/gengateway/generator.go b/protoc-gen-grpc-gateway/gengateway/generator.go index bccd39e0405..a5ed4ede518 100644 --- a/protoc-gen-grpc-gateway/gengateway/generator.go +++ b/protoc-gen-grpc-gateway/gengateway/generator.go @@ -19,15 +19,23 @@ var ( errNoTargetService = errors.New("no target service defined in the file") ) +type pathType int + +const ( + pathTypeImport pathType = iota + pathTypeSourceRelative +) + type generator struct { reg *descriptor.Registry baseImports []descriptor.GoPackage useRequestContext bool registerFuncSuffix string + pathType pathType } // New returns a new generator which generates grpc gateway files. -func New(reg *descriptor.Registry, useRequestContext bool, registerFuncSuffix string) gen.Generator { +func New(reg *descriptor.Registry, useRequestContext bool, registerFuncSuffix, pathTypeString string) gen.Generator { var imports []descriptor.GoPackage for _, pkgpath := range []string{ "io", @@ -57,11 +65,23 @@ func New(reg *descriptor.Registry, useRequestContext bool, registerFuncSuffix st } imports = append(imports, pkg) } + + var pathType pathType + switch pathTypeString { + case "", "import": + // paths=import is default + case "source_relative": + pathType = pathTypeSourceRelative + default: + glog.Fatalf(`Unknown path type %q: want "import" or "source_relative".`, pathTypeString) + } + return &generator{ reg: reg, baseImports: imports, useRequestContext: useRequestContext, registerFuncSuffix: registerFuncSuffix, + pathType: pathType, } } @@ -83,7 +103,7 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGenerato return nil, err } name := file.GetName() - if file.GoPkg.Path != "" { + if g.pathType == pathTypeImport && file.GoPkg.Path != "" { name = fmt.Sprintf("%s/%s", file.GoPkg.Path, filepath.Base(name)) } ext := filepath.Ext(name) diff --git a/protoc-gen-grpc-gateway/gengateway/generator_test.go b/protoc-gen-grpc-gateway/gengateway/generator_test.go index 986ff4151e5..39e5f141dbe 100644 --- a/protoc-gen-grpc-gateway/gengateway/generator_test.go +++ b/protoc-gen-grpc-gateway/gengateway/generator_test.go @@ -101,6 +101,7 @@ func TestGenerateServiceWithoutBindings(t *testing.T) { func TestGenerateOutputPath(t *testing.T) { cases := []struct { file *descriptor.File + pathType pathType expected string }{ { @@ -121,10 +122,31 @@ func TestGenerateOutputPath(t *testing.T) { ), expected: "example", }, + { + file: newExampleFileDescriptorWithGoPkg( + &descriptor.GoPackage{ + Path: "example.com/path/to/example", + Name: "example_pb", + }, + ), + pathType: pathTypeSourceRelative, + expected: ".", + }, + { + file: newExampleFileDescriptorWithGoPkg( + &descriptor.GoPackage{ + Path: "example", + Name: "example_pb", + }, + ), + pathType: pathTypeSourceRelative, + expected: ".", + }, } - g := &generator{} for _, c := range cases { + g := &generator{pathType: c.pathType} + file := c.file gots, err := g.Generate([]*descriptor.File{crossLinkFixture(file)}) if err != nil { diff --git a/protoc-gen-grpc-gateway/main.go b/protoc-gen-grpc-gateway/main.go index 7ef4aa1b5b6..b9d2dcfca6f 100644 --- a/protoc-gen-grpc-gateway/main.go +++ b/protoc-gen-grpc-gateway/main.go @@ -28,6 +28,7 @@ var ( 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") + pathType = flag.String("paths", "", "specifies how the paths of generated files are structured") ) func main() { @@ -62,7 +63,7 @@ func main() { } } - g := gengateway.New(reg, *useRequestContext, *registerFuncSuffix) + g := gengateway.New(reg, *useRequestContext, *registerFuncSuffix, *pathType) if *grpcAPIConfiguration != "" { if err := reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil {