Skip to content
Permalink
Browse files

encoding command from dedicated maps

  • Loading branch information...
Dreat committed Apr 23, 2017
1 parent 1d64594 commit 41929f3570c3c4450d5ffef3a5d824d103b8f606
Showing with 18 additions and 1 deletion.
  1. +8 −1 zmq_ex/lib/zmq_ex.ex
  2. +10 −0 zmq_ex/test/zmq_ex_test.exs
@@ -1,5 +1,4 @@
defmodule ZmqEx do
use Bitwise

def encode(%{flags: flags = %{type: _, long: :short, more: _}, size: size, body: <<body>>}) do
<<encode_flags(flags), size :: size(8), body>>
@@ -14,6 +13,14 @@ defmodule ZmqEx do
|> decode(rest)
end

def encode_command(%{name: name, size: size, data: <<data :: binary>>}) do
case size do
0 -> %{flags: %{type: :command, long: :short, more: false}, size: 8, body: name }
56 -> %{flags: %{type: :command, long: :long, more: false}, size: 64, body: name <> data}
_ -> {:error}
end
end

defp decode(flags = %{type: _, long: :short, more: _}, <<size :: size(8), body :: binary>>) do
%{flags: flags, size: size, body: body}
end
@@ -42,4 +42,14 @@ defmodule ZmqExTest do
result = ZmqEx.encode(%{flags: %{type: :command, long: :short, more: false}, size: 1, body: <<3>>})
assert result == (<<4, 1, 3>>)
end

test "encode short command" do
result = ZmqEx.encode_command(%{name: "command1", size: 0, data: << 0 >>})
assert result == %{flags: %{type: :command, long: :short, more: false}, size: 8, body: <<"command1">>}
end

test "encode long command" do
result = ZmqEx.encode_command(%{name: "command2", size: 56, data: "A command body"})
assert result == %{flags: %{type: :command, long: :long, more: false}, size: 64, body: <<"command2A command body">>}
end
end

0 comments on commit 41929f3

Please sign in to comment.
You can’t perform that action at this time.