| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"net/url" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/hmaps" |
| 21 |
"github.com/spf13/cast" |
| 22 |
) |
| 23 |
|
| 24 |
var ( |
| 25 |
errWrongArgStructure = errors.New("expected a map, a slice with an even number of elements, or an even number of scalar values, and each key must be a string") |
| 26 |
errKeyIsEmptyString = errors.New("one of the keys is an empty string") |
| 27 |
) |
| 28 |
|
| 29 |
// Querify returns a URL query string composed of the given key-value pairs, |
| 30 |
// encoded and sorted by key. |
| 31 |
func (ns *Namespace) Querify(params ...any) (string, error) { |
| 32 |
if len(params) == 0 { |
| 33 |
return "", nil |
| 34 |
} |
| 35 |
|
| 36 |
if len(params) == 1 { |
| 37 |
switch v := params[0].(type) { |
| 38 |
case map[string]any: // created with collections.Dictionary |
| 39 |
return mapToQueryString(v) |
| 40 |
case hmaps.Params: // project configuration or page parameters |
| 41 |
return mapToQueryString(v) |
| 42 |
case []string: |
| 43 |
return stringSliceToQueryString(v) |
| 44 |
case []any: |
| 45 |
s, err := interfaceSliceToStringSlice(v) |
| 46 |
if err != nil { |
| 47 |
return "", err |
| 48 |
} |
| 49 |
return stringSliceToQueryString(s) |
| 50 |
default: |
| 51 |
return "", errWrongArgStructure |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if len(params)%2 != 0 { |
| 56 |
return "", errWrongArgStructure |
| 57 |
} |
| 58 |
|
| 59 |
s, err := interfaceSliceToStringSlice(params) |
| 60 |
if err != nil { |
| 61 |
return "", err |
| 62 |
} |
| 63 |
return stringSliceToQueryString(s) |
| 64 |
} |
| 65 |
|
| 66 |
// mapToQueryString returns a URL query string derived from the given string |
| 67 |
// map, encoded and sorted by key. The function returns an error if it cannot |
| 68 |
// convert an element value to a string. |
| 69 |
func mapToQueryString[T map[string]any | hmaps.Params](m T) (string, error) { |
| 70 |
if len(m) == 0 { |
| 71 |
return "", nil |
| 72 |
} |
| 73 |
|
| 74 |
qs := url.Values{} |
| 75 |
for k, v := range m { |
| 76 |
if len(k) == 0 { |
| 77 |
return "", errKeyIsEmptyString |
| 78 |
} |
| 79 |
vs, err := cast.ToStringE(v) |
| 80 |
if err != nil { |
| 81 |
return "", err |
| 82 |
} |
| 83 |
qs.Add(k, vs) |
| 84 |
} |
| 85 |
return qs.Encode(), nil |
| 86 |
} |
| 87 |
|
| 88 |
// sliceToQueryString returns a URL query string derived from the given slice |
| 89 |
// of strings, encoded and sorted by key. The function returns an error if |
| 90 |
// there are an odd number of elements. |
| 91 |
func stringSliceToQueryString(s []string) (string, error) { |
| 92 |
if len(s) == 0 { |
| 93 |
return "", nil |
| 94 |
} |
| 95 |
if len(s)%2 != 0 { |
| 96 |
return "", errWrongArgStructure |
| 97 |
} |
| 98 |
|
| 99 |
qs := url.Values{} |
| 100 |
for i := 0; i < len(s); i += 2 { |
| 101 |
if len(s[i]) == 0 { |
| 102 |
return "", errKeyIsEmptyString |
| 103 |
} |
| 104 |
qs.Add(s[i], s[i+1]) |
| 105 |
} |
| 106 |
return qs.Encode(), nil |
| 107 |
} |
| 108 |
|
| 109 |
// interfaceSliceToStringSlice converts a slice of interfaces to a slice of |
| 110 |
// strings, returning an error if it cannot convert an element to a string. |
| 111 |
func interfaceSliceToStringSlice(s []any) ([]string, error) { |
| 112 |
if len(s) == 0 { |
| 113 |
return []string{}, nil |
| 114 |
} |
| 115 |
|
| 116 |
ss := make([]string, 0, len(s)) |
| 117 |
for _, v := range s { |
| 118 |
vs, err := cast.ToStringE(v) |
| 119 |
if err != nil { |
| 120 |
return []string{}, err |
| 121 |
} |
| 122 |
ss = append(ss, vs) |
| 123 |
} |
| 124 |
return ss, nil |
| 125 |
} |