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

Commit

Permalink
cue: add Selector.PkgPath
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpvl committed Apr 21, 2021
1 parent 4937cb9 commit f14c9a4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cue/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (sel Selector) IsDefinition() bool {
return sel.sel.kind() == adt.DefinitionLabel
}

// PkgPath reports the package path associated with a hidden label or "" if
// this is not a hidden label.
func (sel Selector) PkgPath() string {
h, _ := sel.sel.(scopedSelector)
return h.pkg
}

var (
// AnyField can be used to ask for any single label.
//
Expand Down
2 changes: 1 addition & 1 deletion cue/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"
)

func Test(t *testing.T) {
func TestPaths(t *testing.T) {
var r Runtime
inst, _ := r.Compile("", `
#Foo: a: b: 1
Expand Down
52 changes: 52 additions & 0 deletions cue/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ package cue_test

import (
"bytes"
"io/ioutil"
"testing"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/internal/cuetxtar"
"cuelang.org/go/internal/diff"
"github.com/rogpeppe/go-internal/txtar"
)

func TestLookupPath(t *testing.T) {
Expand Down Expand Up @@ -139,3 +143,51 @@ func compileT(t *testing.T, r *cue.Runtime, s string) cue.Value {
}
return inst.Value()
}

func TestHidden(t *testing.T) {
in := `
-- cue.mod/module.cue --
module: "example.com"
-- in.cue --
import "example.com/foo"
a: foo.C
b: _c
_c: 2
-- foo/foo.cue --
package foo
C: _d
_d: 3
`

a := txtar.Parse([]byte(in))
dir, _ := ioutil.TempDir("", "*")
instance := cuetxtar.Load(a, dir)[0]
if instance.Err != nil {
t.Fatal(instance.Err)
}

v := cuecontext.New().BuildInstance(instance)

testCases := []struct {
path cue.Path
pkg string
}{{
path: cue.ParsePath("a"),
pkg: "example.com/foo",
}, {
path: cue.ParsePath("b"),
pkg: "_",
}}
for _, tc := range testCases {
t.Run(tc.path.String(), func(t *testing.T) {
v := v.LookupPath(tc.path)
p := cue.Dereference(cue.Dereference(v)).Path().Selectors()
if got := p[len(p)-1].PkgPath(); got != tc.pkg {
t.Errorf("got %v; want %v", got, tc.pkg)
}
})
}
}
8 changes: 7 additions & 1 deletion cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1491,9 +1491,15 @@ func (v Value) Path() Path {
case adt.IntLabel:
a[i] = Selector{indexSelector(f)}

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

case adt.HiddenDefinitionLabel, adt.HiddenLabel:
a[i] = Selector{scopedSelector{
name: f.IdentString(v.idx),
pkg: f.PkgID(v.idx),
}}

case adt.StringLabel:
a[i] = Selector{stringSelector(f.StringValue(v.idx))}
}
Expand Down

0 comments on commit f14c9a4

Please sign in to comment.