Skip to content

Commit f14c9a4

Browse files
committed
cue: add Selector.PkgPath
Also fixes bug in Path, which did not convert hidden fields properly. Change-Id: I02d2173b53642b0da1db0576d5d2717fc6cb6541 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9444 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 4937cb9 commit f14c9a4

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

cue/path.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func (sel Selector) IsDefinition() bool {
4949
return sel.sel.kind() == adt.DefinitionLabel
5050
}
5151

52+
// PkgPath reports the package path associated with a hidden label or "" if
53+
// this is not a hidden label.
54+
func (sel Selector) PkgPath() string {
55+
h, _ := sel.sel.(scopedSelector)
56+
return h.pkg
57+
}
58+
5259
var (
5360
// AnyField can be used to ask for any single label.
5461
//

cue/path_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"testing"
2020
)
2121

22-
func Test(t *testing.T) {
22+
func TestPaths(t *testing.T) {
2323
var r Runtime
2424
inst, _ := r.Compile("", `
2525
#Foo: a: b: 1

cue/query_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ package cue_test
1616

1717
import (
1818
"bytes"
19+
"io/ioutil"
1920
"testing"
2021

2122
"cuelang.org/go/cue"
23+
"cuelang.org/go/cue/cuecontext"
24+
"cuelang.org/go/internal/cuetxtar"
2225
"cuelang.org/go/internal/diff"
26+
"github.com/rogpeppe/go-internal/txtar"
2327
)
2428

2529
func TestLookupPath(t *testing.T) {
@@ -139,3 +143,51 @@ func compileT(t *testing.T, r *cue.Runtime, s string) cue.Value {
139143
}
140144
return inst.Value()
141145
}
146+
147+
func TestHidden(t *testing.T) {
148+
in := `
149+
-- cue.mod/module.cue --
150+
module: "example.com"
151+
152+
-- in.cue --
153+
import "example.com/foo"
154+
155+
a: foo.C
156+
b: _c
157+
_c: 2
158+
-- foo/foo.cue --
159+
package foo
160+
161+
C: _d
162+
_d: 3
163+
`
164+
165+
a := txtar.Parse([]byte(in))
166+
dir, _ := ioutil.TempDir("", "*")
167+
instance := cuetxtar.Load(a, dir)[0]
168+
if instance.Err != nil {
169+
t.Fatal(instance.Err)
170+
}
171+
172+
v := cuecontext.New().BuildInstance(instance)
173+
174+
testCases := []struct {
175+
path cue.Path
176+
pkg string
177+
}{{
178+
path: cue.ParsePath("a"),
179+
pkg: "example.com/foo",
180+
}, {
181+
path: cue.ParsePath("b"),
182+
pkg: "_",
183+
}}
184+
for _, tc := range testCases {
185+
t.Run(tc.path.String(), func(t *testing.T) {
186+
v := v.LookupPath(tc.path)
187+
p := cue.Dereference(cue.Dereference(v)).Path().Selectors()
188+
if got := p[len(p)-1].PkgPath(); got != tc.pkg {
189+
t.Errorf("got %v; want %v", got, tc.pkg)
190+
}
191+
})
192+
}
193+
}

cue/types.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,9 +1491,15 @@ func (v Value) Path() Path {
14911491
case adt.IntLabel:
14921492
a[i] = Selector{indexSelector(f)}
14931493

1494-
case adt.DefinitionLabel, adt.HiddenDefinitionLabel, adt.HiddenLabel:
1494+
case adt.DefinitionLabel:
14951495
a[i] = Selector{definitionSelector(f.SelectorString(v.idx))}
14961496

1497+
case adt.HiddenDefinitionLabel, adt.HiddenLabel:
1498+
a[i] = Selector{scopedSelector{
1499+
name: f.IdentString(v.idx),
1500+
pkg: f.PkgID(v.idx),
1501+
}}
1502+
14971503
case adt.StringLabel:
14981504
a[i] = Selector{stringSelector(f.StringValue(v.idx))}
14991505
}

0 commit comments

Comments
 (0)