zatae
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?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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?
Eiji
Here you go:
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:Helpful resources:
tcoopman
You can also have a look at a library like GitHub - hissssst/pathex: Fastest tool to access data in Elixir · GitHub.
zatae
Works like a charm thank you ! I updated it a bit so it will raise an error instead of putting a
nilvalue.An error should be raised. If you know another way to do it, I would be very interested in seeing it too!
benwilson512
In this case, pattern match!
Pattern matching is super useful here because it provides a way to declaratively extract what you want.
zatae
I think I don’t get what you are trying to explain. This is only validating data, right ? How is this going to “flatten” the map ?
Eiji
The pattern matching is not useful only for validation, but also for assigning variables. Simplest example:
What @benwilson512 suggested is to do exactly same for nested structures, for example:
However since you said that you have many cases for this and they could be even more complicated, the pattern matching here for just a single data structure would take lots of lines. For me it looks much worse comparing to usage of a simple
infovariable in my example.zatae
I understand now ! Thank you
zatae
I have another question regarding the solution you gave. Is that possible to have a non nested value at the root of the map? Like this :
I am having a hard time finding a way to retrieve
macaddress.Eiji
Sure thing! Look that in original version I’m using
mapas a root ofinput, but in code you would find that alsolistis supported. The properinfofor thisdatawould be:However I found one thing. In original code
listpart is not best. I would rewrite it totupleinstead, for example:This way instead of
[%{nested: …}, :field1, …]we have{%{nested: …}, [:field1, …]}which separates nestedmapinfo from rootlistinfo. This way we no longer need to callEnum.groiup_by/2.