Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: properly decode "any" labels
Browse files Browse the repository at this point in the history
Change-Id: I395955e9f715dcdd505a3e2ae101acd8247d034f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9602
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed May 4, 2021
1 parent 4f7caea commit 0457356
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cue/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ func (p Path) String() string {
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(']')
Expand Down Expand Up @@ -427,7 +428,7 @@ func (s indexSelector) feature(r adt.Runtime) adt.Feature {
// 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() }

Expand Down
15 changes: 15 additions & 0 deletions cue/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ func TestPaths(t *testing.T) {
a: 3
b: [4, 5, 6]
c: "#Foo": 7
map: [string]: int
list: [...int]
`)
testCases := []struct {
path Path
out string
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",
Expand Down Expand Up @@ -109,6 +116,14 @@ func TestPaths(t *testing.T) {
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()
Expand Down
8 changes: 6 additions & 2 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,11 +1522,15 @@ func appendPath(a []Selector, v Value) []Selector {
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))

Expand Down

0 comments on commit 0457356

Please sign in to comment.