cue: properly decode "any" labels
Change-Id: I395955e9f715dcdd505a3e2ae101acd8247d034f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9602
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cue/path.go b/cue/path.go
index 465e360..0396c95 100644
--- a/cue/path.go
+++ b/cue/path.go
@@ -156,8 +156,9 @@
for i, sel := range p.path {
x := sel.sel
// TODO: use '.' in all cases, once supported.
+ _, isAny := x.(anySelector)
switch {
- case x.kind() == adt.IntLabel:
+ case x.kind() == adt.IntLabel && !isAny:
b.WriteByte('[')
b.WriteString(x.String())
b.WriteByte(']')
@@ -427,7 +428,7 @@
// an anySelector represents a wildcard option of a particular type.
type anySelector adt.Feature
-func (s anySelector) String() string { return "_" }
+func (s anySelector) String() string { return "[_]" }
func (s anySelector) optional() bool { return true }
func (s anySelector) kind() adt.FeatureType { return adt.Feature(s).Typ() }
diff --git a/cue/path_test.go b/cue/path_test.go
index 05de651..128a5db 100644
--- a/cue/path_test.go
+++ b/cue/path_test.go
@@ -28,6 +28,8 @@
a: 3
b: [4, 5, 6]
c: "#Foo": 7
+ map: [string]: int
+ list: [...int]
`)
testCases := []struct {
path Path
@@ -35,6 +37,11 @@
str string
err bool
}{{
+ path: MakePath(Str("list"), AnyIndex),
+ out: "int",
+ str: "list.[_]",
+ }, {
+
path: MakePath(Def("#Foo"), Str("a"), Str("b")),
out: "1",
str: "#Foo.a.b",
@@ -109,6 +116,14 @@
str: "_|_",
err: true,
out: `_|_ // invalid literal 3.3`,
+ }, {
+ path: MakePath(Str("map"), AnyString),
+ out: "int",
+ str: "map.[_]",
+ }, {
+ path: MakePath(Str("list"), AnyIndex),
+ out: "int",
+ str: "list.[_]",
}}
v := inst.Value()
diff --git a/cue/types.go b/cue/types.go
index 68374c0..cd003a3 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -1522,11 +1522,15 @@
return a
}
+ f := v.v.Label
+ if index := f.Index(); index == adt.MaxIndex {
+ return append(a, Selector{anySelector(f)})
+ }
+
var sel selector
- switch f := v.v.Label; f.Typ() {
+ switch f.Typ() {
case adt.IntLabel:
sel = indexSelector(f)
-
case adt.DefinitionLabel:
sel = definitionSelector(f.SelectorString(v.idx))