Skip to content

Commit

Permalink
✨ Support :if condition on packet serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ImNotAVirus committed Sep 13, 2023
1 parent 35586c1 commit 7b3d817
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/elven_gard/network/packet_serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,26 @@ defmodule ElvenGard.Network.PacketSerializer do
defp def_serialize(%{id: id, fields: fields}) do
fields_ast =
Enum.map(fields, fn %{name: name, type: type, opts: opts} ->
quote location: :keep do
unquote(type).encode(Map.fetch!(var!(packet), unquote(name)), unquote(opts))
case Keyword.get(opts, :if) do
nil ->
quote location: :keep do
unquote(type).encode(Map.fetch!(var!(packet), unquote(name)), unquote(opts))
end

condition ->
quote location: :keep do
if unquote(condition) in [nil, false] do
:"$drop"
else
unquote(type).encode(Map.fetch!(var!(packet), unquote(name)), unquote(opts))
end
end
end
end)

quote location: :keep, generated: true do
def serialize(%__MODULE__{} = var!(packet)) do
{unquote(id), unquote(fields_ast)}
{unquote(id), Enum.reject(unquote(fields_ast), &match?(:"$drop", &1))}
end
end
end
Expand Down

0 comments on commit 7b3d817

Please sign in to comment.