mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
8261613b51
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)
53 lines
1004 B
Go
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
|
|
}
|