forked from arachnys/protostub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
49 lines (41 loc) · 1.06 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
package protostub
import (
"github.com/emicklei/proto"
)
// ServiceVisitor will visit a service and extract the types.
type ServiceVisitor struct {
ProtoData
service *Service
}
// NewServiceVisitor will create a new service visitor, from a name and a comment.
func NewServiceVisitor(name string, comment *proto.Comment) *ServiceVisitor {
sv := &ServiceVisitor{
ProtoData: ProtoData{},
service: &Service{
name: name,
Types: make([]ProtoType, 0),
Functions: make([]Function, 0),
Comment: comment.Lines,
},
}
return sv
}
func (sv *ServiceVisitor) addFunction(f Function) {
sv.service.Functions = append(sv.service.Functions, f)
}
// VisitRPC will visit a RPC, and make sure the function specification is included
// in the service data definition.
func (sv *ServiceVisitor) VisitRPC(r *proto.RPC) {
if r.Comment == nil {
r.Comment = &proto.Comment{
Lines: make([]string, 0),
}
}
f := Function{
name: r.Name,
returnType: r.ReturnsType,
parameters: []string{r.RequestType},
Comment: r.Comment.Lines,
}
sv.addFunction(f)
}