cue/ast: convenience constructors for common types
In line with NewIdent
Updated code to use them, providing evidence of
their usefulness.
Change-Id: I0ccea6cb7df852c6ee56436bd20e008267a14aee
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3242
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go
index dc1a230..76183c7 100644
--- a/encoding/protobuf/parse.go
+++ b/encoding/protobuf/parse.go
@@ -154,9 +154,7 @@
p.sorted = imported
for _, v := range imported {
- spec := &ast.ImportSpec{
- Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(v)},
- }
+ spec := ast.NewImport(nil, v)
imports.Specs = append(imports.Specs, spec)
p.file.Imports = append(p.file.Imports, spec)
}
@@ -293,13 +291,8 @@
func (p *protoConverter) toExpr(pos scanner.Position, name string) (expr ast.Expr) {
a := strings.Split(name, ".")
- for i, s := range a {
- if i == 0 {
- expr = &ast.Ident{NamePos: p.toCUEPos(pos), Name: s}
- continue
- }
- expr = &ast.SelectorExpr{X: expr, Sel: ast.NewIdent(s)}
- }
+ expr = &ast.Ident{NamePos: p.toCUEPos(pos), Name: a[0]}
+ expr = ast.NewSel(expr, a[1:]...)
return expr
}