/
parser.go
112 lines (103 loc) · 2.87 KB
/
parser.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
package parser
import (
"os"
"path/filepath"
"github.com/emicklei/proto"
"github.com/pkg/errors"
)
type Parser struct {
parsedFiles []*File
}
func (p *Parser) ParsedFiles() []*File {
return p.parsedFiles
}
func (p *Parser) parsedFile(filePath string) (*File, bool) {
for _, f := range p.parsedFiles {
if f.FilePath == filePath {
return f, true
}
}
return nil, false
}
func (p *Parser) importFilePath(filename string, importsAliases []map[string]string, paths []string) (filePath string, err error) {
for _, aliases := range importsAliases {
if v, ok := aliases[filename]; ok {
filename = v
break
}
}
for _, path := range paths {
p := filepath.Join(path, filename)
if _, err := os.Stat(p); err == nil {
return p, nil
}
}
return "", errors.Errorf("can't find import %s in any of %s", filename, paths)
}
func (p *Parser) parseFileImports(file *File, importsAliases []map[string]string, paths []string) error {
for _, v := range file.protoFile.Elements {
imprt, ok := v.(*proto.Import)
if !ok {
continue
}
imprtPath, err := p.importFilePath(imprt.Filename, importsAliases, paths)
if err != nil {
return errors.Wrapf(err, "failed to resolve import(%s) File path", imprt.Filename)
}
absImprtPath, err := filepath.Abs(imprtPath)
if err != nil {
return errors.Wrapf(err, "failed to resolve import(%s) absolute File path", imprt.Filename)
}
if fl, ok := p.parsedFile(absImprtPath); ok {
file.Imports = append(file.Imports, fl)
continue
}
importFile, err := p.Parse(absImprtPath, importsAliases, paths)
if err != nil {
return errors.Wrapf(err, "can't parse import %s", imprtPath)
}
file.Imports = append(file.Imports, importFile)
}
return nil
}
func (p *Parser) Parse(path string, importAliases []map[string]string, paths []string) (*File, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, errors.Wrap(err, "failed to resolve File absolute path")
}
if pf, ok := p.parsedFile(absPath); ok {
return pf, nil
}
file, err := os.Open(absPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to open File")
}
f, err := proto.NewParser(file).Parse()
if err != nil {
return nil, errors.Wrap(err, "failed to parse File")
}
result := &File{
FilePath: absPath,
protoFile: f,
PkgName: resolveFilePkgName(f),
Services: map[string]*Service{},
Descriptors: map[string]Type{},
}
result.parseGoPackage()
err = p.parseFileImports(result, importAliases, paths)
if err != nil {
return nil, errors.Wrap(err, "failed to parse File imports")
}
result.parseMessages()
result.parseEnums()
err = result.parseServices()
if err != nil {
return nil, errors.Wrap(err, "failed to parse File services")
}
err = result.parseMessagesFields()
if err != nil {
return nil, errors.Wrap(err, "failed to parse messages fields")
}
p.parsedFiles = append(p.parsedFiles, result)
return result, nil
}