Skip to content

Commit 64baa18

Browse files
committed
mod/module: add qualifier in ImportPath when necessary
Currently the `ImportPath.ExplicitQualifier` field is treated as gospel: if it's false, the qualifier will never be added. However, it's useful to be able to manipulate the `ImportPath` fields (for example to change the package path) and then invoke its `String` method to produce a well-formed import path. So we change `ImportPath.String` to include a package qualifier if needed, even if `ExplicitQualifier` is false. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Iaa7484f5506e10cf2bb9d399e987c69021644c99 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194666 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 185426f commit 64baa18

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

mod/module/module_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,12 @@ func TestParseImportPath(t *testing.T) {
304304
})
305305
}
306306
}
307+
308+
func TestImportPathStringAddsQualifier(t *testing.T) {
309+
ip := ImportPath{
310+
Path: "foo.com/bar",
311+
Version: "v0",
312+
Qualifier: "baz",
313+
}
314+
qt.Assert(t, qt.Equals(ip.String(), "foo.com/bar@v0:baz"))
315+
}

mod/module/path.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,9 @@ type ImportPath struct {
411411
// This is not guaranteed to be a valid CUE identifier.
412412
Qualifier string
413413

414-
// ExplicitQualifier holds whether the qualifier was explicitly
415-
// present in the import path.
414+
// ExplicitQualifier holds whether the qualifier will
415+
// always be added regardless of whether it matches
416+
// the final path element.
416417
ExplicitQualifier bool
417418
}
418419

@@ -435,6 +436,13 @@ func (parts ImportPath) Unqualified() ImportPath {
435436
}
436437

437438
func (parts ImportPath) String() string {
439+
needQualifier := parts.ExplicitQualifier
440+
if !needQualifier && parts.Qualifier != "" {
441+
_, last, _ := cutLast(parts.Path, "/")
442+
if last != "" && last != parts.Qualifier {
443+
needQualifier = true
444+
}
445+
}
438446
if parts.Version == "" && !parts.ExplicitQualifier {
439447
// Fast path.
440448
return parts.Path
@@ -445,7 +453,7 @@ func (parts ImportPath) String() string {
445453
buf.WriteByte('@')
446454
buf.WriteString(parts.Version)
447455
}
448-
if parts.ExplicitQualifier {
456+
if needQualifier {
449457
buf.WriteByte(':')
450458
buf.WriteString(parts.Qualifier)
451459
}
@@ -487,3 +495,10 @@ func CheckPathMajor(v, pathMajor string) error {
487495
}
488496
return nil
489497
}
498+
499+
func cutLast(s, sep string) (before, after string, found bool) {
500+
if i := strings.LastIndex(s, sep); i >= 0 {
501+
return s[:i], s[i+len(sep):], true
502+
}
503+
return "", s, false
504+
}

0 commit comments

Comments
 (0)