-
Notifications
You must be signed in to change notification settings - Fork 170
Modules and imports in the Overlay, also all inputs from overlay #607
Description
Is your feature request related to a problem? Please describe.
I would like to embed a module into the overlay. This would be useful for shipping binaries with the schema embedded, especially with go 1.16 supporting file embeds natively.
This currently does not work due to how the overlay and cue.mod/module.cue is handled in cue/load/...
Another use case would be loading all inputs from the Overlay. This is useful in a server context where one wants to avoid disk usage. This seems to not work because of how the args to load.Instances works. It seems one cannot specify files from the Overlay there.
Am I missing a configuration setting?
Describe the solution you'd like
The ability to do the above. Guidance and input on implementation.
Additional context
I've traced the first use case to some TODOs in the code. The first is here: https://github.com/cuelang/cue/blob/master/cue/load/config.go#L498
The processing in this area generates absolute paths. The fileSystem in fs.go https://github.com/cuelang/cue/blob/master/cue/load/fs.go#L206
With Go 1.16, there is a new FS interface, maybe that should be used? If so, maybe that means this feature is delayed until 1.16 is more widely adopted?
Example that is not working as desired or expected:
package main
import (
"bytes"
"fmt"
"os"
"cuelang.org/go/cue"
"cuelang.org/go/cue/load"
)
func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var stdin string = `
package a
hello: "world"
foo: A
`
func main() {
var b bytes.Buffer
var r cue.Runtime
fmt.Fprintln(&b, stdin)
addOverlay()
config.Package = "*"
config.Stdin = &b
config.ModuleRoot = "./"
config.Dir = "./"
fmt.Println("Overlay:", config.Overlay)
bis := load.Instances([]string{"-"}, &config)
for _, bi := range bis {
check(bi.Err)
I, err := r.Build(bi)
check(err)
v := I.Value()
fmt.Printf("%v\n", v)
}
}
func addOverlay() {
for fn, src := range files {
config.Overlay[fn] = load.FromString(src)
}
}
var config load.Config = load.Config {
Overlay: map[string]load.Source{},
}
var files = map[string]string{
"cue.mod/module.cue": `
module: "hof.io/foo/bar"
`,
"a.cue": `
package a
A: "A"
`,
"b/b.cue": `
package b
B: "B"
`,
}