grafana/pkg/util/url.go

43 lines
620 B
Go
Raw Normal View History

2015-01-08 02:00:00 -06:00
package util
2014-10-03 02:03:16 -05:00
import (
"net/url"
"strings"
2014-10-03 02:03:16 -05:00
)
type UrlQueryReader struct {
values url.Values
}
func NewUrlQueryReader(url *url.URL) *UrlQueryReader {
return &UrlQueryReader{
values: url.Query(),
}
}
func (r *UrlQueryReader) Get(name string, def string) string {
val := r.values[name]
if len(val) == 0 {
return def
}
return val[0]
}
func JoinUrlFragments(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
if len(b) == 0 {
return a
}
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}