Skip to content

Commit 8192b40

Browse files
committed
feat: response time.Time, map type comment
1 parent 41b7a7f commit 8192b40

File tree

11 files changed

+153
-58
lines changed

11 files changed

+153
-58
lines changed

examples/docs/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 用户服务 (svc-user)
22

3+
version: _@1.0.1_
4+
35
用户相关的服务接口
46

57
1. [账户相关](./apis-account.md)
@@ -38,6 +40,8 @@
3840

3941
5.4. [int](./apis-demo.md#4-int)
4042

41-
5.5. [DemoMap](./apis-demo.md#5-DemoMap)
43+
5.5. [map](./apis-demo.md#5-map)
44+
45+
5.6. [xml](./apis-demo.md#6-xml)
4246

43-
5.6. [XML测试](./apis-demo.md#6-XML测试)
47+
5.7. [time](./apis-demo.md#7-time)

examples/docs/apis-demo.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
2. [struct嵌套](#2-struct嵌套)
77
3. [int数组](#3-int数组)
88
4. [int](#4-int)
9-
5. [DemoMap](#5-DemoMap)
10-
6. [XML测试](#6-XML测试)
9+
5. [map](#5-map)
10+
6. [xml](#6-xml)
11+
7. [time](#7-time)
1112

1213
## apis
1314

@@ -32,7 +33,7 @@ __Response__:
3233
123
3334
],
3435
"int_pointer": 123, //int
35-
"map": { //object(Map)
36+
"map": { //object(map[string]int)
3637
"abc": 123 //int
3738
},
3839
"object_1": { //object(handler.DemoObject)
@@ -99,7 +100,7 @@ __Response__:
99100

100101
---
101102

102-
### 5. DemoMap
103+
### 5. map
103104

104105
```text
105106
GET /user/demo/map
@@ -109,7 +110,7 @@ __Response__:
109110

110111
```javascript
111112
//StatusCode: 200 demo map
112-
{ //object(handler.DemoData)
113+
{ //object(map[string]handler.DemoData)
113114
"abc": { //object(handler.DemoData)
114115
"count": 123, //int
115116
"description": "abc", //string
@@ -120,7 +121,7 @@ __Response__:
120121
123
121122
],
122123
"int_pointer": 123, //int
123-
"map": { //object(Map)
124+
"map": { //object(map[string]int)
124125
"abc": 123 //int
125126
},
126127
"object_1": { //object(handler.DemoObject)
@@ -136,7 +137,7 @@ __Response__:
136137

137138
---
138139

139-
### 6. XML测试
140+
### 6. xml
140141

141142
author: _alovn_
142143

@@ -195,3 +196,27 @@ __Response__:
195196
```
196197

197198
---
199+
200+
### 7. time
201+
202+
author: _alovn_
203+
204+
```text
205+
GET /user/demo/time
206+
```
207+
208+
__Response__:
209+
210+
```javascript
211+
//StatusCode: 200
212+
{ //object(common.Response), 通用返回结果
213+
"code": 0, //int, 返回状态码
214+
"data": { //object(handler.DemoTime)
215+
"time_1": "2022-05-14T03:32:48.037644+08:00", //object(time.Time), example1
216+
"time_2": 2022-05-14 15:04:05 //object(time.Time), example2
217+
},
218+
"msg": "success" //string, 返回消息
219+
}
220+
```
221+
222+
---

examples/docs/apis-profile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ __Response__:
2121
{ //object(common.Response), 通用返回结果
2222
"code": 0, //int, 返回状态码
2323
"data": { //object(handler.ProfileResponse)
24-
"extends": { //object(Extends)
24+
"extends": { //object(map[string]string)
2525
"abc": "abc" //string, 扩展信息
2626
},
2727
"gender": 1, //uint8

examples/svc-greeter/main.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/xml"
55
"fmt"
6+
"time"
67

78
"github.com/alovn/apidoc/examples/common"
89
)
@@ -36,6 +37,7 @@ type TestData2 struct {
3637
}
3738
type Map map[string]interface{}
3839
type Map2 map[string]TestData2
40+
type Map3 map[string]string
3941
type Node struct {
4042
Name string
4143
Nodes map[string]Node
@@ -47,15 +49,17 @@ type TestData struct {
4749
// MyFloat64 float64
4850
// MyFloat32 float32
4951
// MyIntArray []int
50-
MyTestData2Array []TestData2
52+
MyTestData2Array []TestData2 `json:"my_test_data_2_array,omitempty"`
5153
// Int *int
5254
// MyInt MyInt
53-
MyInts []MyInt
54-
// Map Map `json:"amap"`
55-
// Map2 Map2
55+
MyInts []MyInt `json:"my_ints,omitempty"`
56+
// Map Map `json:"amap"`
57+
// Map map[string]string
58+
Map2 Map2 `json:"map_2,omitempty"`
5659
// Map3 map[string]TestData2
57-
Nodes map[string]Node
58-
// Map4 map[int]Node
60+
// Nodes map[string]Node
61+
// Map4 map[int]Node
62+
Time1 time.Time `xml:"time_1,omitempty"`
5963
}
6064

6165
type Request struct {
@@ -66,8 +70,9 @@ type Request struct {
6670
} //请求对象
6771

6872
type Struct1 struct {
69-
Name string
70-
Struct2
73+
Name string
74+
Time1 time.Time
75+
// Struct2
7176
}
7277
type Struct2 struct {
7378
Name2 string
@@ -93,17 +98,24 @@ type XMLData2 struct {
9398
Desc string
9499
}
95100

101+
type DemoTime struct {
102+
// Title string //测试
103+
Map map[string]string //map测试
104+
// Time1 time.Time `xml:"time_1" json:"time_1"` //example1
105+
// Time2 time.Time `xml:"time_2" json:"time_2" example:"2022-05-14 15:04:05"` //example2
106+
}
107+
96108
//@title 测试greeter
97109
//@api GET /greeter
98110
//@group greeter
99111
//@accept xml
100-
//@format xml
101-
//@request Request
112+
//@format json
113+
//@request1 Request
102114
//@response1 200 TestData "输出对象"
103115
//@response1 200 Struct1 "struct1"
104-
//@response 200 XMLResponse{data=[]XMLData} "输出xml"
105-
//@response 200 XMLResponse{data=[]XMLData2} "输出xml2"
106-
//@response1 200 common.Response{data=TestData} "输出对象 dd"
116+
//@response1 200 XMLResponse{data=[]XMLData} "输出xml"
117+
//@response1 200 XMLResponse{data=[]XMLData2} "输出xml2"
118+
//@response 200 common.Response{data=DemoTime} "输出对象 dd"
107119
//@response1 500 Response{code=10010,msg="异常"} "出错了"
108120
//@response1 500 int 错误
109121
func greet() {

examples/svc-user/handler/demo.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"encoding/xml"
66
"net/http"
7+
"time"
78

89
"github.com/alovn/apidoc/examples/common"
910
)
@@ -73,7 +74,7 @@ func (h *DemoHandler) Int(w http.ResponseWriter, r *http.Request) {
7374
}
7475

7576
//@api GET /demo/map
76-
//@title DemoMap
77+
//@title map
7778
//@group demo
7879
//@response 200 DemoMap "demo map"
7980
func (h *DemoHandler) Map(w http.ResponseWriter, r *http.Request) {
@@ -98,7 +99,7 @@ type DemoXMLResponse2 struct {
9899
} //XML测试返回对象2
99100

100101
//@api GET /demo/xml
101-
//@title XML测试
102+
//@title xml
102103
//@group demo
103104
//@accept xml
104105
//@request DemoXMLRequest
@@ -113,3 +114,23 @@ func (h *AddressHandler) XML(w http.ResponseWriter, r *http.Request) {
113114
b, _ := json.Marshal(res)
114115
_, _ = w.Write(b)
115116
}
117+
118+
type DemoTime struct {
119+
// Title string //测试
120+
Time1 time.Time `xml:"time_1" json:"time_1"` //example1
121+
Time2 time.Time `xml:"time_2" json:"time_2" example:"2022-05-14 15:04:05"` //example2
122+
}
123+
124+
//@api GET /demo/time
125+
//@title time
126+
//@group demo
127+
//@accept xml
128+
//@format json
129+
//@response 200 common.Response{code=0,msg="success",data=DemoTime}
130+
//@author alovn
131+
func (h *AddressHandler) Time(w http.ResponseWriter, r *http.Request) {
132+
address := DemoXMLRequest{}
133+
res := common.NewResponse(200, "获取成功", address)
134+
b, _ := json.Marshal(res)
135+
_, _ = w.Write(b)
136+
}

examples/svc-user/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
//@title 用户服务
1010
//@service svc-user
11+
//@version 1.0.1
1112
//@desc 用户相关的服务接口
1213
//@baseurl /user
1314
func main() {

gen/template/group_readme.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# {{.Title}} {{if .Service}} ({{.Service}}){{end}}
22

3+
{{- if .Version}}
4+
5+
version: _@{{.Version}}_
6+
{{- end}}
7+
38
{{- if .Description}}
49

510
{{.Description}}

gen/template/single.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# {{.Title}} {{if .Service}} ({{.Service}}){{end}}
22

3+
{{- if .Version}}
4+
5+
version: _@{{.Version}}_
6+
{{- end}}
7+
38
{{- if .Description}}
49

510
{{.Description}}

operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (operation *Operation) ParseRequestComment(commentLine string, astFile *ast
134134
for _, p := range schema.Properties {
135135
tags := p.ParameterTags()
136136
if tags != nil {
137-
if len(tags) > 0 && !p.HasJSONTag() {
137+
if len(tags) > 0 && !p.hasJSONTag() {
138138
parameterCount++
139139
}
140140
}

parser.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ func walkWith(excludes map[string]struct{}) func(path string, fileInfo os.FileIn
346346
}
347347
}
348348
}
349-
350349
return nil
351350
}
352351
}
@@ -361,15 +360,24 @@ func fullTypeName(pkgName, typeName string) string {
361360

362361
func (parser *Parser) getTypeSchema(typeName string, file *ast.File, field *ast.Field, parentSchema *TypeSchema) (*TypeSchema, error) {
363362
if IsGolangPrimitiveType(typeName) {
364-
name := field.Names[0].Name
365-
return &TypeSchema{
366-
Name: name,
367-
FullName: name,
368-
Type: typeName,
369-
Comment: strings.TrimSuffix(field.Comment.Text(), "\n"),
370-
Parent: parentSchema,
371-
TagValue: getAllTagValue(field),
372-
}, nil
363+
if field != nil {
364+
name := field.Names[0].Name
365+
return &TypeSchema{
366+
Name: name,
367+
FullName: name,
368+
Type: typeName,
369+
Comment: strings.TrimSuffix(field.Comment.Text(), "\n"),
370+
Parent: parentSchema,
371+
TagValue: getAllTagValue(field),
372+
}, nil
373+
} else {
374+
return &TypeSchema{
375+
Name: "",
376+
FullName: typeName,
377+
Type: typeName,
378+
Parent: parentSchema,
379+
}, nil
380+
}
373381
}
374382

375383
typeSpecDef := parser.packages.FindTypeSpec(typeName, file, true)
@@ -412,9 +420,12 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, field *ast.Field
412420
if err != nil {
413421
return nil, err
414422
}
415-
if field != nil && field.Names != nil {
416-
schema.Name = field.Names[0].Name
423+
if field != nil {
424+
if field.Names != nil {
425+
schema.Name = field.Names[0].Name
426+
}
417427
schema.TagValue = getAllTagValue(field)
428+
schema.Comment = strings.TrimSuffix(field.Comment.Text(), "\n")
418429
}
419430
return schema, err
420431
case *ast.Ident:
@@ -423,10 +434,6 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, field *ast.Field
423434
if keyIdent, ok := expr.Key.(*ast.Ident); ok {
424435
if IsGolangPrimitiveType(keyIdent.Name) {
425436
example := strings.Trim(getFieldExample(keyIdent.Name, nil), "\"") //map key example
426-
if _, ok := expr.Value.(*ast.InterfaceType); ok {
427-
return &TypeSchema{Type: OBJECT, Properties: nil}, nil
428-
}
429-
430437
mapSchema := &TypeSchema{
431438
Type: OBJECT,
432439
Properties: map[string]*TypeSchema{},
@@ -438,7 +445,7 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, field *ast.Field
438445
}
439446
mapSchema.TagValue = schema.TagValue
440447
mapSchema.Name = schema.Name
441-
mapSchema.FullName = schema.FullName
448+
mapSchema.FullName = fmt.Sprintf("map[%s]%s", keyIdent.Name, schema.FullName)
442449

443450
schema.Name = example
444451
schema.TagValue = ""
@@ -467,6 +474,7 @@ func (parser *Parser) parseTypeExpr(file *ast.File, field *ast.Field, typeExpr a
467474
return &TypeSchema{
468475
Name: field.Names[0].Name,
469476
Type: ANY,
477+
FullName: ANY,
470478
Parent: parentSchema,
471479
TagValue: getAllTagValue(field),
472480
}, nil
@@ -513,9 +521,6 @@ func (parser *Parser) parseTypeExpr(file *ast.File, field *ast.Field, typeExpr a
513521
if keyIdent, ok := expr.Key.(*ast.Ident); ok {
514522
if IsGolangPrimitiveType(keyIdent.Name) {
515523
example := strings.Trim(getFieldExample(keyIdent.Name, nil), "\"") //map key example
516-
if _, ok := expr.Value.(*ast.InterfaceType); ok {
517-
return &TypeSchema{Type: OBJECT, Properties: nil}, nil
518-
}
519524
mapSchema := &TypeSchema{
520525
Type: OBJECT,
521526
Properties: map[string]*TypeSchema{},
@@ -527,7 +532,7 @@ func (parser *Parser) parseTypeExpr(file *ast.File, field *ast.Field, typeExpr a
527532
}
528533
mapSchema.TagValue = schema.TagValue
529534
mapSchema.Name = schema.Name
530-
mapSchema.FullName = schema.FullName
535+
mapSchema.FullName = fmt.Sprintf("map[%s]%s", keyIdent.Name, expr.Value)
531536

532537
schema.Name = example
533538
schema.TagValue = ""
@@ -564,6 +569,9 @@ func (parser *Parser) parseStruct(typeSpecDef *TypeSpecDef, file *ast.File, fiel
564569
if err != nil {
565570
return nil, err
566571
}
572+
if schema == nil {
573+
continue
574+
}
567575
if field.Names == nil { //nested struct, replace with child properties
568576
for _, p := range schema.Properties {
569577
if _, ok := structSchema.Properties[strings.ToLower(p.Name)]; !ok { //if not exists key

0 commit comments

Comments
 (0)