summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
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)
+
+}