You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently Req request and response headers are lists of tuples, e.g.: [{"foo", "bar"}]. This is a standard across the ecosystem (with minor difference that some Erlang libraries use charlists instead of binaries.) This particular representation makes sense because the same header might appear multiple times, see RFC 9110 § 5.2.
There are some problems with this particular choice though:
We cannot use headers[key]
We cannot use pattern matching
In short, this representation isn't very ergonomic to use.
Option 1: Headers as Maps
Both of these problems can be solved by switching the representation to maps of string keys and list values:
An interesting idea is to go further and have a struct:
defmoduleReq.Headersdodefstruct[:map]endresp=Req.get!("https://elixir-lang.org")resp.headers["content-type"]#=> ["text/html"]resp.headers["Content-Type"]#=> ["text/html"] (RFC says they need to be treated case-insensitively!)
All that being said, while I thing these are nice things, I feel like structs are a more complicated solution than plain maps and I'm not sure they give us significant benefits at the moment.
The text was updated successfully, but these errors were encountered:
Currently Req request and response headers are lists of tuples, e.g.:
[{"foo", "bar"}]
. This is a standard across the ecosystem (with minor difference that some Erlang libraries use charlists instead of binaries.) This particular representation makes sense because the same header might appear multiple times, see RFC 9110 § 5.2.There are some problems with this particular choice though:
headers[key]
In short, this representation isn't very ergonomic to use.
Option 1: Headers as Maps
Both of these problems can be solved by switching the representation to maps of string keys and list values:
This is similar to Go's net/http
Header
type.Let's see some example rewrites:
or in certain situations even better:
Option 2: Headers as Structs
An interesting idea is to go further and have a struct:
and we still could pattern match:
however here we cannot treat the names case insensitively so it's probably better to continue assuming they are lowercase (and enforce it: #196).
The struct gives us opportunity for slightly different Access implementation however:
and the semantics would be similar to Go's
Header.Get()
:and then we'd have something like
Header.Values()
.With structs we can implement protocols. For example, we may want to have an enumerable implementation for interop:
And perhaps Collectable too:
Basically, we have extensibility.
All that being said, while I thing these are nice things, I feel like structs are a more complicated solution than plain maps and I'm not sure they give us significant benefits at the moment.
The text was updated successfully, but these errors were encountered: