-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
57 lines (46 loc) · 922 Bytes
/
util.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
56
57
package helper
import (
"fmt"
"net/url"
)
// URL returns url string from endpoint and path.
func URL(endpoint, path string, val ...*string) fmt.Stringer {
u, err := url.Parse(endpoint)
if err != nil {
panic(err)
}
return stringer(func() string {
if len(val) == 0 {
u.Path = path
return u.String()
}
u.Path = format(path, val...).String()
return u.String()
})
}
func format(format string, val ...*string) fmt.Stringer {
return stringer(func() string {
vs := make([]interface{}, 0, len(val))
for _, v := range val {
vs = append(vs, *v)
}
return fmt.Sprintf(format, vs...)
})
}
// stringer implements fmt.Stringer.
type stringer func() string
func (s stringer) String() string {
return s()
}
func toString(val interface{}) string {
switch v := val.(type) {
case string:
return v
case *string:
return *v
case fmt.Stringer:
return v.String()
default:
return ""
}
}