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

Commit

Permalink
cue: expose some path utilities
Browse files Browse the repository at this point in the history
1) allow generating a Selector from an ast.Label
2) allow directly querying whether a Selector is a string or not.

Change-Id: I89a99a8a776949de9f31b1b2b0438578018f4860
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9241
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 2, 2021
1 parent 276e164 commit 20a4878
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cue/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (sel Selector) String() string {
return sel.sel.String()
}

// IsString reports whether sel is a regular label type.
func (sel Selector) IsString() bool {
return sel.sel.kind() == adt.StringLabel
}

type selector interface {
String() string

Expand Down Expand Up @@ -116,7 +121,7 @@ func (p Path) String() string {
func toSelectors(expr ast.Expr) []Selector {
switch x := expr.(type) {
case *ast.Ident:
return []Selector{identSelector(x)}
return []Selector{Label(x)}

case *ast.BasicLit:
return []Selector{basicLitSelector(x)}
Expand All @@ -135,7 +140,7 @@ func toSelectors(expr ast.Expr) []Selector {

case *ast.SelectorExpr:
a := toSelectors(x.X)
return appendSelector(a, identSelector(x.Sel))
return appendSelector(a, Label(x.Sel))

default:
return []Selector{{pathError{
Expand Down Expand Up @@ -197,7 +202,8 @@ func basicLitSelector(b *ast.BasicLit) Selector {
}
}

func identSelector(label ast.Label) Selector {
// Label converts an AST label to a Selector.
func Label(label ast.Label) Selector {
switch x := label.(type) {
case *ast.Ident:
switch s := x.Name; {
Expand Down

0 comments on commit 20a4878

Please sign in to comment.