fireproofsocks

fireproofsocks

Documenting Ecto schemas

I’m working to document a large Elixir project and I’ve noticed that the Ecto schemas do not provide detailed types for their structs.

The best I’ve come up with is to include

@type t :: %__MODULE__{}

inside my Ecto schema modules. This at least causes the shape of the struct to show up in the docs, but it’s not what I would consider “polished”. In the generated ExDoc pages, the type includes the __meta__ attribute (which is confusing to any onlooker unfamiliar with Elixir) and all the values are represented with the term() catch-all:

@type t() :: %MyEctoSchema{
  __meta__: term(),
  foo: term(),
  bar: term(),
  # ...

The source-code says “Generating typespecs for schemas is out of the scope of Ecto.Schema” and that “t/0 has to be defined manually”… but when I tried manually adding my struct fields (in the way I would if this were a normal non-Ecto struct), my definitions all seem to be ignored regardless – it seems that no matter what I define, the output is the same default (with the __meta__ field and every field type as term()).

I did some noodling when I put together the inspecto package, but I don’t think it helps here since I don’t think you can dynamically assemble a type definition (or maybe you can in a macro?).

I think I’m probably missing something so I thought I’d do a sanity check here in the forum. Thanks for any insights!

Marked As Solved

LostKobrakai

LostKobrakai

I just tried it out in a project of mine and the docs update just fine for explicitly listed keys.

Also Liked

LostKobrakai

LostKobrakai

A ecto schema has a __meta__ field if you want it or not. It’s used to store metadata (see Ecto.get_meta). Doing %MyEctoSchema{} will make sure all keys of the schema are part of the typespec no matter if you explicitly type them or not.

Last Post!

fireproofsocks

fireproofsocks

Ah, I see it now – even if you force the fields in your own type definition, all the other fields are still filled in (with the default term() catch-alls)

Where Next?

Popular in Questions Top

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
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement