Elixir确保值不是0或零

时间:2017-12-21 23:03:20

标签: elixir elixir-framework

在这个函数中,我需要检查“price”变量是0还是nil才能通过执行priceFloat = price / 1将其转换为float,或者我得到算术错误。

  def insert_product_shop(conn, product_id, shop_id, price) do
    IO.inspect(price, label: "price")
    priceFloat = price / 1 
    changeset = Api.ProductShop.changeset(%Api.ProductShop{p_id: product_id, s_id: shop_id, not_in_shop_count: 0, price: priceFloat})
    errors = changeset.errors
    valid = changeset.valid?
    case insert(changeset) do
      {:ok, product_shop} ->
        {:ok, product_shop}
      {:error, changeset} ->
        {:error, :failure}
    end
  end

这样做的惯用方法是什么?

我试过这个,但我仍然得到算术错误:

  def insert_product_shop(conn, product_id, shop_id, price) do
    IO.inspect(price, label: "price")
    case {price} do 
      {price} when price > 0 ->
        priceFloat = price / 1 
        changeset = Api.ProductShop.changeset(%Api.ProductShop{p_id: product_id, s_id: shop_id, not_in_shop_count: 0, price: priceFloat})
        errors = changeset.errors
        valid = changeset.valid?
        case insert(changeset) do
          {:ok, product_shop} ->
            {:ok, product_shop}
          {:error, changeset} ->
            {:error, :failure}
        end
      end
  end

2 个答案:

答案 0 :(得分:4)

你的代码不起作用的原因是因为在Elixir中,nil > 0是真的。你可以这样做:

if price not in [0, nil] do
  ...
else
  ...
end

if is_number(price) and price > 0 do
  ...
else
  ...
end

答案 1 :(得分:2)

执行此操作的惯用方法是直接在函数子句中进行模式匹配:

def insert_product_shop(conn, product_id, shop_id, price)
  when not is_number(price), do: {:error, :nil}
def insert_product_shop(conn, product_id, shop_id, price)
  when price <= 0, do: {:error, :not_positive}
def insert_product_shop(conn, product_id, shop_id, price) do
  priceFloat = price / 1
  changeset = 
    Api.ProductShop.changeset(
      %Api.ProductShop{p_id: product_id,
                       s_id: shop_id,
                       not_in_shop_count: 0,
                       price: priceFloat})
  errors = changeset.errors
  valid = changeset.valid?

  case insert(changeset) do
    {:ok, product_shop} ->
      {:ok, product_shop}
    {:error, changeset} ->
      {:error, :failure}
  end
end

前两个条款可能会合并为一个有警卫的条款

when not is_number(price) or price <= 0
相关问题