feat: support struct response with @Failture

This commit is contained in:
aecra 2022-06-30 21:52:36 +08:00
parent ef570ffb7b
commit f6f26bff3f
1 changed files with 40 additions and 14 deletions

View File

@ -695,23 +695,49 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
} }
opts.Parameters = append(opts.Parameters, para) opts.Parameters = append(opts.Parameters, para)
} else if strings.HasPrefix(t, "@Failure") { } else if strings.HasPrefix(t, "@Failure") {
ss := strings.TrimSpace(t[len("@Failure"):])
rs := swagger.Response{} rs := swagger.Response{}
st := strings.TrimSpace(t[len("@Failure"):]) respCode, pos := peekNextSplitString(ss)
var cd []rune ss = strings.TrimSpace(ss[pos:])
var start bool respType, pos := peekNextSplitString(ss)
for i, s := range st { if respType == "{object}" || respType == "{array}" {
if unicode.IsSpace(s) { isArray := respType == "{array}"
if start { ss = strings.TrimSpace(ss[pos:])
rs.Description = strings.TrimSpace(st[i+1:]) schemaName, pos := peekNextSplitString(ss)
break if schemaName == "" {
} else { beeLogger.Log.Fatalf("[%s.%s] Schema must follow {object} or {array}", controllerName, funcName)
continue
}
} }
start = true if strings.HasPrefix(schemaName, "[]") {
cd = append(cd, s) schemaName = schemaName[2:]
isArray = true
}
schema := swagger.Schema{}
if sType, ok := basicTypes[schemaName]; ok {
typeFormat := strings.Split(sType, ":")
schema.Type = typeFormat[0]
schema.Format = typeFormat[1]
} else {
m, mod, realTypes := getModel(schemaName)
schema.Ref = "#/definitions/" + m
if _, ok := modelsList[pkgpath+controllerName]; !ok {
modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema)
}
modelsList[pkgpath+controllerName][schemaName] = mod
appendModels(pkgpath, controllerName, realTypes)
}
if isArray {
rs.Schema = &swagger.Schema{
Type: astTypeArray,
Items: &schema,
}
} else {
rs.Schema = &schema
}
rs.Description = strings.TrimSpace(ss[pos:])
} else {
rs.Description = strings.TrimSpace(ss)
} }
opts.Responses[string(cd)] = rs opts.Responses[respCode] = rs
} else if strings.HasPrefix(t, "@Deprecated") { } else if strings.HasPrefix(t, "@Deprecated") {
opts.Deprecated, _ = strconv.ParseBool(strings.TrimSpace(t[len("@Deprecated"):])) opts.Deprecated, _ = strconv.ParseBool(strings.TrimSpace(t[len("@Deprecated"):]))
} else if strings.HasPrefix(t, "@Accept") { } else if strings.HasPrefix(t, "@Accept") {