-
Notifications
You must be signed in to change notification settings - Fork 68
/
proto.go
133 lines (114 loc) · 3.95 KB
/
proto.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package codegen
import (
"github.com/emicklei/proto"
)
// ProtoModule is an internal representation of a parsed Proto file.
type ProtoModule struct {
PackageName string
FilePath string
Imports []string
Services Services
}
// Services is list of ProtoServices
type Services []*ProtoService
func (a Services) Len() int {
return len(a)
}
func (a Services) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a Services) Less(i, j int) bool {
return a[i].Name < a[j].Name
}
// ProtoService is an internal representation of Proto service and methods in that service.
type ProtoService struct {
Name string
RPC []*ProtoRPC
}
// ProtoRPC is an internal representation of Proto RPC method and its request/response types.
type ProtoRPC struct {
Name string
Request *ProtoMessage
Response *ProtoMessage
}
// ProtoMessage is an internal representation of a Proto Message.
type ProtoMessage struct {
Name string
}
type visitor struct {
Module *ProtoModule
}
func newVisitor() *visitor {
return &visitor{
Module: new(ProtoModule),
}
}
func (v *visitor) Visit(proto *proto.Proto) *ProtoModule {
v.Module = &ProtoModule{
FilePath: proto.Filename,
Services: make([]*ProtoService, 0),
Imports: make([]string, 0),
}
for _, e := range proto.Elements {
e.Accept(v)
}
return v.Module
}
func (v *visitor) VisitService(e *proto.Service) {
v.Module.Services = append(v.Module.Services, &ProtoService{
Name: e.Name,
RPC: make([]*ProtoRPC, 0),
})
for _, c := range e.Elements {
c.Accept(v)
}
}
func (v *visitor) VisitRPC(r *proto.RPC) {
s := v.Module.Services[len(v.Module.Services)-1]
s.RPC = append(s.RPC, &ProtoRPC{
Name: r.Name,
Request: &ProtoMessage{Name: r.RequestType},
Response: &ProtoMessage{Name: r.ReturnsType},
})
}
func (v *visitor) VisitPackage(e *proto.Package) {
v.Module.PackageName = e.Name
}
func (v *visitor) VisitImport(e *proto.Import) {
v.Module.Imports = append(v.Module.Imports, e.Filename)
}
// From the current use case, the following visits are no-op
// since we only require the service, rpc methods and the request/response
// types of those methods.
func (v *visitor) VisitMessage(e *proto.Message) {}
func (v *visitor) VisitSyntax(e *proto.Syntax) {}
func (v *visitor) VisitOption(e *proto.Option) {}
func (v *visitor) VisitNormalField(e *proto.NormalField) {}
func (v *visitor) VisitEnumField(e *proto.EnumField) {}
func (v *visitor) VisitEnum(e *proto.Enum) {}
func (v *visitor) VisitComment(e *proto.Comment) {}
func (v *visitor) VisitOneof(o *proto.Oneof) {}
func (v *visitor) VisitOneofField(o *proto.OneOfField) {}
func (v *visitor) VisitReserved(r *proto.Reserved) {}
func (v *visitor) VisitMapField(f *proto.MapField) {}
func (v *visitor) VisitGroup(g *proto.Group) {}
func (v *visitor) VisitExtensions(e *proto.Extensions) {}