doc-generate/internal/swagger/annotation.go

80 lines
1.7 KiB
Go

package swagger
import (
"strconv"
"go-doc/internal/util"
)
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
if len(properties) == 0 {
return def
}
val, ok := properties[key]
if !ok || len(val) == 0 {
return def
}
//I think this function and those below should handle error, but they didn't.
//Since a default value (def) is provided, any parsing errors will result in the default being returned.
// Try to unquote if the value is quoted, otherwise use as-is
str := val
if unquoted, err := strconv.Unquote(val); err == nil {
str = unquoted
}
if len(str) == 0 {
return def
}
res, _ := strconv.ParseBool(str)
return res
}
func getStringFromKVOrDefault(properties map[string]string, key string, def string) string {
if len(properties) == 0 {
return def
}
val, ok := properties[key]
if !ok || len(val) == 0 {
return def
}
// Try to unquote if the value is quoted, otherwise use as-is
str := val
if unquoted, err := strconv.Unquote(val); err == nil {
str = unquoted
}
return str
}
func getListFromInfoOrDefault(properties map[string]string, key string, def []string) []string {
if len(properties) == 0 {
return def
}
val, ok := properties[key]
if !ok || len(val) == 0 {
return def
}
// Try to unquote if the value is quoted, otherwise use as-is
str := val
if unquoted, err := strconv.Unquote(val); err == nil {
str = unquoted
}
resp := util.FieldsAndTrimSpace(str, commaRune)
if len(resp) == 0 {
return def
}
return resp
}
func getFirstUsableString(def ...string) string {
if len(def) == 0 {
return ""
}
for _, val := range def {
str, err := strconv.Unquote(val)
if err == nil && len(str) != 0 {
return str
}
}
return ""
}