Skip to content

Commit

Permalink
cue/load: use cue package to interpret module.cue file
Browse files Browse the repository at this point in the history
Since cue/load already depends on the CUE package, it
seems better and easier to depend on the public API
rather than the internal API, making the code a little
easier to understand and more amenable to changes
that make the schema more complex.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I113c0b17aad41fa7639fb6bf27674cbfe1149755
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/549000
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed Feb 7, 2023
1 parent d78f1a2 commit 1b6fb4e
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import (
"path/filepath"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/compile"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
)

Expand Down Expand Up @@ -562,21 +561,21 @@ func (c *Config) completeModule() error {
if err != nil {
return errors.Wrapf(err, token.NoPos, "invalid cue.mod file")
}
// TODO disallow non-data-mode CUE.

r := runtime.New()
v, err := compile.Files(nil, r, "_", file)
if err != nil {
ctx := (*cue.Context)(runtime.New())
v := ctx.BuildFile(file)
if err := v.Validate(); err != nil {
return errors.Wrapf(err, token.NoPos, "invalid cue.mod file")
}
ctx := eval.NewContext(r, v)
v.Finalize(ctx)
prefix := v.Lookup(ctx.StringLabel("module"))
if prefix == nil {
prefix := v.LookupPath(cue.MakePath(cue.Str("module")))
if prefix.Err() != nil {
// TODO check better for not-found?
return nil
}
name := ctx.StringValue(prefix.Value())
if err := ctx.Err(); err != nil {
return err.Err
name, err := prefix.String()
if err != nil {
return err
}
if c.Module == "" {
c.Module = name
Expand All @@ -585,11 +584,7 @@ func (c *Config) completeModule() error {
if c.Module == name {
return nil
}
pos := token.NoPos
if src := prefix.Value().Source(); src != nil {
pos = src.Pos()
}
return errors.Newf(pos, "inconsistent modules: got %q, want %q", name, c.Module)
return errors.Newf(prefix.Pos(), "inconsistent modules: got %q, want %q", name, c.Module)
}

func (c Config) isRoot(dir string) bool {
Expand Down

0 comments on commit 1b6fb4e

Please sign in to comment.