Skip to content

Commit 5d1af09

Browse files
committed
url_config does not require password
1 parent 07fcf52 commit 5d1af09

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.7.0 - 2024-04-05
4+
5+
- The password is now optional in the `url_config` function, defaulting to no
6+
password if none is given.
7+
38
## v0.6.1 - 2024-01-16
49

510
- Relaxed the stdlib version constraint.

src/gleam/pgo.gleam

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
// TODO: transactions
66
// TODO: JSON support
77
import gleam/dynamic.{type DecodeErrors, type Decoder, type Dynamic}
8-
import gleam/string
9-
import gleam/result
8+
109
import gleam/list
1110
import gleam/option.{type Option, None, Some}
11+
import gleam/result
12+
import gleam/string
1213
import gleam/uri.{Uri}
1314

1415
/// The configuration for a pool of connections.
@@ -86,16 +87,20 @@ pub fn url_config(database_url: String) -> Result(Config, Nil) {
8687
use uri <- result.then(uri.parse(database_url))
8788
use #(userinfo, host, path, db_port) <- result.then(case uri {
8889
Uri(
89-
scheme: Some("postgres"),
90-
userinfo: Some(userinfo),
91-
host: Some(host),
92-
port: Some(db_port),
93-
path: path,
94-
..,
95-
) -> Ok(#(userinfo, host, path, db_port))
90+
scheme: Some("postgres"),
91+
userinfo: Some(userinfo),
92+
host: Some(host),
93+
port: Some(db_port),
94+
path: path,
95+
..,
96+
) -> Ok(#(userinfo, host, path, db_port))
97+
_ -> Error(Nil)
98+
})
99+
use #(user, password) <- result.then(case string.split(userinfo, ":") {
100+
[user] -> Ok(#(user, None))
101+
[user, password] -> Ok(#(user, Some(password)))
96102
_ -> Error(Nil)
97103
})
98-
use #(user, password) <- result.then(string.split_once(userinfo, ":"))
99104
case string.split(path, "/") {
100105
["", database] ->
101106
Ok(
@@ -105,7 +110,7 @@ pub fn url_config(database_url: String) -> Result(Config, Nil) {
105110
port: db_port,
106111
database: database,
107112
user: user,
108-
password: Some(password),
113+
password: password,
109114
),
110115
)
111116
_ -> Error(Nil)

test/gleam/pgo_test.gleam

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import gleam/dynamic.{type Decoder}
2-
import gleam/pgo
32
import gleam/option.{None, Some}
3+
import gleam/pgo
44
import gleam/string
55
import gleeunit/should
66

7-
pub fn url_config_test() {
7+
pub fn url_config_everything_test() {
88
pgo.url_config("postgres://u:p@db.test:1234/my_db")
99
|> should.equal(Ok(
1010
pgo.Config(
@@ -16,13 +16,28 @@ pub fn url_config_test() {
1616
password: Some("p"),
1717
),
1818
))
19+
}
1920

21+
pub fn url_config_not_postgres_protocol_test() {
2022
pgo.url_config("foo://u:p@db.test:1234/my_db")
2123
|> should.equal(Error(Nil))
24+
}
2225

26+
pub fn url_config_no_password_test() {
2327
pgo.url_config("postgres://u@db.test:1234/my_db")
24-
|> should.equal(Error(Nil))
28+
|> should.equal(Ok(
29+
pgo.Config(
30+
..pgo.default_config(),
31+
host: "db.test",
32+
port: 1234,
33+
database: "my_db",
34+
user: "u",
35+
password: None,
36+
),
37+
))
38+
}
2539

40+
pub fn url_config_path_slash_test() {
2641
pgo.url_config("postgres://u:p@db.test:1234/my_db/foo")
2742
|> should.equal(Error(Nil))
2843
}

0 commit comments

Comments
 (0)