Skip to content

Commit

Permalink
cue: add Value.BuildInstance method
Browse files Browse the repository at this point in the history
Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I8305b61a881a15420df128937800a2c0ea1be5e3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/542769
Reviewed-by: Marcel van Lohuizen <[email protected]>
Unity-Result: CUEcueckoo <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Aug 31, 2022
1 parent 351a9fb commit a552c1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
13 changes: 13 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cockroachdb/apd/v2"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
Expand Down Expand Up @@ -1088,6 +1089,18 @@ func (v Value) Source() ast.Node {
return v.v.Value().Source()
}

// If v exactly represents a package, BuildInstance returns
// the build instance corresponding to the value; otherwise it returns nil.
//
// The value returned by Value.ReferencePath will commonly represent
// a package.
func (v Value) BuildInstance() *build.Instance {
if v.idx == nil {
return nil
}
return v.idx.GetInstanceFromNode(v.v)
}

// Err returns the error represented by v or nil v is not an error.
func (v Value) Err() error {
if err := v.checkKind(v.ctx(), adt.BottomKind); err != nil {
Expand Down
27 changes: 21 additions & 6 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2858,9 +2858,10 @@ func TestTrimZeros(t *testing.T) {

func TestReferencePath(t *testing.T) {
testCases := []struct {
input string
want string
alt string
input string
want string
wantImportPath string
alt string
}{{
input: "v: w: x: _|_",
want: "",
Expand Down Expand Up @@ -2926,8 +2927,9 @@ func TestReferencePath(t *testing.T) {
v: w: x: math.Pi
`,
want: "Pi",
alt: "3.14159265358979323846264338327950288419716939937510582097494459",
want: "Pi",
wantImportPath: "math",
alt: "3.14159265358979323846264338327950288419716939937510582097494459",
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
Expand All @@ -2939,7 +2941,6 @@ func TestReferencePath(t *testing.T) {
if got := path.String(); got != tc.want {
t.Errorf("\n got %v;\nwant %v", got, tc.want)
}

if tc.want != "" {
want := "1"
if tc.alt != "" {
Expand All @@ -2949,6 +2950,13 @@ func TestReferencePath(t *testing.T) {
if v != want {
t.Errorf("path resolved to %s; want %s", v, want)
}
buildInst := root.BuildInstance()
if buildInst == nil {
t.Fatalf("no build instance found for reference path root")
}
if got, want := buildInst.ImportPath, tc.wantImportPath; got != want {
t.Errorf("unexpected import path; got %q want %q", got, want)
}
}

inst, a := v.Reference()
Expand All @@ -2970,6 +2978,13 @@ func TestReferencePath(t *testing.T) {
}
}

func TestZeroValueBuildInstance(t *testing.T) {
inst := Value{}.BuildInstance()
if inst != nil {
t.Error("unexpected non-nil instance")
}
}

func TestPos(t *testing.T) {
testCases := []struct {
value string
Expand Down

0 comments on commit a552c1f

Please sign in to comment.