-
Notifications
You must be signed in to change notification settings - Fork 2
/
referenceresolver.go
181 lines (135 loc) · 4.27 KB
/
referenceresolver.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
package simplecue
import (
"fmt"
"strings"
"cuelang.org/go/cue"
cueast "cuelang.org/go/cue/ast"
)
type referenceResolverConfig struct {
Libraries []LibraryInclude
}
type referenceResolver struct {
config referenceResolverConfig
importsAliasMap map[string]string // map[alias]packageName
librariesMap map[string]string // map[libraryImportPath]packageName
}
func newReferenceResolver(root cue.Value, config referenceResolverConfig) *referenceResolver {
resolver := &referenceResolver{
config: config,
}
resolver.importsAliasMap = resolver.buildImportsAliasMap(root)
resolver.librariesMap = resolver.buildLibrariesMap(config.Libraries)
return resolver
}
// FIXME: this is probably very brittle and not always correct :|
func (resolver *referenceResolver) PackageForNode(source cueast.Node, defaultPackage string) (string, error) {
switch source.(type) { //nolint: gocritic
case *cueast.SelectorExpr:
selector := source.(*cueast.SelectorExpr)
x := selector.X.(*cueast.Ident)
return resolver.resolveImportAlias(x.Name), nil
case *cueast.Field:
field := source.(*cueast.Field)
if _, ok := field.Value.(*cueast.SelectorExpr); ok {
return resolver.PackageForNode(field.Value, defaultPackage)
}
ident := field.Value.(*cueast.Ident)
if ident.Scope == nil {
return defaultPackage, nil
}
if _, ok := ident.Scope.(*cueast.File); !ok {
return defaultPackage, nil
}
scope := ident.Scope.(*cueast.File)
if len(scope.Decls) == 0 {
return defaultPackage, nil
}
if _, ok := scope.Decls[0].(*cueast.Package); !ok {
return defaultPackage, nil
}
referredTypePkg := scope.Decls[0].(*cueast.Package).Name
return resolver.resolveImportAlias(referredTypePkg.Name), nil
case *cueast.Ident:
ident := source.(*cueast.Ident)
if ident.Scope == nil {
return defaultPackage, nil
}
if _, ok := ident.Scope.(*cueast.File); !ok {
return defaultPackage, nil
}
scope := ident.Scope.(*cueast.File)
if len(scope.Decls) == 0 {
return defaultPackage, nil
}
if _, ok := ident.Scope.(*cueast.File).Decls[0].(*cueast.Package); !ok {
return defaultPackage, nil
}
referredTypePkg := ident.Scope.(*cueast.File).Decls[0].(*cueast.Package).Name
return resolver.resolveImportAlias(referredTypePkg.Name), nil
case *cueast.Ellipsis:
if source.(*cueast.Ellipsis).Type == nil {
return defaultPackage, nil
}
if _, ok := source.(*cueast.Ellipsis).Type.(*cueast.SelectorExpr); ok {
return resolver.PackageForNode(source.(*cueast.Ellipsis).Type, defaultPackage)
}
return resolver.packageForToken(source, defaultPackage), nil
default:
return "", fmt.Errorf("can't extract reference package")
}
}
func (resolver *referenceResolver) packageForToken(source cueast.Node, defaultPackage string) string {
filename := source.Pos().Filename()
if filename == "" {
return defaultPackage
}
for importPath, pkg := range resolver.librariesMap {
if strings.Contains(filename, importPath) {
return pkg
}
}
return defaultPackage
}
func (resolver *referenceResolver) resolveImportAlias(alias string) string {
if resolved, found := resolver.importsAliasMap[alias]; found {
return resolved
}
return alias
}
func (resolver *referenceResolver) buildImportsAliasMap(v cue.Value) map[string]string {
aliasMap := make(map[string]string)
syntax := v.Syntax()
if _, ok := syntax.(*cueast.File); !ok {
return aliasMap
}
file := syntax.(*cueast.File)
for _, decl := range file.Decls {
if _, ok := decl.(*cueast.ImportDecl); !ok {
continue
}
importDecl := decl.(*cueast.ImportDecl)
for _, spec := range importDecl.Specs {
pkgName := resolver.packageFromImportPath(spec.Path.Value)
alias := pkgName
if spec.Name != nil {
alias = spec.Name.Name
}
aliasMap[alias] = pkgName
}
}
return aliasMap
}
func (resolver *referenceResolver) buildLibrariesMap(libraries []LibraryInclude) map[string]string {
librariesMap := make(map[string]string)
for _, library := range libraries {
path := "cue.mod/pkg/" + library.ImportPath
pkg := resolver.packageFromImportPath(path)
librariesMap[path] = pkg
}
return librariesMap
}
func (resolver *referenceResolver) packageFromImportPath(importPath string) string {
importPath = strings.Trim(importPath, "\"")
parts := strings.Split(importPath, "/")
return parts[len(parts)-1]
}