Skip to content

Commit

Permalink
Make support paths option
Browse files Browse the repository at this point in the history
Make protoc-gen-grpc-gateway support paths option such like golang/protobuf#515.
  • Loading branch information
izumin5210 committed Jul 30, 2018
1 parent 209d7ce commit 5130bb3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
24 changes: 22 additions & 2 deletions protoc-gen-grpc-gateway/gengateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion protoc-gen-grpc-gateway/gengateway/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
func TestGenerateOutputPath(t *testing.T) {
cases := []struct {
file *descriptor.File
pathType pathType
expected string
}{
{
Expand All @@ -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 {
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 @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5130bb3

Please sign in to comment.