From b8cb9a46f7f268ef6ec36bfaaf965ed9f51e559c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 14 Sep 2023 11:59:45 +0100 Subject: [PATCH] cue/ast/astutil: use generics to remove some reflect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: Ia28dfdcf6e371f9d1a2de9c0ce2302b83df4eb80 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168830 Unity-Result: CUE porcuepine Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo --- cue/ast/astutil/apply.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/cue/ast/astutil/apply.go b/cue/ast/astutil/apply.go index 694ad05f6e1..1c43629e230 100644 --- a/cue/ast/astutil/apply.go +++ b/cue/ast/astutil/apply.go @@ -199,7 +199,7 @@ type applyVisitor interface { // Helper functions for common node lists. They may be empty. -func applyExprList(v applyVisitor, parent Cursor, ptr interface{}, list []ast.Expr) { +func applyExprList(v applyVisitor, parent Cursor, list []ast.Expr) { c := newCursor(parent, nil, nil) for i, x := range list { c.index = i @@ -286,14 +286,12 @@ func applyDeclList(v applyVisitor, parent Cursor, list []ast.Decl) []ast.Decl { return c.decls } -func apply(v applyVisitor, parent Cursor, nodePtr interface{}) { - res := reflect.Indirect(reflect.ValueOf(nodePtr)) - n := res.Interface() - node := n.(ast.Node) +func apply[N ast.Node](v applyVisitor, parent Cursor, nodePtr *N) { + node := *nodePtr c := newCursor(parent, node, nodePtr) applyCursor(v, c) - if node != c.node { - res.Set(reflect.ValueOf(c.node)) + if ast.Node(node) != c.node { + *nodePtr = c.node.(N) } } @@ -349,10 +347,10 @@ func applyCursor(v applyVisitor, c Cursor) { // nothing to do case *ast.Interpolation: - applyExprList(v, c, &n, n.Elts) + applyExprList(v, c, n.Elts) case *ast.ListLit: - applyExprList(v, c, &n, n.Elts) + applyExprList(v, c, n.Elts) case *ast.Ellipsis: if n.Type != nil { @@ -381,7 +379,7 @@ func applyCursor(v applyVisitor, c Cursor) { case *ast.CallExpr: apply(v, c, &n.Fun) - applyExprList(v, c, &n, n.Args) + applyExprList(v, c, n.Args) case *ast.UnaryExpr: apply(v, c, &n.X)