Arsenalist

Arsenalist

Overriding module results in "duplicated modules" error

I overrode the Ecto NotLoaded association because in my controller the json(conn, ad_campaign) would fail if any relation of ad_campaign was nil, and I really didn’t want to manually craft every single response and would prefer to use the json/2 method.

So I did this:

defimpl Jason.Encoder, for: Ecto.Association.NotLoaded do
  def encode(_, opts) do
    Jason.Encode.map(%{}, opts)
  end
end

This solved the problem well enough, but then when I tried to release the app, I get this error:

** (Mix) Duplicated modules: 
        'Elixir.Jason.Encoder.Ecto.Association.NotLoaded' specified in amplify and ecto

I understand this error - is there a way I can override nicely?

Most Liked

LostKobrakai

LostKobrakai

There is not. Duplicate protocol implementations are considered a problem to be fixed by elixir, not one to be worked around.

Imo the proper way to solve this is to be explicit about which data should be included in the json format and not include incorrect data – not loaded could be an existing association or no existing association.

Protocols are also a problematic way of doing encoding for APIs, because a single datatype might need to encode to multiple distinct json representations: Phoenix Views for JSON APIs | Benjamin Milde

VOUwCNOD

VOUwCNOD

There is a way

defmodule Overrider do
  @on_load :override

  def override do
    defmodule :"Elixir.COMPLETE MODULE NAME HERE" do
      ...
    end
    :ok
  end
end

You can safely inner defmodule with defimpl, but you have to disable protocol consolidation in project configuration.

This is a dark magic, but please, embrace it, make everybody get goosebumps from this

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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

Other popular topics Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

We're in Beta

About us Mission Statement