metrics: Fix --templateMetricsHints

Also improve non-string comparisons.

Fixes #7048
This commit is contained in:
Bjørn Erik Pedersen
2020-03-12 17:09:49 +01:00
parent 18cb21ff2e
commit 5eadc4c0a8
3 changed files with 85 additions and 10 deletions

View File

@@ -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)
}