-
Notifications
You must be signed in to change notification settings - Fork 150
/
protoc-gen-grpc-gateway.go
61 lines (52 loc) · 1.81 KB
/
protoc-gen-grpc-gateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package grpcgateway
import (
"path"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/stackb/rules_proto/pkg/protoc"
)
func init() {
protoc.Plugins().MustRegisterPlugin(&protocGenGrpcGatewayPlugin{})
}
// protocGenGrpcGatewayPlugin implements Plugin for protoc-gen-grpc-gateway.
type protocGenGrpcGatewayPlugin struct{}
// Name implements part of the Plugin interface.
func (p *protocGenGrpcGatewayPlugin) Name() string {
return "grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway"
}
// Configure implements part of the Plugin interface.
func (p *protocGenGrpcGatewayPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration {
generateUnbound := ctx.PluginConfig.Options["generate_unbound_methods=true"]
if !p.shouldApply(ctx.ProtoLibrary, generateUnbound) {
return nil
}
return &protoc.PluginConfiguration{
Label: label.New("build_stack_rules_proto", "plugin/grpc-ecosystem/grpc-gateway", "protoc-gen-grpc-gateway"),
Outputs: p.outputs(ctx.Rel, ctx.ProtoLibrary, generateUnbound),
Options: ctx.PluginConfig.GetOptions(),
}
}
func (p *protocGenGrpcGatewayPlugin) shouldApply(lib protoc.ProtoLibrary, generateUnbound bool) bool {
for _, f := range lib.Files() {
if p.shouldOutputForFile(f, generateUnbound) {
return true
}
}
return false
}
func (p *protocGenGrpcGatewayPlugin) outputs(rel string, lib protoc.ProtoLibrary, generateUnbound bool) []string {
srcs := make([]string, 0)
for _, f := range lib.Files() {
if !p.shouldOutputForFile(f, generateUnbound) {
continue
}
base := f.Name
if rel != "" {
base = path.Join(rel, base)
}
srcs = append(srcs, base+".pb.gw.go")
}
return srcs
}
func (p *protocGenGrpcGatewayPlugin) shouldOutputForFile(f *protoc.File, generateUnbound bool) bool {
return f.HasRPCs() && (generateUnbound || f.HasRPCOption("(google.api.http)"))
}