55// TODO: transactions
66// TODO: JSON support
77import gleam/dynamic . { type DecodeErrors , type Decoder , type Dynamic }
8- import gleam/string
9- import gleam/result
8+
109import gleam/list
1110import gleam/option . { type Option , None , Some }
11+ import gleam/result
12+ import gleam/string
1213import 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 )
0 commit comments