Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR Feedback implementation #24

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Internal coding style changes.

## [1.8.0] - 2024-10-25

### Added
Expand Down
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.
Ivor marked this conversation as resolved.
Show resolved Hide resolved
"""

@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()]
Ivor marked this conversation as resolved.
Show resolved Hide resolved
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
Ivor marked this conversation as resolved.
Show resolved Hide resolved
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),
Ivor marked this conversation as resolved.
Show resolved Hide resolved
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),
Ivor marked this conversation as resolved.
Show resolved Hide resolved
do: IO.iodata_to_binary(Enum.reverse(current_slice))
Ivor marked this conversation as resolved.
Show resolved Hide resolved
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 """
Ivor marked this conversation as resolved.
Show resolved Hide resolved
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,
Ivor marked this conversation as resolved.
Show resolved Hide resolved
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
Loading