Skip to content

Commit

Permalink
cue/load: use constructor for fileSystem
Browse files Browse the repository at this point in the history
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 <rogpeppe@gmail.com>
Change-Id: I279a469095a66c0c1acc51da2255fc896a6efd6c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197528
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Jul 11, 2024
1 parent 5cbddef commit f787af8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
6 changes: 4 additions & 2 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ type Config struct {
// will be used.
Env []string

fileSystem fileSystem
fileSystem *fileSystem
}

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

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

// TODO: determine root on a package basis. Maybe we even need a
// pkgname.cue.mod
Expand Down
16 changes: 9 additions & 7 deletions cue/load/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,24 @@ func (fs *fileSystem) ioFS(root string) iofs.FS {
}
}

func (fs *fileSystem) init(cwd string, overlay map[string]Source) error {
fs.cwd = cwd
fs.overlayDirs = map[string]map[string]*overlayFile{}
func newFileSystem(cfg *Config) (*fileSystem, error) {
fs := &fileSystem{
cwd: cfg.Dir,
overlayDirs: map[string]map[string]*overlayFile{},
}

// Organize overlay
for filename, src := range overlay {
for filename, src := range cfg.Overlay {
if !filepath.IsAbs(filename) {
return fmt.Errorf("non-absolute file path %q in overlay", filename)
return nil, fmt.Errorf("non-absolute file path %q in overlay", filename)
}
// TODO: do we need to further clean the path or check that the
// specified files are within the root/ absolute files?
dir, base := filepath.Split(filename)
m := fs.getDir(dir, true)
b, file, err := src.contents()
if err != nil {
return err
return nil, err
}
m[base] = &overlayFile{
basename: base,
Expand All @@ -134,7 +136,7 @@ func (fs *fileSystem) init(cwd string, overlay map[string]Source) error {
}
}
}
return nil
return fs, nil
}

func (fs *fileSystem) makeAbs(path string) string {
Expand Down
6 changes: 4 additions & 2 deletions cue/load/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func TestIOFS(t *testing.T) {
for _, f := range onDiskFiles {
writeFile(t, filepath.Join(dir, f), f)
}
var fsys fileSystem
overlayFiles := []string{
"foo/bar/a",
"foo/bar/c",
Expand All @@ -33,7 +32,10 @@ func TestIOFS(t *testing.T) {
overlay[filepath.Join(dir, f)] = FromString(f + " overlay")
}

err := fsys.init(filepath.Join(dir, "foo"), overlay)
fsys, err := newFileSystem(&Config{
Dir: filepath.Join(dir, "foo"),
Overlay: overlay,
})
qt.Assert(t, qt.IsNil(err))
ffsys := fsys.ioFS(dir)
err = fstest.TestFS(ffsys, append(slices.Clip(onDiskFiles), overlayFiles...)...)
Expand Down
2 changes: 1 addition & 1 deletion cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
defer l.stk.Pop()

cfg := l.cfg
ctxt := &cfg.fileSystem
ctxt := cfg.fileSystem

if p.Err != nil {
return []*build.Instance{p}
Expand Down

0 comments on commit f787af8

Please sign in to comment.