From 43ccb75cc42a2a78b09e75e942e8f3bcce740964 Mon Sep 17 00:00:00 2001 From: ImNotAVirus Date: Sat, 7 Dec 2019 13:05:21 +0100 Subject: [PATCH] :sparkles: Adding optional fields for packets --- lib/elven_gard/packet.ex | 10 ++++++---- mix.lock | 2 +- test/lib/elven_gard/packet_test.exs | 4 ++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/elven_gard/packet.ex b/lib/elven_gard/packet.ex index a464174..2387a7a 100644 --- a/lib/elven_gard/packet.ex +++ b/lib/elven_gard/packet.ex @@ -140,12 +140,14 @@ defmodule ElvenGard.Packet do Module.put_attribute(caller, :elven_packet_definitions, @elven_current_packet) params_map = - Enum.map(@elven_current_packet.fields, fn %FieldDefinition{name: name} = x -> - case Keyword.get(x.opts, :using) do - nil -> {name, {:_, [], Elixir}} - val -> {name, val} + Enum.reduce(@elven_current_packet.fields, [], fn %FieldDefinition{name: name} = x, acc -> + case {Keyword.get(x.opts, :optional), Keyword.get(x.opts, :using)} do + {true, _} -> acc + {_, nil} -> [{name, {:_, [], Elixir}} | acc] + {_, val} -> [{name, val} | acc] end end) + |> Enum.reverse() @doc false def handle_packet( diff --git a/mix.lock b/mix.lock index 55e772b..cc48920 100644 --- a/mix.lock +++ b/mix.lock @@ -15,7 +15,7 @@ "makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"}, - "nimble_parsec": {:hex, :nimble_parsec, "0.5.2", "1d71150d5293d703a9c38d4329da57d3935faed2031d64bc19e77b654ef2d177", [], [], "hexpm"}, + "nimble_parsec": {:hex, :nimble_parsec, "0.5.2", "1d71150d5293d703a9c38d4329da57d3935faed2031d64bc19e77b654ef2d177", [:mix], [], "hexpm"}, "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"}, "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"}, diff --git a/test/lib/elven_gard/packet_test.exs b/test/lib/elven_gard/packet_test.exs index e892aeb..43fa3b1 100644 --- a/test/lib/elven_gard/packet_test.exs +++ b/test/lib/elven_gard/packet_test.exs @@ -1,4 +1,8 @@ defmodule ElvenGard.PacketTest do + # + # TODO: Add tests for `using` and `optional` fields properties + # + use ExUnit.Case alias ElvenGard.Structures.{FieldDefinition, PacketDefinition}