Skip to content

Commit

Permalink
all: use ast.ParseImportPath and ast.ImportPath
Browse files Browse the repository at this point in the history
The `module.ParseImportPath` and its associated type `module.ImportPath`
are now deprecated in favor of the entry points in `cue/ast`.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ieeb9e6d37f79a2ec44ac76315514e7131ae71cea
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208072
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed Feb 4, 2025
1 parent 8dbff63 commit d5ab208
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 31 deletions.
6 changes: 3 additions & 3 deletions cmd/cue/cmd/modrename.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func runModRename(cmd *Command, args []string) error {
if err != nil {
return err
}
mr.oldModuleQualifier = module.ParseImportPath(mr.oldModulePath).Qualifier
mr.oldModuleQualifier = ast.ParseImportPath(mr.oldModulePath).Qualifier
mf.Module = args[0]
mr.newModulePath, mr.newModuleMajor, err = splitModulePath(mf.Module)
if err != nil {
return err
}
mr.newModuleQualifier = module.ParseImportPath(mr.newModulePath).Qualifier
mr.newModuleQualifier = ast.ParseImportPath(mr.newModulePath).Qualifier

// TODO if we're renaming to a module that we currently depend on,
// perhaps we should detect that and give an error.
Expand Down Expand Up @@ -163,7 +163,7 @@ func (mr *modRenamer) rewriteImport(spec *ast.ImportSpec) (changed bool, err err
if err != nil {
return false, fmt.Errorf("malformed import path in AST: %v", err)
}
ip := module.ParseImportPath(importPath)
ip := ast.ParseImportPath(importPath)
if !pkgIsUnderneath(ip.Path, mr.oldModulePath) {
return false, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func addImportQualifier(pkg importPath, name string) (importPath, error) {
if name == "" {
return pkg, nil
}
ip := module.ParseImportPath(string(pkg))
ip := ast.ParseImportPath(string(pkg))
if ip.Qualifier == "_" {
return "", fmt.Errorf("invalid import qualifier _ in %q", pkg)
}
Expand Down
7 changes: 4 additions & 3 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"slices"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
Expand Down Expand Up @@ -376,7 +377,7 @@ func importPathFromAbsDir(c *Config, absDir string, origPath string) (importPath
case c.Module == "":
return "", fmt.Errorf("cannot determine import path for %q (no module)", origPath)
default:
impPath := module.ParseImportPath(c.Module)
impPath := ast.ParseImportPath(c.Module)
impPath.Path += pkg
impPath.Qualifier = ""
pkg = impPath.String()
Expand All @@ -390,7 +391,7 @@ func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
i.Err = errors.Append(i.Err, err)
i.Dir = dir

parts := module.ParseImportPath(string(p))
parts := ast.ParseImportPath(string(p))
i.PkgName = parts.Qualifier
if i.PkgName == "" {
i.Err = errors.Append(i.Err, l.errPkgf([]token.Pos{pos}, "cannot determine package name for %q; set it explicitly with ':'", p))
Expand Down Expand Up @@ -433,7 +434,7 @@ func (l *loader) absDirFromImportPath1(pos token.Pos, p importPath) (absDir stri
return "", "", fmt.Errorf("imports are unavailable because there is no cue.mod/module.cue file")
}
// Extract the package name.
parts := module.ParseImportPath(string(p))
parts := ast.ParseImportPath(string(p))
unqualified := parts.Unqualified().String()
// TODO predicate registry-aware lookup on module.cue-declared CUE version?

Expand Down
4 changes: 2 additions & 2 deletions cue/load/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Instances(args []string, c *Config) []*build.Instance {
// of those, which don't.
pkgArgs1 := make([]string, 0, len(pkgArgs))
for _, p := range pkgArgs {
if ip := module.ParseImportPath(p); !ip.ExplicitQualifier {
if ip := ast.ParseImportPath(p); !ip.ExplicitQualifier {
ip.Qualifier = c.Package
p = ip.String()
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func loadPackages(
return nil, fmt.Errorf("invalid import path %q in %s", imp.Path.Value, f.Filename)
}
// Canonicalize the path.
pkgPath = module.ParseImportPath(pkgPath).Canonical().String()
pkgPath = ast.ParseImportPath(pkgPath).Canonical().String()
pkgPaths[pkgPath] = true
}
}
Expand Down
22 changes: 11 additions & 11 deletions cue/load/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func appendExpandedPackageArg(c *Config, pkgPaths []resolvedPackageArg, p string
// in command-line arguments. Handles .\... and so on.
p = filepath.ToSlash(p)

ip := module.ParseImportPath(p)
ip := ast.ParseImportPath(p)
if ip.Qualifier == "_" {
return nil, fmt.Errorf("invalid import path qualifier _ in %q", origp)
}
Expand All @@ -339,7 +339,7 @@ func appendExpandedPackageArg(c *Config, pkgPaths []resolvedPackageArg, p string
if err != nil {
return nil, err
}
ip1 := module.ParseImportPath(string(pkgPath))
ip1 := ast.ParseImportPath(string(pkgPath))
// Leave ip.Qualifier and ip.ExplicitQualifier intact.
ip.Path = ip1.Path
ip.Version = ip1.Version
Expand Down Expand Up @@ -389,7 +389,7 @@ func appendExpandedPackageArg(c *Config, pkgPaths []resolvedPackageArg, p string
// 5. if there's more than one package in the directory, it returns a MultiplePackageError.
// 6. if there are no package files in the directory, it just appends the import path as is, leaving it
// to later logic to produce an error in this case.
func appendExpandedUnqualifiedPackagePath(pkgPaths []resolvedPackageArg, origp string, ip module.ImportPath, pkgQual string, mainModRoot module.SourceLoc, mainModPath string, tg *tagger) (_ []resolvedPackageArg, _err error) {
func appendExpandedUnqualifiedPackagePath(pkgPaths []resolvedPackageArg, origp string, ip ast.ImportPath, pkgQual string, mainModRoot module.SourceLoc, mainModPath string, tg *tagger) (_ []resolvedPackageArg, _err error) {
ipRel, ok := cutModulePrefix(ip, mainModPath)
if !ok {
// Should never happen.
Expand Down Expand Up @@ -493,8 +493,8 @@ func appendExpandedUnqualifiedPackagePath(pkgPaths []resolvedPackageArg, origp s
// Note:
// * We know that pattern contains "..."
// * We know that pattern is relative to the module root
func appendExpandedWildcardPackagePath(pkgPaths []resolvedPackageArg, pattern module.ImportPath, pkgQual string, mainModRoot module.SourceLoc, mainModPath string, tg *tagger) (_ []resolvedPackageArg, _err error) {
modIpath := module.ParseImportPath(mainModPath)
func appendExpandedWildcardPackagePath(pkgPaths []resolvedPackageArg, pattern ast.ImportPath, pkgQual string, mainModRoot module.SourceLoc, mainModPath string, tg *tagger) (_ []resolvedPackageArg, _err error) {
modIpath := ast.ParseImportPath(mainModPath)
// Find directory to begin the scan.
// Could be smarter but this one optimization is enough for now,
// since ... is usually at the end of a path.
Expand Down Expand Up @@ -525,7 +525,7 @@ func appendExpandedWildcardPackagePath(pkgPaths []resolvedPackageArg, pattern mo
}

var prevFile modimports.ModuleFile
var prevImportPath module.ImportPath
var prevImportPath ast.ImportPath
iter := modimports.AllModuleFiles(mainModRoot.FS, dir)
iter(func(f modimports.ModuleFile, err error) bool {
if err != nil {
Expand All @@ -542,7 +542,7 @@ func appendExpandedWildcardPackagePath(pkgPaths []resolvedPackageArg, pattern mo
if pkgName == "" {
pkgName = "_"
}
ip := module.ImportPath{
ip := ast.ImportPath{
Path: path.Join(modIpath.Path, path.Dir(f.FilePath)),
Qualifier: pkgName,
Version: modIpath.Version,
Expand Down Expand Up @@ -587,23 +587,23 @@ func appendExpandedWildcardPackagePath(pkgPaths []resolvedPackageArg, pattern mo
// It returns a relative package path within m.
//
// If p does not contain a major version suffix but otherwise matches mod, it counts as a match.
func cutModulePrefix(p module.ImportPath, mod string) (module.ImportPath, bool) {
func cutModulePrefix(p ast.ImportPath, mod string) (ast.ImportPath, bool) {
if mod == "" {
return p, true
}
modPath, modVers, _ := ast.SplitPackageVersion(mod)
if !strings.HasPrefix(p.Path, modPath) {
return module.ImportPath{}, false
return ast.ImportPath{}, false
}
if p.Path == modPath {
p.Path = "."
return p, true
}
if p.Path[len(modPath)] != '/' {
return module.ImportPath{}, false
return ast.ImportPath{}, false
}
if p.Version != "" && modVers != "" && p.Version != modVers {
return module.ImportPath{}, false
return ast.ImportPath{}, false
}
p.Path = "." + p.Path[len(modPath):]
p.Version = ""
Expand Down
3 changes: 1 addition & 2 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/mod/module"
)

const (
Expand Down Expand Up @@ -916,7 +915,7 @@ func (s *state) refExpr(n cue.Value, importPath string, path cue.Path) ast.Expr
return expr
}
// External reference
ip := module.ParseImportPath(importPath)
ip := ast.ParseImportPath(importPath)
if ip.Qualifier == "" {
// TODO choose an arbitrary name here.
s.errf(n, "cannot determine package name from import path %q", importPath)
Expand Down
7 changes: 3 additions & 4 deletions internal/encoding/gotypes/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/mod/module"
)

// Generate produces Go type definitions from exported CUE definitions.
Expand Down Expand Up @@ -135,11 +134,11 @@ func Generate(ctx *cue.Context, insts ...*build.Instance) error {
// To keep the filename short for common cases, if we are generating a CUE package
// whose package name is implied from its import path, omit the package name element.
basename := "cue_types_gen.go"
ip := module.ParseImportPath(inst.ImportPath)
ip := ast.ParseImportPath(inst.ImportPath)
ip1 := ip
ip1.Qualifier = ""
ip1.ExplicitQualifier = false
ip1 = module.ParseImportPath(ip1.String())
ip1 = ast.ParseImportPath(ip1.String())
if ip.Qualifier != ip1.Qualifier {
basename = fmt.Sprintf("cue_types_%s_gen.go", inst.PkgName)
}
Expand Down Expand Up @@ -436,7 +435,7 @@ func (g *generator) emitTypeReference(val cue.Value, optional bool) bool {
// Go has no notion of qualified import paths; if a CUE file imports
// "foo.com/bar:qualified", we import just "foo.com/bar" on the Go side.
// TODO: deal with multiple packages existing in the same directory.
unqualifiedPath := module.ParseImportPath(inst.ImportPath).Unqualified().String()
unqualifiedPath := ast.ParseImportPath(inst.ImportPath).Unqualified().String()

var sb strings.Builder
if optional && cue.Dereference(val).IncompleteKind() == cue.StructKind {
Expand Down
2 changes: 1 addition & 1 deletion internal/mod/modimports/modimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func AllImports(modFilesIter func(func(ModuleFile, error) bool)) (_ []string, re
return false
}
// Canonicalize the path.
pkgPath = module.ParseImportPath(pkgPath).Canonical().String()
pkgPath = ast.ParseImportPath(pkgPath).Canonical().String()
pkgPaths[pkgPath] = true
}
return true
Expand Down
3 changes: 2 additions & 1 deletion internal/mod/modload/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"sync"

"cuelang.org/go/cue/ast"
"cuelang.org/go/internal/mod/modpkgload"
"cuelang.org/go/internal/mod/modrequirements"
"cuelang.org/go/internal/mod/semver"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (ld *loader) queryImport(ctx context.Context, pkgPath string, rs *modrequir
// It does not return modules that are already present in the given requirements.
// It also reports whether a default major version will be required.
func (ld *loader) queryLatestModules(ctx context.Context, pkgPath string, rs *modrequirements.Requirements) ([]module.Version, bool, error) {
parts := module.ParseImportPath(pkgPath)
parts := ast.ParseImportPath(pkgPath)
latestModuleForPrefix := func(prefix string) (module.Version, error) {
mv := parts.Version
if mv == "" {
Expand Down
3 changes: 2 additions & 1 deletion internal/mod/modpkgload/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"slices"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/internal/mod/modrequirements"
"cuelang.org/go/mod/module"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func (pkgs *Packages) importFromModules(ctx context.Context, pkgPath string) (m
// Note: we don't care about the package qualifier at this point
// because any directory with CUE files in counts as a possible
// candidate, regardless of what packages are in it.
pathParts := module.ParseImportPath(pkgPath)
pathParts := ast.ParseImportPath(pkgPath)
pkgPathOnly := pathParts.Path

if filepath.IsAbs(pkgPathOnly) || path.IsAbs(pkgPathOnly) {
Expand Down
3 changes: 2 additions & 1 deletion internal/mod/modpkgload/pkgload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync/atomic"

"cuelang.org/go/cue/ast"
"cuelang.org/go/internal/mod/modimports"
"cuelang.org/go/internal/mod/modrequirements"
"cuelang.org/go/internal/par"
Expand Down Expand Up @@ -256,7 +257,7 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
if pkgs.mainModuleVersion.Path() == pkg.mod.Path() {
pkgs.applyPkgFlags(pkg, PkgInAll)
}
ip := module.ParseImportPath(pkg.path)
ip := ast.ParseImportPath(pkg.path)
pkgQual := ip.Qualifier
switch pkgQual {
case "":
Expand Down
2 changes: 1 addition & 1 deletion mod/modfile/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func (f *File) DefaultMajorVersions() map[string]string {
// mentioned in the file, which means it will not work in general unless
// the module is tidy (as with `cue mod tidy`).
func (f *File) ModuleForImportPath(importPath string) (module.Version, bool) {
ip := module.ParseImportPath(importPath)
ip := ast.ParseImportPath(importPath)
for prefix := ip.Path; prefix != "."; prefix = path.Dir(prefix) {
pkgVersion := ip.Version
if pkgVersion == "" {
Expand Down

0 comments on commit d5ab208

Please sign in to comment.