mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 17:20:24 +08:00
api support empty request or empty response (#72)
* rebase upstream * rebase * trim no need line * trim no need line * trim no need line * update doc * remove update * api support empty request or empty response * update readme Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
parent
9a57993e83
commit
598ff6d0fc
@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
)
|
||||
|
||||
@ -57,13 +58,14 @@ type serviceEntityParser struct {
|
||||
}
|
||||
|
||||
func (p *serviceEntityParser) parseLine(line string, api *spec.ApiSpec, annos []spec.Annotation) error {
|
||||
line = strings.TrimSpace(line)
|
||||
var defaultErr = fmt.Errorf("wrong line %q, %q", line, routeSyntax)
|
||||
|
||||
line = strings.TrimSpace(line)
|
||||
var buffer = new(bytes.Buffer)
|
||||
buffer.WriteString(line)
|
||||
reader := bufio.NewReader(buffer)
|
||||
var builder strings.Builder
|
||||
var fields []string
|
||||
var fields = make([]string, 0)
|
||||
for {
|
||||
ch, _, err := reader.ReadRune()
|
||||
if err != nil {
|
||||
@ -87,17 +89,22 @@ func (p *serviceEntityParser) parseLine(line string, api *spec.ApiSpec, annos []
|
||||
}
|
||||
|
||||
if len(fields) < 3 {
|
||||
return fmt.Errorf("wrong line %q, %q", line, routeSyntax)
|
||||
return defaultErr
|
||||
}
|
||||
|
||||
method := fields[0]
|
||||
path := fields[1]
|
||||
req := fields[2]
|
||||
var returns string
|
||||
if len(fields) > 4 {
|
||||
returns = fields[4]
|
||||
if fields[3] != returnsTag {
|
||||
return fmt.Errorf("wrong line %q, %q", line, routeSyntax)
|
||||
|
||||
if stringx.Contains(fields, returnsTag) {
|
||||
if fields[len(fields)-1] != returnsTag {
|
||||
returns = fields[len(fields)-1]
|
||||
} else {
|
||||
return defaultErr
|
||||
}
|
||||
if fields[2] == returnsTag {
|
||||
req = ""
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,18 +17,11 @@
|
||||
> -api 自定义api所在路径
|
||||
>
|
||||
> -dir 自定义生成目录
|
||||
|
||||
#### 保持goctl总是最新版
|
||||
|
||||
第一次运行会在~/.goctl里增加下面两行:
|
||||
|
||||
```Plain Text
|
||||
url = http://47.97.184.41:7777/
|
||||
```
|
||||
|
||||
#### API 语法说明
|
||||
|
||||
```Plain Text
|
||||
```golang
|
||||
|
||||
info(
|
||||
title: doc title
|
||||
desc: >
|
||||
@ -128,7 +121,7 @@ service user-api {
|
||||
|
||||
1. info部分:描述了api基本信息,比如Auth,api是哪个用途。
|
||||
2. type部分:type类型声明和golang语法兼容。
|
||||
3. service部分:service代表一组服务,一个服务可以由多组名称相同的service组成,可以针对每一组service配置jwt和auth认证,另外通过folder属性可以指定service生成所在子目录。
|
||||
3. service部分:service代表一组服务,一个服务可以由多组名称相同的service组成,可以针对每一组service配置folder属性来指定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…
Reference in New Issue
Block a user