summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-03-12 17:09:49 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-03-12 19:26:19 +0100
commit5eadc4c0a8e5c51e72670591c4b7877e79c15e3c (patch)
treed0f9449daf2b078cb9d6ce496dd83fc8f6262676 /common
parent18cb21ff2e4a60e7094908e4d6113a9d5a086316 (diff)
metrics: Fix --templateMetricsHints
Also improve non-string comparisons. Fixes #7048
Diffstat (limited to 'common')
-rw-r--r--common/types/convert.go42
1 files changed, 41 insertions, 1 deletions
diff --git a/common/types/convert.go b/common/types/convert.go
index b55330757..24e01c273 100644
--- a/common/types/convert.go
+++ b/common/types/convert.go
@@ -13,7 +13,11 @@
package types
-import "github.com/spf13/cast"
+import (
+ "html/template"
+
+ "github.com/spf13/cast"
+)
// ToStringSlicePreserveString converts v to a string slice.
// If v is a string, it will be wrapped in a string slice.
@@ -26,3 +30,39 @@ func ToStringSlicePreserveString(v interface{}) []string {
}
return cast.ToStringSlice(v)
}
+
+// TypeToString converts v to a string if it's a valid string type.
+// Note that this will not try to convert numeric values etc.,
+// use ToString for that.
+func TypeToString(v interface{}) (string, bool) {
+ switch s := v.(type) {
+ case string:
+ return s, true
+ case template.HTML:
+ return string(s), true
+ case template.CSS:
+ return string(s), true
+ case template.HTMLAttr:
+ return string(s), true
+ case template.JS:
+ return string(s), true
+ case template.JSStr:
+ return string(s), true
+ case template.URL:
+ return string(s), true
+ case template.Srcset:
+ return string(s), true
+ }
+
+ return "", false
+}
+
+// ToString converts v to a string.
+func ToString(v interface{}) string {
+ if s, ok := TypeToString(v); ok {
+ return s
+ }
+
+ return cast.ToString(v)
+
+}