Skip to content

Commit

Permalink
Merge pull request #425 from IrineSistiana/fix-424
Browse files Browse the repository at this point in the history
fix: rueidis.ParseURL() cannot parse unix address
  • Loading branch information
rueian authored Dec 12, 2023
2 parents 0fac21f + 901d5f9 commit b79ac22
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=1"); opt.DialFn == nil || opt.InitAddress[0] != "/path/to/redis.sock" || opt.SelectDB != 1 {
t.Fatalf("unexpected %v %v", opt, err)
}
}

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

0 comments on commit b79ac22

Please sign in to comment.