Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-06-05 13:44:45 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-06-06 14:32:12 +0300
commitfcd63de3a54fadcd30972654d8eb86dc4d889784 (patch)
tree5140863493b65783f73ecab8885f684fc623b1da /common
parent150d75738b54acddc485d363436757189144da6a (diff)
tpl/data: Misc header improvements, tests, allow multiple headers of same key
Closes #5617
Diffstat (limited to 'common')
-rw-r--r--common/types/convert.go41
-rw-r--r--common/types/convert_test.go2
2 files changed, 38 insertions, 5 deletions
diff --git a/common/types/convert.go b/common/types/convert.go
index 137029a0e..7beb3404e 100644
--- a/common/types/convert.go
+++ b/common/types/convert.go
@@ -15,21 +15,52 @@ package types
import (
"encoding/json"
+ "fmt"
"html/template"
+ "reflect"
"github.com/spf13/cast"
)
-// ToStringSlicePreserveString converts v to a string slice.
-// If v is a string, it will be wrapped in a string slice.
+// ToStringSlicePreserveString is the same as ToStringSlicePreserveStringE,
+// but it never fails.
func ToStringSlicePreserveString(v interface{}) []string {
+ vv, _ := ToStringSlicePreserveStringE(v)
+ return vv
+}
+
+// ToStringSlicePreserveStringE converts v to a string slice.
+// If v is a string, it will be wrapped in a string slice.
+func ToStringSlicePreserveStringE(v interface{}) ([]string, error) {
if v == nil {
- return nil
+ return nil, nil
}
if sds, ok := v.(string); ok {
- return []string{sds}
+ return []string{sds}, nil
+ }
+ result, err := cast.ToStringSliceE(v)
+ if err == nil {
+ return result, nil
}
- return cast.ToStringSlice(v)
+
+ // Probably []int or similar. Fall back to reflect.
+ vv := reflect.ValueOf(v)
+
+ switch vv.Kind() {
+ case reflect.Slice, reflect.Array:
+ result = make([]string, vv.Len())
+ for i := 0; i < vv.Len(); i++ {
+ s, err := cast.ToStringE(vv.Index(i).Interface())
+ if err != nil {
+ return nil, err
+ }
+ result[i] = s
+ }
+ return result, nil
+ default:
+ return nil, fmt.Errorf("failed to convert %T to a string slice", v)
+ }
+
}
// TypeToString converts v to a string if it's a valid string type.
diff --git a/common/types/convert_test.go b/common/types/convert_test.go
index d053ede60..364228f41 100644
--- a/common/types/convert_test.go
+++ b/common/types/convert_test.go
@@ -24,7 +24,9 @@ func TestToStringSlicePreserveString(t *testing.T) {
c := qt.New(t)
c.Assert(ToStringSlicePreserveString("Hugo"), qt.DeepEquals, []string{"Hugo"})
+ c.Assert(ToStringSlicePreserveString(qt.Commentf("Hugo")), qt.DeepEquals, []string{"Hugo"})
c.Assert(ToStringSlicePreserveString([]interface{}{"A", "B"}), qt.DeepEquals, []string{"A", "B"})
+ c.Assert(ToStringSlicePreserveString([]int{1, 3}), qt.DeepEquals, []string{"1", "3"})
c.Assert(ToStringSlicePreserveString(nil), qt.IsNil)
}