Skip to content

Commit 67f3877

Browse files
committed
feat: response replace field
1 parent 19ba0a4 commit 67f3877

File tree

8 files changed

+87
-34
lines changed

8 files changed

+87
-34
lines changed

examples/docs/apis-greeter.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,31 @@ greeter分组说明
99
```text
1010
GET /greeter
1111
```
12+
**Response**:
1213

13-
14-
Response:
1514
```json
1615

1716
// 输出对象 dd
18-
// HTTP StatusCode: 200
17+
// StatusCode: 200
1918
// object main
2019
{
2120
"code": 0, // int, 返回状态码
2221
"msg": "返回消息", // string, 返回文本消息
23-
"data": nil // any, 返回的具体数据
22+
"data": {
23+
"my_title": "example" // string, 标题
24+
}
25+
}
26+
```
27+
28+
```json
29+
30+
// 出错了
31+
// StatusCode: 500
32+
// object main
33+
{
34+
"data": null, // any, 返回的具体数据
35+
"code": 10010, // int, 返回状态码
36+
"msg": "异常" // string, 返回文本消息
2437
}
2538
```
2639

@@ -30,16 +43,15 @@ Response:
3043
```text
3144
GET /greeter2
3245
```
46+
**Response**:
3347

34-
35-
Response:
3648
```json
3749

3850
// 输出对象 dd
39-
// HTTP StatusCode: 200
51+
// StatusCode: 200
4052
// object main
4153
{
42-
"my_title": ""
54+
"my_title": "example" // string, 标题
4355
}
4456
```
4557

examples/docs/apis-hello.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
```text
1010
GET /hello
1111
```
12-
12+
**Response**:
1313

1414

1515
### 测试hello2
1616

1717
```text
1818
GET /hello2
1919
```
20-
20+
**Response**:
2121

examples/docs/apis-ungrouped.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Ungrouped apis
99
```text
1010
GET /other
1111
```
12-
12+
**Response**:
1313

examples/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type Response struct {
2323
Msg string `json:"msg,omitempty" example:"返回消息"` //返回文本消息
2424
Data interface{} `json:"data,omitempty"` //返回的具体数据
2525
}
26-
type TestData struct {
27-
MyTitle string `json:"my_title,omitempty"`
26+
type TestData struct { //测试数据
27+
MyTitle string `json:"my_title,omitempty"` //标题
2828
}
2929

3030
type Request struct {
@@ -39,8 +39,8 @@ type Request struct {
3939
//@group greeter
4040
//@accept json
4141
//@request Request
42-
//@response1 200 Response{data=TestData} 输出对象 dd
43-
//@response 200 Response 输出对象 dd
42+
//@response 200 Response{data=TestData} 输出对象 dd
43+
//@response 500 Response{code=10010,msg="异常"} 出错了
4444
func greet() {
4545
var msg = "Hello World!"
4646
fmt.Println(msg)

gen/template/group_apis.tpl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ author: {{$v.Author}}
1111
```text
1212
{{$v.HTTPMethod}} {{$v.Api}}
1313
```
14-
14+
**Response**:
1515
{{range $res := $v.Responses}}
16-
Response:
1716
```json
1817
{{if $res.Description}}
1918
// {{$res.Description}}{{end}}
20-
// HTTP StatusCode: {{$res.StatusCode}}
19+
// StatusCode: {{$res.StatusCode}}
2120
{{$res.Examples}}
2221
```
2322
{{end}}

operation.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010

1111
var (
1212
// 200 Response{data=Data} examples
13-
responsePattern = regexp.MustCompile(`^(\d+)\s+([\w\-.\\{}=,\[\]]+)\s+(.*)?`)
13+
// responsePattern = regexp.MustCompile(`^(\d+)\s+([\w\-.\\{}=,\[\]]+)\s+(.*)?`)
14+
responsePattern = regexp.MustCompile(`^(\d+)\s+([\w\-.\\{}=,\"\[\]]+|[\w.]+{.*?})\s+(.*)?`)
1415
// responsePattern = regexp.MustCompile(`^([\w,]+)\s+([\w{}]+)\s+([\w\-.\\{}=,\[\]]+)[^"]*(.*)?`)
1516

1617
// ResponseType{data1=Type1,data2=Type2}.
@@ -114,10 +115,13 @@ func (operation *Operation) ParseRouterComment(commentLine string) error {
114115

115116
// ParseResponseComment parses comment for given `response` comment string.
116117
func (operation *Operation) ParseResponseComment(commentLine string, astFile *ast.File) error {
118+
operation.parser.clearStructStack()
119+
fmt.Println(commentLine)
117120
matches := responsePattern.FindStringSubmatch(commentLine)
118121
for i, m := range matches {
119122
fmt.Println(i, m)
120123
}
124+
fmt.Println(commentLine, len(matches))
121125
if len(matches) != 4 {
122126
return nil
123127
}
@@ -172,30 +176,41 @@ func (operation *Operation) parseCombinedObject(refType string, astFile *ast.Fil
172176
}
173177
fmt.Println("parseCombinedObject matches:", matches)
174178

175-
schema, err := operation.parseObject(matches[1], astFile)
179+
schemaA, err := operation.parseObject(matches[1], astFile)
176180
if err != nil {
177181
return nil, err
178182
}
179-
fmt.Printf("parseCombinedObject schema=%+v\n", schema)
180-
181-
fields, props := parseFields(matches[2]), map[string]TypeSchema{}
183+
fmt.Printf("parseCombinedObject schema=%+v\n", schemaA)
182184

185+
fields := parseFields(matches[2])
186+
// props := map[string]TypeSchema{}
183187
for _, field := range fields {
184188
keyVal := strings.SplitN(field, "=", 2)
185189
if len(keyVal) == 2 {
186-
schema, err := operation.parseObject(keyVal[1], astFile)
187-
if err != nil {
188-
return nil, err
190+
// fmt.Println("keyVal", keyVal[0], keyVal[1]) //data TestData
191+
// if is number or string wrap, replace it
192+
if isReplaceValue(keyVal[1]) {
193+
if p, ok := schemaA.Properties[keyVal[0]]; ok {
194+
p.Example = keyVal[1]
195+
}
196+
} else {
197+
schema, err := operation.parseObject(keyVal[1], astFile)
198+
if err != nil {
199+
return nil, err
200+
}
201+
schemaA.Properties[keyVal[0]] = schema //data=xx
202+
// props[keyVal[0]] = *schema
189203
}
190204

191-
props[keyVal[0]] = *schema
192205
}
193206
}
194-
fmt.Printf("props: %+v\n", props)
195-
if len(props) == 0 {
196-
return schema, nil
197-
}
198-
return schema, nil
207+
// fmt.Printf("props: %+v\n", props)
208+
// if len(props) == 0 {
209+
// return schema, nil
210+
// }
211+
fmt.Printf("parseCombinedObject2 schema=%+v\n", schemaA)
212+
fmt.Println(schemaA.Properties["data"])
213+
return schemaA, nil
199214

200215
// return spec.ComposedSchema(*schema, spec.Schema{
201216
// SchemaProps: spec.SchemaProps{
@@ -205,6 +220,22 @@ func (operation *Operation) parseCombinedObject(refType string, astFile *ast.Fil
205220
// }), nil
206221
}
207222

223+
func isReplaceValue(val string) bool {
224+
if (strings.HasPrefix(val, "\"") && strings.HasSuffix(val, "\"")) || (strings.HasPrefix(val, "'") && strings.HasSuffix(val, "'")) {
225+
return true
226+
}
227+
_, err := strconv.ParseInt(val, 10, 64)
228+
if err == nil {
229+
return true
230+
}
231+
_, err = strconv.ParseFloat(val, 64)
232+
if err == nil {
233+
return true
234+
}
235+
_, err = strconv.ParseBool(val)
236+
return err == nil
237+
}
238+
208239
func parseFields(s string) []string {
209240
nestLevel := 0
210241
return strings.FieldsFunc(s, func(char rune) bool {

parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ func (parser *Parser) isInStructStack(typeSpecDef *TypeSpecDef) bool {
392392
return false
393393
}
394394

395+
func (parser *Parser) clearStructStack() {
396+
parser.structStack = parser.structStack[:0]
397+
}
398+
395399
// ParseDefinition parses given type spec that corresponds to the type under
396400
// given name and package
397401
func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*TypeSchema, error) {

spec.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ func getExampleValue(typeName string, field *ast.Field) string {
159159
case "rune":
160160
return fmt.Sprintf("'%c'", exampleRune(example))
161161
case "string":
162-
return fmt.Sprintf("\"%s\"", example)
162+
return fmt.Sprintf("\"%s\"", exampleString(example))
163163
case "bool":
164164
return fmt.Sprintf("%t", exampleBool(example))
165165
case "any":
166-
return "nil"
166+
return "null"
167167
}
168168

169169
return example
@@ -199,6 +199,13 @@ func exampleBool(example string) bool {
199199
return val
200200
}
201201

202+
func exampleString(example string) string {
203+
if example == "" {
204+
return "example"
205+
}
206+
return example
207+
}
208+
202209
//getFieldName format json/xml
203210
func getFieldName(name string, field *ast.Field, format string) string {
204211
if field != nil {

0 commit comments

Comments
 (0)