grafana/pkg/util/url.go

53 lines
1016 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
)
// URLQueryReader is a ApiURL query type.
type URLQueryReader struct {
2014-10-03 02:03:16 -05:00
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)
2017-11-28 08:20:22 -06:00
if err != nil {
return nil, err
2014-10-03 02:03:16 -05:00
}
2017-11-28 08:20:22 -06:00
return &URLQueryReader{
2017-11-28 08:20:22 -06:00
values: u,
}, nil
2014-10-03 02:03:16 -05:00
}
// Get parse parameters from an ApiURL. If the parameter does not exist, it returns
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// the default value.
func (r *URLQueryReader) Get(name string, def string) string {
2014-10-03 02:03:16 -05:00
val := r.values[name]
if len(val) == 0 {
return def
}
return val[0]
}
// JoinURLFragments joins two ApiURL fragments into only one ApiURL 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
}