Skip to content

Commit 75d4005

Browse files
committed
cue/load: CUE files only contain a single *ast.File
By way of preparation for syntax caching, update the syntax cache to assume that a CUE file only ever corresponds to a single `*ast.File`. Currently it copes with a list of files, which makes the code a bit more complex than it needs to be. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Iad13aaf5f95505a84982608f08c5ab8d15178ea4 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197526 Reviewed-by: Paul Jolly <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 14deefa commit 75d4005

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

cue/load/instances.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,19 @@ func loadPackages(
205205
// not a CUE file; assume it has no imports for now.
206206
continue
207207
}
208-
syntaxes, err := synCache.getSyntax(f)
208+
syntax, err := synCache.getSyntax(f)
209209
if err != nil {
210210
return nil, fmt.Errorf("cannot get syntax for %q: %v", f.Filename, err)
211211
}
212-
for _, syntax := range syntaxes {
213-
for _, imp := range syntax.Imports {
214-
pkgPath, err := strconv.Unquote(imp.Path.Value)
215-
if err != nil {
216-
// Should never happen.
217-
return nil, fmt.Errorf("invalid import path %q in %s", imp.Path.Value, f.Filename)
218-
}
219-
// Canonicalize the path.
220-
pkgPath = module.ParseImportPath(pkgPath).Canonical().String()
221-
pkgPaths[pkgPath] = true
212+
for _, imp := range syntax.Imports {
213+
pkgPath, err := strconv.Unquote(imp.Path.Value)
214+
if err != nil {
215+
// Should never happen.
216+
return nil, fmt.Errorf("invalid import path %q in %s", imp.Path.Value, f.Filename)
222217
}
218+
// Canonicalize the path.
219+
pkgPath = module.ParseImportPath(pkgPath).Canonical().String()
220+
pkgPaths[pkgPath] = true
223221
}
224222
}
225223
// TODO use maps.Keys when we can.

cue/load/loader.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,11 @@ func (l *loader) cueFilesPackage(files []*build.File) *build.Instance {
148148
// addFiles populates p.Files by reading CUE syntax from p.BuildFiles.
149149
func (l *loader) addFiles(p *build.Instance) {
150150
for _, bf := range p.BuildFiles {
151-
files, err := l.syntaxCache.getSyntax(bf)
151+
f, err := l.syntaxCache.getSyntax(bf)
152152
if err != nil {
153153
p.ReportError(errors.Promote(err, "load"))
154154
}
155-
for _, f := range files {
156-
_ = p.AddSyntax(f)
157-
}
155+
_ = p.AddSyntax(f)
158156
}
159157
}
160158

@@ -165,14 +163,15 @@ type syntaxCache struct {
165163
}
166164

167165
type syntaxCacheEntry struct {
168-
err error
169-
files []*ast.File
166+
err error
167+
file *ast.File
170168
}
171169

172170
func newSyntaxCache(cfg *Config) *syntaxCache {
173171
return &syntaxCache{
174172
config: encoding.Config{
175-
Stdin: cfg.stdin(),
173+
// Note: no need to pass Stdin, as we take care
174+
// always to pass a non-nil source when the file is "-".
176175
ParseFile: cfg.ParseFile,
177176
},
178177
ctx: cuecontext.New(),
@@ -181,20 +180,19 @@ func newSyntaxCache(cfg *Config) *syntaxCache {
181180
}
182181

183182
// getSyntax returns the CUE syntax corresponding to the file argument f.
184-
func (c *syntaxCache) getSyntax(bf *build.File) ([]*ast.File, error) {
183+
func (c *syntaxCache) getSyntax(bf *build.File) (*ast.File, error) {
185184
syntax, ok := c.cache[bf.Filename]
186185
if ok {
187-
return syntax.files, syntax.err
186+
return syntax.file, syntax.err
188187
}
189188
if bf.Encoding != build.CUE {
190189
panic("syntax for non-CUE file")
191190
}
192191
d := encoding.NewDecoder(c.ctx, bf, &c.config)
193-
for ; !d.Done(); d.Next() {
194-
syntax.files = append(syntax.files, d.File())
195-
}
196-
d.Close()
192+
defer d.Close()
193+
// Note: CUE files can never have multiple file parts.
194+
syntax.file = d.File()
197195
syntax.err = d.Err()
198196
c.cache[bf.Filename] = syntax
199-
return syntax.files, syntax.err
197+
return syntax.file, syntax.err
200198
}

0 commit comments

Comments
 (0)