desmond
My application has many serializers for turning structs into something suitable for clients. These serializers have many serialize/1 functions defined to handle the nil case, list case, and finally the struct itself:
defmodule MyApp.ItemSerializer do
def serialize(nil), do: nil
def serialize(items) when is_list(items) do
Enum.map(items, serialize/1)
end
def serialize(item) do
%{
# ...
}
end
end
I’d like to have a @behaviour MyApp.Serializer directive to guarantee that individual implementations follow the contract of implementing all the serialize/1 functions. The problem is, it looks like callbacks only respect one function implementation, and creating many callbacks with the same function name but different typespecs does not help.
Is this the expected --ahem-- behavior of callbacks?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tty
This is linked to how functions are differentiated in the BEAM. Functions are differentiated by the module they reside in, their name and arity (number of arguments the function accepts). As such serialize/1 is the same function and the various function clauses a contraction of an if-else.
If having these 3 cases are import the define the 3 different functions in the behaviour individually e.g. serialize_nil/1, serialize_lists/1, serialize_item/1. The behaviour can auto-generate a serialize/1 which calls the relevant callbacks.
If you have many different structs perhaps look into using Protocols.
NobbZ
Why don’t you make it a
protocoll? Then you could implement the list-case once in adefimpl X, for: List, thenilindefimpl X, for: Atom, while having discretedefimpl X, for: Yfor your different structs.desmond
The problem with using a Protocol is I want to be able to support different versions of the serializer for the same object, so
GameSerializer.V1.serialize/1andGameSerializer.V2.serialize/1, with the caller selecting which one to use. So I can’t just call a genericSerializer.serialize/1without knowing what I’m passing in (list, nil, actual object) which I believe rules out the use of a single protocol. Unless I’m missing something?OvermindDL1
How do you determine the version?
desmond
The controller itself calls either the
V1orV2serializer, since the controller/route is namespaced with versions as well. So basically hardcoded.OvermindDL1
Why not encode that into the dispatch argument then?
Or use a protocol per version that can delegate to the lower version for the parts that are the same.
Or use my ProtocolEx to dispatch on both the version and value.
There are even more things you can do.
michalmuskala
Here’s an idea - use a protocol with two callbacks -
serialize_v1andserialize_v2. For implementations where those are close enough or even the same, it’s pretty easy to delegate to a common implementation.OvermindDL1
Yep, precisely my second suggestion. ^.^