Re: [elixir-core:11001] [Proposal] URI.put_query/1 (and put_host/1, etc.?)

65 views
Skip to first unread message

José Valim

unread,
Jul 29, 2022, 4:16:10 PM7/29/22
to elixir-lang-core
Elixir v1.14 adds append_query, so we can definitely add put_query. I am not super convinced on put_host and friends though. I would love to hear everyone's thoughts, but changing the struct should be fine.

On Fri, Jul 29, 2022 at 10:05 PM 'Adam Millerchip' via elixir-lang-core <elixir-l...@googlegroups.com> wrote:
Inspired by this Stack Overflow post:

I thought it might be useful to have helpers to update the struct fields, rather than manipulating the struct directly.

Instead of:

%URI{uri | host: "example.com"}
Map.put(uri, :host, "example.com")
etc.

We could do:

URI.put_host(uri, "example.com")

I was thinking that one helper per struct field would be useful. Or maybe only adding a helper for query: `put_query/1` would be an option. Query seems useful, as there is currently no way to programmatically construct a URI with a safely-encoded query string using the `URI` module without manually updating the `query` property (or manually appending the query to a string and calling URI.new again).

In the case of `put_query`, it could have two versions: one that puts a string, and one that accepts an enumerable and wraps up encode_query/2.

URI.new!("https://example.com") |> URI.put_query(foo: "bar", baz: "qux")

Come to think of it, accepting a query enumerable to `new` might even be worth it.

I realise this is just syntax sugar, so there might not be desire for such a feature, hence this post. :)

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/666650f2-7a75-4784-b79e-883bff6a3eb8n%40googlegroups.com.

Artur Plysyuk

unread,
Aug 11, 2022, 10:57:38 AM8/11/22
to elixir-lang-core
I'm not sure about put_query, as similar effect could be achieved by URI.merge("http://example.com/?x=1", "?x=2")

пʼятниця, 29 липня 2022 р. о 23:16:10 UTC+3 José Valim пише:
Reply all
Reply to author
Forward
0 new messages