optimize: fix experimental api (#3604)

master
kesonan 1 year ago committed by GitHub
parent 1ff541afe4
commit 02c95108b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -178,13 +178,10 @@ func (p parser) fieldToMember(field *ast.TypeField) spec.Member {
tag := "" tag := ""
if !field.IsAnonymous { if !field.IsAnonymous {
name = field.Name.Text() name = field.Name.Text()
if field.Tag == nil { if field.Tag != nil {
panic(fmt.Sprintf("error: line %d:%d field %s has no tag",
field.Name.Line(), field.Name.Column(), field.Name.Text()))
}
tag = field.Tag.Text() tag = field.Tag.Text()
} }
}
return spec.Member{ return spec.Member{
Name: name, Name: name,
Type: p.astTypeToSpec(field.DataType), Type: p.astTypeToSpec(field.DataType),

@ -2,6 +2,7 @@ package parser
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"github.com/zeromicro/go-zero/tools/goctl/api/spec" "github.com/zeromicro/go-zero/tools/goctl/api/spec"
@ -101,7 +102,25 @@ func (a *Analyzer) convert2Spec() error {
return err return err
} }
return a.fillService() if err := a.fillService(); err != nil {
return err
}
sort.SliceStable(a.spec.Types, func(i, j int) bool {
return a.spec.Types[i].Name() < a.spec.Types[j].Name()
})
groups := make([]spec.Group, 0, len(a.spec.Service.Groups))
for _, v := range a.spec.Service.Groups {
sort.SliceStable(v.Routes, func(i, j int) bool {
return v.Routes[i].Path < v.Routes[j].Path
})
groups = append(groups, v)
}
sort.SliceStable(groups, func(i, j int) bool {
return groups[i].Annotation.Properties["group"] < groups[j].Annotation.Properties["group"]
})
a.spec.Service.Groups = groups
return nil
} }
func (a *Analyzer) convertAtDoc(atDoc ast.AtDocStmt) spec.AtDoc { func (a *Analyzer) convertAtDoc(atDoc ast.AtDocStmt) spec.AtDoc {

@ -31,19 +31,41 @@ func convert2API(a *ast.AST, importManager map[string]placeholder.Type) (*API, e
one := a.Stmts[0] one := a.Stmts[0]
syntax, ok := one.(*ast.SyntaxStmt) syntax, ok := one.(*ast.SyntaxStmt)
if !ok { if !ok {
return nil, ast.SyntaxError(one.Pos(), "expected syntax statement, got <%T>", one) syntax = &ast.SyntaxStmt{
Syntax: ast.NewTokenNode(token.Token{
Type: token.IDENT,
Text: token.Syntax,
}),
Assign: ast.NewTokenNode(token.Token{
Type: token.ASSIGN,
Text: "=",
}),
Value: ast.NewTokenNode(token.Token{
Type: token.STRING,
Text: `"v1"`,
}),
}
} }
api.Syntax = syntax
for i := 1; i < len(a.Stmts); i++ { api.Syntax = syntax
var hasSyntax, hasInfo bool
for i := 0; i < len(a.Stmts); i++ {
one := a.Stmts[i] one := a.Stmts[i]
switch val := one.(type) { switch val := one.(type) {
case *ast.SyntaxStmt: case *ast.SyntaxStmt:
if hasSyntax {
return nil, ast.DuplicateStmtError(val.Pos(), "duplicate syntax statement") return nil, ast.DuplicateStmtError(val.Pos(), "duplicate syntax statement")
} else {
hasSyntax = true
}
case *ast.InfoStmt: case *ast.InfoStmt:
if api.info != nil { if api.info != nil {
if hasInfo {
return nil, ast.DuplicateStmtError(val.Pos(), "duplicate info statement") return nil, ast.DuplicateStmtError(val.Pos(), "duplicate info statement")
} }
} else {
hasInfo = true
}
api.info = val api.info = val
case ast.ImportStmt: case ast.ImportStmt:
api.importStmt = append(api.importStmt, val) api.importStmt = append(api.importStmt, val)

@ -1,7 +1,3 @@
// test case: expected syntax statement
info ()
-----
// test case: duplicate syntax statement // test case: duplicate syntax statement
syntax = "v1" syntax = "v1"
syntax = "v1" syntax = "v1"

Loading…
Cancel
Save