Skip to content

Commit

Permalink
improvement: upgrade docs/tooling for elixir_sense
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Sep 17, 2021
1 parent b2fea24 commit 1f47e63
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 44 deletions.
22 changes: 10 additions & 12 deletions lib/ash/dsl/extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,19 @@ defmodule Ash.Dsl.Extension do
end

defp set_docs(%Ash.Dsl.Entity{} = entity) do
%{
entity
| docs: Ash.Dsl.Extension.doc_entity(entity),
entities:
Enum.map(entity.entities || [], fn {key, value} -> {key, set_docs(value)} end)
}
entity
|> Map.put(:docs, Ash.Dsl.Extension.doc_entity(entity))
|> Map.put(
:entities,
Enum.map(entity.entities || [], fn {key, value} -> {key, set_docs(value)} end)
)
end

defp set_docs(%Ash.Dsl.Section{} = section) do
%{
section
| entities: set_docs(section.entities),
sections: set_docs(section.sections),
docs: Ash.Dsl.Extension.doc_section(section)
}
section
|> Map.put(:entities, set_docs(section.entities))
|> Map.put(:sections, set_docs(section.sections))
|> Map.put(:docs, Ash.Dsl.Extension.doc_section(section))
end

@doc false
Expand Down
29 changes: 29 additions & 0 deletions lib/ash/options_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ defmodule Ash.OptionsHelpers do
{:one_of, values} ->
Keyword.put(opts, :type, {:in, values})

{:ash_behaviour, behaviour, builtins} ->
Keyword.put(opts, :type, {:custom, __MODULE__, :ash_behaviour, [behaviour, builtins]})

:ash_resource ->
Keyword.put(opts, :type, :atom)

:ash_type ->
# We don't want to add compile time dependencies on types
# TODO: consider making this a legitimate validation
Keyword.put(opts, :type, :any)

_ ->
opts
end
Expand All @@ -53,6 +64,24 @@ defmodule Ash.OptionsHelpers do
end)
end

def ash_behaviour({module, opts}, _behaviour, _builtins) when is_atom(module) do
if Keyword.keyword?(opts) do
# We can't check if it implements the behaviour here, unfortunately
# As it may not be immediately available
{:ok, {module, opts}}
else
{:error, "Expected opts to be a keyword, got: #{inspect(opts)}"}
end
end

def ash_behaviour(module, behaviour, builtins) when is_atom(module) do
ash_behaviour({module, []}, behaviour, builtins)
end

def ash_behaviour(other, _, _) do
{:error, "Expected a module and opts, got: #{inspect(other)}"}
end

def map(value) when is_map(value), do: {:ok, value}
def map(_), do: {:error, "must be a map"}

Expand Down
5 changes: 5 additions & 0 deletions lib/ash/query/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ defmodule Ash.Query do
end
end

@doc """
Creates an Ash expression for evaluation later.
#{File.read!("documentation/topics/expressions.md")}
"""
defmacro expr(do: body) do
quote do
Ash.Query.expr(unquote(body))
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/actions/argument.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Ash.Resource.Actions.Argument do
doc: "Whether or not the argument may be ommitted or set to `nil`"
],
type: [
type: :any,
type: :ash_type,
required: true,
doc: "The type of the argument"
],
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/attribute.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Ash.Resource.Attribute do
doc: "The name of the attribute."
],
type: [
type: :any,
type: :ash_type,
doc: "The type of the attribute."
],
constraints: [
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/calculation/calculation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Ash.Resource.Calculation do
doc: "The name to use for the argument"
],
type: [
type: :any,
type: :ash_type,
required: true,
doc: "The type of the argument"
],
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/change/change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Ash.Resource.Change do
def schema do
[
change: [
type: {:custom, __MODULE__, :change, []},
type: {:ash_behaviour, Ash.Resource.Change, Ash.Resource.Change.Builtins},
doc: """
The module and options for a change.
""",
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/preparation/preparation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Ash.Resource.Preparation do
def schema do
[
preparation: [
type: {:custom, __MODULE__, :preparation, []},
type: {:ash_behaviour, Ash.Resource.Preparation, Ash.Resource.Preparation.Builtins},
doc: """
The module and options for a preparation.
""",
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/relationships/many_to_many.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Ash.Resource.Relationships.ManyToMany do
"The field on the join table that should line up with `destination_field` on the related resource."
],
through: [
type: :atom,
type: :ash_resource,
required: true,
doc: "The resource to use as the join resource."
],
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/relationships/shared_options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Ash.Resource.Relationships.SharedOptions do
doc: "The name of the relationship"
],
destination: [
type: :atom,
type: :ash_resource,
doc: "The destination resource"
],
destination_field: [
Expand Down
32 changes: 7 additions & 25 deletions lib/ash/resource/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule Ash.Resource.Validation do

@schema [
validation: [
type: {:custom, __MODULE__, :validation, []},
type: {:ash_behaviour, Ash.Resource.Validation, Ash.Resource.Validation.Builtins},
required: true,
doc: "The module/opts pair of the validation"
],
Expand Down Expand Up @@ -112,8 +112,12 @@ defmodule Ash.Resource.Validation do
end
end

def transform(%__MODULE__{validation: {module, opts}} = validation) do
{:ok, %{validation | module: module, opts: opts}}
@doc false
def transform(%{validation: {module, opts}} = validation) do
case module.init(opts) do
{:ok, opts} -> {:ok, %{validation | validation: {module, opts}, module: module, opts: opts}}
{:error, error} -> {:error, error}
end
end

def opt_schema, do: @schema
Expand All @@ -131,26 +135,4 @@ defmodule Ash.Resource.Validation do
{:error, "Expected items of [:create, :update, :destroy], got: #{inspect(list)}"}
end
end

def validation({module, opts}) when is_atom(module) do
if Keyword.keyword?(opts) do
case module.init(opts) do
{:ok, opts} ->
{:ok, {module, opts}}

{:error, error} ->
{:error, error}
end
else
{:error, "Validation must be a `{module, opts}` tuple, got: #{inspect({module, opts})}"}
end
end

def validation(module) when is_atom(module) do
validation({module, []})
end

def validation(other) do
{:error, "Validation must be a `{module, opts}` tuple, got: #{inspect(other)}"}
end
end
5 changes: 5 additions & 0 deletions lib/ash/type/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ defmodule Ash.Type do

@builtin_types Keyword.values(@short_names)

@doc false
def builtin_types do
@short_names
end

def builtin?(type) when type in @builtin_types, do: true
def builtin?(_), do: false

Expand Down

0 comments on commit 1f47e63

Please sign in to comment.