Skip to content

Commit

Permalink
✨ Adding optional fields for packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ImNotAVirus committed Dec 7, 2019
1 parent 83fe8c0 commit 43ccb75
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/elven_gard/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
4 changes: 4 additions & 0 deletions test/lib/elven_gard/packet_test.exs
Original file line number Diff line number Diff line change
@@ -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}
Expand Down

0 comments on commit 43ccb75

Please sign in to comment.