Skip to content

Commit bded6fb

Browse files
committed
enhance connection_string parser
* use URI module to parse connection string * convert types in parser, not in connect function * add ability to select database
1 parent db2e088 commit bded6fb

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

lib/exredis.ex

+3-8
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@ defmodule Exredis do
1616
@doc """
1717
Connect to the Redis server using a connection string:
1818
19-
* `start_using_connection_string("redis://user:password@127.0.0.1:6379")`
19+
* `start_using_connection_string("redis://user:password@127.0.0.1:6379/0")`
2020
* `start_using_connection_string("redis://127.0.0.1:6379")`
2121
2222
Returns pid of the connected client
2323
"""
2424
@spec start_using_connection_string(binary, :no_reconnect | integer) :: pid
25-
def start_using_connection_string(connection_string, database \\ 0, reconnect_sleep \\ :no_reconnect) do
25+
def start_using_connection_string(connection_string, reconnect_sleep \\ :no_reconnect) do
2626
config = Exredis.ConnectionString.parse(connection_string)
27-
28-
# FIXME: Make the "start" method use binary strings as well and put the translation at the lowest possible level?
29-
host = config.host |> String.to_char_list
30-
password = config.password |> String.to_char_list
31-
port = config.port
32-
start(host, port, database, password, reconnect_sleep)
27+
start(config.host, config.port, config.db, config.password, reconnect_sleep)
3328
end
3429

3530
@doc """

lib/exredis/connection_string.ex

+14-28
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
11
defmodule Exredis.ConnectionString do
22
defmodule Config do
3-
defstruct host: nil, port: nil, password: nil
3+
defstruct host: nil, port: nil, password: nil, db: nil
44
end
55

66
def parse(connection_string) do
7-
connection_string
8-
|> extract_auth_and_path
9-
|> convert_to_config
7+
uri = URI.parse(connection_string)
8+
9+
%Config{
10+
host: uri.host |> String.to_char_list,
11+
port: uri.port,
12+
password: uri.userinfo |> parse_password,
13+
db: uri.path |> parse_db
14+
}
1015
end
1116

12-
defp extract_auth_and_path(connection_string) do
13-
connection_string
14-
|> String.split("/")
15-
|> Enum.at(-1)
16-
|> String.split("@")
17-
end
18-
19-
defp convert_to_config([ auth, path ]) do
20-
[ host, port ] = parse_host_and_port(path)
21-
password = parse_password(auth)
22-
%Config{ host: host, port: port, password: password }
23-
end
24-
25-
defp convert_to_config([ path ]) do
26-
[ host, port ] = parse_host_and_port(path)
27-
password = ""
28-
%Config{ host: host, port: port, password: password }
29-
end
30-
31-
defp parse_host_and_port(path) do
32-
[ host, port ] = path |> String.split(":")
33-
[ host, String.to_integer(port) ]
17+
defp parse_db(nil), do: 0
18+
defp parse_db(path) do
19+
path |> String.split("/") |> Enum.at(1)
3420
end
3521

22+
defp parse_password(nil), do: ''
3623
defp parse_password(auth) do
37-
[ _user, password ] = auth |> String.split(":")
38-
password
24+
auth |> String.split(":") |> Enum.at(1) |> String.to_char_list
3925
end
4026
end

test/connection_string_test.exs

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,30 @@ defmodule ConnectionStringTest do
55

66
test "parsing a connection string" do
77
config = Exredis.ConnectionString.parse("redis://user:password@host:1234")
8-
assert config.host == "host"
8+
assert config.host == 'host'
99
assert config.port == 1234
10-
assert config.password == "password"
10+
assert config.password == 'password'
11+
end
12+
13+
test "parsing a connection string with db" do
14+
config = Exredis.ConnectionString.parse("redis://user:password@host:1234/10")
15+
assert config.host == 'host'
16+
assert config.port == 1234
17+
assert config.password == 'password'
18+
assert config.db == "10"
19+
end
20+
21+
test "parsing a connection string without user" do
22+
config = Exredis.ConnectionString.parse("redis://:password@host:1234")
23+
assert config.host == 'host'
24+
assert config.port == 1234
25+
assert config.password == 'password'
1126
end
1227

1328
test "parsing a connection string without password" do
1429
config = Exredis.ConnectionString.parse("redis://host:1234")
15-
assert config.host == "host"
30+
assert config.host == 'host'
1631
assert config.port == 1234
17-
assert config.password == ""
32+
assert config.password == ''
1833
end
1934
end

0 commit comments

Comments
 (0)