add: 多个响应定义方式

This commit is contained in:
yuanzhao
2023-03-17 14:16:34 +08:00
parent ef57bb2899
commit 1d4e2bfc52
2 changed files with 34 additions and 6 deletions

View File

@@ -160,6 +160,10 @@ func getUrl(opts map[string][]parser.Option) string {
return urlPath
}
func isValidHTTPStatusCode(code int) bool {
return code >= 100 && code <= 599
}
func rpcToPath(pge string, service parser.ServiceRpc, swagger *openapi.Spec, nowDirProtoc []parser.ProtocFileParser, allProtoc map[string][]parser.ProtocFileParser, serviceOpt map[string]parser.Option) {
for _, options := range service.Opt {
for _, option := range options {
@@ -196,11 +200,28 @@ func rpcToPath(pge string, service parser.ServiceRpc, swagger *openapi.Spec, now
if endpoint != nil {
code := option.Map["Code"]
resp := option.Map["Response"]
endpoint.Responses[code] = &openapi.Response{
Description: option.Doc,
Schema: &openapi.Schema{
Ref: "#/definitions/" + resp,
},
codeInt, _ := strconv.Atoi(code)
if isValidHTTPStatusCode(codeInt) {
endpoint.Responses[code] = &openapi.Response{
Description: option.Doc,
Schema: &openapi.Schema{
Ref: "#/definitions/" + resp,
},
}
} else {
// 非法的状态码,自动补充一个
for i := 201; i < 599; i++ {
intCode := strconv.Itoa(i)
if _, ok := endpoint.Responses[intCode]; !ok {
endpoint.Responses[intCode] = &openapi.Response{
Description: fmt.Sprintf("logic(%s)", code) + option.Doc,
Schema: &openapi.Schema{
Ref: "#/definitions/" + resp,
},
}
break
}
}
}
}
}

View File

@@ -204,11 +204,12 @@ func serverOption(l []*word, offset int) (Option, int) {
}
val, i := GetFistWord(l[offset:])
var opVal string
opMap := make(map[string]string)
var opMap map[string]string = nil
if HasPrefix(l[offset+i].Str, "\"") {
opVal = val[1 : len(val)-1]
offset = offset + i
} else {
opMap = make(map[string]string)
// option 值是对象
st, et := GetBrackets(l[offset+i-1:], "{", "}")
nl := l[offset+i-1+st : offset+i-1+et]
@@ -268,7 +269,9 @@ func protoService(l []*word, offset int) (Service, int) {
case "option":
var val Option
val, offset = serverOption(nl, offset)
val.Doc = strings.ReplaceAll(doc, "//", "")
got.Opt[val.Key] = val
doc = ""
case "rpc":
var val ServiceRpc
val, offset = protoRpc(nl, offset)
@@ -309,18 +312,22 @@ func protoRpc(l []*word, offset int) (ServiceRpc, int) {
st, et := GetBrackets(l[offset:], "{", "}")
newOffset := offset + et + 1
nl := l[offset+st : newOffset]
doc := ""
for offset := 0; offset < len(nl); offset++ {
work := nl[offset]
switch work.Ty {
case wordT_line:
case wordT_division:
case wordT_doc:
doc += work.Str
case wordT_word:
switch work.Str {
case "option":
var val Option
val, offset = serverOption(nl, offset)
val.Doc = strings.ReplaceAll(doc, "//", "")
opt[val.Key] = append(opt[val.Key], val)
doc = ""
}
}
}