zatae

zatae

Most efficient way to retrieve multiple values from nested map

Let’s say I have nested map like this :

data = %{
    gps: %{
        lat: 48.857144,
        lon: 2.340242,
        altitude: 35.0
    },
    metadata: %{
        payload: %{
            content: "1aff4c0002154a080bff4c0010774058308876aa3a29",
            scantime: 1644362438
        }
        macaddress: "B8:C0:78:CD:12:AB",
        receivetime: 1663000169,
        tags: "v1.2,test.app"
    }
}

# There is way more depth and values but I will keep it simple here.

What would be the best way to put lat, lon, scantime & receivetime in a new map ? The idea behind this would be to use this new simplified map with Ecto to add rows in database. I also need to ensure that every needed values is set.

Currently I am using something like this :

with {:ok, gps} <- Map.fetch(data, :gps),
     {:ok, lat} <- Map.fetch(gps, :lat),
     {:ok, lon} <- Map.fetch(gps, :lon),
     {:ok, metadata} <- Map.fetch(data, :metadata),
     {:ok, payload} <- Map.fetch(metadata, :payload),
     {:ok, scantime} <- Map.fetch(payload, :scantime),
     ... do
        %{
            lat: lat,
            lon: lon,
            scantime: scantime,
            ...
        }
     end

I need to extract something like twelve values from even more nested map, this looks really awful. Any idea?

Marked As Solved

Eiji

Eiji

Here you go:

defmodule Example do
  def sample(acc \\ %{}, data, info)

  # in case nested value does not exists
  def sample(acc, nil, _info), do: acc

  # instead of above you may want to use another code
  # as it would place a nil value for each nested info who does not exists in data you passed
  # 
  # def sample(acc, nil, info) when is_atom(info), do: Map.put(acc, info, nil)
  # def sample(acc, nil, info) when is_list(info), do: Enum.reduce(info, acc, &sample(&2, nil, &1))
  # 
  # def sample(acc, nil, info) when is_map(info) do
  #   Enum.reduce(info, acc, &sample(&2, nil, elem(&1, 1)))
  # end

  # when we need to fetch a flat list of fields
  # or said list + some extra nested fields
  def sample(acc, data, info) when is_list(info) do
    # there should be no more than one map in info list
    # the map info stores information for nested fields
    # the remaining items is a list of fields to take from current data
    groups = Enum.group_by(info, &is_map/1)
    [map_info] = groups[true] || [%{}]
    groups[false] |> Enum.reduce(acc, &Map.put(&2, &1, data[&1])) |> sample(data, map_info)
  end

  # here we are reducing info map over our acc
  # which means all nested fields logic goes here
  def sample(acc, data, info) when is_map(info) do
    Enum.reduce(info, acc, fn {info_key, info_value}, acc ->
      sample(acc, data, info_key, info_value)
    end)
  end

  # this clause would match if we want to fetch just one nested field
  defp sample(acc, data, info_key, info_value) when is_atom(info_value) do
    Map.put(acc, info_value, get_in(data, [info_key, info_value]))
  end

  # in any other case we have a map or list which we already support
  # so all we need to do is to call the same logic, but with nested data
  defp sample(acc, data, info_key, info_value) when is_list(info_value) or is_map(info_value) do
    sample(acc, data[info_key], info_value)
  end
end

data = %{
  gps: %{
    lat: 48.857144,
    lon: 2.340242,
    altitude: 35.0
  },
  metadata: %{
    payload: %{
      content: "1aff4c0002154a080bff4c0010774058308876aa3a29",
      scantime: 1_644_362_438
    },
    macaddress: "B8:C0:78:CD:12:AB",
    receivetime: 1_663_000_169,
    tags: "v1.2,test.app"
  }
}

info = %{gps: [:lat, :lon], metadata: [%{payload: :scantime}, :receivetime], a: %{b: :c}}
iex> Example.sample(data, info)

This code would automatically pick data you need by simply passing an info. Also if you want to add something to result you simply can pass it as first argument which means that you can call this function multiple times for different data:

data1
# without passing acc
|> Example.sample(info1)
# with acc (result of above pipe)
|> Example.sample(data2, info2)

Helpful resources:

  1. is_atom/1, is_list/1 and is_map/1 guards
  2. Enum.reduce/3
  3. Map.put/3

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

In this case, pattern match!

%{
    gps: %{
        lat: lat,
        lon: lon,
        altitude: alt
    },
    metadata: %{
        payload: %{
            scantime: scantime
        }
        receivetime: receive_time,
    } = data
}

Pattern matching is super useful here because it provides a way to declaratively extract what you want.

tcoopman

tcoopman

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What should happen if a value is missing? Is that a case you need to worry about, or is the shape of the payload reliable?

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement