From 964cb4f6cba985fb556f7868bd4c767ae336ae5c Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 25 Jul 2019 14:28:03 +0300 Subject: [PATCH] fix credo case warnings --- .../lib/block_scout_web/chain.ex | 20 ++- .../controllers/api/v1/health_controller.ex | 10 +- .../block_transaction_controller.ex | 156 +++++++++--------- apps/explorer/lib/explorer/chain.ex | 14 +- apps/explorer/lib/explorer/chain/wei.ex | 24 +-- 5 files changed, 120 insertions(+), 104 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/chain.ex b/apps/block_scout_web/lib/block_scout_web/chain.ex index 9e82c30023..08475b1db6 100644 --- a/apps/block_scout_web/lib/block_scout_web/chain.ex +++ b/apps/block_scout_web/lib/block_scout_web/chain.ex @@ -246,18 +246,22 @@ defmodule BlockScoutWeb.Chain do end defp transaction_from_param(param) do - with {:ok, hash} <- string_to_transaction_hash(param) do - hash_to_transaction(hash) - else - :error -> {:error, :not_found} + case string_to_transaction_hash(param) do + {:ok, hash} -> + hash_to_transaction(hash) + + :error -> + {:error, :not_found} end end defp hash_string_to_block(hash_string) do - with {:ok, hash} <- string_to_block_hash(hash_string) do - hash_to_block(hash) - else - :error -> {:error, :not_found} + case string_to_block_hash(hash_string) do + {:ok, hash} -> + hash_to_block(hash) + + :error -> + {:error, :not_found} end end end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex index 957dc797be..15b8dc4660 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex @@ -4,10 +4,12 @@ defmodule BlockScoutWeb.API.V1.HealthController do alias Explorer.Chain def health(conn, _) do - with {:ok, number, timestamp} <- Chain.last_block_status() do - send_resp(conn, :ok, result(number, timestamp)) - else - status -> send_resp(conn, :internal_server_error, error(status)) + case Chain.last_block_status() do + {:ok, number, timestamp} -> + send_resp(conn, :ok, result(number, timestamp)) + + status -> + send_resp(conn, :internal_server_error, error(status)) end end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/block_transaction_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/block_transaction_controller.ex index a1e4cbeb09..dd362cd065 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/block_transaction_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/block_transaction_controller.ex @@ -11,57 +11,57 @@ defmodule BlockScoutWeb.BlockTransactionController do alias Phoenix.View def index(conn, %{"block_hash_or_number" => formatted_block_hash_or_number, "type" => "JSON"} = params) do - with {:ok, block} <- - param_block_hash_or_number_to_block(formatted_block_hash_or_number, []) do - full_options = - Keyword.merge( - [ - necessity_by_association: %{ - :block => :optional, - [created_contract_address: :names] => :optional, - [from_address: :names] => :required, - [to_address: :names] => :optional - } - ], - paging_options(params) - ) - - transactions_plus_one = Chain.block_to_transactions(block.hash, full_options) - - {transactions, next_page} = split_list_by_page(transactions_plus_one) - - next_page_path = - case next_page_params(next_page, transactions, params) do - nil -> - nil + case param_block_hash_or_number_to_block(formatted_block_hash_or_number, []) do + {:ok, block} -> + full_options = + Keyword.merge( + [ + necessity_by_association: %{ + :block => :optional, + [created_contract_address: :names] => :optional, + [from_address: :names] => :required, + [to_address: :names] => :optional + } + ], + paging_options(params) + ) - next_page_params -> - block_transaction_path( - conn, - :index, - block, - Map.delete(next_page_params, "type") + transactions_plus_one = Chain.block_to_transactions(block.hash, full_options) + + {transactions, next_page} = split_list_by_page(transactions_plus_one) + + next_page_path = + case next_page_params(next_page, transactions, params) do + nil -> + nil + + next_page_params -> + block_transaction_path( + conn, + :index, + block, + Map.delete(next_page_params, "type") + ) + end + + items = + transactions + |> Enum.map(fn transaction -> + View.render_to_string( + TransactionView, + "_tile.html", + transaction: transaction ) - end - - items = - transactions - |> Enum.map(fn transaction -> - View.render_to_string( - TransactionView, - "_tile.html", - transaction: transaction - ) - end) - - json( - conn, - %{ - items: items, - next_page_path: next_page_path - } - ) - else + end) + + json( + conn, + %{ + items: items, + next_page_path: next_page_path + } + ) + {:error, {:invalid, :hash}} -> not_found(conn) @@ -80,25 +80,25 @@ defmodule BlockScoutWeb.BlockTransactionController do end def index(conn, %{"block_hash_or_number" => formatted_block_hash_or_number}) do - with {:ok, block} <- - param_block_hash_or_number_to_block(formatted_block_hash_or_number, - necessity_by_association: %{ - [miner: :names] => :required, - :uncles => :optional, - :nephews => :optional, - :rewards => :optional - } - ) do - block_transaction_count = Chain.block_to_transaction_count(block.hash) - - render( - conn, - "index.html", - block: block, - block_transaction_count: block_transaction_count, - current_path: current_path(conn) - ) - else + case param_block_hash_or_number_to_block(formatted_block_hash_or_number, + necessity_by_association: %{ + [miner: :names] => :required, + :uncles => :optional, + :nephews => :optional, + :rewards => :optional + } + ) do + {:ok, block} -> + block_transaction_count = Chain.block_to_transaction_count(block.hash) + + render( + conn, + "index.html", + block: block, + block_transaction_count: block_transaction_count, + current_path: current_path(conn) + ) + {:error, {:invalid, :hash}} -> not_found(conn) @@ -117,19 +117,23 @@ defmodule BlockScoutWeb.BlockTransactionController do end defp param_block_hash_or_number_to_block("0x" <> _ = param, options) do - with {:ok, hash} <- string_to_block_hash(param) do - hash_to_block(hash, options) - else - :error -> {:error, {:invalid, :hash}} + case string_to_block_hash(param) do + {:ok, hash} -> + hash_to_block(hash, options) + + :error -> + {:error, {:invalid, :hash}} end end defp param_block_hash_or_number_to_block(number_string, options) when is_binary(number_string) do - with {:ok, number} <- BlockScoutWeb.Chain.param_to_block_number(number_string) do - number_to_block(number, options) - else - {:error, :invalid} -> {:error, {:invalid, :number}} + case BlockScoutWeb.Chain.param_to_block_number(number_string) do + {:ok, number} -> + number_to_block(number, options) + + {:error, :invalid} -> + {:error, {:invalid, :number}} end end diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 46eda15b62..09dc7ae063 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2447,9 +2447,10 @@ defmodule Explorer.Chain do |> Multi.run(:set_address_verified, &set_address_verified/2) |> Repo.transaction() - with {:ok, %{smart_contract: smart_contract}} <- insert_result do - {:ok, smart_contract} - else + case insert_result do + {:ok, %{smart_contract: smart_contract}} -> + {:ok, smart_contract} + {:error, :smart_contract, changeset, _} -> {:error, changeset} @@ -2919,9 +2920,10 @@ defmodule Explorer.Chain do ) |> Repo.transaction() - with {:ok, %{token: token}} <- insert_result do - {:ok, token} - else + case insert_result do + {:ok, %{token: token}} -> + {:ok, token} + {:error, :token, changeset, _} -> {:error, changeset} end diff --git a/apps/explorer/lib/explorer/chain/wei.ex b/apps/explorer/lib/explorer/chain/wei.ex index c1c434aa44..78483632a7 100644 --- a/apps/explorer/lib/explorer/chain/wei.ex +++ b/apps/explorer/lib/explorer/chain/wei.ex @@ -31,21 +31,25 @@ defmodule Explorer.Chain.Wei do @impl Ecto.Type def cast("0x" <> hex_wei) do - with {int_wei, ""} <- Integer.parse(hex_wei, 16) do - decimal = Decimal.new(int_wei) - {:ok, %__MODULE__{value: decimal}} - else - _ -> :error + case Integer.parse(hex_wei, 16) do + {int_wei, ""} -> + decimal = Decimal.new(int_wei) + {:ok, %__MODULE__{value: decimal}} + + _ -> + :error end end @impl Ecto.Type def cast(string_wei) when is_binary(string_wei) do - with {int_wei, ""} <- Integer.parse(string_wei) do - decimal = Decimal.new(int_wei) - {:ok, %__MODULE__{value: decimal}} - else - _ -> :error + case Integer.parse(string_wei) do + {int_wei, ""} -> + decimal = Decimal.new(int_wei) + {:ok, %__MODULE__{value: decimal}} + + _ -> + :error end end