From efd22f6bc892595ac8eba4e4912f435393ebcc4b Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Fri, 26 Mar 2021 21:44:22 +0100 Subject: [PATCH] cue: allow string value as first element in ParsePath This makes ParsePath more generally applicable. Change-Id: I6e7eaeed7a3da85036ea39fb7949b6b1a1622652 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9161 Reviewed-by: CUE cueckoo Reviewed-by: Paul Jolly --- cue/path.go | 9 +++++++++ cue/path_test.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/cue/path.go b/cue/path.go index 28d0182e0..538eacbf3 100644 --- a/cue/path.go +++ b/cue/path.go @@ -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")}}) @@ -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 diff --git a/cue/path_test.go b/cue/path_test.go index 5da4aa1cc..6cabd9818 100644 --- a/cue/path_test.go +++ b/cue/path_test.go @@ -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")),