自定义配置参数不区分大小写

This commit is contained in:
sk 2024-12-04 16:35:50 +08:00
parent 5a67d8f073
commit 22dc0a767a
1 changed files with 60 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package common
import ( import (
"encoding/json" "encoding/json"
"strings"
"mongo.games.com/goserver/core" "mongo.games.com/goserver/core"
) )
@ -53,6 +54,13 @@ func (this *CustomConfiguration) GetString(key string) string {
return str return str
} }
} }
if v, exist := (*this)[strings.ToLower(key)]; exist {
if str, ok := v.(string); ok {
return str
}
}
return "" return ""
} }
@ -67,6 +75,18 @@ func (this *CustomConfiguration) GetStrings(key string) (strs []string) {
return return
} }
} }
if v, exist := (*this)[strings.ToLower(key)]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if str, ok := s.(string); ok {
strs = append(strs, str)
}
}
return
}
}
return return
} }
@ -86,6 +106,23 @@ func (this *CustomConfiguration) GetCustomCfgs(key string) (strs []*CustomConfig
return return
} }
} }
if v, exist := (*this)[strings.ToLower(key)]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if data, ok := s.(map[string]interface{}); ok {
var pkg *CustomConfiguration
modelBuff, _ := json.Marshal(data)
err := json.Unmarshal(modelBuff, &pkg)
if err == nil {
strs = append(strs, pkg)
}
}
}
return
}
}
return return
} }
@ -100,6 +137,18 @@ func (this *CustomConfiguration) GetInts(key string) (strs []int) {
return return
} }
} }
if v, exist := (*this)[strings.ToLower(key)]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if str, ok := s.(float64); ok {
strs = append(strs, int(str))
}
}
return
}
}
return return
} }
func (this *CustomConfiguration) GetInt(key string) int { func (this *CustomConfiguration) GetInt(key string) int {
@ -108,6 +157,12 @@ func (this *CustomConfiguration) GetInt(key string) int {
return int(val) return int(val)
} }
} }
if v, exist := (*this)[strings.ToLower(key)]; exist {
if val, ok := v.(float64); ok {
return int(val)
}
}
return 0 return 0
} }
@ -117,5 +172,10 @@ func (this *CustomConfiguration) GetBool(key string) bool {
return val return val
} }
} }
if v, exist := (*this)[strings.ToLower(key)]; exist {
if val, ok := v.(bool); ok {
return val
}
}
return false return false
} }