mirror of
https://github.com/beego/bee.git
synced 2024-11-22 15:10:54 +00:00
Default values in structure
Swagger default value for golang stucture. format: doc:"default(my_defaul_value)" type MyStruct struct { Template string `json:"template" doc:"default(<h1>Hello, {{ name }}! </h1>)"` Name string `json:"name"` MyNumber int64 `json:"temp" doc:"default(10)"` MyBool bool `json:"bl" doc:"default(true)"` MyFloat float64 `json:"fl" doc:"default(12.1234)"` }
This commit is contained in:
parent
244e9ccdc9
commit
31406ebe5a
24
g_docs.go
24
g_docs.go
@ -770,6 +770,30 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string
|
|||||||
|
|
||||||
var tagValues []string
|
var tagValues []string
|
||||||
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
|
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":
|
||||||
|
mp.Default, _ = strconv.Atoi(res[1])
|
||||||
|
case "bool":
|
||||||
|
mp.Default, _ = strconv.ParseBool(res[1])
|
||||||
|
case "float64":
|
||||||
|
mp.Default, _ = strconv.ParseFloat(res[1], 64)
|
||||||
|
case "float32":
|
||||||
|
mp.Default, _ = strconv.ParseFloat(res[1], 32)
|
||||||
|
default:
|
||||||
|
mp.Default = res[1]
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
ColorLog("[WARN] Invalid default value: %s\n", defaultValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tag := stag.Get("json")
|
tag := stag.Get("json")
|
||||||
|
|
||||||
if tag != "" {
|
if tag != "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user