Skip to content

Commit

Permalink
fix: fix dialyzer errors for enum + code interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed May 19, 2021
1 parent 53e272c commit 41aaeee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
45 changes: 30 additions & 15 deletions lib/ash/api/interface.ex
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
defmodule Ash.Api.Interface do
@moduledoc false

@doc false
def require_action(resource, interface) do
action = Ash.Resource.Info.action(resource, interface.action || interface.name)

unless action do
raise Ash.Error.Dsl.DslError,
module: resource,
message:
"The interface of #{inspect(resource)} refers to a non-existent action #{
interface.action || interface.name
}",
path: [:interfaces, :interface, interface.name]
end

action
end

defmacro define_interface(api, resource) do
quote bind_quoted: [api: api, resource: resource], generated: true do
quote bind_quoted: [api: api, resource: resource], generated: true, location: :keep do
for interface <- Ash.Resource.Info.interfaces(resource) do
action = Ash.Resource.Info.action(resource, interface.action || interface.name)

unless action do
raise Ash.Error.Dsl.DslError,
module: resource,
message:
"The interface of #{inspect(resource)} refers to a non-existent action #{
interface.action || interface.name
}",
path: [:interfaces, :interface, interface.name]
end
action = Ash.Api.Interface.require_action(resource, interface)

args = interface.args || []
arg_vars = Enum.map(args, &{&1, [], Elixir})
Expand All @@ -33,6 +40,7 @@ defmodule Ash.Api.Interface do
case action.type do
:read ->
@doc doc
@dialyzer {:nowarn_function, {interface.name, Enum.count(args) + 2}}
def unquote(interface.name)(
unquote_splicing(arg_vars),
params_or_opts \\ %{},
Expand Down Expand Up @@ -86,6 +94,7 @@ defmodule Ash.Api.Interface do

@doc doc
# sobelow_skip ["DOS.BinToAtom"]
@dialyzer {:nowarn_function, {:"#{interface.name}!", Enum.count(args) + 2}}
def unquote(:"#{interface.name}!")(
unquote_splicing(arg_vars),
params_or_opts \\ %{},
Expand Down Expand Up @@ -136,6 +145,7 @@ defmodule Ash.Api.Interface do

:create ->
@doc doc
@dialyzer {:nowarn_function, {interface.name, Enum.count(args) + 2}}
def unquote(interface.name)(
unquote_splicing(arg_vars),
params_or_opts \\ %{},
Expand Down Expand Up @@ -168,6 +178,7 @@ defmodule Ash.Api.Interface do
end

@doc doc
@dialyzer {:nowarn_function, {:"#{interface.name}!", Enum.count(args) + 2}}
# sobelow_skip ["DOS.BinToAtom"]
def unquote(:"#{interface.name}!")(
unquote_splicing(arg_vars),
Expand Down Expand Up @@ -202,6 +213,7 @@ defmodule Ash.Api.Interface do

:update ->
@doc doc
@dialyzer {:nowarn_function, {interface.name, Enum.count(args) + 3}}
def unquote(interface.name)(
record,
unquote_splicing(arg_vars),
Expand Down Expand Up @@ -236,6 +248,7 @@ defmodule Ash.Api.Interface do

@doc doc
# sobelow_skip ["DOS.BinToAtom"]
@dialyzer {:nowarn_function, {:"#{interface.name}!", Enum.count(args) + 3}}
def unquote(:"#{interface.name}!")(
record,
unquote_splicing(arg_vars),
Expand Down Expand Up @@ -270,6 +283,7 @@ defmodule Ash.Api.Interface do

:destroy ->
@doc doc
@dialyzer {:nowarn_function, {interface.name, Enum.count(args) + 3}}
def unquote(interface.name)(
record,
unquote_splicing(arg_vars),
Expand Down Expand Up @@ -304,6 +318,7 @@ defmodule Ash.Api.Interface do

@doc doc
# sobelow_skip ["DOS.BinToAtom"]
@dialyzer {:nowarn_function, {:"#{interface.name}!", Enum.count(args) + 3}}
def unquote(:"#{interface.name}!")(
record,
unquote_splicing(arg_vars),
Expand Down Expand Up @@ -565,7 +580,7 @@ defmodule Ash.Api.Interface do
end

defmacro enforce_query_or_resource!(query_or_resource) do
quote do
quote generated: true do
case Ash.Api.Interface.do_enforce_query_or_resource!(unquote(query_or_resource)) do
:ok ->
:ok
Expand All @@ -591,7 +606,7 @@ defmodule Ash.Api.Interface do
def do_enforce_query_or_resource!(_something), do: :error

defmacro enforce_resource!(resource) do
quote do
quote generated: true do
if Ash.Resource.Info.resource?(unquote(resource)) do
:ok
else
Expand All @@ -605,7 +620,7 @@ defmodule Ash.Api.Interface do
end

defmacro enforce_keyword_list!(list) do
quote do
quote generated: true do
if Keyword.keyword?(unquote(list)) do
:ok
else
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/type/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule Ash.Type.Enum do
end

@impl unquote(__MODULE__)
@spec match(term) :: {:ok, term} | :error
@spec match(term) :: {:ok, atom} | :error
def match(value) when value in @values, do: {:ok, value}
def match(value) when value in @string_values, do: {:ok, String.to_existing_atom(value)}

Expand Down

0 comments on commit 41aaeee

Please sign in to comment.