Skip to content

Commit d5e700e

Browse files
committed
feat: response array
1 parent 76457ee commit d5e700e

File tree

6 files changed

+36
-80
lines changed

6 files changed

+36
-80
lines changed

examples/docs/apis-greeter.md

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,34 @@ greeter分组说明
44

55
## Apis
66

7-
### @api 测试greeter
7+
### 测试greeter
88

99
```text
1010
GET /greeter
1111
```
1212

13-
**Request**:
14-
parameters|type|required|validate|example|description
15-
--|--|--|--|--|--
16-
**id**|*header,query*|true||12357|this id
17-
**tid**|*param*|true|required|123|
18-
**token**|*header*|false||"example"|
1913
**Response**:
2014

2115
```json
2216
// StatusCode: 200
2317

2418
// 输出对象 dd
25-
{ //object(common.Response)
26-
"Code": 123, //int
27-
"Common": 1.23, //float64
28-
"Common2": "example", //string
29-
"Data": null, //any
30-
"Msg": "example", //string
19+
{ //object(main.Response), 通用返回结果
20+
"code": 0, //int, 返回状态码
3121
"data": { //object(main.TestData)
32-
"MyIntArray": [ //array[int]
22+
"MyInt": 123, //int
23+
"MyInts": [ //array[int]
3324
123
34-
],
35-
"MyTestData2Array": [ //array[main.TestData2]
36-
{ //object(main.TestData2)
37-
"MyAge2": 123, //int
38-
"MyTitle2": "example" //string, 标题2
39-
}
40-
],
41-
"data2": { //object(main.TestData2)
42-
"MyAge2": 123, //int
43-
"MyTitle2": "example" //string, 标题2
44-
},
45-
"my_title": "example" //string, 标题
46-
}
47-
}
48-
```
49-
50-
```json
51-
// StatusCode: 500
52-
53-
// 出错了
54-
{ //object(main.Response), 通用返回结果
55-
"code": 10010, //int, 返回状态码
56-
"msg": "异常", //string, 返回文本消息
25+
]
26+
},
27+
"msg": "返回消息" //string, 返回文本消息
5728
}
5829
```
5930

60-
### @api 测试greeter2
31+
### 测试greeter2
6132

6233
```text
6334
GET /greeter2
6435
```
6536

6637
**Response**:
67-
68-
```json
69-
// StatusCode: 200
70-
71-
// 输出对象 dd
72-
{ //object(main.TestData)
73-
"MyIntArray": [ //array[int]
74-
123
75-
],
76-
"MyTestData2Array": [ //array[main.TestData2]
77-
{ //object(main.TestData2)
78-
"MyAge2": 123, //int
79-
"MyTitle2": "example" //string, 标题2
80-
}
81-
],
82-
"data2": { //object(main.TestData2)
83-
"MyAge2": 123, //int
84-
"MyTitle2": "example" //string, 标题2
85-
},
86-
"my_title": "example" //string, 标题
87-
}
88-
```

examples/docs/apis-hello.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
## Apis
66

7-
### @api 测试hello
7+
### 测试hello
88

99
```text
1010
GET /hello
1111
```
1212

1313
**Response**:
1414

15-
### @api 测试hello2
15+
### 测试hello2
1616

1717
```text
1818
GET /hello2

examples/docs/apis-ungrouped.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Ungrouped apis
44

55
## Apis
66

7-
### @api 测试other
7+
### 测试other
88

99
```text
1010
GET /other

examples/main.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ func main() {
1313
//@group greeter
1414
//@title greeter分组
1515
//@desc greeter分组说明
16-
resigter(greet)
17-
18-
resigter(greet)
16+
group(greet)
1917
}
2018

21-
func resigter(f func()) {
19+
func group(f func()) {
2220

2321
}
2422

23+
type MyInt int
24+
2525
type Response struct {
2626
Code int `json:"code" example:"0"` //返回状态码
2727
Msg string `json:"msg,omitempty" example:"返回消息"` //返回文本消息
@@ -33,13 +33,16 @@ type TestData2 struct {
3333
MyAge2 int
3434
}
3535
type TestData struct {
36-
MyTitle string `json:"my_title,omitempty"` //标题
37-
Data2 *TestData2 `json:"data2,omitempty"`
36+
// MyTitle string `json:"my_title,omitempty"` //标题
37+
// Data2 *TestData2 `json:"data2,omitempty"`
3838
// MyIntData int
3939
// MyFloat64 float64
4040
// MyFloat32 float32
41-
MyIntArray []int
42-
MyTestData2Array []TestData2
41+
// MyIntArray []int
42+
// MyTestData2Array []TestData2
43+
// Int *int
44+
MyInt MyInt
45+
MyInts []MyInt
4346
}
4447

4548
type Request struct {
@@ -53,20 +56,21 @@ type Request struct {
5356
//@api GET /greeter
5457
//@group greeter
5558
//@accept json
56-
//@request Request
57-
//@response 200 common.Response{data=TestData} 输出对象 dd
58-
//@response 500 Response{code=10010,msg="异常"} 出错了
59+
//@request1 Request
60+
//@response 200 Response{data=TestData} 输出对象 dd
61+
//@response1 200 common.Response{data=TestData} 输出对象 dd
62+
//@response1 500 Response{code=10010,msg="异常"} 出错了
5963
//@response1 500 int 错误
60-
func greet() *common.Response {
64+
func greet() {
6165
var msg = "Hello World!"
6266
fmt.Println(msg)
63-
return &common.Response{}
67+
fmt.Println(&common.Response{})
6468
}
6569

6670
//@title 测试greeter2
6771
//@api GET /greeter2
6872
//@group greeter
69-
//@response 200 TestData 输出对象 dd
73+
//@response1 200 TestData 输出对象 dd
7074
func greet2() {
7175
var msg = "Hello World!"
7276
fmt.Println(msg)

gen/template/group_apis.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Apis
66
{{range $k,$v := .Apis}}
7-
### @api {{$v.Title}}
7+
### {{$v.Title}}
88
{{if $v.Author}}
99
author: {{$v.Author}}
1010
{{end}}

parser.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func (parser *Parser) getTypeSchema(typeName string, file *ast.File, field *ast.
376376
}
377377
fmt.Println("typeSpecDef", typeSpecDef.Name())
378378

379-
schema, err := parser.ParseDefinition(typeSpecDef)
379+
schema, err := parser.ParseDefinition(typeSpecDef, field)
380380
if err != nil {
381381
return nil, err
382382
}
@@ -407,7 +407,7 @@ func (parser *Parser) clearStructStack() {
407407

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

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

0 commit comments

Comments
 (0)