lint: fix several lll, wrapcheck, and copy-paste errors
This commit is contained in:
parent
c09a2fe1fb
commit
e53546a7f5
@ -2,6 +2,7 @@ package modelgenerate
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/printer"
|
"go/printer"
|
||||||
@ -65,14 +66,14 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
if err := printer.Fprint(&buf, fset, file); err != nil {
|
if err := printer.Fprint(&buf, fset, file); err != nil {
|
||||||
return err
|
return fmt.Errorf("initial pretty-printing to get positioning: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-parse to get real positions (ParseComments preserves doc comments)
|
// Re-parse to get real positions (ParseComments preserves doc comments)
|
||||||
fset = token.NewFileSet()
|
fset = token.NewFileSet()
|
||||||
parsed, err := parser.ParseFile(fset, "", buf.Bytes(), parser.ParseComments)
|
parsed, err := parser.ParseFile(fset, "", buf.Bytes(), parser.ParseComments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("re-parsing pretty-print: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the tree-of-nodes into a slice-of-nodes
|
// Convert the tree-of-nodes into a slice-of-nodes
|
||||||
@ -98,8 +99,8 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
|||||||
origNodes := collectNodes(file)
|
origNodes := collectNodes(file)
|
||||||
reparsedNodes := collectNodes(parsed)
|
reparsedNodes := collectNodes(parsed)
|
||||||
for i, orig := range origNodes {
|
for i, orig := range origNodes {
|
||||||
text, ok := TrailingComments[orig]
|
text, isOk := TrailingComments[orig]
|
||||||
if !ok {
|
if !isOk {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
reparsed := reparsedNodes[i]
|
reparsed := reparsedNodes[i]
|
||||||
@ -110,40 +111,43 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extractCommentMarker := func(stmt ast.Stmt) (string, bool) {
|
extractCommentMarker := func(stmt ast.Stmt) (string, bool) {
|
||||||
expr, ok := stmt.(*ast.ExprStmt)
|
expr, isOk := stmt.(*ast.ExprStmt)
|
||||||
if !ok {
|
if !isOk {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
call, ok := expr.X.(*ast.CallExpr)
|
call, isOk := expr.X.(*ast.CallExpr)
|
||||||
if !ok {
|
if !isOk {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
ident, ok := call.Fun.(*ast.Ident)
|
ident, isOk := call.Fun.(*ast.Ident)
|
||||||
if !ok || ident.Name != commentMarker {
|
if !isOk || ident.Name != commentMarker {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
lit, isOk := call.Args[0].(*ast.BasicLit)
|
||||||
|
if !isOk {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
lit := call.Args[0].(*ast.BasicLit)
|
|
||||||
return lit.Value[1 : len(lit.Value)-1], true
|
return lit.Value[1 : len(lit.Value)-1], true
|
||||||
}
|
}
|
||||||
|
|
||||||
isBlankLineMarker := func(stmt ast.Stmt) bool {
|
isBlankLineMarker := func(stmt ast.Stmt) bool {
|
||||||
expr, ok := stmt.(*ast.ExprStmt)
|
expr, isOk := stmt.(*ast.ExprStmt)
|
||||||
if !ok {
|
if !isOk {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
call, ok := expr.X.(*ast.CallExpr)
|
call, isOk := expr.X.(*ast.CallExpr)
|
||||||
if !ok {
|
if !isOk {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ident, ok := call.Fun.(*ast.Ident)
|
ident, isOk := call.Fun.(*ast.Ident)
|
||||||
return ok && ident.Name == blankLineMarker
|
return isOk && ident.Name == blankLineMarker
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert comment and blank-line markers
|
// Convert comment and blank-line markers
|
||||||
ast.Inspect(parsed, func(n ast.Node) bool {
|
ast.Inspect(parsed, func(n ast.Node) bool {
|
||||||
// We only care about Block nodes
|
// We only care about Block nodes
|
||||||
block, ok := n.(*ast.BlockStmt)
|
block, isOk := n.(*ast.BlockStmt)
|
||||||
if !ok {
|
if !isOk {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +155,7 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
|||||||
// in the block statement's body with actual ones
|
// in the block statement's body with actual ones
|
||||||
filtered := block.List[:0]
|
filtered := block.List[:0]
|
||||||
for _, stmt := range block.List {
|
for _, stmt := range block.List {
|
||||||
if text, ok := extractCommentMarker(stmt); ok {
|
if text, isOk := extractCommentMarker(stmt); isOk {
|
||||||
// If it's a comment, add it to the fileset's list of Comments
|
// If it's a comment, add it to the fileset's list of Comments
|
||||||
parsed.Comments = append(parsed.Comments, &ast.CommentGroup{
|
parsed.Comments = append(parsed.Comments, &ast.CommentGroup{
|
||||||
List: []*ast.Comment{{Slash: stmt.Pos(), Text: "// " + text}},
|
List: []*ast.Comment{{Slash: stmt.Pos(), Text: "// " + text}},
|
||||||
@ -176,5 +180,9 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
|||||||
delete(TrailingComments, k)
|
delete(TrailingComments, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
return printer.Fprint(w, fset, parsed)
|
err = printer.Fprint(w, fset, parsed)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("re-pretty-printing: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -457,7 +457,7 @@ func GenerateGetItemByIDFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
return funcDecl
|
return funcDecl
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateGetItemByIDFunc produces an AST for the `GetXyzByID()` function.
|
// GenerateGetItemByUniqColFunc produces an AST for the `GetXyzByID()` function.
|
||||||
// E.g., a table with `table.TypeName = "foods"` will produce a "GetFoodByID()" function.
|
// E.g., a table with `table.TypeName = "foods"` will produce a "GetFoodByID()" function.
|
||||||
func GenerateGetItemByUniqColFunc(tbl schema.Table, col schema.Column) *ast.FuncDecl {
|
func GenerateGetItemByUniqColFunc(tbl schema.Table, col schema.Column) *ast.FuncDecl {
|
||||||
// Use the xyzSQLFields constant in the select query
|
// Use the xyzSQLFields constant in the select query
|
||||||
|
|||||||
@ -191,7 +191,9 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
ast.NewIdent("t"),
|
ast.NewIdent("t"),
|
||||||
testObj2,
|
testObj2,
|
||||||
mustCall(&ast.CallExpr{
|
mustCall(&ast.CallExpr{
|
||||||
Fun: &ast.SelectorExpr{X: testDB, Sel: ast.NewIdent("Get" + pkgschema.TypenameFromTablename(tbl.TableName) + "By" + col.GoFieldName())},
|
Fun: &ast.SelectorExpr{X: testDB, Sel: ast.NewIdent(
|
||||||
|
"Get" + pkgschema.TypenameFromTablename(tbl.TableName) + "By" + col.GoFieldName(),
|
||||||
|
)},
|
||||||
Args: []ast.Expr{&ast.SelectorExpr{X: testObj2, Sel: ast.NewIdent(col.GoFieldName())}},
|
Args: []ast.Expr{&ast.SelectorExpr{X: testObj2, Sel: ast.NewIdent(col.GoFieldName())}},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user