1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 10:10:53 +00:00

Merge pull request #292 from ihippik/patch-4

Default values in structure
This commit is contained in:
astaxie 2016-10-11 10:16:51 +08:00 committed by GitHub
commit 1d8aa1a48d

View File

@ -769,7 +769,42 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string
}
var tagValues []string
var err error
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
defaultValue := stag.Get("doc")
if defaultValue != ""{
r, _ := regexp.Compile(`default\((.*)\)`)
if r.MatchString(defaultValue) {
res := r.FindStringSubmatch(defaultValue)
mp.Default = res[1]
switch realType{
case "int","int64", "int32", "int16", "int8":
if mp.Default, err = strconv.Atoi(res[1]); err != nil{
ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1])
}
case "bool":
if mp.Default, err = strconv.ParseBool(res[1]); err != nil{
ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1])
}
case "float64":
if mp.Default, err = strconv.ParseFloat(res[1], 64); err != nil{
ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1])
}
case "float32":
if mp.Default, err = strconv.ParseFloat(res[1], 32); err != nil{
ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1])
}
default:
mp.Default = res[1]
}
}else{
ColorLog("[WARN] Invalid default value: %s\n", defaultValue)
}
}
tag := stag.Get("json")
if tag != "" {