Skip to content

Commit b8cb9a4

Browse files
committed
cue/ast/astutil: use generics to remove some reflect
We don't need reflect to read and write nodePtr; we can make it a pointer to a generic ast.Node instead. While here, applyExprList didn't use its ptr argument. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ia28dfdcf6e371f9d1a2de9c0ce2302b83df4eb80 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168830 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 7389fe9 commit b8cb9a4

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

cue/ast/astutil/apply.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ type applyVisitor interface {
199199

200200
// Helper functions for common node lists. They may be empty.
201201

202-
func applyExprList(v applyVisitor, parent Cursor, ptr interface{}, list []ast.Expr) {
202+
func applyExprList(v applyVisitor, parent Cursor, list []ast.Expr) {
203203
c := newCursor(parent, nil, nil)
204204
for i, x := range list {
205205
c.index = i
@@ -286,14 +286,12 @@ func applyDeclList(v applyVisitor, parent Cursor, list []ast.Decl) []ast.Decl {
286286
return c.decls
287287
}
288288

289-
func apply(v applyVisitor, parent Cursor, nodePtr interface{}) {
290-
res := reflect.Indirect(reflect.ValueOf(nodePtr))
291-
n := res.Interface()
292-
node := n.(ast.Node)
289+
func apply[N ast.Node](v applyVisitor, parent Cursor, nodePtr *N) {
290+
node := *nodePtr
293291
c := newCursor(parent, node, nodePtr)
294292
applyCursor(v, c)
295-
if node != c.node {
296-
res.Set(reflect.ValueOf(c.node))
293+
if ast.Node(node) != c.node {
294+
*nodePtr = c.node.(N)
297295
}
298296
}
299297

@@ -349,10 +347,10 @@ func applyCursor(v applyVisitor, c Cursor) {
349347
// nothing to do
350348

351349
case *ast.Interpolation:
352-
applyExprList(v, c, &n, n.Elts)
350+
applyExprList(v, c, n.Elts)
353351

354352
case *ast.ListLit:
355-
applyExprList(v, c, &n, n.Elts)
353+
applyExprList(v, c, n.Elts)
356354

357355
case *ast.Ellipsis:
358356
if n.Type != nil {
@@ -381,7 +379,7 @@ func applyCursor(v applyVisitor, c Cursor) {
381379

382380
case *ast.CallExpr:
383381
apply(v, c, &n.Fun)
384-
applyExprList(v, c, &n, n.Args)
382+
applyExprList(v, c, n.Args)
385383

386384
case *ast.UnaryExpr:
387385
apply(v, c, &n.X)

0 commit comments

Comments
 (0)