4 ms·
Mind that this is not an idiomatic code. One would usually use pattern matching. case Req.get!("https://hex.pm/api/packages/req") do %{body: %{"meta" =
by nesarkvechnep 3y ago
Mind that this is not an idiomatic code. One would usually use pattern matching.
case Req.get!("https://hex.pm/api/packages/req") do
%{body: %{"meta" => %{"description" => desc}}} ->
# use the value of desc here
%{body: body} ->
# report for unexpected body
end
As others mentioned, the ! at the end of the function name tells that the user should expect an exception.
- jolux 3y agoThat depends. I think that's a bit deep for pattern matching personally but I've evolved on this over time.
- nesarkvechnep 3y agoWho decides when pattern matching is a bit deep?
- tentacleuno 3y agoWell, the person you're replying to did, to be fair.
- thibaut_barrere 3y agoI would not say this is not idiomatic - it depends on the situation. On production code or scripts where I want to be sure (which is the case mostly), I would indeed use something like: %{status: 200, body: json} = Req.get!(".....") And then later restructure as needed. Sometimes it would be: meta = json |> Map.fetch!("meta") |> Map.fetch!("description") Or pattern matching as you did!