Skip to content

Commit

Permalink
PR Feedback implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivor committed Oct 25, 2024
1 parent 0427826 commit 27da344
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/meta_logger/slicer.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule MetaLogger.Slicer do
@moduledoc """
A bebaviour for slicing long entries into a list of entries shorter than a passed `max_entry_length` value.
A behaviour for slicing long entries into a list of entries shorter than a passed `max_entry_length` value.
"""

@typedoc "Max length in bytes or `:infinity` if the entry should not be sliced."
Expand Down
3 changes: 1 addition & 2 deletions lib/meta_logger/slicer/default_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ defmodule MetaLogger.Slicer.DefaultImpl do
"""
@impl MetaLogger.Slicer
@spec slice(binary(), MetaLogger.Slicer.max_entry_length()) :: [binary()]
def slice(entry, max_entry_length)
when max_entry_length == :infinity
when byte_size(entry) <= max_entry_length,
do: [entry]

def slice(entry, max_entry_length) do
def slice(entry, max_entry_length) when is_binary(entry) do
entry_length = byte_size(entry)
rem = rem(entry_length, max_entry_length)
sliced_entries = for <<slice::binary-size(max_entry_length) <- entry>>, do: slice
Expand Down
15 changes: 6 additions & 9 deletions lib/meta_logger/slicer/utf8_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ defmodule MetaLogger.Slicer.Utf8Impl do
when byte_size(entry) <= max_entry_length,
do: [entry]

def slice(entry, max_entry_length) do
do_slice(entry, max_entry_length, [], [], 0)
end
def slice(entry, max_entry_length) when is_binary(entry),
do: do_slice(entry, max_entry_length, [], [], 0)

@spec do_slice(binary(), integer(), [binary()], [iodata()], integer()) :: [String.t()]
defp do_slice(<<>>, _max_length, slices, partial_slice, _partial_size) do
Expand Down Expand Up @@ -78,12 +77,10 @@ defmodule MetaLogger.Slicer.Utf8Impl do
# Converts the inverted list of codepoints into a
# binary slice and appends it to our list of slices.
@spec bank_partial_slice([binary()], [iodata()]) :: [binary()]
defp bank_partial_slice(slices, partial_slice) do
[reconstruct_current_slice_as_binary(partial_slice) | slices]
end
defp bank_partial_slice(slices, partial_slice),
do: [reconstruct_current_slice_as_binary(partial_slice) | slices]

@spec reconstruct_current_slice_as_binary([iodata()]) :: binary()
defp reconstruct_current_slice_as_binary(current_slice) do
IO.iodata_to_binary(Enum.reverse(current_slice))
end
defp reconstruct_current_slice_as_binary(current_slice),
do: IO.iodata_to_binary(Enum.reverse(current_slice))
end
12 changes: 8 additions & 4 deletions test/meta_logger/slicer/default_impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ defmodule MetaLogger.Slicer.DefaultImplTest do
assert Subject.slice(entry, :infinity) == [entry]
end

test "when max entry length is smaller than the size of given entry, " <>
"returns a list with one entry",
test """
when max entry length is smaller than the size of given entry, \
returns a list with one entry
""",
%{
entry: entry
} do
Expand All @@ -33,8 +35,10 @@ defmodule MetaLogger.Slicer.DefaultImplTest do
assert Subject.slice(entry, 5) == ["01234", "56789"]
end

test "when given max entry length is three and the given entry size is 10, " <>
"returns a list with four entries",
test """
when given max entry length is three and the given entry size is 10, \
returns a list with four entries
""",
%{
entry: entry
} do
Expand Down
12 changes: 8 additions & 4 deletions test/meta_logger/slicer/utf8_impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ defmodule MetaLogger.Slicer.Utf8ImplTest do
assert Subject.slice(entry, :infinity) == [entry]
end

test "when max entry length is smaller than the size of given entry, " <>
"returns a list with one entry",
test """
when max entry length is smaller than the size of given entry, \
returns a list with one entry
""",
%{
entry: entry
} do
Expand All @@ -33,8 +35,10 @@ defmodule MetaLogger.Slicer.Utf8ImplTest do
assert Subject.slice(entry, 5) == ["01234", "56789"]
end

test "when given max entry length is three and the given entry size is 10, " <>
"returns a list with four entries",
test """
when given max entry length is three and the given entry size is 10,
returns a list with four entries
""",
%{
entry: entry
} do
Expand Down
6 changes: 4 additions & 2 deletions test/tesla/middleware/meta_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ defmodule Tesla.Middleware.MetaLoggerTest do
assert logs =~ ~s([debug] [#{inspect(Subject)}] {"email":"[email protected]","response":"value"})
end

test "when the max entry length is given, " <>
"logs the request and the response splitting the body" do
test """
when the max entry length is given, \
logs the request and the response splitting the body
""" do
request_body_slice1 = String.duplicate("x", 100)
request_body_slice2 = String.duplicate("y", 100)

Expand Down

0 comments on commit 27da344

Please sign in to comment.