/
url.go
55 lines (52 loc) · 1.21 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package tpl
import (
"bytes"
"context"
"fmt"
"net/url"
"strings"
"github.com/KarpelesLab/webutil"
)
func QueryEscapeAny(ctx context.Context, val interface{}) string {
switch v := val.(type) {
case string:
return url.QueryEscape(v)
case []byte:
return url.QueryEscape(string(v))
case fmt.Stringer:
return url.QueryEscape(v.String())
case bytableIf:
return url.QueryEscape(string(v.Bytes()))
case func() string:
return url.QueryEscape(v())
case func() []byte:
return url.QueryEscape(string(v()))
case func() (Value, error):
rv, err := v()
if err != nil {
LogWarn(ctx, "unable to read value in QueryEscapeAny: %s", err)
return ""
}
return QueryEscapeAny(ctx, rv)
case bytes.Buffer:
return url.QueryEscape(v.String())
case strings.Builder:
return url.QueryEscape(v.String())
case ValueReader:
rv, err := v.ReadValue(ctx)
if err != nil {
LogWarn(ctx, "unable to read value in QueryEscapeAny: %s", err)
return ""
}
return QueryEscapeAny(ctx, rv)
case map[string]interface{}:
return webutil.EncodePhpQuery(v)
case int, int64, uint64:
return fmt.Sprintf("%d", v)
case nil:
return ""
default:
LogWarn(ctx, "unable to handle type %T in QueryEscapeAny", val)
return ""
}
}