Skip to content

Commit 7cd6afd

Browse files
committed
protoc-gen增加导出ServiceDesc方法插件desc
1 parent 30e84ff commit 7cd6afd

File tree

6 files changed

+74
-19
lines changed

6 files changed

+74
-19
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.12
55
require (
66
github.com/golang/protobuf v1.3.2
77
github.com/hashicorp/golang-lru v0.5.3 // indirect
8-
github.com/micro/go-micro v1.7.1-0.20190724203029-7ca8f8f0ab98
8+
github.com/micro/go-micro v1.8.0
99
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
1010
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect
1111
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
171171
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
172172
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
173173
github.com/micro/cli v0.2.0/go.mod h1:jRT9gmfVKWSS6pkKcXQ8YhUyj6bzwxK8Fp5b0Y7qNnk=
174-
github.com/micro/go-micro v1.7.1-0.20190724203029-7ca8f8f0ab98 h1:n99uuhM5kq8UlwpkD4ZCpVz7QB+VkzlEq/5pSPUuJfQ=
175-
github.com/micro/go-micro v1.7.1-0.20190724203029-7ca8f8f0ab98/go.mod h1:wyjw4MrxhEdQ3kVLcyXklmYKV0HjYm9F2E1YNuORRl4=
176-
github.com/micro/mdns v0.1.0 h1:fuLybUsfynbigJmCot/54i+gwe0hpc/vtCMvWt2WfDI=
177-
github.com/micro/mdns v0.1.0/go.mod h1:KJ0dW7KmicXU2BV++qkLlmHYcVv7/hHnbtguSWt9Aoc=
174+
github.com/micro/go-micro v1.8.0 h1:4etDwf3DqP61cM0xrI+3OP+FlAsRUnM2rwBXFRTrl+Y=
175+
github.com/micro/go-micro v1.8.0/go.mod h1:RUJTXVb+qUnGx6jBK2yMCOzuB/xEKh/vdZc3uagM5TI=
176+
github.com/micro/mdns v0.1.1-0.20190729112526-ef68c9635478 h1:L6jnZZ763dMLlvst8P0dWHa1WbUu7ppUY1q3AY2hhIU=
177+
github.com/micro/mdns v0.1.1-0.20190729112526-ef68c9635478/go.mod h1:KJ0dW7KmicXU2BV++qkLlmHYcVv7/hHnbtguSWt9Aoc=
178178
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
179179
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
180180
github.com/miekg/dns v1.1.15 h1:CSSIDtllwGLMoA6zjdKnaE6Tx6eVUxQ29LUgGetiDCI=

proto/service.hb.grpc.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-hb-grpc/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"github.com/golang/protobuf/proto"
5656

5757
"github.com/hb-go/grpc-contrib/protoc-gen-hb-grpc/generator"
58+
_ "github.com/hb-go/grpc-contrib/protoc-gen-hb-grpc/plugin/desc"
5859
_ "github.com/hb-go/grpc-contrib/protoc-gen-hb-grpc/plugin/registry"
5960
)
6061

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package desc
2+
3+
import (
4+
pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
5+
"github.com/hb-go/grpc-contrib/protoc-gen-hb-grpc/generator"
6+
)
7+
8+
func init() {
9+
generator.RegisterPlugin(new(exportDesc))
10+
}
11+
12+
type exportDesc struct {
13+
gen *generator.Generator
14+
}
15+
16+
// Name returns the name of this plugin, "desc".
17+
func (g *exportDesc) Name() string {
18+
return "desc"
19+
}
20+
21+
// The names for packages imported in the generated code.
22+
// They may vary from the final path component of the import path
23+
// if the name is used by other packages.
24+
var (
25+
pkgImports map[generator.GoPackageName]bool
26+
)
27+
28+
// Init initializes the plugin.
29+
func (g *exportDesc) Init(gen *generator.Generator) {
30+
g.gen = gen
31+
}
32+
33+
// P forwards to g.gen.P.
34+
func (g *exportDesc) P(args ...interface{}) { g.gen.P(args...) }
35+
36+
// Generate generates code for the services in the given file.
37+
func (g *exportDesc) Generate(file *generator.FileDescriptor) {
38+
if len(file.FileDescriptorProto.Service) == 0 {
39+
return
40+
}
41+
42+
g.P()
43+
g.P("// Export service desc")
44+
45+
for i, service := range file.FileDescriptorProto.Service {
46+
g.generateService(file, service, i)
47+
}
48+
}
49+
50+
func (g *exportDesc) GenerateImports(file *generator.FileDescriptor) {
51+
52+
}
53+
54+
func (g *exportDesc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
55+
g.P()
56+
g.P("// " + service.GetName() + " desc")
57+
g.P("func ServiceDesc" + service.GetName() + "() *grpc.ServiceDesc {")
58+
g.P("return &_" + service.GetName() + "_serviceDesc")
59+
g.P("}")
60+
}

protoc-gen-hb-grpc/plugin/registry/registry.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type grpcRegistry struct {
2020
gen *generator.Generator
2121
}
2222

23-
// Name returns the name of this plugin, "hb-grpc".
23+
// Name returns the name of this plugin, "registry".
2424
func (g *grpcRegistry) Name() string {
2525
return "registry"
2626
}
@@ -30,7 +30,6 @@ func (g *grpcRegistry) Name() string {
3030
// if the name is used by other packages.
3131
var (
3232
registryPkg string
33-
pkgImports map[generator.GoPackageName]bool
3433
)
3534

3635
// Init initializes the plugin.
@@ -39,18 +38,6 @@ func (g *grpcRegistry) Init(gen *generator.Generator) {
3938
registryPkg = generator.RegisterUniquePackageName("registry", nil)
4039
}
4140

42-
// Given a type name defined in a .proto, return its object.
43-
// Also record that we're using it, to guarantee the associated import.
44-
func (g *grpcRegistry) objectNamed(name string) generator.Object {
45-
g.gen.RecordTypeUse(name)
46-
return g.gen.ObjectNamed(name)
47-
}
48-
49-
// Given a type name defined in a .proto, return its name as we will print it.
50-
func (g *grpcRegistry) typeName(str string) string {
51-
return g.gen.TypeName(g.objectNamed(str))
52-
}
53-
5441
// P forwards to g.gen.P.
5542
func (g *grpcRegistry) P(args ...interface{}) { g.gen.P(args...) }
5643

0 commit comments

Comments
 (0)