Skip to content

Commit

Permalink
Apply mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Feb 5, 2019
1 parent 0dfa3e3 commit d930d9e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
14 changes: 9 additions & 5 deletions lib/protox/define.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,16 @@ defmodule Protox.Define do
defp make_required_fields_typespec([]) do
quote do: []
end

defp make_required_fields_typespec(fields) do
specs = Enum.reduce(
fields,
fn field, acc ->
quote do: unquote(acc) | unquote(field)
end)
specs =
Enum.reduce(
fields,
fn field, acc ->
quote do: unquote(acc) | unquote(field)
end
)

quote do: [unquote(specs)]
end

Expand Down
3 changes: 2 additions & 1 deletion lib/protox/define_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ defmodule Protox.DefineEncoder do
defp make_encode_unknown_fields_fun() do
quote do
defp encode_unknown_fields(acc, msg) do
Enum.reduce(msg.__struct__.get_unknown_fields(msg), acc, fn {tag, wire_type, bytes}, acc ->
Enum.reduce(msg.__struct__.get_unknown_fields(msg), acc, fn {tag, wire_type, bytes},
acc ->
case wire_type do
0 ->
[acc, make_key_bytes(tag, :int32), bytes]
Expand Down
6 changes: 4 additions & 2 deletions lib/protox/descriptor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ defmodule Protox.Descriptor do
{4, :repeated, :enum_type, :unpacked, {:message, Google.Protobuf.EnumDescriptorProto}},
{5, :repeated, :extension_range, :unpacked,
{:message, Google.Protobuf.DescriptorProto.ExtensionRange}},
{8, :repeated, :oneof_decl, :unpacked, {:message, Google.Protobuf.OneofDescriptorProto}},
{8, :repeated, :oneof_decl, :unpacked,
{:message, Google.Protobuf.OneofDescriptorProto}},
{7, :none, :options, {:default, nil}, {:message, Google.Protobuf.MessageOptions}}
]
},
Expand All @@ -85,7 +86,8 @@ defmodule Protox.Descriptor do
# Ignored: 10
{1, :none, :name, {:default, nil}, :string},
{3, :none, :number, {:default, nil}, :int32},
{4, :none, :label, {:default, nil}, {:enum, Google.Protobuf.FieldDescriptorProto.Label}},
{4, :none, :label, {:default, nil},
{:enum, Google.Protobuf.FieldDescriptorProto.Label}},
{5, :none, :type, {:default, nil}, {:enum, Google.Protobuf.FieldDescriptorProto.Type}},
{6, :none, :type_name, {:default, nil}, :string},
{2, :none, :extendee, {:default, nil}, :string},
Expand Down
4 changes: 3 additions & 1 deletion lib/protox/parse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ defmodule Protox.Parse do
Module.concat([namespace | mname]),
Enum.map(
fields,
&(&1 |> resolve_types(enums, messages) |> default_value(enums)
&(&1
|> resolve_types(enums, messages)
|> default_value(enums)
|> concat_names(namespace))
)
}
Expand Down
53 changes: 35 additions & 18 deletions test/protox/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ defmodule Protox.EncodeTest do
end

test "Sub.g; Sub.h; Sub.i" do
assert %Sub{g: [0], h: [-1], i: [33.2, -44.0]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{g: [0], h: [-1], i: [33.2, -44.0]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<106, 8, 0, 0, 0, 0, 0, 0, 0, 0, 114, 4, 255, 255, 255, 255, 122, 16, 154, 153, 153,
153, 153, 153, 64, 64, 0, 0, 0, 0, 0, 0, 70, 192>>
end

test "Sub.i, infinity, -infinity, nan" do
assert %Sub{i: [:infinity, :"-infinity", :nan]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{i: [:infinity, :"-infinity", :nan]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<122, 24, 0, 0, 0, 0, 0, 0, 0xF0, 0x7F, 0, 0, 0, 0, 0, 0, 0xF0, 0xFF, 0, 0, 0, 0, 0,
1, 241, 255>>
end
Expand Down Expand Up @@ -80,27 +84,29 @@ defmodule Protox.EncodeTest do
end

test "Sub.n" do
assert %Sub{n: [true, false, false, true]}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{n: [true, false, false, true]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<162, 1, 4, 1, 0, 0, 1>>
end

test "Sub.n (all true)" do
assert %Sub{n: [true, true, true, true]}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{n: [true, true, true, true]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<162, 1, 4, 1, 1, 1, 1>>
end

test "Sub.n (all false)" do
assert %Sub{n: [false, false, false, false]}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{n: [false, false, false, false]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<162, 1, 4, 0, 0, 0, 0>>
end

test "Sub.o " do
assert %Sub{o: [:FOO, :BAR, :FOO]}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{o: [:FOO, :BAR, :FOO]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<170, 1, 3, 0, 1, 0>>
end

test "Sub.o (unknown entry) " do
assert %Sub{o: [:FOO, :BAR, :FOO, 2]}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{o: [:FOO, :BAR, :FOO, 2]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<170, 1, 4, 0, 1, 0, 2>>
end

Expand All @@ -115,17 +121,17 @@ defmodule Protox.EncodeTest do
end

test "Sub.q (unpacked in definition, with unknown values) " do
assert %Sub{q: [:FOO, :BAR, 2, :FOO]}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{q: [:FOO, :BAR, 2, :FOO]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<184, 1, 0, 184, 1, 1, 184, 1, 2, 184, 1, 0>>
end

test "Sub.r, negative constant" do
assert %Sub{r: :NEG}|> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Sub{r: :NEG} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<192, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1>>
end

test "Sub.z" do
assert %Sub{z: -20}|> Protox.Encode.encode() |> :binary.list_to_bin() == <<136, 241, 4, 39>>
assert %Sub{z: -20} |> Protox.Encode.encode() |> :binary.list_to_bin() == <<136, 241, 4, 39>>
end

test "Sub, unknown fields (double)" do
Expand All @@ -141,7 +147,8 @@ defmodule Protox.EncodeTest do
end

test "Sub, unknown tag (embedded message)" do
assert %Sub{a: 42, b: "", z: -42, __uf__: [{4, 2, <<>>}]}|> Protox.Encode.encode()
assert %Sub{a: 42, b: "", z: -42, __uf__: [{4, 2, <<>>}]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() == <<8, 42, 136, 241, 4, 83, 34, 0>>
end

Expand All @@ -159,7 +166,8 @@ defmodule Protox.EncodeTest do
test "Sub, unknown tag (bytes)" do
bytes = Stream.repeatedly(fn -> <<0>> end) |> Enum.take(128) |> Enum.join()

assert %Sub{a: 3342, b: "", z: -10, __uf__: [{10, 2, bytes}]}|> Protox.Encode.encode()
assert %Sub{a: 3342, b: "", z: -10, __uf__: [{10, 2, bytes}]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() == <<8, 142, 26, 136, 241, 4, 19, 82, 128, 1, bytes::binary>>
end

Expand Down Expand Up @@ -200,7 +208,7 @@ defmodule Protox.EncodeTest do
end

test "Msg.d, :BAR" do
assert %Msg{d: :BAR}|> Protox.Encode.encode() |> :binary.list_to_bin() == <<8, 1>>
assert %Msg{d: :BAR} |> Protox.Encode.encode() |> :binary.list_to_bin() == <<8, 1>>
end

test "Msg.d, :BAZ" do
Expand Down Expand Up @@ -238,7 +246,8 @@ defmodule Protox.EncodeTest do
end

test "Msg.g" do
assert %Msg{g: [1, 2, 3]} |> Protox.Encode.encode() |> :binary.list_to_bin() == <<34, 3, 1, 2, 3>>
assert %Msg{g: [1, 2, 3]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
<<34, 3, 1, 2, 3>>
end

test "Msg.h, empty" do
Expand All @@ -256,7 +265,9 @@ defmodule Protox.EncodeTest do
end

test "Msg.i, infinity, -infinity, nan" do
assert %Msg{i: [:infinity, :"-infinity", :nan]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Msg{i: [:infinity, :"-infinity", :nan]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<50, 12, 0, 0, 0x80, 0x7F, 0, 0, 0x80, 0xFF, 0, 1, 129, 255>>
end

Expand All @@ -276,7 +287,9 @@ defmodule Protox.EncodeTest do
end

test "Msg.j" do
assert %Msg{j: [%Sub{a: 42}, %Sub{b: "foo"}]} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Msg{j: [%Sub{a: 42}, %Sub{b: "foo"}]}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<58, 2, 8, 42, 58, 5, 18, 3, 102, 111, 111>>
end

Expand All @@ -286,7 +299,9 @@ defmodule Protox.EncodeTest do
end

test "Msg.l" do
assert %Msg{l: %{"bar" => 1.0, "foo" => 43.2}} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Msg{l: %{"bar" => 1.0, "foo" => 43.2}}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<74, 14, 10, 3, 98, 97, 114, 17, 0, 0, 0, 0, 0, 0, 240, 63, 74, 14, 10, 3, 102, 111,
111, 17, 154, 153, 153, 153, 153, 153, 69, 64>>
end
Expand All @@ -311,7 +326,9 @@ defmodule Protox.EncodeTest do
end

test "Msg.oneof_double" do
assert %Msg{oneof_field: {:oneof_double, 0}} |> Protox.Encode.encode() |> :binary.list_to_bin() ==
assert %Msg{oneof_field: {:oneof_double, 0}}
|> Protox.Encode.encode()
|> :binary.list_to_bin() ==
<<177, 7, 0, 0, 0, 0, 0, 0, 0, 0>>
end

Expand Down
3 changes: 2 additions & 1 deletion test/protox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ defmodule ProtoxTest do
9 => {:bytes, {:default, ""}, :bytes},
10 => {:map_int64_nested_enum, :map, {:int64, {:enum, Abc.Def.Proto3.NestedEnum}}},
134 => {:oneof_2_int32, {:oneof, :oneof_2}, :int32},
135 => {:oneof_2_nested_enum, {:oneof, :oneof_2}, {:enum, Abc.Def.Proto3.NestedEnum}},
135 =>
{:oneof_2_nested_enum, {:oneof, :oneof_2}, {:enum, Abc.Def.Proto3.NestedEnum}},
9999 => {:nested_enum, {:default, :FOO}, {:enum, Abc.Def.Proto3.NestedEnum}}
}
end
Expand Down

0 comments on commit d930d9e

Please sign in to comment.