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

Commit

Permalink
cue: allow string value as first element in ParsePath
Browse files Browse the repository at this point in the history
This makes ParsePath more generally applicable.

Change-Id: I6e7eaeed7a3da85036ea39fb7949b6b1a1622652
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9161
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Mar 29, 2021
1 parent cbda0d3 commit efd22f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cue/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func MakePath(selectors ...Selector) Path {

// ParsePath parses a CUE expression into a Path. Any error resulting from
// this conversion can be obtained by calling Err on the result.
//
// Unlike with normal CUE expressions, the first element of the path may be
// a string literal.
func ParsePath(s string) Path {
if s == "" {
return Path{}
}
expr, err := parser.ParseExpr("", s)
if err != nil {
return MakePath(Selector{pathError{errors.Promote(err, "invalid path")}})
Expand Down Expand Up @@ -101,6 +107,9 @@ func toSelectors(expr ast.Expr) []Selector {
case *ast.Ident:
return []Selector{identSelector(x)}

case *ast.BasicLit:
return []Selector{basicLitSelector(x)}

case *ast.IndexExpr:
a := toSelectors(x.X)
var sel Selector
Expand Down
8 changes: 8 additions & 0 deletions cue/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func Test(t *testing.T) {
path: p(Def("#Foo"), Str("a"), Str("b")),
out: "1",
str: "#Foo.a.b",
}, {
path: ParsePath(`#Foo.a.b`),
out: "1",
str: "#Foo.a.b",
}, {
path: ParsePath(`"#Foo".c.d`),
out: "2",
str: `"#Foo".c.d`,
}, {
// fallback Def(Foo) -> Def(#Foo)
path: p(Def("Foo"), Str("a"), Str("b")),
Expand Down

0 comments on commit efd22f6

Please sign in to comment.