feat(goctl): Add api parser (#2585)

This commit is contained in:
anqiansong
2023-03-28 23:45:26 +08:00
committed by GitHub
parent 455a6c8f97
commit 50bc361430
59 changed files with 11633 additions and 6 deletions

View File

@@ -13,8 +13,11 @@ import (
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/api/util"
"github.com/zeromicro/go-zero/tools/goctl/pkg/env"
apiF "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -90,6 +93,10 @@ func apiFormatReader(reader io.Reader, filename string, skipCheckDeclare bool) e
// ApiFormatByPath format api from file path
func ApiFormatByPath(apiFilePath string, skipCheckDeclare bool) error {
if env.UseExperimental() {
return apiF.File(apiFilePath)
}
data, err := os.ReadFile(apiFilePath)
if err != nil {
return err

View File

@@ -158,7 +158,7 @@ func sweep() error {
tm := time.Unix(seconds, 0)
if tm.Before(keepTime) {
if err := os.Remove(fpath); err != nil {
if err := os.RemoveAll(fpath); err != nil {
fmt.Println(aurora.Red(fmt.Sprintf("failed to remove file: %s", fpath)))
return err
}

View File

@@ -8,6 +8,8 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/ast"
"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/pkg/env"
apiParser "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
)
type parser struct {
@@ -17,6 +19,10 @@ type parser struct {
// Parse parses the api file
func Parse(filename string) (*spec.ApiSpec, error) {
if env.UseExperimental() {
return apiParser.Parse(filename, "")
}
astParser := ast.NewParser(ast.WithParserPrefix(filepath.Base(filename)), ast.WithParserDebug())
parsedApi, err := astParser.Parse(filename)
if err != nil {

View File

@@ -11,6 +11,11 @@
}
// service doc
@server(
group: test
middleware: m1,m2
prefix: v1
)
service greet-api {
// handler doc
@handler GreetHandler // handler comment

View File

@@ -21,9 +21,9 @@ type (
// ApiSpec describes an api file
ApiSpec struct {
Info Info
Syntax ApiSyntax
Imports []Import
Info Info // Deprecated: useless expression
Syntax ApiSyntax // Deprecated: useless expression
Imports []Import // Deprecated: useless expression
Types []Type
Service Service
}
@@ -70,6 +70,7 @@ type (
// Route describes api route
Route struct {
// Deprecated: Use Service AtServer instead.
AtServerAnnotation Annotation
Method string
Path string