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

Commit

Permalink
cue: more path cleanup
Browse files Browse the repository at this point in the history
Change-Id: I4bb273b019e40b0c7b6dd838b7cfb7def6ad2627
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9573
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Apr 30, 2021
1 parent c365513 commit ed5cdf0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cue/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions cue/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
23 changes: 0 additions & 23 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import (
"io"
"math"
"math/big"
"strconv"
"strings"

"github.com/cockroachdb/apd/v2"

"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"
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

})
}
}
Expand Down

0 comments on commit ed5cdf0

Please sign in to comment.