Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Clean up dialyzer issues (#329)
Browse files Browse the repository at this point in the history
* Bump dialyxir to 1.0.0-rc.3
* Uses default dialyzer flags
* Fix dialyzer warnings
* Turn dialyzer ignore to exs script for more flexibility
* Add iex and mix to Dialyzer's PLT to remove most of the ignores
  • Loading branch information
unnawut authored Jul 25, 2018
1 parent c8f9d3f commit 4407bcd
Show file tree
Hide file tree
Showing 58 changed files with 325 additions and 295 deletions.
32 changes: 0 additions & 32 deletions .dialyzer_ignore

This file was deleted.

12 changes: 12 additions & 0 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
# Warnings from `Arc.Ecto.Schema.cast_attachments/3` macro that we couldn't touch
{"apps/ewallet_db/lib/ewallet_db/account.ex", :pattern_match},
{"apps/ewallet_db/lib/ewallet_db/user.ex", :pattern_match},

# `Account.load_accounts/1` uses `query_result.columns` which is available in `%Postgrex.Result{}`
# but not for other adapters. Hence not returned in typespec of `Ecto.Adapters.SQL.query/4`.
{"apps/ewallet_db/lib/ewallet_db/account.ex", :no_return},

# TODO: Remove extra attributes e.g. `Map.put(wallet, :account_id, account.id)` from `%Wallet{}`
{"apps/ewallet/lib/ewallet/gates/transaction_consumption_consumer_gate.ex", :call}
]
9 changes: 5 additions & 4 deletions apps/admin_api/lib/admin_api/invites/inviter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ defmodule AdminAPI.Inviter do
{:ok, %Invite{}} | {:error, :invalid_parameter, String.t()}
def send_email(invite, redirect_url) do
if valid_url?(redirect_url) do
invite
|> Repo.preload(:user)
|> InviteEmail.create(redirect_url)
|> Mailer.deliver_now()
_ =
invite
|> Repo.preload(:user)
|> InviteEmail.create(redirect_url)
|> Mailer.deliver_now()

{:ok, invite}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule AdminAPI.V1.AccountController do
@doc """
Retrieves a list of accounts based on current account for users.
"""
@spec all(Plug.Conn.t(), map()) :: Plug.Conn.t()
def all(conn, attrs) do
with :ok <- permit(:all, conn.assigns, nil),
account_uuids <- AccountHelper.get_accessible_account_uuids(conn.assigns) do
Expand All @@ -44,10 +45,10 @@ defmodule AdminAPI.V1.AccountController do
|> respond(conn)
else
error -> respond(error, conn)
nil -> respond(conn, :account_id_not_found)
end
end

@spec descendants_for_account(Plug.Conn.t(), map()) :: Plug.Conn.t()
def descendants_for_account(conn, %{"id" => account_id} = attrs) do
with %Account{} = account <- Account.get(account_id) || {:error, :unauthorized},
:ok <- permit(:all, conn.assigns, account.id),
Expand All @@ -69,6 +70,7 @@ defmodule AdminAPI.V1.AccountController do
@doc """
Retrieves a specific account by its id.
"""
@spec get(Plug.Conn.t(), map()) :: Plug.Conn.t()
def get(conn, %{"id" => id}) do
with %Account{} = account <- Account.get_by(id: id) || {:error, :unauthorized},
:ok <- permit(:get, conn.assigns, account.id),
Expand All @@ -88,6 +90,7 @@ defmodule AdminAPI.V1.AccountController do
The requesting user must have write permission on the given parent account.
"""
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
def create(conn, attrs) do
parent =
if attrs["parent_id"] do
Expand Down Expand Up @@ -115,6 +118,7 @@ defmodule AdminAPI.V1.AccountController do
The requesting user must have write permission on the given account.
"""
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()
def update(conn, %{"id" => account_id} = attrs) do
with %Account{} = original <- Account.get(account_id) || {:error, :unauthorized},
:ok <- permit(:update, conn.assigns, original.id),
Expand All @@ -135,6 +139,7 @@ defmodule AdminAPI.V1.AccountController do
@doc """
Uploads an image as avatar for a specific account.
"""
@spec upload_avatar(Plug.Conn.t(), map()) :: Plug.Conn.t()
def upload_avatar(conn, %{"id" => id, "avatar" => _} = attrs) do
with %Account{} = account <- Account.get(id) || {:error, :unauthorized},
:ok <- permit(:update, conn.assigns, account.id),
Expand Down Expand Up @@ -170,7 +175,7 @@ defmodule AdminAPI.V1.AccountController do
handle_error(conn, code, description)
end

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update, map(), String.t() | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, account_id) do
Bodyguard.permit(AccountPolicy, action, params, account_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule AdminAPI.V1.AccountMembershipController do

def unassign_user(conn, _attrs), do: handle_error(conn, :invalid_parameter)

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update | :delete, map(), String.t()) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, account_id) do
Bodyguard.permit(AccountMembershipPolicy, action, params, account_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ defmodule AdminAPI.V1.AdminAuthController do

def switch_account(conn, %{"account_id" => account_id}) do
with {:ok, _current_user} <- permit(:get, conn.assigns),
%Account{} = account <- Account.get(account_id) || {:error, :unauthorized},
%Account{} = account <- Account.get(account_id) || :unauthorized,
:ok <- permit_account(:get, conn.assigns, account.id),
token <- conn.private.auth_auth_token,
%AuthToken{} = token <-
AuthToken.get_by_token(token, :admin_api) || :auth_token_not_found,
{:ok, token} <- AuthToken.switch_account(token, account) do
render_token(conn, token)
else
error when is_atom(error) ->
render_error(conn, {:error, error})
{:error, error} ->
handle_error(conn, error)

error ->
render_error(conn, error)
handle_error(conn, error)
end
end

Expand All @@ -47,17 +47,13 @@ defmodule AdminAPI.V1.AdminAuthController do
end

defp respond_with_token(conn) do
render_error(conn, {:error, :invalid_login_credentials})
handle_error(conn, :invalid_login_credentials)
end

defp render_token(conn, auth_token) do
render(conn, :auth_token, %{auth_token: auth_token})
end

defp render_error(conn, {:error, code}) do
handle_error(conn, code)
end

@doc """
Invalidates the authentication token used in this request.
"""
Expand All @@ -67,13 +63,12 @@ defmodule AdminAPI.V1.AdminAuthController do
|> AdminUserAuthenticator.expire_token()
|> render(:empty_response, %{})
else
error ->
render_error(conn, {:error, error})
error_code ->
handle_error(conn, error_code)
end
end

@spec permit(:all | :create | :get | :update, any()) ::
{:ok, User.t()} | {:error, any()} | no_return()
@spec permit(:get | :update, map()) :: {:ok, %EWalletDB.User{}} | atom() | no_return()
defp permit(_action, %{admin_user: admin_user}) do
{:ok, admin_user}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule AdminAPI.V1.AdminUserController do
@doc """
Retrieves a list of admins that the current user/key has access to.
"""
@spec all(Plug.Conn.t(), map()) :: Plug.Conn.t()
def all(conn, attrs) do
with :ok <- permit(:all, conn.assigns, nil),
account_uuids <- AccountHelper.get_accessible_account_uuids(conn.assigns) do
Expand All @@ -46,6 +47,7 @@ defmodule AdminAPI.V1.AdminUserController do
@doc """
Retrieves a specific admin by its id.
"""
@spec get(Plug.Conn.t(), map()) :: Plug.Conn.t()
def get(conn, %{"id" => user_id}) do
with %User{} = user <- User.get(user_id) || {:error, :unauthorized},
:ok <- permit(:get, conn.assigns, user) do
Expand All @@ -69,12 +71,7 @@ defmodule AdminAPI.V1.AdminUserController do
render(conn, UserView, :user, %{user: user})
end

# Responds when the admin is not found
defp respond_single(nil, conn) do
handle_error(conn, :user_id_not_found)
end

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update, map(), %User{} | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, user) do
Bodyguard.permit(AdminUserPolicy, action, params, user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule AdminAPI.V1.APIKeyController do
@doc """
Retrieves a list of API keys including soft-deleted.
"""
@spec all(Plug.Conn.t(), map()) :: Plug.Conn.t()
def all(conn, attrs) do
with :ok <- permit(:all, conn.assigns, nil) do
APIKey
Expand All @@ -52,6 +53,7 @@ defmodule AdminAPI.V1.APIKeyController do
@doc """
Creates a new API key. Currently API keys are assigned to the master account only.
"""
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
def create(conn, _attrs) do
with :ok <- permit(:create, conn.assigns, nil) do
# Admin API doesn't use API Keys anymore. Defaulting to "ewallet_api".
Expand All @@ -67,6 +69,7 @@ defmodule AdminAPI.V1.APIKeyController do
@doc """
Update an API key.
"""
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()
def update(conn, %{"id" => id} = attrs) do
with :ok <- permit(:update, conn.assigns, id),
%APIKey{} = api_key <- APIKey.get(id) || :api_key_not_found,
Expand Down Expand Up @@ -101,6 +104,7 @@ defmodule AdminAPI.V1.APIKeyController do
@doc """
Soft-deletes an existing API key by its id.
"""
@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t()
def delete(conn, %{"id" => id}) do
with :ok <- permit(:delete, conn.assigns, id),
%APIKey{} = key <- APIKey.get(id) do
Expand All @@ -126,7 +130,7 @@ defmodule AdminAPI.V1.APIKeyController do
end
end

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update | :delete, map(), String.t() | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, api_key_id) do
Bodyguard.permit(APIKeyPolicy, action, params, api_key_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule AdminAPI.V1.CategoryController do
@doc """
Retrieves a list of categories.
"""
@spec all(Plug.Conn.t(), map()) :: Plug.Conn.t()
def all(conn, attrs) do
with :ok <- permit(:all, conn.assigns, nil) do
Category
Expand All @@ -53,6 +54,7 @@ defmodule AdminAPI.V1.CategoryController do
@doc """
Retrieves a specific category by its id.
"""
@spec get(Plug.Conn.t(), map()) :: Plug.Conn.t()
def get(conn, %{"id" => id}) do
with :ok <- permit(:get, conn.assigns, id),
%Category{} = category <- Category.get_by(id: id),
Expand All @@ -70,6 +72,7 @@ defmodule AdminAPI.V1.CategoryController do
@doc """
Creates a new category.
"""
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
def create(conn, attrs) do
with :ok <- permit(:create, conn.assigns, nil),
{:ok, category} <- Category.insert(attrs),
Expand All @@ -87,6 +90,7 @@ defmodule AdminAPI.V1.CategoryController do
@doc """
Updates the category if all required parameters are provided.
"""
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()
def update(conn, %{"id" => id} = attrs) do
with :ok <- permit(:update, conn.assigns, id),
%Category{} = original <- Category.get(id) || {:error, :category_id_not_found},
Expand All @@ -107,6 +111,7 @@ defmodule AdminAPI.V1.CategoryController do
@doc """
Soft-deletes an existing category by its id.
"""
@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t()
def delete(conn, %{"id" => id}) do
with %Category{} = category <- Category.get(id) || {:error, :category_id_not_found},
{:ok, deleted} <- Category.delete(category),
Expand All @@ -123,7 +128,7 @@ defmodule AdminAPI.V1.CategoryController do

def delete(conn, _), do: handle_error(conn, :invalid_parameter)

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update, map(), String.t() | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, account_id) do
Bodyguard.permit(CategoryPolicy, action, params, account_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ defmodule AdminAPI.V1.ExchangePairController do

def delete(conn, _), do: handle_error(conn, :invalid_parameter)

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update | :delete, map(), String.t() | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, exchange_pair_id) do
Bodyguard.permit(ExchangePairPolicy, action, params, exchange_pair_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule AdminAPI.V1.KeyController do
@doc """
Retrieves a list of keys including soft-deleted.
"""
@spec all(Plug.Conn.t(), map()) :: Plug.Conn.t()
def all(conn, attrs) do
with :ok <- permit(:all, conn.assigns, nil),
account_uuids <- AccountHelper.get_accessible_account_uuids(conn.assigns) do
Expand Down Expand Up @@ -55,6 +56,7 @@ defmodule AdminAPI.V1.KeyController do
@doc """
Creates a new key. Currently keys are assigned to the master account only.
"""
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
def create(conn, _attrs) do
with :ok <- permit(:create, conn.assigns, nil) do
%{}
Expand All @@ -79,6 +81,7 @@ defmodule AdminAPI.V1.KeyController do
@doc """
Updates a key.
"""
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()
def update(conn, %{"id" => id} = attrs) do
with %Key{} = api_key <- Key.get(id) || {:error, :key_not_found},
{:ok, key} <- Key.update(api_key, attrs) do
Expand All @@ -99,6 +102,7 @@ defmodule AdminAPI.V1.KeyController do
@doc """
Soft-deletes an existing key.
"""
@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t()
def delete(conn, %{"access_key" => access_key}) do
with :ok <- permit(:delete, conn.assigns, nil) do
key = Key.get(:access_key, access_key)
Expand Down Expand Up @@ -133,7 +137,7 @@ defmodule AdminAPI.V1.KeyController do

defp do_delete(conn, nil), do: handle_error(conn, :key_not_found)

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update | :delete, map(), String.t() | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, key_id) do
Bodyguard.permit(KeyPolicy, action, params, key_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule AdminAPI.V1.MintController do
render(conn, :mint, %{mint: mint})
end

@spec permit(:all | :create | :get | :update, map(), String.t()) ::
@spec permit(:all | :create | :get | :update, map(), String.t() | nil) ::
:ok | {:error, any()} | no_return()
defp permit(action, params, account_id) do
Bodyguard.permit(MintPolicy, action, params, account_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ defmodule AdminAPI.V1.SelfController do
handle_error(conn, :user_id_not_found)
end

@spec permit(:all | :create | :get | :update, map()) ::
{:ok, User.t()} | {:error, any()} | no_return()
@spec permit(:get | :update, map()) :: {:ok, %User{}} | :access_key_unauthorized
defp permit(_action, %{admin_user: admin_user}) do
{:ok, admin_user}
end
Expand Down
Loading

0 comments on commit 4407bcd

Please sign in to comment.