Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements to astbuilder #1613

Merged
merged 4 commits into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions hack/generator/pkg/astbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ import (
// return <otherReturns...>, err
// }
func CheckErrorAndReturn(otherReturns ...dst.Expr) dst.Stmt {
returnValues := append([]dst.Expr{}, cloneExprSlice(otherReturns)...)
returnValues = append(returnValues, dst.NewIdent("err"))

retStmt := &dst.ReturnStmt{
Results: returnValues,
Results: Expressions(otherReturns, dst.NewIdent("err")),
}

return CheckErrorAndSingleStatement(retStmt)
Expand Down Expand Up @@ -230,7 +227,7 @@ func ReturnIfExpr(cond dst.Expr, returns ...dst.Expr) *dst.IfStmt {
Body: &dst.BlockStmt{
List: []dst.Stmt{
&dst.ReturnStmt{
Results: cloneExprSlice(returns),
Results: Expressions(returns),
},
},
},
Expand Down Expand Up @@ -294,7 +291,7 @@ func Returns(returns ...dst.Expr) dst.Stmt {
Before: dst.NewLine,
},
},
Results: cloneExprSlice(returns),
Results: Expressions(returns),
}
}

Expand Down Expand Up @@ -376,7 +373,7 @@ func Nil() *dst.Ident {
// StatementBlock generates a block containing the supplied statements
func StatementBlock(statements ...dst.Stmt) *dst.BlockStmt {
return &dst.BlockStmt{
List: cloneStmtSlice(statements),
List: Statements(statements),
}
}

Expand All @@ -393,41 +390,54 @@ func EnsureStatementBlock(statement dst.Stmt) *dst.BlockStmt {
// Statements creates a sequence of statements from the provided values, each of which may be a
// single dst.Stmt or a slice of multiple []dst.Stmts
func Statements(statements ...interface{}) []dst.Stmt {
var result []dst.Stmt
var stmts []dst.Stmt
for _, s := range statements {
switch s := s.(type) {
case nil:
// Skip nils
continue
case dst.Stmt:
// Add a single statement
result = append(result, s)
stmts = append(stmts, s)
case []dst.Stmt:
// Add many statements
result = append(result, s...)
stmts = append(stmts, s...)
default:
panic(fmt.Sprintf("expected dst.Stmt or []dst.Stmt, but found %T", s))
}
}

return result
}

// cloneExprSlice is a utility method to clone a slice of expressions
func cloneExprSlice(exprs []dst.Expr) []dst.Expr {
var result []dst.Expr
for _, exp := range exprs {
result = append(result, dst.Clone(exp).(dst.Expr))
var result []dst.Stmt
for _, st := range stmts {
result = append(result, dst.Clone(st).(dst.Stmt))
}

return result
}

// cloneStmtSlice is a utility method to clone a slice of statements
func cloneStmtSlice(stmts []dst.Stmt) []dst.Stmt {
var result []dst.Stmt
for _, st := range stmts {
result = append(result, dst.Clone(st).(dst.Stmt))
// Expression creates a sequence of expressions from the provided values, each of which may be a
// single dst.Expr or a slice of multiple []dst.Expr
func Expressions(statements ...interface{}) []dst.Expr {
var exprs []dst.Expr
for _, e := range statements {
switch s := e.(type) {
case nil:
// Skip nils
continue
case dst.Expr:
// Add a single expression
exprs = append(exprs, s)
case []dst.Expr:
// Add many expressions
exprs = append(exprs, s...)
default:
panic(fmt.Sprintf("expected dst.Expr or []dst.Expr, but found %T", s))
}
}

var result []dst.Expr
for _, ex := range exprs {
result = append(result, dst.Clone(ex).(dst.Expr))
}

return result
Expand Down
6 changes: 3 additions & 3 deletions hack/generator/pkg/astbuilder/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func CallFunc(funcName string, arguments ...dst.Expr) *dst.CallExpr {
return &dst.CallExpr{
Fun: dst.NewIdent(funcName),
Args: cloneExprSlice(arguments),
Args: Expressions(arguments),
}
}

Expand All @@ -31,7 +31,7 @@ func CallQualifiedFunc(qualifier string, funcName string, arguments ...dst.Expr)
X: dst.NewIdent(qualifier),
Sel: dst.NewIdent(funcName),
},
Args: cloneExprSlice(arguments),
Args: Expressions(arguments),
}
}

Expand All @@ -45,7 +45,7 @@ func CallExpr(expr dst.Expr, funcName string, arguments ...dst.Expr) dst.Expr {
X: expr,
Sel: dst.NewIdent(funcName),
},
Args: cloneExprSlice(arguments),
Args: Expressions(arguments),
}
}

Expand Down