forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
240 lines (194 loc) · 5.7 KB
/
file.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package pgs
import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
// File describes the contents of a single proto file.
type File interface {
ParentEntity
// InputPath returns the input FilePath. This is equivalent to the value
// returned by Name.
InputPath() FilePath
// Descriptor returns the underlying descriptor for the proto file
Descriptor() *descriptor.FileDescriptorProto
// Dependents returns all files where the given file was directly or
// transitively imported.
Dependents() []File
// Services returns the services from this proto file.
Services() []Service
// SyntaxSourceCodeInfo returns the comment info attached to the `syntax`
// stanza of the file. This method is an alias of the SourceCodeInfo method.
SyntaxSourceCodeInfo() SourceCodeInfo
// PackageSourceCodeInfo returns the comment info attached to the `package`
// stanza of the file.
PackageSourceCodeInfo() SourceCodeInfo
setPackage(p Package)
addFileDependency(fl File)
addDependent(fl File)
addService(s Service)
addPackageSourceCodeInfo(info SourceCodeInfo)
}
type file struct {
desc *descriptor.FileDescriptorProto
fqn string
pkg Package
enums []Enum
defExts []Extension
dependents []File
dependentsCache []File
fileDependencies []File
msgs []Message
srvs []Service
buildTarget bool
syntaxInfo, packageInfo SourceCodeInfo
}
func (f *file) Name() Name { return Name(f.desc.GetName()) }
func (f *file) FullyQualifiedName() string { return f.fqn }
func (f *file) Syntax() Syntax { return Syntax(f.desc.GetSyntax()) }
func (f *file) Package() Package { return f.pkg }
func (f *file) File() File { return f }
func (f *file) BuildTarget() bool { return f.buildTarget }
func (f *file) Descriptor() *descriptor.FileDescriptorProto { return f.desc }
func (f *file) InputPath() FilePath { return FilePath(f.Name().String()) }
func (f *file) MapEntries() (me []Message) { return nil }
func (f *file) SourceCodeInfo() SourceCodeInfo { return f.SyntaxSourceCodeInfo() }
func (f *file) SyntaxSourceCodeInfo() SourceCodeInfo { return f.syntaxInfo }
func (f *file) PackageSourceCodeInfo() SourceCodeInfo { return f.packageInfo }
func (f *file) Enums() []Enum {
return f.enums
}
func (f *file) AllEnums() []Enum {
es := f.Enums()
for _, m := range f.msgs {
es = append(es, m.AllEnums()...)
}
return es
}
func (f *file) Messages() []Message {
return f.msgs
}
func (f *file) AllMessages() []Message {
msgs := f.Messages()
for _, m := range f.msgs {
msgs = append(msgs, m.AllMessages()...)
}
return msgs
}
func (f *file) Services() []Service {
return f.srvs
}
func (f *file) Imports() (i []File) {
// Mapping for avoiding duplicate entries
importMap := make(map[string]File, len(f.AllMessages())+len(f.srvs))
for _, fl := range f.fileDependencies {
importMap[fl.Name().String()] = fl
for _, imp := range fl.Imports() {
importMap[imp.File().Name().String()] = imp
}
}
for _, imp := range importMap {
i = append(i, imp)
}
return
}
func (f *file) Dependents() []File {
if f.dependentsCache == nil {
set := make(map[string]File)
for _, fl := range f.dependents {
set[fl.Name().String()] = fl
for _, d := range fl.Dependents() {
set[d.Name().String()] = d
}
}
f.dependentsCache = make([]File, 0, len(set))
for _, d := range set {
f.dependentsCache = append(f.dependentsCache, d)
}
}
return f.dependentsCache
}
func (f *file) Extension(desc *proto.ExtensionDesc, ext interface{}) (bool, error) {
return extension(f.desc.GetOptions(), desc, &ext)
}
func (f *file) DefinedExtensions() []Extension {
return f.defExts
}
func (f *file) accept(v Visitor) (err error) {
if v == nil {
return nil
}
if v, err = v.VisitFile(f); err != nil || v == nil {
return
}
for _, e := range f.enums {
if err = e.accept(v); err != nil {
return
}
}
for _, m := range f.msgs {
if err = m.accept(v); err != nil {
return
}
}
for _, s := range f.srvs {
if err = s.accept(v); err != nil {
return
}
}
for _, ext := range f.defExts {
if err = ext.accept(v); err != nil {
return
}
}
return
}
func (f *file) addDefExtension(ext Extension) {
f.defExts = append(f.defExts, ext)
}
func (f *file) setPackage(pkg Package) { f.pkg = pkg }
func (f *file) addEnum(e Enum) {
e.setParent(f)
f.enums = append(f.enums, e)
}
func (f *file) addFileDependency(fl File) {
f.fileDependencies = append(f.fileDependencies, fl)
}
func (f *file) addDependent(fl File) {
f.dependents = append(f.dependents, fl)
}
func (f *file) addMessage(m Message) {
m.setParent(f)
f.msgs = append(f.msgs, m)
}
func (f *file) addService(s Service) {
s.setFile(f)
f.srvs = append(f.srvs, s)
}
func (f *file) addMapEntry(m Message) { panic("cannot add map entry directly to file") }
func (f *file) childAtPath(path []int32) Entity {
switch {
case len(path) == 0:
return f
case len(path)%2 == 1: // all declaration paths are multiples of two
return nil
}
var child Entity
switch path[0] {
case messageTypePath:
child = f.msgs[path[1]]
case enumTypePath:
child = f.enums[path[1]]
case servicePath:
child = f.srvs[path[1]]
default:
return nil
}
return child.childAtPath(path[2:])
}
func (f *file) addSourceCodeInfo(info SourceCodeInfo) {
f.syntaxInfo = info
}
func (f *file) addPackageSourceCodeInfo(info SourceCodeInfo) {
f.packageInfo = info
}
var _ File = (*file)(nil)