summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-10-10 10:18:30 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-10-10 13:30:39 +0200
commit0d7b05be4cb2391cbd280f6109c01ec2d3d7e0c6 (patch)
tree47fb0303d7d43bc3d19e7e9736433b3715696b17 /tpl
parent71b18a0786894893eafa01263a0915149ed303ec (diff)
tpl: Make getJSON/getCVS accept non-string args
This broke for the Twitter simple shortcode now that Shortcodes accepts typed arguments. Fixes #6382
Diffstat (limited to 'tpl')
-rw-r--r--tpl/data/data.go14
-rw-r--r--tpl/data/data_test.go6
2 files changed, 16 insertions, 4 deletions
diff --git a/tpl/data/data.go b/tpl/data/data.go
index 15f039294..f64ba0127 100644
--- a/tpl/data/data.go
+++ b/tpl/data/data.go
@@ -23,6 +23,8 @@ import (
"net/http"
"strings"
+ "github.com/spf13/cast"
+
"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/deps"
_errors "github.com/pkg/errors"
@@ -54,8 +56,8 @@ type Namespace struct {
// The data separator can be a comma, semi-colon, pipe, etc, but only one character.
// If you provide multiple parts for the URL they will be joined together to the final URL.
// GetCSV returns nil or a slice slice to use in a short code.
-func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err error) {
- url := strings.Join(urlParts, "")
+func (ns *Namespace) GetCSV(sep string, urlParts ...interface{}) (d [][]string, err error) {
+ url := joinURL(urlParts)
cache := ns.cacheGetCSV
unmarshal := func(b []byte) (bool, error) {
@@ -93,9 +95,9 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err e
// GetJSON expects one or n-parts of a URL to a resource which can either be a local or a remote one.
// If you provide multiple parts they will be joined together to the final URL.
// GetJSON returns nil or parsed JSON to use in a short code.
-func (ns *Namespace) GetJSON(urlParts ...string) (interface{}, error) {
+func (ns *Namespace) GetJSON(urlParts ...interface{}) (interface{}, error) {
var v interface{}
- url := strings.Join(urlParts, "")
+ url := joinURL(urlParts)
cache := ns.cacheGetJSON
req, err := http.NewRequest("GET", url, nil)
@@ -122,6 +124,10 @@ func (ns *Namespace) GetJSON(urlParts ...string) (interface{}, error) {
return v, nil
}
+func joinURL(urlParts []interface{}) string {
+ return strings.Join(cast.ToStringSlice(urlParts), "")
+}
+
// parseCSV parses bytes of CSV data into a slice slice string or an error
func parseCSV(c []byte, sep string) ([][]string, error) {
if len(sep) != 1 {
diff --git a/tpl/data/data_test.go b/tpl/data/data_test.go
index 04575707c..8bd4edc98 100644
--- a/tpl/data/data_test.go
+++ b/tpl/data/data_test.go
@@ -204,6 +204,12 @@ func TestGetJSON(t *testing.T) {
}
}
+func TestJoinURL(t *testing.T) {
+ t.Parallel()
+ c := qt.New(t)
+ c.Assert(joinURL([]interface{}{"https://foo?id=", 32}), qt.Equals, "https://foo?id=32")
+}
+
func TestParseCSV(t *testing.T) {
t.Parallel()
c := qt.New(t)