refactor and rename folder to group (#106)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* update doc

* remove update

* remove no need

* remove no need

* goctl add jwt support

* goctl add jwt support

* goctl add jwt support

* goctl support import

* goctl support import

* support return ()

* support return ()

* revert

* format api

* refactor and rename folder to group

Co-authored-by: kingxt <dream4kingxt@163.com>
master
kingxt 4 years ago committed by GitHub
parent b282304054
commit e7d46aa6e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -95,20 +95,20 @@ service user-api {
)
@server(
handler: GetUserHandler
folder: user
group: user
)
get /api/user/:name(getRequest) returns(getResponse)
@server(
handler: CreateUserHandler
folder: user
group: user
)
post /api/users/create(createRequest)
}
@server(
jwt: Auth
folder: profile
group: profile
)
service user-api {
@doc(summary: user title)
@ -135,7 +135,7 @@ service user-api {
1. info部分描述了api基本信息比如Authapi是哪个用途。
2. type部分type类型声明和golang语法兼容。
3. service部分service代表一组服务一个服务可以由多组名称相同的service组成可以针对每一组service配置jwt和auth认证另外通过folder属性可以指定service生成所在子目录。
3. service部分service代表一组服务一个服务可以由多组名称相同的service组成可以针对每一组service配置jwt和auth认证另外通过group属性可以指定service生成所在子目录。
service里面包含api路由比如上面第一组service的第一个路由doc用来描述此路由的用途GetProfileHandler表示处理这个路由的handler
`get /api/profile/:name(getRequest) returns(getResponse)` 中get代表api的请求方式get/post/put/delete, `/api/profile/:name` 描述了路由path`:name`通过
请求getRequest里面的属性赋值getResponse为返回的结构体这两个类型都定义在2描述的类型中。

@ -183,9 +183,9 @@ func getHandlerBaseName(handler string) string {
}
func getHandlerFolderPath(group spec.Group, route spec.Route) string {
folder, ok := apiutil.GetAnnotationValue(route.Annotations, "server", folderProperty)
folder, ok := apiutil.GetAnnotationValue(route.Annotations, "server", groupProperty)
if !ok {
folder, ok = apiutil.GetAnnotationValue(group.Annotations, "server", folderProperty)
folder, ok = apiutil.GetAnnotationValue(group.Annotations, "server", groupProperty)
if !ok {
return handlerDir
}

@ -113,9 +113,9 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
}
func getLogicFolderPath(group spec.Group, route spec.Route) string {
folder, ok := util.GetAnnotationValue(route.Annotations, "server", folderProperty)
folder, ok := util.GetAnnotationValue(route.Annotations, "server", groupProperty)
if !ok {
folder, ok = util.GetAnnotationValue(group.Annotations, "server", folderProperty)
folder, ok = util.GetAnnotationValue(group.Annotations, "server", groupProperty)
if !ok {
return logicDir
}

@ -136,9 +136,9 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
for _, group := range api.Service.Groups {
for _, route := range group.Routes {
folder, ok := apiutil.GetAnnotationValue(route.Annotations, "server", folderProperty)
folder, ok := apiutil.GetAnnotationValue(route.Annotations, "server", groupProperty)
if !ok {
folder, ok = apiutil.GetAnnotationValue(group.Annotations, "server", folderProperty)
folder, ok = apiutil.GetAnnotationValue(group.Annotations, "server", groupProperty)
if !ok {
continue
}
@ -165,11 +165,11 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
return nil, fmt.Errorf("missing handler annotation for route %q", r.Path)
}
handler = getHandlerBaseName(handler) + "Handler(serverCtx)"
folder, ok := apiutil.GetAnnotationValue(r.Annotations, "server", folderProperty)
folder, ok := apiutil.GetAnnotationValue(r.Annotations, "server", groupProperty)
if ok {
handler = folder + "." + strings.ToUpper(handler[:1]) + handler[1:]
} else {
folder, ok = apiutil.GetAnnotationValue(g.Annotations, "server", folderProperty)
folder, ok = apiutil.GetAnnotationValue(g.Annotations, "server", groupProperty)
if ok {
handler = folder + "." + strings.ToUpper(handler[:1]) + handler[1:]
}

@ -8,5 +8,5 @@ const (
handlerDir = interval + "handler"
logicDir = interval + "logic"
typesDir = interval + typesPacket
folderProperty = "folder"
groupProperty = "group"
)

@ -88,15 +88,17 @@ func (p *serviceEntityParser) parseLine(line string, api *spec.ApiSpec, annos []
}
}
if len(fields) < 3 {
if len(fields) < 2 {
return defaultErr
}
method := fields[0]
path := fields[1]
req := fields[2]
var req string
var resp string
if len(fields) > 2 {
req = fields[2]
}
if stringx.Contains(fields, returnsTag) {
if fields[len(fields)-1] != returnsTag {
resp = fields[len(fields)-1]

@ -90,21 +90,20 @@ func MatchStruct(api string) (*ApiStruct, error) {
continue
}
if strings.HasPrefix(line, "import") {
if isImportBeginLine(line) {
parseImport = true
}
if parseImport && (strings.HasPrefix(line, "type") || strings.HasPrefix(line, "@server") ||
strings.HasPrefix(line, "service")) {
if parseImport && (isTypeBeginLine(line) || isServiceBeginLine(line)) {
parseImport = false
result.Imports = segment
segment = line + "\n"
continue
}
if strings.HasPrefix(line, "type") {
if isTypeBeginLine(line) {
parseType = true
}
if strings.HasPrefix(line, "@server") || strings.HasPrefix(line, "service") {
if isServiceBeginLine(line) {
if parseType {
parseType = false
result.StructBody = segment
@ -122,3 +121,15 @@ func MatchStruct(api string) (*ApiStruct, error) {
result.Service = segment
return &result, nil
}
func isImportBeginLine(line string) bool {
return strings.HasPrefix(line, "import") && strings.HasSuffix(line, ".api")
}
func isTypeBeginLine(line string) bool {
return strings.HasPrefix(line, "type")
}
func isServiceBeginLine(line string) bool {
return strings.HasPrefix(line, "@server(") || (strings.HasPrefix(line, "service") && strings.HasSuffix(line, "{"))
}

@ -82,20 +82,20 @@ service user-api {
)
@server(
handler: GetUserHandler
folder: user
group: user
)
get /api/user/:name(getRequest) returns(getResponse)
@server(
handler: CreateUserHandler
folder: user
group: user
)
post /api/users/create(createRequest)
}
@server(
jwt: Auth
folder: profile
group: profile
)
service user-api {
@doc(summary: user title)
@ -121,7 +121,7 @@ service user-api {
1. info部分描述了api基本信息比如Authapi是哪个用途。
2. type部分type类型声明和golang语法兼容。
3. service部分service代表一组服务一个服务可以由多组名称相同的service组成可以针对每一组service配置folder属性来指定service生成所在子目录。
3. service部分service代表一组服务一个服务可以由多组名称相同的service组成可以针对每一组service配置group属性来指定service生成所在子目录。
service里面包含api路由比如上面第一组service的第一个路由doc用来描述此路由的用途GetProfileHandler表示处理这个路由的handler
`get /api/profile/:name(getRequest) returns(getResponse)` 中get代表api的请求方式get/post/put/delete, `/api/profile/:name` 描述了路由path`:name`通过
请求getRequest里面的属性赋值getResponse为返回的结构体这两个类型都定义在2描述的类型中。

Loading…
Cancel
Save