From eb9d483127ccea1ec4d684af8558f5c65efde91b Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Thu, 20 Oct 2022 21:02:24 +0800 Subject: [PATCH] fix GetString for metadata --- metadata/util/util.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/metadata/util/util.go b/metadata/util/util.go index 8772a44..2e73df2 100644 --- a/metadata/util/util.go +++ b/metadata/util/util.go @@ -79,9 +79,29 @@ func GetDuration(md metadata.Metadata, key string) (v time.Duration) { } func GetString(md metadata.Metadata, key string) (v string) { - if md != nil { - v, _ = md.Get(key).(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 }