diff --git a/metadata/util/util.go b/metadata/util/util.go index 2e73df2..9d61fee 100644 --- a/metadata/util/util.go +++ b/metadata/util/util.go @@ -8,154 +8,209 @@ import ( "github.com/go-gost/core/metadata" ) -func GetBool(md metadata.Metadata, key string) (v bool) { - if md == nil || !md.IsExists(key) { - 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) { +func GetBool(md metadata.Metadata, keys ...string) (v bool) { + if md == nil { return } - switch vv := md.Get(key).(type) { - case bool: - if vv { - v = 1 + for _, key := range keys { + if !md.IsExists(key) { + continue } - case int: - return vv - case string: - v, _ = strconv.Atoi(vv) - return - } - return -} - -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 + switch vv := md.Get(key).(type) { + case bool: + v = vv + case int: + v = vv != 0 + case string: + v, _ = strconv.ParseBool(vv) } - } - 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) + break } return } -func GetStrings(md metadata.Metadata, key string) (ss []string) { - if md == nil || !md.IsExists(key) { +func GetInt(md metadata.Metadata, keys ...string) (v int) { + if md == nil { return } - switch v := md.Get(key).(type) { - case []string: - ss = v - case []any: - for _, vv := range v { - if s, ok := vv.(string); ok { - ss = append(ss, s) + for _, key := range keys { + if !md.IsExists(key) { + continue + } + switch vv := md.Get(key).(type) { + case bool: + 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 } -func GetStringMap(md metadata.Metadata, key string) (m map[string]any) { - if md == nil || !md.IsExists(key) { +func GetString(md metadata.Metadata, keys ...string) (v string) { + if md == nil { return } - switch vv := md.Get(key).(type) { - case map[string]any: - return vv - case map[any]any: - m = make(map[string]any) - for k, v := range vv { - m[fmt.Sprintf("%v", k)] = v + for _, key := range keys { + if !md.IsExists(key) { + continue } + + 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 } -func GetStringMapString(md metadata.Metadata, key string) (m map[string]string) { - if md == nil || !md.IsExists(key) { +func GetStrings(md metadata.Metadata, keys ...string) (ss []string) { + if md == nil { return } - 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) + for _, key := range keys { + if !md.IsExists(key) { + continue } - case map[any]any: - m = make(map[string]string) - for k, v := range vv { - m[fmt.Sprintf("%v", k)] = fmt.Sprintf("%v", v) + + switch v := md.Get(key).(type) { + case []string: + ss = v + case []any: + for _, vv := range v { + if s, ok := vv.(string); ok { + ss = append(ss, s) + } + } } + break } 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 +}