From ed5cdf0e82b11d60697c3979f7e666335e63ca84 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Fri, 30 Apr 2021 13:52:59 +0200 Subject: [PATCH] cue: more path cleanup Change-Id: I4bb273b019e40b0c7b6dd838b7cfb7def6ad2627 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9573 Reviewed-by: CUE cueckoo Reviewed-by: Paul Jolly --- cue/errors.go | 2 +- cue/path.go | 8 ++++++++ cue/types.go | 23 ----------------------- cue/types_test.go | 12 +++++++++++- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/cue/errors.go b/cue/errors.go index f1a803016..8ac9891af 100644 --- a/cue/errors.go +++ b/cue/errors.go @@ -87,7 +87,7 @@ func (e *valueError) Path() (a []string) { return a } } - return e.v.appendPath(nil) + return pathToStrings(e.v.Path()) } var errNotExists = &adt.Bottom{ diff --git a/cue/path.go b/cue/path.go index b5574c057..465e3606b 100644 --- a/cue/path.go +++ b/cue/path.go @@ -106,6 +106,14 @@ func MakePath(selectors ...Selector) Path { return Path{path: selectors} } +// pathToString is a utility function for creating debugging info. +func pathToStrings(p Path) (a []string) { + for _, sel := range p.Selectors() { + a = append(a, sel.String()) + } + return a +} + // ParsePath parses a CUE expression into a Path. Any error resulting from // this conversion can be obtained by calling Err on the result. // diff --git a/cue/types.go b/cue/types.go index 75b70b2a9..71abe0a19 100644 --- a/cue/types.go +++ b/cue/types.go @@ -21,7 +21,6 @@ import ( "io" "math" "math/big" - "strconv" "strings" "github.com/cockroachdb/apd/v2" @@ -29,7 +28,6 @@ import ( "cuelang.org/go/cue/ast" "cuelang.org/go/cue/ast/astutil" "cuelang.org/go/cue/errors" - "cuelang.org/go/cue/literal" "cuelang.org/go/cue/token" "cuelang.org/go/internal" "cuelang.org/go/internal/core/adt" @@ -551,27 +549,6 @@ func (v Value) Float64() (float64, error) { return f, nil } -func (v Value) appendPath(a []string) []string { - for _, f := range v.v.Path() { - switch f.Typ() { - case adt.IntLabel: - a = append(a, strconv.FormatInt(int64(f.Index()), 10)) - - case adt.StringLabel: - label := v.idx.LabelStr(f) - if !f.IsDef() && !f.IsHidden() { - if !ast.IsValidIdent(label) { - label = literal.String.Quote(label) - } - } - a = append(a, label) - default: - a = append(a, f.SelectorString(v.idx)) - } - } - return a -} - // Value holds any value, which may be a Boolean, Error, List, Null, Number, // Struct, or String. type Value struct { diff --git a/cue/types_test.go b/cue/types_test.go index e94933631..88535a595 100644 --- a/cue/types_test.go +++ b/cue/types_test.go @@ -2248,7 +2248,7 @@ func TestPath(t *testing.T) { v = v.Lookup(e) } } - got := v.appendPath(nil) + got := pathToStrings(v.Path()) if !reflect.DeepEqual(got, tc) { t.Errorf("got %v; want %v", got, tc) } @@ -3124,6 +3124,16 @@ func TestPathCorrection(t *testing.T) { if gotPath != tc.want { t.Errorf("got path %s; want %s", gotPath, tc.want) } + + x, p := v.ReferencePath() + if x.Value() != inst.Value() { + t.Error("reference not in original instance") + } + gotPath = p.String() + if gotPath != tc.want { + t.Errorf("got path %s; want %s", gotPath, tc.want) + } + }) } }