How to check if multiple values are something

Hi,

I am trying to change each struct to a map, with structs that are embedded.

For example:

I have a struct that looks like this:

%Family{
    parnets: [
        %Parent{
            name: "Name",
            sex: "female",
            created_at: ~N[1234-07-13 12:39:23]}
    ],
    kids: [
        %Kid{
            name: "Name",
            height: 1.90}
    ]
}

I would like to get something like this:

%{
    parnets: [
        %{
            name: "Name",
            sex: "female",
            created_at: ~N[1234-07-13 12:39:23]}
    ],
    kids: [
        %{
            name: "Name",
            height: 1.90}
    ]
}

How can I achieve this? I will be grateful for help.

With something like:

family
|> Map.from_struct()
|> Map.new(fn 
  {k, list_of_structs} -> {k, Enum.map(list_of_structs, & Map.from_struct/1) } 
end)
2 Likes

Thanks a lot! Works like a charm!