Skip to content

Commit

Permalink
fix: rueidis.ParseURL() cannot parse unix address
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Dec 12, 2023
1 parent 0fac21f commit 2c46b82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 7 additions & 5 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ func ParseURL(str string) (opt ClientOption, err error) {
opt.Username = u.User.Username()
opt.Password, _ = u.User.Password()
}
if ps := strings.Split(u.Path, "/"); len(ps) == 2 {
if opt.SelectDB, err = strconv.Atoi(ps[1]); err != nil {
return opt, fmt.Errorf("redis: invalid database number: %q", ps[1])
if u.Scheme != "unix" {
if ps := strings.Split(u.Path, "/"); len(ps) == 2 {
if opt.SelectDB, err = strconv.Atoi(ps[1]); err != nil {
return opt, fmt.Errorf("redis: invalid database number: %q", ps[1])
}
} else if len(ps) > 2 {
return opt, fmt.Errorf("redis: invalid URL path: %s", u.Path)
}
} else if len(ps) > 2 {
return opt, fmt.Errorf("redis: invalid URL path: %s", u.Path)
}
q := u.Query()
if q.Has("db") {
Expand Down
3 changes: 3 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func TestParseURL(t *testing.T) {
if opt, err := ParseURL("rediss://myhost:6379"); err != nil || opt.TLSConfig.ServerName != "myhost" {
t.Fatalf("unexpected %v %v", opt, err)
}
if opt, err := ParseURL("unix:///path/to/redis.sock?db=0"); opt.DialFn == nil || opt.InitAddress[0] != "/path/to/redis.sock" || opt.SelectDB != 0 {
t.Fatalf("unexpected %v %v", opt, err)
}
}

func TestMustParseURL(t *testing.T) {
Expand Down

0 comments on commit 2c46b82

Please sign in to comment.