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

Add primitives to set fields on Uri objects. #618

Open
vxern opened this issue May 31, 2024 · 4 comments
Open

Add primitives to set fields on Uri objects. #618

vxern opened this issue May 31, 2024 · 4 comments

Comments

@vxern
Copy link

vxern commented May 31, 2024

Considering the signature of Uri, to make working with the type a little nicer, I would propose to add value setter primitives corresponding to fields on the Uri type:

pub type Uri {
  Uri(
    scheme: Option(String),
    userinfo: Option(String),
    host: Option(String),
    port: Option(Int),
    path: String,
    query: Option(String),
    fragment: Option(String),
  )
}

The primitives would have the following signatures:

  • set_scheme(uri: Uri, scheme: String) -> Uri
  • set_userinfo(uri: Uri, userinfo: String) -> Uri
  • set_host(uri: Uri, host: String) -> Uri
  • set_port(uri: Uri, port: Int) -> Uri
  • set_path(uri: Uri, path: String) -> Uri
  • set_query(uri: Uri, query: String) -> Uri
  • set_fragment(uri: Uri, fragment: String) -> Uri

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

@lpil
Copy link
Member

lpil commented Jul 18, 2024

Hello! What's the use case here? Could you give some examples from your programs and how these functions would help with them. Thanks!

@chuckwondo
Copy link
Contributor

@lpil, this appears to be merely for convenience. See some examples in #619.

I don't know how prevalent this pattern is throughout the various gleam libraries, but at least one precedent for this type of "builder" pattern can be seen in the gleam http library, where there are a number of "setter" functions for the Request type, such as set_header, set_body, set_scheme, set_host, etc.

@vxern
Copy link
Author

vxern commented Jul 25, 2024

Sure.

https://github.com/vxern/tatoeba/blob/main/src%2Ftatoeba%2Fapi.gleam#L11

This is an example where it would have been quicker and nicer to write:

pub fn url() -> String {
  url.new()
  |> url.set_scheme("https")
  |> url.set_host("tatoeba.org")
  |> url.set_path("/eng/api_v0")
}

over:

pub const url = Uri(
  scheme: Some("https"),
  userinfo: None,
  host: Some("tatoeba.org"),
  port: None,
  query: None,
  path: "/eng/api_v0",
  fragment: None,
)

@lpil
Copy link
Member

lpil commented Jul 25, 2024

They seem about the same to me, and it's nice that the constant one has no runtime cost.

I've done a search on GitHub and it seems that directly constructing Uris is uncommon. It doesn't seem like something that there's a huge benefit to adding a small amount of convenience for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants