Skip to content

Commit

Permalink
style: first step of using quokka
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Feb 26, 2025
1 parent 15c6753 commit da87525
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 43 deletions.
6 changes: 5 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[
inputs: ["mix.exs", "{config,lib,test,conformance,benchmark}/**/*.{ex,exs}"]
inputs: ["mix.exs", "{config,lib,test,conformance,benchmark}/**/*.{ex,exs}"],
plugins: [Quokka],
quokka: [
only: [:line_length]
]
]
6 changes: 2 additions & 4 deletions lib/protox/define_decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ defmodule Protox.DefineDecoder do

_ ->
quote do
<<unquote(first_byte)::5, _wire_type::3, unquote(tail),
unquote(vars.bytes)::binary>>
<<unquote(first_byte)::5, _wire_type::3, unquote(tail), unquote(vars.bytes)::binary>>
end
end
end
Expand All @@ -264,8 +263,7 @@ defmodule Protox.DefineDecoder do
quote do
{entry_key, entry_value} = unquote(value)

{unquote(field.name),
Map.put(unquote(vars.msg).unquote(field.name), entry_key, entry_value)}
{unquote(field.name), Map.put(unquote(vars.msg).unquote(field.name), entry_key, entry_value)}
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/protox/errors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ defmodule Protox.InvalidFieldAttribute do
@doc false
def new(attribute, expected, got) do
%__MODULE__{
message:
"Field attribute #{attribute} should be in #{inspect(expected)}, got #{inspect(got)}"
message: "Field attribute #{attribute} should be in #{inspect(expected)}, got #{inspect(got)}"
}
end
end
Expand Down
30 changes: 12 additions & 18 deletions lib/protox/varint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ defmodule Protox.Varint do
do: {<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, v >>> 28>>, 5}

def encode(v) when v < 1 <<< 42,
do:
{<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, 1::1, v >>> 28::7,
v >>> 35>>, 6}
do: {<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, 1::1, v >>> 28::7, v >>> 35>>, 6}

def encode(v) when v < 1 <<< 49,
do:
{<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, 1::1, v >>> 28::7,
1::1, v >>> 35::7, v >>> 42>>, 7}
{<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, 1::1, v >>> 28::7, 1::1, v >>> 35::7,
v >>> 42>>, 7}

def encode(v) when v < 1 <<< 56,
do:
{<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, 1::1, v >>> 28::7,
1::1, v >>> 35::7, 1::1, v >>> 42::7, v >>> 49>>, 8}
{<<1::1, v::7, 1::1, v >>> 7::7, 1::1, v >>> 14::7, 1::1, v >>> 21::7, 1::1, v >>> 28::7, 1::1, v >>> 35::7, 1::1,
v >>> 42::7, v >>> 49>>, 8}

def encode(v) do
{next_bytes, size} = encode(v >>> 7)
Expand All @@ -53,32 +51,28 @@ defmodule Protox.Varint do
def decode(<<1::1, byte3::7, 1::1, byte2::7, 1::1, byte1::7, 0::1, byte0::7, rest::binary>>),
do: {byte3 <<< 0 ||| byte2 <<< 7 ||| byte1 <<< 14 ||| byte0 <<< 21, rest}

def decode(
<<1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1, byte1::7, 0::1, byte0::7,
rest::binary>>
),
do: {byte4 <<< 0 ||| byte3 <<< 7 ||| byte2 <<< 14 ||| byte1 <<< 21 ||| byte0 <<< 28, rest}
def decode(<<1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1, byte1::7, 0::1, byte0::7, rest::binary>>),
do: {byte4 <<< 0 ||| byte3 <<< 7 ||| byte2 <<< 14 ||| byte1 <<< 21 ||| byte0 <<< 28, rest}

def decode(
<<1::1, byte5::7, 1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1, byte1::7, 0::1,
byte0::7, rest::binary>>
<<1::1, byte5::7, 1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1, byte1::7, 0::1, byte0::7, rest::binary>>
),
do:
{byte5 <<< 0 ||| byte4 <<< 7 ||| byte3 <<< 14 ||| byte2 <<< 21 ||| byte1 <<< 28 |||
byte0 <<< 35, rest}

def decode(
<<1::1, byte6::7, 1::1, byte5::7, 1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1,
byte1::7, 0::1, byte0::7, rest::binary>>
<<1::1, byte6::7, 1::1, byte5::7, 1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1, byte1::7, 0::1,
byte0::7, rest::binary>>
),
do:
{byte6 <<< 0 ||| byte5 <<< 7 ||| byte4 <<< 14 ||| byte3 <<< 21 ||| byte2 <<< 28 |||
byte1 <<< 35 |||
byte0 <<< 42, rest}

def decode(
<<1::1, byte7::7, 1::1, byte6::7, 1::1, byte5::7, 1::1, byte4::7, 1::1, byte3::7, 1::1,
byte2::7, 1::1, byte1::7, 0::1, byte0::7, rest::binary>>
<<1::1, byte7::7, 1::1, byte6::7, 1::1, byte5::7, 1::1, byte4::7, 1::1, byte3::7, 1::1, byte2::7, 1::1,
byte1::7, 0::1, byte0::7, rest::binary>>
),
do:
{byte7 <<< 0 ||| byte6 <<< 7 ||| byte5 <<< 14 ||| byte4 <<< 21 ||| byte3 <<< 28 |||
Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ defmodule Protox.Mixfile do
{:excoveralls, "~> 0.13", only: [:test], runtime: false},
{:ex_doc, "~> 0.22", only: [:dev], runtime: false},
{:propcheck, github: "alfert/propcheck", ref: "c564e89d", only: [:test, :dev]},
{:stream_data, "~> 1.0", only: [:dev, :test], runtime: false}
{:stream_data, "~> 1.0", only: [:dev, :test], runtime: false},
{:quokka, "~> 2.0", only: [:dev, :test], runtime: false}
]
|> maybe_add_muzak_pro()
|> maybe_download_protobuf()
Expand All @@ -53,8 +54,7 @@ defmodule Protox.Mixfile do

creds ->
muzak_pro =
{:muzak,
git: "https://#{creds}@github.com/ahamez/muzak.git", tag: "1.1.0", only: [:test]}
{:muzak, git: "https://#{creds}@github.com/ahamez/muzak.git", tag: "1.1.0", only: [:test]}

[muzak_pro | deps]
end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"propcheck": {:git, "https://github.com/alfert/propcheck.git", "c564e89d3873caf9c6bf64a2af4bb3890e24ecf1", [ref: "c564e89d"]},
"proper": {:git, "https://github.com/proper-testing/proper.git", "a5ae5669f01143b0828fc21667d4f5e344aa760b", [ref: "a5ae5669f01143b0828fc21667d4f5e344aa760b"]},
"protobuf": {:git, "https://github.com/protocolbuffers/protobuf.git", "b407e8416e3893036aee5af9a12bd9b6a0e2b2e6", [tag: "v29.3", submodules: true]},
"quokka": {:hex, :quokka, "2.0.0", "cbdd3024c93320818374a8ee53411e50f24456b32b7186e92347a7a3dc8daf33", [:mix], [{:credo, "~> 1.7", [hex: :credo, repo: "hexpm", optional: false]}], "hexpm", "a140bf724daaa6e3f879eef5900d72710bdd83c444892e07106ddf988ed9e43b"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
"stream_data": {:hex, :stream_data, "1.1.2", "05499eaec0443349ff877aaabc6e194e82bda6799b9ce6aaa1aadac15a9fdb4d", [:mix], [], "hexpm", "129558d2c77cbc1eb2f4747acbbea79e181a5da51108457000020a906813a1a9"},
}
14 changes: 6 additions & 8 deletions test/protox/decode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ defmodule Protox.DecodeTest do
{
"Repeated fixed64, not contiguous, should be concatenated",
# first part of repeated fixed64 | XXXX | second part of repeated fixed64
<<178, 2, 8, 1, 0, 0, 0, 0, 0, 0, 0, 8, 150, 1, 178, 2, 8, 254, 255, 255, 255, 255, 255,
255, 255>>,
<<178, 2, 8, 1, 0, 0, 0, 0, 0, 0, 0, 8, 150, 1, 178, 2, 8, 254, 255, 255, 255, 255, 255, 255, 255>>,
%TestAllTypesProto3{optional_int32: 150, repeated_fixed64: [1, 18_446_744_073_709_551_614]}
},
{
Expand Down Expand Up @@ -61,8 +60,8 @@ defmodule Protox.DecodeTest do
},
{
"Repeated int64",
<<130, 2, 24, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 2, 253, 255, 255, 255, 255,
255, 255, 255, 255, 1, 144, 78>>,
<<130, 2, 24, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 2, 253, 255, 255, 255, 255, 255, 255, 255, 255,
1, 144, 78>>,
%TestAllTypesProto3{repeated_int64: [0, -1, 2, -3, 10_000]}
},
{
Expand Down Expand Up @@ -92,8 +91,8 @@ defmodule Protox.DecodeTest do
},
{
"Repeated sfixed64",
<<194, 2, 40, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255,
255, 255, 3, 0, 0, 0, 0, 0, 0, 0, 240, 216, 255, 255, 255, 255, 255, 255>>,
<<194, 2, 40, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255, 3, 0, 0, 0,
0, 0, 0, 0, 240, 216, 255, 255, 255, 255, 255, 255>>,
%TestAllTypesProto3{repeated_sfixed64: [0, 1, -2, 3, -10_000]}
},
{
Expand Down Expand Up @@ -205,8 +204,7 @@ defmodule Protox.DecodeTest do
},
{
"Message without fields and with unknown fields",
<<8, 42, 61, 42, 0, 0, 0, 65, 42, 0, 0, 0, 0, 0, 0, 0, 114, 3, 102, 111, 111, 218, 4, 3, 1,
2, 3>>,
<<8, 42, 61, 42, 0, 0, 0, 65, 42, 0, 0, 0, 0, 0, 0, 0, 114, 3, 102, 111, 111, 218, 4, 3, 1, 2, 3>>,
%NullHypothesisProto3{
__uf__: [
{1, 0, "*"},
Expand Down
11 changes: 4 additions & 7 deletions test/protox/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,15 @@ defmodule Protox.EncodeTest do
assert encode(%TestAllTypesProto3{
repeated_double: [33.2, -44.0, :infinity, :"-infinity", :nan]
}) ==
{<<210, 2, 40, 154, 153, 153, 153, 153, 153, 64, 64, 0, 0, 0, 0, 0, 0, 70, 192, 0, 0,
0, 0, 0, 0, 240, 127, 0, 0, 0, 0, 0, 0, 240, 255, 0, 0, 0, 0, 0, 1, 241, 255>>,
43}
{<<210, 2, 40, 154, 153, 153, 153, 153, 153, 64, 64, 0, 0, 0, 0, 0, 0, 70, 192, 0, 0, 0, 0, 0, 0, 240, 127,
0, 0, 0, 0, 0, 0, 240, 255, 0, 0, 0, 0, 0, 1, 241, 255>>, 43}
end

test "Repeated float" do
assert encode(%TestAllTypesProto3{
repeated_float: [33.2, -44.0, :infinity, :"-infinity", :nan]
}) ==
{<<202, 2, 20, 205, 204, 4, 66, 0, 0, 48, 194, 0, 0, 128, 127, 0, 0, 128, 255, 0, 1,
129, 255>>, 23}
{<<202, 2, 20, 205, 204, 4, 66, 0, 0, 48, 194, 0, 0, 128, 127, 0, 0, 128, 255, 0, 1, 129, 255>>, 23}
end

test "Repeated bool" do
Expand All @@ -94,8 +92,7 @@ defmodule Protox.EncodeTest do

test "Unpacked Repeated int32" do
assert encode(%TestAllTypesProto3{unpacked_int32: [-1, 2, 3]}) ==
{<<200, 5, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 200, 5, 2, 200, 5, 3>>,
18}
{<<200, 5, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 200, 5, 2, 200, 5, 3>>, 18}
end

test "Bytes" do
Expand Down

0 comments on commit da87525

Please sign in to comment.