-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/gf: improve gf gen ctrl using ast (gogf#3616)
- Loading branch information
Showing
8 changed files
with
169 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package genctrl | ||
|
||
import ( | ||
"bytes" | ||
"go/ast" | ||
"go/parser" | ||
"go/printer" | ||
"go/token" | ||
|
||
"github.com/gogf/gf/v2/os/gfile" | ||
"github.com/gogf/gf/v2/text/gstr" | ||
) | ||
|
||
// getStructsNameInSrc retrieves all struct names | ||
// that end in "Req" and have "g.Meta" in their body. | ||
func (c CGenCtrl) getStructsNameInSrc(filePath string) (structsName []string, err error) { | ||
var ( | ||
fileContent = gfile.GetContents(filePath) | ||
fileSet = token.NewFileSet() | ||
) | ||
|
||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments) | ||
if err != nil { | ||
return | ||
} | ||
|
||
ast.Inspect(node, func(n ast.Node) bool { | ||
if typeSpec, ok := n.(*ast.TypeSpec); ok { | ||
methodName := typeSpec.Name.Name | ||
if !gstr.HasSuffix(methodName, "Req") { | ||
// ignore struct name that do not end in "Req" | ||
return true | ||
} | ||
if structType, ok := typeSpec.Type.(*ast.StructType); ok { | ||
var buf bytes.Buffer | ||
if err := printer.Fprint(&buf, fileSet, structType); err != nil { | ||
return false | ||
} | ||
// ignore struct name that match a request, but has no g.Meta in its body. | ||
if !gstr.Contains(buf.String(), `g.Meta`) { | ||
return true | ||
} | ||
structsName = append(structsName, methodName) | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
return | ||
} | ||
|
||
// getImportsInDst retrieves all import paths in the file. | ||
func (c CGenCtrl) getImportsInDst(filePath string) (imports []string, err error) { | ||
var ( | ||
fileContent = gfile.GetContents(filePath) | ||
fileSet = token.NewFileSet() | ||
) | ||
|
||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments) | ||
if err != nil { | ||
return | ||
} | ||
|
||
ast.Inspect(node, func(n ast.Node) bool { | ||
if imp, ok := n.(*ast.ImportSpec); ok { | ||
imports = append(imports, imp.Path.Value) | ||
} | ||
return true | ||
}) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package genctrl | ||
|
||
import ( | ||
"bytes" | ||
"go/ast" | ||
"go/parser" | ||
"go/printer" | ||
"go/token" | ||
|
||
"github.com/gogf/gf/v2/os/gfile" | ||
) | ||
|
||
// getFuncInDst retrieves all function declarations and bodies in the file. | ||
func (c *controllerClearer) getFuncInDst(filePath string) (funcs []string, err error) { | ||
var ( | ||
fileContent = gfile.GetContents(filePath) | ||
fileSet = token.NewFileSet() | ||
) | ||
|
||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments) | ||
if err != nil { | ||
return | ||
} | ||
|
||
ast.Inspect(node, func(n ast.Node) bool { | ||
if fun, ok := n.(*ast.FuncDecl); ok { | ||
var buf bytes.Buffer | ||
if err := printer.Fprint(&buf, fileSet, fun); err != nil { | ||
return false | ||
} | ||
funcs = append(funcs, buf.String()) | ||
} | ||
return true | ||
}) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
cmd/gf/internal/cmd/testdata/issue/3569/api/hello/hello_expect.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.