stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Add a `new()` primitive to the `uri` package.

Open vxern opened this issue 8 months ago • 5 comments

Inspired by the request.new() and response.new() functions from the gleam_http package, and while working with Uris, I thought that it would also be a good idea to be able to do the same with Uri.

Provisionally, the implementation of the function would look as follows:

pub fn new() -> Uri {
  Uri(
    scheme: None,
    userinfo: None,
    host: None,
    port: None,
    path: "",
    query: None,
    fragment: None,
  )
}

This would create a completely bare Uri object that the developer could then go ahead and build on top of. I have created a related issue here that would implement 'builder' primitives to set fields on the Uri object. Coupled, these primitives would create a really nice pattern for building out a Uri, but even without those 'setter' primitives being present yet, the developer could still utilise this implementation of new() as follows:

let uri = Uri(..uri.new(), scheme: "http", host: "localhost")

This primitive would make cases like the following taken from uri_test.gleam more concise:

uri.Uri(Some("ftp"), None, None, None, "", None, None)
|> uri.to_string
|> should.equal("ftp:")

allowing them to be switched out for:

uri.Uri(..uri.new(), scheme: Some("ftp"))
|> uri.to_string
|> should.equal("ftp:")

and coupled with the 'setter' primitive proposal mentioned earlier (here), if that proposal were to be implemented:

uri.new()
|> uri.set_scheme("ftp")
|> uri.to_string
|> should.equal("ftp:")

If okay'd, I'd be happy to go ahead and implement this myself.

vxern avatar May 31 '24 21:05 vxern