mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-02 22:52:51 +02:00
tpl: Allow 'Querify' to take lone slice/interface argument
Querify can now take a lone string/interface slice (with string keys) as a parameter, or multiple string parameters, to build URL queries. Querify earlier used 'Dictionary' to add key/value pairs to a map to build URL queries. Changed to dynamically generate ordered key/value pairs. Cannot take string slice as key (earlier possible due to Dictionary). Added tests and benchmarks for querify. Closes #6735
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
504c78da4b
commit
c46fc838a9
@@ -424,13 +424,39 @@ func (ns *Namespace) Last(limit interface{}, seq interface{}) (interface{}, erro
|
||||
// Querify encodes the given parameters in URL-encoded form ("bar=baz&foo=quux") sorted by key.
|
||||
func (ns *Namespace) Querify(params ...interface{}) (string, error) {
|
||||
qs := url.Values{}
|
||||
vals, err := ns.Dictionary(params...)
|
||||
if err != nil {
|
||||
return "", errors.New("querify keys must be strings")
|
||||
|
||||
if len(params) == 1 {
|
||||
switch v := params[0].(type) {
|
||||
case []string:
|
||||
if len(v)%2 != 0 {
|
||||
return "", errors.New("invalid query")
|
||||
}
|
||||
|
||||
for i := 0; i < len(v); i += 2 {
|
||||
qs.Add(v[i], v[i+1])
|
||||
}
|
||||
|
||||
return qs.Encode(), nil
|
||||
|
||||
case []interface{}:
|
||||
params = v
|
||||
|
||||
default:
|
||||
return "", errors.New("query keys must be strings")
|
||||
}
|
||||
}
|
||||
|
||||
for name, value := range vals {
|
||||
qs.Add(name, fmt.Sprintf("%v", value))
|
||||
if len(params)%2 != 0 {
|
||||
return "", errors.New("invalid query")
|
||||
}
|
||||
|
||||
for i := 0; i < len(params); i += 2 {
|
||||
switch v := params[i].(type) {
|
||||
case string:
|
||||
qs.Add(v, fmt.Sprintf("%v", params[i+1]))
|
||||
default:
|
||||
return "", errors.New("query keys must be strings")
|
||||
}
|
||||
}
|
||||
|
||||
return qs.Encode(), nil
|
||||
|
Reference in New Issue
Block a user