forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
104 lines (84 loc) · 2.58 KB
/
service.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pgs
import (
"google.golang.org/protobuf/runtime/protoimpl"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)
// Service describes a proto service definition (typically, gRPC)
type Service interface {
Entity
// Descriptor returns the underlying proto descriptor for this service
Descriptor() *descriptor.ServiceDescriptorProto
// Methods returns each rpc method exposed by this service
Methods() []Method
setFile(f File)
addMethod(m Method)
}
type service struct {
desc *descriptor.ServiceDescriptorProto
methods []Method
file File
fqn string
info SourceCodeInfo
}
func (s *service) Name() Name { return Name(s.desc.GetName()) }
func (s *service) FullyQualifiedName() string { return s.fqn }
func (s *service) Syntax() Syntax { return s.file.Syntax() }
func (s *service) Package() Package { return s.file.Package() }
func (s *service) File() File { return s.file }
func (s *service) BuildTarget() bool { return s.file.BuildTarget() }
func (s *service) SourceCodeInfo() SourceCodeInfo { return s.info }
func (s *service) Descriptor() *descriptor.ServiceDescriptorProto { return s.desc }
func (s *service) Extension(desc *protoimpl.ExtensionInfo, ext interface{}) (bool, error) {
return extension(s.desc.GetOptions(), desc, &ext)
}
func (s *service) Imports() (i []File) {
// Mapping for avoiding duplicate entries
mp := make(map[string]File, len(s.methods))
for _, m := range s.methods {
for _, imp := range m.Imports() {
mp[imp.File().Name().String()] = imp
}
}
for _, f := range mp {
i = append(i, f)
}
return
}
func (s *service) Methods() []Method {
m := make([]Method, len(s.methods))
copy(m, s.methods)
return m
}
func (s *service) setFile(f File) { s.file = f }
func (s *service) addMethod(m Method) {
m.setService(s)
s.methods = append(s.methods, m)
}
func (s *service) accept(v Visitor) (err error) {
if v == nil {
return
}
if v, err = v.VisitService(s); err != nil || v == nil {
return
}
for _, m := range s.methods {
if err = m.accept(v); err != nil {
return
}
}
return
}
func (s *service) childAtPath(path []int32) Entity {
switch {
case len(path) == 0:
return s
case len(path)%2 != 0:
return nil
case path[0] == serviceTypeMethodPath:
return s.methods[path[1]].childAtPath(path[2:])
default:
return nil
}
}
func (s *service) addSourceCodeInfo(info SourceCodeInfo) { s.info = info }
var _ Service = (*service)(nil)