feat(goctl): Add api parser (#2585)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
5
tools/goctl/api/parser/testdata/test.api
vendored
5
tools/goctl/api/parser/testdata/test.api
vendored
@@ -11,6 +11,11 @@
|
||||
}
|
||||
|
||||
// service doc
|
||||
@server(
|
||||
group: test
|
||||
middleware: m1,m2
|
||||
prefix: v1
|
||||
)
|
||||
service greet-api {
|
||||
// handler doc
|
||||
@handler GreetHandler // handler comment
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user