gmile

gmile

Getting rid of a transitive compile dependency with custom Ecto type

Hi! I’m continuing my battle (1, 2) against transitive compile-time dependencies on a large code base :slight_smile:

This time I am facing an issue with getting rid of a comp. time dependency coming from a custom Ecto type. The source code repo depicting the issue is available here.

Basically, I have 3 modules: Ecto schema, custom type and a module with helper functions. Heavily simplified versions of them look like this:

defmodule MySchema do
  use Ecto.Schema

  schema "my_table" do
    field :my_field, MyType
  end
end
defmodule MyType do
  use Ecto.Type

  def type, do: :binary

  def cast(value), do: {:ok, MyHelperModule.heavy_casting_work(value)}
  def load(value), do: {:ok, MyHelperModule.heavy_loading_work(value)}
  def dump(value), do: {:ok, MyHelperModule.heavy_dumping_work(value)}
end
defmodule MyHelperModule do
  def heavy_casting_work(value), do: value
  def heavy_loading_work(value), do: value
  def heavy_dumping_work(value), do: value
end

The problem here is that, in case implementation of MyHelperModule changes, both MyType and MySchema modules need to be re-compiled. It can be seen by calling mix xref graph:

mix xref graph --source lib/my_schema.ex

Output:

lib/my_schema.ex
└── lib/my_type.ex (compile)
    └── lib/my_helper_module.ex

The rationale for MyHelperModule’s existence is that it can be quite complicated, and best for it is to exist & be tested on its own. Additionally, it is sometimes used in other contexts in the application, such that don’t really require an Ecto.Type interface.

What would be a strategy to get rid of a transitive dependency here?

It seems to me that Ecto absolutely needs to compile all custom types ahead of compiling the schema, so the only way to ensure there’s no transitive com.-time dependency is to have MyType be absolutely free of dependencies on any other module in the app :frowning:

Marked As Solved

LostKobrakai

LostKobrakai

You can always go and change a compile time dependency to a runtime dependency e.g. by using something like Module.concat to build the module name for your helper module in a function body of a function on the type module.

If the compiler cannot know the module at compile time it cannot react in any way.

Another option would be inverting the dependency. Put the implementations for provided functionality into the type module and let the helper delegate to the implementation on the type.

Also Liked

josevalim

josevalim

Creator of Elixir

:+1: for this particular bit. In this case, it has to be a compile-time dependency, so minimizing the subgraph makes sense.

Where Next?

Popular in Questions Top

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 th...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics 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
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
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