Closed GPrimola closed 3 years ago
I don't think we'll be able to use funs in attributes like that. I believe this is not a limitation of Jason, but of attributes in Elixir in general.
In particular the issue is that the attribute is set before functions are compiled, so by the time you're setting it the function is not yet available.
@michalmuskala got it! Do you think that anonymous function could do the trick?
I gave it a bit more thought, and TBH I'm not sure I see an advantage of that over writing the full implementation for the protocol manually. Leveraging Jason.Encode.keyword/2
function, this could look like:
defimpl Jason.Encoder, for: MyStruct do
def encode(value, opts) do
value
|> Map.from_struct()
|> Enum.reject(&match({_, %Ecto.Association.NotLoaded{}}, &1))
|> Jason.Encode.keyword(opts)
end
end
@michalmuskala thank you very much! That's a much better solution, of course, since Jason.Encoder is a protocol!
thanks again! :)
I'm going to close this now. Let me know if there's anything else 🙂
I'd like to propose the following option to
__deriving__
macro, so we could do something like that:WDYT?