|
|
@ -1,6 +1,7 @@
|
|
|
|
package gogen
|
|
|
|
package gogen
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
_ "embed"
|
|
|
|
goformat "go/format"
|
|
|
|
goformat "go/format"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
@ -10,314 +11,45 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const testApiTemplate = `
|
|
|
|
var (
|
|
|
|
info(
|
|
|
|
//go:embed testdata/test_api_template.api
|
|
|
|
title: doc title
|
|
|
|
testApiTemplate string
|
|
|
|
desc: ">
|
|
|
|
//go:embed testdata/test_multi_service_template.api
|
|
|
|
doc description first part,
|
|
|
|
testMultiServiceTemplate string
|
|
|
|
doc description second part<"
|
|
|
|
//go:embed testdata/ap_ino_info.api
|
|
|
|
version: 1.0
|
|
|
|
apiNoInfo string
|
|
|
|
|
|
|
|
//go:embed testdata/invalid_api_file.api
|
|
|
|
|
|
|
|
invalidApiFile string
|
|
|
|
|
|
|
|
//go:embed testdata/anonymous_annotation.api
|
|
|
|
|
|
|
|
anonymousAnnotation string
|
|
|
|
|
|
|
|
//go:embed testdata/api_has_middleware.api
|
|
|
|
|
|
|
|
apiHasMiddleware string
|
|
|
|
|
|
|
|
//go:embed testdata/api_jwt.api
|
|
|
|
|
|
|
|
apiJwt string
|
|
|
|
|
|
|
|
//go:embed testdata/api_jwt_with_middleware.api
|
|
|
|
|
|
|
|
apiJwtWithMiddleware string
|
|
|
|
|
|
|
|
//go:embed testdata/api_has_no_request.api
|
|
|
|
|
|
|
|
apiHasNoRequest string
|
|
|
|
|
|
|
|
//go:embed testdata/api_route_test.api
|
|
|
|
|
|
|
|
apiRouteTest string
|
|
|
|
|
|
|
|
//go:embed testdata/has_comment_api_test.api
|
|
|
|
|
|
|
|
hasCommentApiTest string
|
|
|
|
|
|
|
|
//go:embed testdata/has_inline_no_exist_test.api
|
|
|
|
|
|
|
|
hasInlineNoExistTest string
|
|
|
|
|
|
|
|
//go:embed testdata/import_api.api
|
|
|
|
|
|
|
|
importApi string
|
|
|
|
|
|
|
|
//go:embed testdata/has_import_api.api
|
|
|
|
|
|
|
|
hasImportApi string
|
|
|
|
|
|
|
|
//go:embed testdata/no_struct_tag_api.api
|
|
|
|
|
|
|
|
noStructTagApi string
|
|
|
|
|
|
|
|
//go:embed testdata/nest_type_api.api
|
|
|
|
|
|
|
|
nestTypeApi string
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
type Request struct { // TODO: test
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // }
|
|
|
|
|
|
|
|
} // TODO: test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
// C0
|
|
|
|
|
|
|
|
group: greet/s1
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
// C1
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
// C2
|
|
|
|
|
|
|
|
@server( // C3
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response) // hello
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// C4
|
|
|
|
|
|
|
|
@handler NoResponseHandler // C5
|
|
|
|
|
|
|
|
get /greet/get(Request)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const testMultiServiceTemplate = `
|
|
|
|
|
|
|
|
info(
|
|
|
|
|
|
|
|
title: doc title
|
|
|
|
|
|
|
|
desc: doc description first part
|
|
|
|
|
|
|
|
version: 1.0
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: NoResponseHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/get(Request)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiNoInfo = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const invalidApiFile = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const anonymousAnnotation = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiHasMiddleware = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
middleware: TokenValidate
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiJwt = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
jwt: Auth
|
|
|
|
|
|
|
|
signature: true
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiJwtWithMiddleware = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
jwt: Auth
|
|
|
|
|
|
|
|
jwtTransition: Trans
|
|
|
|
|
|
|
|
middleware: TokenValidate
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiHasNoRequest = `
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
post /greet/ping ()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiRouteTest = `
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler NormalHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
@handler NoResponseHandler
|
|
|
|
|
|
|
|
get /greet/from/:sex(Request)
|
|
|
|
|
|
|
|
@handler NoRequestHandler
|
|
|
|
|
|
|
|
get /greet/from/request returns (Response)
|
|
|
|
|
|
|
|
@handler NoRequestNoResponseHandler
|
|
|
|
|
|
|
|
get /greet/from
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hasCommentApiTest = `
|
|
|
|
|
|
|
|
type Inline struct {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Inline
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // name in path
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"msg"` + "`" + ` // message
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@doc ("helloworld")
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hasInlineNoExistTest = `
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Inline
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + ` // message
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@doc ("helloworld")
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const importApi = `
|
|
|
|
|
|
|
|
type ImportData struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hasImportApi = `
|
|
|
|
|
|
|
|
import "importApi.api"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + ` // message
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@server(
|
|
|
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const noStructTagApi = `
|
|
|
|
|
|
|
|
type Request {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type XXX {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
|
|
|
Response {
|
|
|
|
|
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
B struct {}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const nestTypeApi = `
|
|
|
|
|
|
|
|
type Request {
|
|
|
|
|
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
|
|
|
|
|
|
XXX struct {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service A-api {
|
|
|
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
|
|
|
get /greet/from/:name(Request)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestParser(t *testing.T) {
|
|
|
|
func TestParser(t *testing.T) {
|
|
|
|
filename := "greet.api"
|
|
|
|
filename := "greet.api"
|
|
|
|
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
|
|
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
|
@ -537,8 +269,15 @@ func validate(t *testing.T, api string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func validateWithCamel(t *testing.T, api, camel string) {
|
|
|
|
func validateWithCamel(t *testing.T, api, camel string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
dir := "workspace"
|
|
|
|
err := DoGenProject(api, dir, camel)
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := pathx.MkdirIfNotExist(dir)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = initMod(dir)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = DoGenProject(api, dir, camel)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if strings.HasSuffix(path, ".go") {
|
|
|
|
if strings.HasSuffix(path, ".go") {
|
|
|
@ -550,6 +289,11 @@ func validateWithCamel(t *testing.T, api, camel string) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func initMod(mod string) error {
|
|
|
|
|
|
|
|
_, err := execx.Run("go mod init "+mod, mod)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func validateCode(code string) error {
|
|
|
|
func validateCode(code string) error {
|
|
|
|
_, err := goformat.Source([]byte(code))
|
|
|
|
_, err := goformat.Source([]byte(code))
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|