update metadata utils
This commit is contained in:
@ -8,154 +8,209 @@ import (
|
|||||||
"github.com/go-gost/core/metadata"
|
"github.com/go-gost/core/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetBool(md metadata.Metadata, key string) (v bool) {
|
func GetBool(md metadata.Metadata, keys ...string) (v bool) {
|
||||||
if md == nil || !md.IsExists(key) {
|
if md == nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
switch vv := md.Get(key).(type) {
|
|
||||||
case bool:
|
|
||||||
return vv
|
|
||||||
case int:
|
|
||||||
return vv != 0
|
|
||||||
case string:
|
|
||||||
v, _ = strconv.ParseBool(vv)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetInt(md metadata.Metadata, key string) (v int) {
|
|
||||||
if md == nil || !md.IsExists(key) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vv := md.Get(key).(type) {
|
for _, key := range keys {
|
||||||
case bool:
|
if !md.IsExists(key) {
|
||||||
if vv {
|
continue
|
||||||
v = 1
|
|
||||||
}
|
}
|
||||||
case int:
|
switch vv := md.Get(key).(type) {
|
||||||
return vv
|
case bool:
|
||||||
case string:
|
v = vv
|
||||||
v, _ = strconv.Atoi(vv)
|
case int:
|
||||||
return
|
v = vv != 0
|
||||||
}
|
case string:
|
||||||
return
|
v, _ = strconv.ParseBool(vv)
|
||||||
}
|
|
||||||
|
|
||||||
func GetFloat(md metadata.Metadata, key string) (v float64) {
|
|
||||||
if md == nil || !md.IsExists(key) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch vv := md.Get(key).(type) {
|
|
||||||
case float64:
|
|
||||||
return vv
|
|
||||||
case int:
|
|
||||||
return float64(vv)
|
|
||||||
case string:
|
|
||||||
v, _ = strconv.ParseFloat(vv, 64)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDuration(md metadata.Metadata, key string) (v time.Duration) {
|
|
||||||
if md == nil || !md.IsExists(key) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch vv := md.Get(key).(type) {
|
|
||||||
case int:
|
|
||||||
return time.Duration(vv) * time.Second
|
|
||||||
case string:
|
|
||||||
v, _ = time.ParseDuration(vv)
|
|
||||||
if v == 0 {
|
|
||||||
n, _ := strconv.Atoi(vv)
|
|
||||||
v = time.Duration(n) * time.Second
|
|
||||||
}
|
}
|
||||||
}
|
break
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetString(md metadata.Metadata, key string) (v string) {
|
|
||||||
if md == nil || !md.IsExists(key) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch vv := md.Get(key).(type) {
|
|
||||||
case string:
|
|
||||||
v = vv
|
|
||||||
case int:
|
|
||||||
v = strconv.FormatInt(int64(vv), 10)
|
|
||||||
case int64:
|
|
||||||
v = strconv.FormatInt(vv, 10)
|
|
||||||
case uint:
|
|
||||||
v = strconv.FormatUint(uint64(vv), 10)
|
|
||||||
case uint64:
|
|
||||||
v = strconv.FormatUint(uint64(vv), 10)
|
|
||||||
case bool:
|
|
||||||
v = strconv.FormatBool(vv)
|
|
||||||
case float32:
|
|
||||||
v = strconv.FormatFloat(float64(vv), 'f', -1, 32)
|
|
||||||
case float64:
|
|
||||||
v = strconv.FormatFloat(float64(vv), 'f', -1, 64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetStrings(md metadata.Metadata, key string) (ss []string) {
|
func GetInt(md metadata.Metadata, keys ...string) (v int) {
|
||||||
if md == nil || !md.IsExists(key) {
|
if md == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch v := md.Get(key).(type) {
|
for _, key := range keys {
|
||||||
case []string:
|
if !md.IsExists(key) {
|
||||||
ss = v
|
continue
|
||||||
case []any:
|
}
|
||||||
for _, vv := range v {
|
switch vv := md.Get(key).(type) {
|
||||||
if s, ok := vv.(string); ok {
|
case bool:
|
||||||
ss = append(ss, s)
|
if vv {
|
||||||
|
v = 1
|
||||||
|
}
|
||||||
|
case int:
|
||||||
|
v = vv
|
||||||
|
case string:
|
||||||
|
v, _ = strconv.Atoi(vv)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFloat(md metadata.Metadata, keys ...string) (v float64) {
|
||||||
|
if md == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
if !md.IsExists(key) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch vv := md.Get(key).(type) {
|
||||||
|
case float64:
|
||||||
|
v = vv
|
||||||
|
case int:
|
||||||
|
v = float64(vv)
|
||||||
|
case string:
|
||||||
|
v, _ = strconv.ParseFloat(vv, 64)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDuration(md metadata.Metadata, keys ...string) (v time.Duration) {
|
||||||
|
if md == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
if !md.IsExists(key) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch vv := md.Get(key).(type) {
|
||||||
|
case int:
|
||||||
|
v = time.Duration(vv) * time.Second
|
||||||
|
case string:
|
||||||
|
v, _ = time.ParseDuration(vv)
|
||||||
|
if v == 0 {
|
||||||
|
n, _ := strconv.Atoi(vv)
|
||||||
|
v = time.Duration(n) * time.Second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetStringMap(md metadata.Metadata, key string) (m map[string]any) {
|
func GetString(md metadata.Metadata, keys ...string) (v string) {
|
||||||
if md == nil || !md.IsExists(key) {
|
if md == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vv := md.Get(key).(type) {
|
for _, key := range keys {
|
||||||
case map[string]any:
|
if !md.IsExists(key) {
|
||||||
return vv
|
continue
|
||||||
case map[any]any:
|
|
||||||
m = make(map[string]any)
|
|
||||||
for k, v := range vv {
|
|
||||||
m[fmt.Sprintf("%v", k)] = v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch vv := md.Get(key).(type) {
|
||||||
|
case string:
|
||||||
|
v = vv
|
||||||
|
case int:
|
||||||
|
v = strconv.FormatInt(int64(vv), 10)
|
||||||
|
case int64:
|
||||||
|
v = strconv.FormatInt(vv, 10)
|
||||||
|
case uint:
|
||||||
|
v = strconv.FormatUint(uint64(vv), 10)
|
||||||
|
case uint64:
|
||||||
|
v = strconv.FormatUint(uint64(vv), 10)
|
||||||
|
case bool:
|
||||||
|
v = strconv.FormatBool(vv)
|
||||||
|
case float32:
|
||||||
|
v = strconv.FormatFloat(float64(vv), 'f', -1, 32)
|
||||||
|
case float64:
|
||||||
|
v = strconv.FormatFloat(float64(vv), 'f', -1, 64)
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetStringMapString(md metadata.Metadata, key string) (m map[string]string) {
|
func GetStrings(md metadata.Metadata, keys ...string) (ss []string) {
|
||||||
if md == nil || !md.IsExists(key) {
|
if md == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vv := md.Get(key).(type) {
|
for _, key := range keys {
|
||||||
case map[string]any:
|
if !md.IsExists(key) {
|
||||||
m = make(map[string]string)
|
continue
|
||||||
for k, v := range vv {
|
|
||||||
m[k] = fmt.Sprintf("%v", v)
|
|
||||||
}
|
}
|
||||||
case map[any]any:
|
|
||||||
m = make(map[string]string)
|
switch v := md.Get(key).(type) {
|
||||||
for k, v := range vv {
|
case []string:
|
||||||
m[fmt.Sprintf("%v", k)] = fmt.Sprintf("%v", v)
|
ss = v
|
||||||
|
case []any:
|
||||||
|
for _, vv := range v {
|
||||||
|
if s, ok := vv.(string); ok {
|
||||||
|
ss = append(ss, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetStringMap(md metadata.Metadata, keys ...string) (m map[string]any) {
|
||||||
|
if md == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
if !md.IsExists(key) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch vv := md.Get(key).(type) {
|
||||||
|
case map[string]any:
|
||||||
|
m = vv
|
||||||
|
case map[any]any:
|
||||||
|
m = make(map[string]any)
|
||||||
|
for k, v := range vv {
|
||||||
|
m[fmt.Sprintf("%v", k)] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetStringMapString(md metadata.Metadata, keys ...string) (m map[string]string) {
|
||||||
|
if md == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
if !md.IsExists(key) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch vv := md.Get(key).(type) {
|
||||||
|
case map[string]any:
|
||||||
|
m = make(map[string]string)
|
||||||
|
for k, v := range vv {
|
||||||
|
m[k] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
case map[any]any:
|
||||||
|
m = make(map[string]string)
|
||||||
|
for k, v := range vv {
|
||||||
|
m[fmt.Sprintf("%v", k)] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user