Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URL parsing without port #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pog.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ pub fn default_config() -> Config {
/// Parse a database url into configuration that can be used to start a pool.
pub fn url_config(database_url: String) -> Result(Config, Nil) {
use uri <- result.then(uri.parse(database_url))
let uri = case uri.port {
Some(_) -> uri
None -> Uri(..uri, port: Some(default_config().port))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than building the default config and then accessing the port let's move the default port to a private const and reference that in both places.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will make this change and update the changelog shortly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made this change and updated the changelog.

}
use #(userinfo, host, path, db_port, query) <- result.then(case uri {
Uri(
scheme: Some(scheme),
Expand Down
12 changes: 12 additions & 0 deletions test/pog_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ pub fn url_config_no_password_test() {
|> should.equal(Ok(expected))
}

pub fn url_config_no_port_test() {
let expected =
pog.default_config()
|> pog.host("db.test")
|> pog.port(5432)
|> pog.database("my_db")
|> pog.user("u")
|> pog.password(None)
pog.url_config("postgres://[email protected]/my_db")
|> should.equal(Ok(expected))
}

pub fn url_config_path_slash_test() {
pog.url_config("postgres://u:[email protected]:1234/my_db/foo")
|> should.equal(Error(Nil))
Expand Down
Loading