I count 44 instances of this in our production code (~100k lines of Elixir), but I don’t think I’ve ever used key and value pinning as shown in the examples above.
But dynamic key matching is precisely what is required when it is required, although it’s rare enough.
Here's a sample GraphQL response parser or the Shopify Customers API:
```elixir
def parse_customer_response(%{} = body, resource, action) do
gql_action = resource <> String.capitalize(action)
case body do
%{status: 200, body: %{^gql_action => %{^resource => %{} = result, "userErrors" => []}}} ->
{:ok, result}
%{status: 200, body: %{^gql_action => %{"userErrors" => []} = result}} ->
{:ok, result}
%{errors: errors} ->
{:error, Enum.map(errors, &Map.get(&1, "message"))}
%{body: %{^gql_action => %{"userErrors" => errors}}} ->
{:error, Enum.map(errors, &Map.get(&1, "message"))}
%{body: %{^gql_action => nil}} ->
{:error, [%{message: "#{gql_action} access denied"}]}
%{body: nil} ->
{:error, [%{message: "#{gql_action} access denied"}]}
_ ->
{:error, [%{message: "Unknown error"}]}
end
end
```
I haven’t started using pattern matching in Ruby, but I could see some simplified code with dynamic key matching.
-a