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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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

We're in Beta

About us Mission Statement