jkbbwr

jkbbwr

Conditionally construct map

So I have found a pothole and I am not sure what the solution is.

I have a pretty standard JSON api in phoenix, I am using ecto, and I am mapping those to maps inside the ThingJSON this is all pretty standard.

Now depending on the route, the controller might decide it doesn’t want to preload some data. For example

%Aos.Schema.Shipyard{
  __meta__: #Ecto.Schema.Metadata<:loaded, "shipyard">,
  id: "103b0451-7396-4f11-9cf8-7081c1829f5e",
  port_id: "a748b9d2-40ff-4f14-8c8c-258b343145eb",
  port: %Aos.Schema.Port{
    __meta__: #Ecto.Schema.Metadata<:loaded, "port">,
    id: "a748b9d2-40ff-4f14-8c8c-258b343145eb",
    name: "London",
    shortcode: "lond",
    destinations: #Ecto.Association.NotLoaded<association :destinations is not loaded>,
    ships: #Ecto.Association.NotLoaded<association :ships is not loaded>,
    shipyard: #Ecto.Association.NotLoaded<association :shipyard is not loaded>,
    agents: #Ecto.Association.NotLoaded<association :agents is not loaded>,
    inserted_at: ~U[2024-07-01 02:02:57Z],
    updated_at: ~U[2024-07-01 02:02:57Z]
  },
  ships: [
    %Aos.Schema.ShipyardStock{
      __meta__: #Ecto.Schema.Metadata<:loaded, "shipyard_stock">,
      id: "8117b318-b0ef-4aa7-be31-88437acbe8fc",
      shipyard_id: "103b0451-7396-4f11-9cf8-7081c1829f5e",
      shipyard: #Ecto.Association.NotLoaded<association :shipyard is not loaded>,
      ship_id: "0a238468-1cf0-47b4-b891-d5936d6554a8",
      ship: #Ecto.Association.NotLoaded<association :ship is not loaded>,
      cost: 100,
      inserted_at: ~U[2024-07-01 02:02:57Z],
      updated_at: ~U[2024-07-01 02:02:57Z]
    }
  ],
  inserted_at: ~U[2024-07-01 02:02:57Z],
  updated_at: ~U[2024-07-01 02:02:57Z]
}

On one route it might want to preload the :ships but on another it might not want to.

I have some pretty standard view code that looks like this


  def render("shipyards.json", %{page: page}) do
    %{
      data: for(yard <- page.entries, do: shipyard(yard)),
      meta: %{
        page_number: page.page_number,
        page_size: page.page_size,
        total_entries: page.total_entries,
        total_pages: page.total_pages
      }
    }
  end

  def shipyard(shipyard) do
    %{
      id: shipyard.id,
      port: PortJSON.port(shipyard.port)
    }
  end

Now I want to modify the shipyard function to also display the ships but only if the assoc is actually preloaded.

Okay so I think

  def shipyard(shipyard) do
    %{
      id: shipyard.id,
      port: PortJSON.port(shipyard.port),
      ships:
        if Ecto.assoc_loaded?(shipyard.ships) do
          for(entry <- shipyard.ships, do: stock(entry))
        else
          # ???
        end
    }
  end

Problem is, if ships is not loaded it will export json of ships: null which is misleading IMO. I’d rather it just omitted the key.

I could do something like this

  def render_if(map, key, assoc, render) do
    if Ecto.assoc_loaded?(assoc) do
      Map.put(map, key, render.(assoc))
    else
      map
    end
  end

  def shipyard(shipyard) do
    %{
      id: shipyard.id,
      port: PortJSON.port(shipyard.port)
    }
    |> render_if(:ships, shipyard.ships, &stock/1)
  end

But it feels kinda wrong.

Also why is Ecto.assoc_loaded? not available as a guard.

Anyone got any ideas?

Most Liked

tfwright

tfwright

I don’t think there’s anything wrong with the way you’re handling it. I have done similar although I had a util that constructed the json with variable guards, among them filtering out unloaded associations–something like this:

def serialize(data) do
  Map.flat_map(data, fn 
    {field, %Ecto.AssocNotLoaded{}} -> []
    {field, assoc = %{}} -> [{field, serialize(assoc)}]
    {field, val} -> [{field, val}]
  end)
  # other selecting/filtering...
end
dimitarvp

dimitarvp

You can pattern match on Ecto.AssocNotLoaded and then have the map construction not include the key in that clause, or you can put nil and then have Enum.reject that deletes any map pairs with a nil value. Either one would be just fine.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement