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

Make support paths option #711

Merged
merged 1 commit into from
Aug 2, 2018
Merged
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
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