forked from sirkon/protoast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
297 lines (257 loc) · 8.17 KB
/
builder.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package protoast
import (
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"text/scanner"
"github.com/emicklei/proto"
"github.com/ricdeau/protoast/internal/errors"
"github.com/ricdeau/protoast/internal/namespace"
"github.com/ricdeau/protoast/ast"
)
// NewBuilder конструктор построителя AST-представления
func NewBuilder(imports Files, errProcessing func(error)) *Builder {
return newBuilderWithCustomNaming(imports, errProcessing, defaultNaming)
}
func defaultNaming(first, last string) string {
return first + "." + last
}
func newBuilderWithCustomNaming(imports Files, errProcessing func(error), scopeNaming func(string, string) string) *Builder {
return &Builder{
protos: &protos{
files: imports,
trees: map[string]*proto.Proto{},
},
nsBuilder: namespace.NewBuilderNaming(scopeNaming),
asts: map[string]*ast.File{},
finalNses: map[string]Namespace{},
errProcessing: errProcessing,
comments: map[string]*ast.Comment{},
positions: map[string]scanner.Position{},
uniqueContext: ast.UniqueContext{},
imports: imports,
}
}
// StringRef создаёт ссылку на строку
func StringRef(value string) *string {
return &value
}
// Builder построитель структурированной информации
type Builder struct {
protos *protos
nsBuilder *namespace.Builder
asts map[string]*ast.File
finalNses map[string]Namespace
errProcessing func(error)
comments map[string]*ast.Comment
positions map[string]scanner.Position
uniqueContext ast.UniqueContext
imports Files
errCount int
}
// SameDirProtos Отдать список все файлы из директории содержащей указанный файл, включая данный.
//
// Deprecated: лучше использовать SamePackage
func (s *Builder) SameDirProtos(file *ast.File) ([]*ast.File, error) {
abs, err := s.protos.files.Abs(file.Name)
if err != nil {
return nil, errors.Wrapf(err, "get absolute path name for the file")
}
dir, _ := filepath.Split(abs)
lst, err := os.ReadDir(dir)
if err != nil {
return nil, errors.Wrap(err, "read file's directory "+dir)
}
var res []*ast.File
relDir, _ := filepath.Split(file.Name)
for _, info := range lst {
if info.IsDir() {
continue
}
if !strings.HasSuffix(info.Name(), ".proto") {
continue
}
_, base := filepath.Split(info.Name())
newFile, err := s.AST(filepath.Join(relDir, base))
if err != nil {
return nil, err
}
res = append(res, newFile)
}
return res, nil
}
// SamePackage отдать все файлы пакета для данного файла
func (s *Builder) SamePackage(file *ast.File) (*ast.Package, error) {
files, err := s.SameDirProtos(file)
if err != nil {
return nil, errors.Wrap(err, "get list of proto files from the same directory")
}
res, err := ast.NewPackage(files)
if err != nil {
return nil, errors.Wrap(err, "form package of the given file")
}
return res, nil
}
// Package отдать все файлы пакета для proto-файлов из данной директории.
// Будет работать только для резолвера-функции. Если в директории нет файлов
// то будет возвращена ошибка ast.ErrorPackageMissingFiles
func (s *Builder) Package(dir string) (*ast.Package, error) {
abs, err := s.imports.Abs(dir)
if err != nil {
return nil, errors.Wrap(err, "get absolute path for the directory")
}
readDir, err := os.ReadDir(abs)
if err != nil {
return nil, errors.Wrap(err, "list files in the directory")
}
var files []*ast.File
for _, entry := range readDir {
if entry.IsDir() {
continue
}
if !strings.HasSuffix(entry.Name(), ".proto") {
continue
}
file, err := s.AST(path.Join(dir, entry.Name()))
if err != nil {
return nil, errors.Wrap(err, "get AST for "+entry.Name())
}
files = append(files, file)
}
res, err := ast.NewPackage(files)
if err != nil {
return nil, errors.Wrap(err, "form proto-package for directory "+dir)
}
return res, nil
}
// Comment возвращает комментарий для сущности реализующей Unique
func (s *Builder) Comment(k ast.Unique) *ast.Comment {
return s.comments[ast.GetUnique(k)]
}
// CommentField возвращает комментарий для поля сущности реализующей Unique
func (s *Builder) CommentField(k ast.Unique, fieldAddr interface{}) *ast.Comment {
return s.comments[ast.GetFieldKey(k, fieldAddr)]
}
// Position возвращает позицию данного Unique
func (s *Builder) Position(k ast.Unique) scanner.Position {
res, ok := s.positions[ast.GetUnique(k)]
var name string
switch v := k.(type) {
case *ast.Message:
name = fmt.Sprintf("message %s::%s.%s %s", v.File.Name, v.File.Package, v.Name, ast.GetUnique(k))
case *ast.Enum:
name = fmt.Sprintf("enum %s.%s", v.File.Package, v.Name)
case *ast.Service:
name = fmt.Sprintf("service %s.%s", v.File.Package, v.Name)
case *ast.Extension:
name = fmt.Sprintf("extension %s.%s", v.File.Package, v.Name)
default:
name = fmt.Sprintf("%T", v)
}
if !ok {
panic(errors.New(name))
}
return res
}
// PositionField возвращает позицию данного для поля данного Uniq
func (s *Builder) PositionField(k ast.Unique, fieldAddr interface{}) scanner.Position {
res, ok := s.positions[ast.GetFieldKey(k, fieldAddr)]
if !ok {
val := reflect.ValueOf(k).Elem()
addr := reflect.ValueOf(fieldAddr).Pointer()
for i := 0; i < val.NumField(); i++ {
if val.Field(i).Addr().Pointer() == addr {
panic(errors.Newf("no position was set for %T.%s", k, val.Type().Field(i).Name))
}
}
panic("must not be here")
}
return res
}
// AST представление для файла с данным относительным путём
func (s *Builder) AST(importPath string) (*ast.File, error) {
_, res, err := s.get(importPath)
return res, err
}
// Namespace получение пространства имён для данного файла
func (s *Builder) Namespace(importPath string) (Namespace, error) {
_, _, err := s.get(importPath)
if err != nil {
return nil, errors.Wrap(err, "get namespace for the file")
}
return s.finalNses[importPath], nil
}
// Scope пространство имён для данной области видимости
func (s *Builder) Scope(access string) Namespace {
return s.nsBuilder.Get(access)
}
// Proto представление для прохода визиторами из github.com/emicklei/proto
func (s *Builder) Proto(importPath string) (*proto.Proto, error) {
return s.protos.fileProto(importPath)
}
func (s *Builder) get(importPath string) (namespace.Namespace, *ast.File, error) {
ns := s.nsBuilder.Get(importPath)
if ns.Finalized() {
return ns, s.asts[importPath], nil
}
protoItem, err := s.protos.fileProto(importPath)
if err != nil {
return nil, nil, errors.Wrap(err, "get proto file data")
}
if err = s.processFile(ns, protoItem, importPath); err != nil {
return nil, nil, errors.Wrap(err, "process proto file data")
}
ns.Finalize()
return ns, s.asts[importPath], nil
}
func (s *Builder) processFile(ns namespace.Namespace, p *proto.Proto, importPath string) error {
errChan := make(chan error)
pf := prefetcher{
file: &ast.File{
Name: importPath,
},
ns: ns,
nss: s,
errors: s.processError,
}
v := typesVisitor{
file: pf.file,
ns: ns,
nss: s,
errors: s.errProcessing,
enumCtx: struct {
item *ast.Enum
prevField map[string]scanner.Position
prevInteger map[int]scanner.Position
}{},
msgCtx: struct {
onMsg bool
item *ast.Message
prevField map[string]scanner.Position
prevInteger map[int]scanner.Position
}{},
oneOf: nil,
}
p.Accept(&pf)
p.Accept(&v)
close(errChan)
s.asts[importPath] = v.file
s.finalNses[importPath] = v.ns
switch s.errCount {
case 0:
return nil
case 1:
return errors.New("an error occured")
default:
return errors.Newf("%d errors occured", s.errCount)
}
}
func (s *Builder) processError(err error) {
if err != nil {
s.errCount++
}
s.errProcessing(err)
}