mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
Add document & comment for spec (#703)
* Add document & comment for spec * remove duplicate field * use alias
This commit is contained in:
parent
f300408fc0
commit
94417be018
@ -114,8 +114,10 @@ func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{} {
|
|||||||
return alias
|
return alias
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc := v.getDoc(ctx)
|
||||||
st, ok := typeLit.(*TypeStruct)
|
st, ok := typeLit.(*TypeStruct)
|
||||||
if ok {
|
if ok {
|
||||||
|
st.DocExpr = doc
|
||||||
return st
|
return st
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,14 +76,22 @@ func (p parser) fillInfo() {
|
|||||||
|
|
||||||
func (p parser) fillSyntax() {
|
func (p parser) fillSyntax() {
|
||||||
if p.ast.Syntax != nil {
|
if p.ast.Syntax != nil {
|
||||||
p.spec.Syntax = spec.ApiSyntax{Version: p.ast.Syntax.Version.Text()}
|
p.spec.Syntax = spec.ApiSyntax{
|
||||||
|
Version: p.ast.Syntax.Version.Text(),
|
||||||
|
Doc: p.stringExprs(p.ast.Syntax.DocExpr),
|
||||||
|
Comment: p.stringExprs([]ast.Expr{p.ast.Syntax.CommentExpr}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p parser) fillImport() {
|
func (p parser) fillImport() {
|
||||||
if len(p.ast.Import) > 0 {
|
if len(p.ast.Import) > 0 {
|
||||||
for _, item := range p.ast.Import {
|
for _, item := range p.ast.Import {
|
||||||
p.spec.Imports = append(p.spec.Imports, spec.Import{Value: item.Value.Text()})
|
p.spec.Imports = append(p.spec.Imports, spec.Import{
|
||||||
|
Value: item.Value.Text(),
|
||||||
|
Doc: p.stringExprs(item.DocExpr),
|
||||||
|
Comment: p.stringExprs([]ast.Expr{item.CommentExpr}),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -173,10 +181,14 @@ func (p parser) astTypeToSpec(in ast.DataType) spec.Type {
|
|||||||
case *ast.Literal:
|
case *ast.Literal:
|
||||||
raw := v.Literal.Text()
|
raw := v.Literal.Text()
|
||||||
if api.IsBasicType(raw) {
|
if api.IsBasicType(raw) {
|
||||||
return spec.PrimitiveType{RawName: raw}
|
return spec.PrimitiveType{
|
||||||
|
RawName: raw,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return spec.DefineStruct{RawName: raw}
|
return spec.DefineStruct{
|
||||||
|
RawName: raw,
|
||||||
|
}
|
||||||
case *ast.Interface:
|
case *ast.Interface:
|
||||||
return spec.InterfaceType{RawName: v.Literal.Text()}
|
return spec.InterfaceType{RawName: v.Literal.Text()}
|
||||||
case *ast.Map:
|
case *ast.Map:
|
||||||
@ -198,6 +210,9 @@ func (p parser) astTypeToSpec(in ast.DataType) spec.Type {
|
|||||||
func (p parser) stringExprs(docs []ast.Expr) []string {
|
func (p parser) stringExprs(docs []ast.Expr) []string {
|
||||||
var result []string
|
var result []string
|
||||||
for _, item := range docs {
|
for _, item := range docs {
|
||||||
|
if item == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
result = append(result, item.Text())
|
result = append(result, item.Text())
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@ -222,9 +237,13 @@ func (p parser) fillService() error {
|
|||||||
AtServerAnnotation: spec.Annotation{},
|
AtServerAnnotation: spec.Annotation{},
|
||||||
Method: astRoute.Route.Method.Text(),
|
Method: astRoute.Route.Method.Text(),
|
||||||
Path: astRoute.Route.Path.Text(),
|
Path: astRoute.Route.Path.Text(),
|
||||||
|
Doc: p.stringExprs(astRoute.Route.DocExpr),
|
||||||
|
Comment: p.stringExprs([]ast.Expr{astRoute.Route.CommentExpr}),
|
||||||
}
|
}
|
||||||
if astRoute.AtHandler != nil {
|
if astRoute.AtHandler != nil {
|
||||||
route.Handler = astRoute.AtHandler.Name.Text()
|
route.Handler = astRoute.AtHandler.Name.Text()
|
||||||
|
route.HandlerDoc = append(route.HandlerDoc, p.stringExprs(astRoute.AtHandler.DocExpr)...)
|
||||||
|
route.HandlerComment = append(route.HandlerComment, p.stringExprs([]ast.Expr{astRoute.AtHandler.CommentExpr})...)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := p.fillRouteAtServer(astRoute, &route)
|
err := p.fillRouteAtServer(astRoute, &route)
|
||||||
|
28
tools/goctl/api/parser/parser_test.go
Normal file
28
tools/goctl/api/parser/parser_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testApi = "// syntax doc\nsyntax = \"v1\" // syntax comment\n\n// type doc\ntype Request {\n\tName string `path:\"name,options=you|me\"`\n}\n\ntype Response {\n\tMessage string `json:\"message\"`\n}\n\n// service doc\nservice greet-api {\n\t// handler doc\n\t@handler GreetHandler // handler comment\n\tget /from/:name(Request) returns (Response);\n}"
|
||||||
|
|
||||||
|
func TestParseContent(t *testing.T) {
|
||||||
|
sp, err := ParseContent(testApi)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, spec.Doc{`// syntax doc`}, sp.Syntax.Doc)
|
||||||
|
assert.Equal(t, spec.Doc{`// syntax comment`}, sp.Syntax.Comment)
|
||||||
|
for _, tp := range sp.Types {
|
||||||
|
if tp.Name() == "Request" {
|
||||||
|
assert.Equal(t, []string{`// type doc`}, tp.Documents())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, e := range sp.Service.Routes() {
|
||||||
|
if e.Handler == "GreetHandler" {
|
||||||
|
assert.Equal(t, spec.Doc{"// handler doc"}, e.HandlerDoc)
|
||||||
|
assert.Equal(t, spec.Doc{"// handler comment"}, e.HandlerComment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,27 +5,87 @@ func (t PrimitiveType) Name() string {
|
|||||||
return t.RawName
|
return t.RawName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments returns the comments of struct
|
||||||
|
func (t PrimitiveType) Comments() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents returns the documents of struct
|
||||||
|
func (t PrimitiveType) Documents() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns a structure string, such as User
|
// Name returns a structure string, such as User
|
||||||
func (t DefineStruct) Name() string {
|
func (t DefineStruct) Name() string {
|
||||||
return t.RawName
|
return t.RawName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments returns the comments of struct
|
||||||
|
func (t DefineStruct) Comments() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents returns the documents of struct
|
||||||
|
func (t DefineStruct) Documents() []string {
|
||||||
|
return t.Docs
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns a map string, such as map[string]int
|
// Name returns a map string, such as map[string]int
|
||||||
func (t MapType) Name() string {
|
func (t MapType) Name() string {
|
||||||
return t.RawName
|
return t.RawName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments returns the comments of struct
|
||||||
|
func (t MapType) Comments() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents returns the documents of struct
|
||||||
|
func (t MapType) Documents() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns a slice string, such as []int
|
// Name returns a slice string, such as []int
|
||||||
func (t ArrayType) Name() string {
|
func (t ArrayType) Name() string {
|
||||||
return t.RawName
|
return t.RawName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments returns the comments of struct
|
||||||
|
func (t ArrayType) Comments() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents returns the documents of struct
|
||||||
|
func (t ArrayType) Documents() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns a pointer string, such as *User
|
// Name returns a pointer string, such as *User
|
||||||
func (t PointerType) Name() string {
|
func (t PointerType) Name() string {
|
||||||
return t.RawName
|
return t.RawName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments returns the comments of struct
|
||||||
|
func (t PointerType) Comments() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents returns the documents of struct
|
||||||
|
func (t PointerType) Documents() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns a interface string, Its fixed value is interface{}
|
// Name returns a interface string, Its fixed value is interface{}
|
||||||
func (t InterfaceType) Name() string {
|
func (t InterfaceType) Name() string {
|
||||||
return t.RawName
|
return t.RawName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments returns the comments of struct
|
||||||
|
func (t InterfaceType) Comments() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents returns the documents of struct
|
||||||
|
func (t InterfaceType) Documents() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -12,6 +12,8 @@ type (
|
|||||||
// ApiSyntax describes the syntax grammar
|
// ApiSyntax describes the syntax grammar
|
||||||
ApiSyntax struct {
|
ApiSyntax struct {
|
||||||
Version string
|
Version string
|
||||||
|
Doc Doc
|
||||||
|
Comment Doc
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApiSpec describes a api file
|
// ApiSpec describes a api file
|
||||||
@ -26,6 +28,8 @@ type (
|
|||||||
// Import describes api import
|
// Import describes api import
|
||||||
Import struct {
|
Import struct {
|
||||||
Value string
|
Value string
|
||||||
|
Doc Doc
|
||||||
|
Comment Doc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group defines a set of routing information
|
// Group defines a set of routing information
|
||||||
@ -71,6 +75,10 @@ type (
|
|||||||
Docs Doc
|
Docs Doc
|
||||||
Handler string
|
Handler string
|
||||||
AtDoc AtDoc
|
AtDoc AtDoc
|
||||||
|
HandlerDoc Doc
|
||||||
|
HandlerComment Doc
|
||||||
|
Doc Doc
|
||||||
|
Comment Doc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Service describes api service
|
// Service describes api service
|
||||||
@ -82,6 +90,8 @@ type (
|
|||||||
// Type defines api type
|
// Type defines api type
|
||||||
Type interface {
|
Type interface {
|
||||||
Name() string
|
Name() string
|
||||||
|
Comments() []string
|
||||||
|
Documents() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefineStruct describes api structure
|
// DefineStruct describes api structure
|
||||||
|
Loading…
Reference in New Issue
Block a user