Skip to content

Commit f787af8

Browse files
committed
cue/load: use constructor for fileSystem
Currently `cue/load.fileSystem` uses an `init` method rather than a constructor function. There doesn't seem to be any good reason for that, so just use a regular constructor function and use a pointer type. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I279a469095a66c0c1acc51da2255fc896a6efd6c Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197528 Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 5cbddef commit f787af8

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

cue/load/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ type Config struct {
297297
// will be used.
298298
Env []string
299299

300-
fileSystem fileSystem
300+
fileSystem *fileSystem
301301
}
302302

303303
func (c *Config) stdin() io.Reader {
@@ -354,9 +354,11 @@ func (c Config) complete() (cfg *Config, err error) {
354354

355355
// TODO: we could populate this already with absolute file paths,
356356
// but relative paths cannot be added. Consider what is reasonable.
357-
if err := c.fileSystem.init(c.Dir, c.Overlay); err != nil {
357+
fsys, err := newFileSystem(&c)
358+
if err != nil {
358359
return nil, err
359360
}
361+
c.fileSystem = fsys
360362

361363
// TODO: determine root on a package basis. Maybe we even need a
362364
// pkgname.cue.mod

cue/load/fs.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,24 @@ func (fs *fileSystem) ioFS(root string) iofs.FS {
9494
}
9595
}
9696

97-
func (fs *fileSystem) init(cwd string, overlay map[string]Source) error {
98-
fs.cwd = cwd
99-
fs.overlayDirs = map[string]map[string]*overlayFile{}
97+
func newFileSystem(cfg *Config) (*fileSystem, error) {
98+
fs := &fileSystem{
99+
cwd: cfg.Dir,
100+
overlayDirs: map[string]map[string]*overlayFile{},
101+
}
100102

101103
// Organize overlay
102-
for filename, src := range overlay {
104+
for filename, src := range cfg.Overlay {
103105
if !filepath.IsAbs(filename) {
104-
return fmt.Errorf("non-absolute file path %q in overlay", filename)
106+
return nil, fmt.Errorf("non-absolute file path %q in overlay", filename)
105107
}
106108
// TODO: do we need to further clean the path or check that the
107109
// specified files are within the root/ absolute files?
108110
dir, base := filepath.Split(filename)
109111
m := fs.getDir(dir, true)
110112
b, file, err := src.contents()
111113
if err != nil {
112-
return err
114+
return nil, err
113115
}
114116
m[base] = &overlayFile{
115117
basename: base,
@@ -134,7 +136,7 @@ func (fs *fileSystem) init(cwd string, overlay map[string]Source) error {
134136
}
135137
}
136138
}
137-
return nil
139+
return fs, nil
138140
}
139141

140142
func (fs *fileSystem) makeAbs(path string) string {

cue/load/fs_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestIOFS(t *testing.T) {
2222
for _, f := range onDiskFiles {
2323
writeFile(t, filepath.Join(dir, f), f)
2424
}
25-
var fsys fileSystem
2625
overlayFiles := []string{
2726
"foo/bar/a",
2827
"foo/bar/c",
@@ -33,7 +32,10 @@ func TestIOFS(t *testing.T) {
3332
overlay[filepath.Join(dir, f)] = FromString(f + " overlay")
3433
}
3534

36-
err := fsys.init(filepath.Join(dir, "foo"), overlay)
35+
fsys, err := newFileSystem(&Config{
36+
Dir: filepath.Join(dir, "foo"),
37+
Overlay: overlay,
38+
})
3739
qt.Assert(t, qt.IsNil(err))
3840
ffsys := fsys.ioFS(dir)
3941
err = fstest.TestFS(ffsys, append(slices.Clip(onDiskFiles), overlayFiles...)...)

cue/load/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
7171
defer l.stk.Pop()
7272

7373
cfg := l.cfg
74-
ctxt := &cfg.fileSystem
74+
ctxt := cfg.fileSystem
7575

7676
if p.Err != nil {
7777
return []*build.Instance{p}

0 commit comments

Comments
 (0)