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:
Ujjwal Goyal
2021-03-07 00:08:10 +05:30
committed by Bjørn Erik Pedersen
parent 504c78da4b
commit c46fc838a9
3 changed files with 68 additions and 5 deletions

View File

@@ -564,9 +564,16 @@ func TestQuerify(t *testing.T) {
}{
{[]interface{}{"a", "b"}, "a=b"},
{[]interface{}{"a", "b", "c", "d", "f", " &"}, `a=b&c=d&f=+%26`},
{[]interface{}{[]string{"a", "b"}}, "a=b"},
{[]interface{}{[]string{"a", "b", "c", "d", "f", " &"}}, `a=b&c=d&f=+%26`},
{[]interface{}{[]interface{}{"x", "y"}}, `x=y`},
{[]interface{}{[]interface{}{"x", 5}}, `x=5`},
// errors
{[]interface{}{5, "b"}, false},
{[]interface{}{"a", "b", "c"}, false},
{[]interface{}{[]string{"a", "b", "c"}}, false},
{[]interface{}{[]string{"a", "b"}, "c"}, false},
{[]interface{}{[]interface{}{"c", "d", "e"}}, false},
} {
errMsg := qt.Commentf("[%d] %v", i, test.params)
@@ -582,6 +589,32 @@ func TestQuerify(t *testing.T) {
}
}
func BenchmarkQuerify(b *testing.B) {
ns := New(&deps.Deps{})
params := []interface{}{"a", "b", "c", "d", "f", " &"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := ns.Querify(params...)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkQuerifySlice(b *testing.B) {
ns := New(&deps.Deps{})
params := []string{"a", "b", "c", "d", "f", " &"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := ns.Querify(params)
if err != nil {
b.Fatal(err)
}
}
}
func TestSeq(t *testing.T) {
t.Parallel()
c := qt.New(t)