Function generation / dynamic dispatch

Part of my goal is to avoid creating a map of header => Module manually. I want the packet header to be part of the packet definition and all the parse functions to be generated.

I just can’t seem to figure out how to create that list at compile time. Heck if I could even just extend the Packet module that would be great.

defextend Packet do
    def parse(1, data), do: # parse packet
end

defmodule Packets.Login do
  def run() do: # logic to handle the packet
end

I just can’t seem to figure out a way to do this. Even if I keep a module attribute around using persist: true I will end up with an error because the module gets compiled and I can no longer continue to Module.put_attribute

I really just don’t want to end up with a separate file to touch every time a new packet type is created and I also don’t want to have a module that is just a massive list of mappings if this kind of thing can be generated.