Skip to content

Add Uri.empty constant representing an empty URI, equivalent to "" #798

New issue

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

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

Already on GitHub? # to your account

Merged
merged 7 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Fixed a bug that would result in `list.unique` having quadratic runtime.
- Fixed the implementation of `list.key_set` to be tail recursive.
- The `pop` and `pop_map` functions in the `list` module have been deprecated.
- The `uri` module gains the `empty` value, representing an empty URI which
equivalent to `""`.

## v0.53.0 - 2025-01-23

Expand Down
48 changes: 32 additions & 16 deletions src/gleam/uri.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ pub type Uri {
)
}

/// Constant representing an empty URI, equivalent to "".
///
/// ## Examples
///
/// ```gleam
/// let uri = Uri(..empty, scheme: Some("https"), host: Some("example.com"))
/// // -> Uri(
/// // scheme: Some("https"),
/// // userinfo: None,
/// // host: Some("example.com"),
/// // port: None,
/// // path: "",
/// // query: None,
/// // fragment: None,
/// // )
/// ```
///
pub const empty = Uri(
scheme: None,
userinfo: None,
host: None,
port: None,
path: "",
query: None,
fragment: None,
)

/// Parses a compliant URI string into the `Uri` Type.
/// If the string is not a valid URI string then an error is returned.
///
Expand Down Expand Up @@ -58,18 +85,7 @@ pub fn parse(uri_string: String) -> Result(Uri, Nil) {
// TODO: This is not perfect and will be more permissive than its Erlang
// counterpart, ideally we want to replicate Erlang's implementation on the js
// target as well.
let default_pieces =
Uri(
scheme: None,
userinfo: None,
host: None,
port: None,
path: "",
query: None,
fragment: None,
)

parse_scheme_loop(uri_string, uri_string, default_pieces, 0)
parse_scheme_loop(uri_string, uri_string, empty, 0)
}

fn parse_scheme_loop(
Expand Down Expand Up @@ -620,9 +636,9 @@ fn remove_dot_segments_loop(
/// ## Examples
///
/// ```gleam
/// let uri = Uri(Some("http"), None, Some("example.com"), ...)
/// let uri = Uri(..empty, scheme: Some("https"), host: Some("example.com"))
/// to_string(uri)
/// // -> "http://example.com"
/// // -> "https://example.com"
/// ```
///
pub fn to_string(uri: Uri) -> String {
Expand Down Expand Up @@ -664,9 +680,9 @@ pub fn to_string(uri: Uri) -> String {
/// ## Examples
///
/// ```gleam
/// let assert Ok(uri) = parse("http://example.com/path?foo#bar")
/// let assert Ok(uri) = parse("https://example.com/path?foo#bar")
/// origin(uri)
/// // -> Ok("http://example.com")
/// // -> Ok("https://example.com")
/// ```
///
pub fn origin(uri: Uri) -> Result(String, Nil) {
Expand Down