vt-elixir / ja_serializer

JSONAPI.org Serialization in Elixir.
Other
640 stars 148 forks source link

(ArgumentError) cannot set attribute @location inside function/macro #271

Closed InteNs closed 6 years ago

InteNs commented 6 years ago

I'm having a really weird issue with my PostView:

other views with the exact same code don't have this problem.

edit: the location attribute is the problem, is this a bug or intended?

defmodule KidseeApiWeb.PostView do
  use KidseeApiWeb, :view
  use JaSerializer.PhoenixView

  attributes [
    :title,
    :location,
    :content
  ]
end

gives this at compile:

== Compilation error in file lib/kidsee_api_web/views/post_view.ex ==
** (ArgumentError) cannot set attribute @location inside function/macro
    (elixir) lib/kernel.ex:2662: Kernel.do_at/5
    (elixir) expanding macro: Kernel.@/1
    lib/kidsee_api_web/views/post_view.ex:5: KidseeApiWeb.PostView.location/2
    expanding macro: JaSerializer.DSL.location/1
    lib/kidsee_api_web/views/post_view.ex:5: KidseeApiWeb.PostView.location/2

i'm using {:ja_serializer, "~> 0.13"}

alanpeabody commented 6 years ago

Unfortunately you can not use the attributes DSL with attributes that match the callbacks.

In this case I would just define the attributes callback instead, something like:

def attributes(post, _), do: Map.take(post, [:title, :location, :content])