munksgaard

munksgaard

How to add .t() types for ecto schemas / Handling credo warnings

I have the following context:

defmodule Foo.Companies.Company do
  use Foo.Schema

  schema "companies" do
    field :name, :string

    belongs_to :organization, Foo.Organizations.Organization

    timestamps()
  end
end

Somewhere else I have a function like this:

  @spec do_foo(%Company{}) :: :ok
  def do_foo(%Company{} = company) do
    ...
  end

This seems to work fine, but when I run mix credo I get the following warning:

┃ [W] → Struct %Company{} found in @spec

Upon expanding the warning with mix credo explain ... it says that I should be using something like Company.t() in the type spec instead of the struct directly.

My question is: What is the preferred way of adding such a type to a struct generated by an Ecto schema? I could add something like:

  @type t :: %__MODULE__{
          name: String.t(),
          organization_id: non_neg_integer()
        }

but it seems a bit strange to have to do that manually. Is there a better way? Besides having to do it manually, I see the following problems with the type I just defined:

  • It doesn’t capture the fact that a Company can sometimes also have a organization associated with it. Some functions that take a %Company{} might require this organization to be preloaded, while others might not.
  • It doesn’t capture the fact that there are timestamps in the struct.
  • If I change the schema I also have to change this type manually.

What are best practices for combining credo dialyzer and ecto like this?

Marked As Solved

munksgaard

munksgaard

Aha, I see that e.g. typed_ecto_schema is trying to address this issue.

Also Liked

sodapopcan

sodapopcan

The company I used to work at must have been using such a library. Sorry @munksgaard, I really shouldn’t have answered as I haven’t used typespecs lately. My bad :grimacing:

munksgaard

munksgaard

No hard feelings my friend, I appreciate the effort :slight_smile:

Where Next?

Popular in Questions Top

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
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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement