elixir-maru/maru_entity

Can't pass captured function as value for ´:if´ option in ´expose´.

nicooga opened this issue · 0 comments

The value for ´:if´ option in ´expose´ is forced to be a function via pattern matching on it's quoted form, not allowing any other kind of value, like a captured function. This forces me to repeat code, like in this example:

expose :rating_count, [if: fn(_, %{action: a}) -> a == "public.v1.cars.makes.show" end], &rating_count/2
expose :rating_value, [if: fn(_, %{action: a}) -> a == "public.v1.cars.makes.show" end], &rating_value/2 expose :max_price, [if: fn(_, %{action: a}) -> a == "public.v1.cars.makes.show" end], &max_price/2
expose :min_price, [if: fn(_, %{action: a}) -> a == "public.v1.cars.makes.show" end], &min_price/2
expose :image, [if: fn(_, %{action: a}) -> a == "public.v1.cars.makes.show" end], &image/2

If :if allowed a captured function as a value I could instead write this:

expose :rating_count, [if: &should_show_metrics?/2], &rating_count/2
expose :rating_value, [if: &should_show_metrics?/2], &rating_value/2
expose :max_price,    [if: &should_show_metrics?/2], &max_price/2
expose :min_price,    [if: &should_show_metrics?/2], &min_price/2
expose :image,        [if: &should_show_metrics?/2], &image/2

defp should_show_metrics?(_, %{action: a}), do: a == "public.v1.cars.makes.show"

Instead I get a compilation error:

== Compilation error in file lib/my_lib/api/public/entities/car/make.ex ==
** (CaseClauseError) no case clause matching: {{:&, [line: 17], [{:/, [line: 17], [{:should_show_metrics?, [line: 17], nil}, 2]}]}, nil}
    lib/maru/entity.ex:275: Maru.Entity.do_parse/2