mirror of
https://github.com/redis/go-redis.git
synced 2025-07-20 22:42:59 +03:00
feat: add helper func, cleanup code
This commit is contained in:
44
options.go
44
options.go
@ -240,16 +240,7 @@ func setupTCPConn(u *url.URL) (*Options, error) {
|
||||
|
||||
o.Username, o.Password = getUserPassword(u)
|
||||
|
||||
h, p, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
h = u.Host
|
||||
}
|
||||
if h == "" {
|
||||
h = "localhost"
|
||||
}
|
||||
if p == "" {
|
||||
p = "6379"
|
||||
}
|
||||
h, p := getHostPortWithDefaults(u)
|
||||
o.Addr = net.JoinHostPort(h, p)
|
||||
|
||||
f := strings.FieldsFunc(u.Path, func(r rune) bool {
|
||||
@ -259,6 +250,7 @@ func setupTCPConn(u *url.URL) (*Options, error) {
|
||||
case 0:
|
||||
o.DB = 0
|
||||
case 1:
|
||||
var err error
|
||||
if o.DB, err = strconv.Atoi(f[0]); err != nil {
|
||||
return nil, fmt.Errorf("redis: invalid database number: %q", f[0])
|
||||
}
|
||||
@ -273,6 +265,23 @@ func setupTCPConn(u *url.URL) (*Options, error) {
|
||||
return setupConnParams(u, o)
|
||||
}
|
||||
|
||||
// getHostPortWithDefaults is a helper function that splits the url into
|
||||
// a host and a port. If the host is missing, it defaults to localhost
|
||||
// and if the port is missing, it defaults to 6379
|
||||
func getHostPortWithDefaults(u *url.URL) (string, string) {
|
||||
host, port, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
host = u.Host
|
||||
}
|
||||
if host == "" {
|
||||
host = "localhost"
|
||||
}
|
||||
if port == "" {
|
||||
port = "6379"
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
func setupUnixConn(u *url.URL) (*Options, error) {
|
||||
o := &Options{
|
||||
Network: "unix",
|
||||
@ -292,19 +301,20 @@ type queryOptions struct {
|
||||
}
|
||||
|
||||
func (o *queryOptions) string(name string) string {
|
||||
vs := o.q[name]
|
||||
if len(vs) == 0 {
|
||||
if len(o.q[name]) == 0 {
|
||||
return ""
|
||||
}
|
||||
// get the first item from the array to return
|
||||
// and remove it so it isn't processed again
|
||||
param := o.q[name][0]
|
||||
o.q[name] = o.q[name][1:]
|
||||
|
||||
// enable detection of unknown parameters
|
||||
if len(vs) > 1 {
|
||||
o.q[name] = o.q[name][:len(vs)-1]
|
||||
} else {
|
||||
// remove the key to enable detection of unknown params
|
||||
if len(o.q[name]) == 0 {
|
||||
delete(o.q, name)
|
||||
}
|
||||
|
||||
return vs[len(vs)-1]
|
||||
return param
|
||||
}
|
||||
|
||||
func (o *queryOptions) int(name string) int {
|
||||
|
Reference in New Issue
Block a user