KiKi

KiKi

Help with Enum.reduce on a map

Hi,

I’m learning Elixir at the moment and I’m trying to build custom file uploader without arc/waffle. So far, I’ve managed to build uploader for single files and it works fine; uploaded file is validated, uploaded, renamed and inserted into db via embedded schema.

Now, I would like to add the ability to upload multiple files, also using embedded schema, but I got stuck and I hope someone could help me out.

When form is filled out, I get a map with file number as a key and a map with file data as a value

%{
  "0" => %{
    "image" => %Plug.Upload{
      content_type: "image/jpeg",
      filename: "image_1.jpg",
      path: "/tmp/plug-1611/multipart-1611242895-176617937866775-2"
    }
  },
  "1" => %{
    "image" => %Plug.Upload{
      content_type: "image/jpeg",
      filename: "image_2.jpg",
      path: "/tmp/plug-1611/multipart-1611242895-604710812086625-5"
    }
  }
}

As I want to modify %Plug.Upload{} struct and make some changes to it, I have to convert it into a map using Map.from_struct. This all works when uploading single files but how do I do it with multiple files?

My goal is to get the same map but with %Plug.Upload{} structs converted to maps, nothing else.

%{
  "0" => %{
    "image" => %{
      content_type: "image/jpeg",
      filename: "image_1.jpg",
      path: "/tmp/plug-1611/multipart-1611242895-176617937866775-2"
    }
  },
  "1" => %{
    "image" => %{
      content_type: "image/jpeg",
      filename: "image_2.jpg",
      path: "/tmp/plug-1611/multipart-1611242895-604710812086625-5"
    }
  }
}

I’ve tried with Enum.each but it didn’t work and Jose explained why in his answer on StackOverflow https://stackoverflow.com/a/29924465 , he also gave instructions on how to use Enum.reduce() to do this properly. The thing is, after a couple of hours of trying, I still can’t get it working, I don’t understand how to use it properly with a map so I hope someone could help out me here.

Thanks

Marked As Solved

ityonemo

ityonemo

You want

source
|> Enum.map(fn {k, v} ->
  new_v = Map.put(v, "image", Map.from_struct(v["image"]))
  {k, new_v}
end)
|> Enum.into(%{})

Also Liked

John-Goff

John-Goff

One tiny optimization here, whenever you do Enum.map/2 followed by Enum.into(..., %{}), that can be replaced with a single call to Map.new/2. So the refactored code would look like

Map.new(source, fn {k, v} ->
  new_v = Map.put(v, "image", Map.from_struct(v["image"]))
  {k, new_v}
end)
ityonemo

ityonemo

To understand this just keep in mind that in elixir maps that are not structs are enumerable as key/value tuples, and you can collect key/value tuples into them.

And the other thing is that generally speaking (there are four exceptions) the only way a value can find its way out of a lexical scope is through the return value of the lexical scope.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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 ...
1140 51847 244
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement