Skip to content

Commit

Permalink
feat: response array
Browse files Browse the repository at this point in the history
  • Loading branch information
alovn committed May 9, 2022
1 parent 76457ee commit d5e700e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 80 deletions.
69 changes: 9 additions & 60 deletions examples/docs/apis-greeter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,34 @@ greeter分组说明

## Apis

### @api 测试greeter
### 测试greeter

```text
GET /greeter
```

**Request**:
parameters|type|required|validate|example|description
--|--|--|--|--|--
**id**|*header,query*|true||12357|this id
**tid**|*param*|true|required|123|
**token**|*header*|false||"example"|
**Response**:

```json
// StatusCode: 200

// 输出对象 dd
{ //object(common.Response)
"Code": 123, //int
"Common": 1.23, //float64
"Common2": "example", //string
"Data": null, //any
"Msg": "example", //string
{ //object(main.Response), 通用返回结果
"code": 0, //int, 返回状态码
"data": { //object(main.TestData)
"MyIntArray": [ //array[int]
"MyInt": 123, //int
"MyInts": [ //array[int]
123
],
"MyTestData2Array": [ //array[main.TestData2]
{ //object(main.TestData2)
"MyAge2": 123, //int
"MyTitle2": "example" //string, 标题2
}
],
"data2": { //object(main.TestData2)
"MyAge2": 123, //int
"MyTitle2": "example" //string, 标题2
},
"my_title": "example" //string, 标题
}
}
```

```json
// StatusCode: 500

// 出错了
{ //object(main.Response), 通用返回结果
"code": 10010, //int, 返回状态码
"msg": "异常", //string, 返回文本消息
]
},
"msg": "返回消息" //string, 返回文本消息
}
```

### @api 测试greeter2
### 测试greeter2

```text
GET /greeter2
```

**Response**:

```json
// StatusCode: 200

// 输出对象 dd
{ //object(main.TestData)
"MyIntArray": [ //array[int]
123
],
"MyTestData2Array": [ //array[main.TestData2]
{ //object(main.TestData2)
"MyAge2": 123, //int
"MyTitle2": "example" //string, 标题2
}
],
"data2": { //object(main.TestData2)
"MyAge2": 123, //int
"MyTitle2": "example" //string, 标题2
},
"my_title": "example" //string, 标题
}
```
4 changes: 2 additions & 2 deletions examples/docs/apis-hello.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

## Apis

### @api 测试hello
### 测试hello

```text
GET /hello
```

**Response**:

### @api 测试hello2
### 测试hello2

```text
GET /hello2
Expand Down
2 changes: 1 addition & 1 deletion examples/docs/apis-ungrouped.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Ungrouped apis

## Apis

### @api 测试other
### 测试other

```text
GET /other
Expand Down
32 changes: 18 additions & 14 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ func main() {
//@group greeter
//@title greeter分组
//@desc greeter分组说明
resigter(greet)

resigter(greet)
group(greet)
}

func resigter(f func()) {
func group(f func()) {

}

type MyInt int

type Response struct {
Code int `json:"code" example:"0"` //返回状态码
Msg string `json:"msg,omitempty" example:"返回消息"` //返回文本消息
Expand All @@ -33,13 +33,16 @@ type TestData2 struct {
MyAge2 int
}
type TestData struct {
MyTitle string `json:"my_title,omitempty"` //标题
Data2 *TestData2 `json:"data2,omitempty"`
// MyTitle string `json:"my_title,omitempty"` //标题
// Data2 *TestData2 `json:"data2,omitempty"`
// MyIntData int
// MyFloat64 float64
// MyFloat32 float32
MyIntArray []int
MyTestData2Array []TestData2
// MyIntArray []int
// MyTestData2Array []TestData2
// Int *int
MyInt MyInt
MyInts []MyInt
}

type Request struct {
Expand All @@ -53,20 +56,21 @@ type Request struct {
//@api GET /greeter
//@group greeter
//@accept json
//@request Request
//@response 200 common.Response{data=TestData} 输出对象 dd
//@response 500 Response{code=10010,msg="异常"} 出错了
//@request1 Request
//@response 200 Response{data=TestData} 输出对象 dd
//@response1 200 common.Response{data=TestData} 输出对象 dd
//@response1 500 Response{code=10010,msg="异常"} 出错了
//@response1 500 int 错误
func greet() *common.Response {
func greet() {
var msg = "Hello World!"
fmt.Println(msg)
return &common.Response{}
fmt.Println(&common.Response{})
}

//@title 测试greeter2
//@api GET /greeter2
//@group greeter
//@response 200 TestData 输出对象 dd
//@response1 200 TestData 输出对象 dd
func greet2() {
var msg = "Hello World!"
fmt.Println(msg)
Expand Down
2 changes: 1 addition & 1 deletion gen/template/group_apis.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Apis
{{range $k,$v := .Apis}}
### @api {{$v.Title}}
### {{$v.Title}}
{{if $v.Author}}
author: {{$v.Author}}
{{end}}
Expand Down
7 changes: 5 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (parser *Parser) getTypeSchema(typeName string, file *ast.File, field *ast.
}
fmt.Println("typeSpecDef", typeSpecDef.Name())

schema, err := parser.ParseDefinition(typeSpecDef)
schema, err := parser.ParseDefinition(typeSpecDef, field)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func (parser *Parser) clearStructStack() {

// ParseDefinition parses given type spec that corresponds to the type under
// given name and package
func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*TypeSchema, error) {
func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, field *ast.Field) (*TypeSchema, error) {
typeName := typeSpecDef.FullName()
refTypeName := TypeDocName(typeName, typeSpecDef.TypeSpec)

Expand All @@ -434,6 +434,9 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*TypeSchema, er
schema.Name = typeSpecDef.Name()
schema.FullName = typeSpecDef.FullName()
return schema, err
case *ast.Ident:
fmt.Println("myint:", expr.Name)
return parser.getTypeSchema(expr.Name, typeSpecDef.File, field, false)
default:
fmt.Printf("Type definition of type '%T' is not supported yet. Using 'object' instead.\n", typeSpecDef.TypeSpec.Type)
}
Expand Down

0 comments on commit d5e700e

Please sign in to comment.