Skip to content

Expanding philosophy to moduledoc #41

@NoBrainSkull

Description

@NoBrainSkull

Hello,

I'd love to expand this module philosophy to the documentation of the schema. Would you consider adding an option to the DSL to specify details about each field ?

Maybe something like :

defmodule Person do
  use TypedEctoSchema

  typed_schema "people", """
    Record of a customer.

   [...] Some other details about the schema.
    """ do
    field(:name, :string, enforce: true, null: false, doc: "fullname of the user")
    field(:age, :integer) :: non_neg_integer() | nil
    field(:happy, :boolean, default: true, null: false, doc: "satisfaction threshold computed from order satisfaction")
    field(:phone, :string, doc: "phone number obtained at first-order time")
    belongs_to(:company, Company)
    timestamps(type: :naive_datetime_usec)
  end
end

This would generate somehing like :

defmodule Person do
 @moduledoc """
    Record of a customer.

   [...] Some other details about the schema.

   ### Fields
   name: string - fullname of the user
   age: non_neg_integer - age
   happy: boolean - satisfaction threshold computed from order satisfaction
   phone: string - phone number obtained at first-order time
  """
  use Ecto.Schema

  @enforce_keys [:name]

  schema "people" do
    field(:name, :string)
    field(:age, :integer)
    field(:happy, :boolean, default: true)
    field(:phone, :string)
    belongs_to(:company, Company)
    timestamps(type: :naive_datetime_usec)
  end

  @type t() :: %__MODULE__{
          __meta__: Ecto.Schema.Metadata.t(),
          id: integer() | nil,
          name: String.t(),
          age: non_neg_integer() | nil,
          happy: boolean(),
          phone: String.t() | nil,
          company_id: integer() | nil,
          company: Company.t() | Ecto.Association.NotLoaded.t() | nil,
          inserted_at: NaiveDateTime.t(),
          updated_at: NaiveDateTime.t()
        }
end

As you know, we can very much define module attributes in macros. I'd be glad to do a pull request if you are open to the idea :)

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions