grafana/pkg/util/url.go
Mario Trangoni 8261613b51 pkg/util/{ip.go,url.go}: Fix some golint issues
See,
$ gometalinter --vendor --deadline 10m --disable-all --enable=golint  ./...
ip.go:8:6⚠️ func SplitIpPort should be SplitIPPort (golint)
url.go:14:6⚠️ func NewUrlQueryReader should be NewURLQueryReader (golint)
url.go:9:6⚠️ type UrlQueryReader should be URLQueryReader (golint)
url.go:37:6⚠️ func JoinUrlFragments should be JoinURLFragments (golint)
2019-01-28 22:29:20 +01:00

53 lines
1004 B
Go

package util
import (
"net/url"
"strings"
)
// URLQueryReader is a URL query type.
type URLQueryReader struct {
values url.Values
}
// NewURLQueryReader parses a raw query and returns it as a URLQueryReader type.
func NewURLQueryReader(urlInfo *url.URL) (*URLQueryReader, error) {
u, err := url.ParseQuery(urlInfo.RawQuery)
if err != nil {
return nil, err
}
return &URLQueryReader{
values: u,
}, nil
}
// Get parse parameters from an URL. If the parameter does not exist, it returns
// the default value.
func (r *URLQueryReader) Get(name string, def string) string {
val := r.values[name]
if len(val) == 0 {
return def
}
return val[0]
}
// JoinURLFragments joins two URL fragments into only one URL string.
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
}