Skip to content

Commit

Permalink
Keep native DateTime, Date and Time struct encoding (#125)
Browse files Browse the repository at this point in the history
* Keep native DateTime, Date and Time struct encoding

* Add NaiveDateTime

* Add Decimal

* Add tests
  • Loading branch information
imricardoramos committed Jul 30, 2024
1 parent 222e612 commit cad5979
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/logger_json/formatter/redactor_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ defmodule LoggerJSON.Formatter.RedactorEncoder do
def encode("[REDACTED]", _redactors), do: "[REDACTED]"
def encode(binary, _redactors) when is_binary(binary), do: encode_binary(binary)
def encode(%Jason.Fragment{} = fragment, _redactors), do: fragment
def encode(%NaiveDateTime{} = naive_datetime, _redactors), do: naive_datetime
def encode(%DateTime{} = datetime, _redactors), do: datetime
def encode(%Date{} = date, _redactors), do: date
def encode(%Time{} = time, _redactors), do: time
def encode(%Decimal{} = decimal, _redactors), do: decimal

def encode(%_struct{} = struct, redactors) do
struct
Expand Down
11 changes: 11 additions & 0 deletions test/logger_json/formatter/redactor_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ defmodule LoggerJSON.Formatter.RedactorEncoderTest do
assert encode(123, @redactors) == 123
end

test "allows dates and times" do
assert encode(~U[2024-01-01 00:00:00Z], @redactors) == ~U[2024-01-01 00:00:00Z]
assert encode(~N[2024-01-01 00:00:00], @redactors) == ~N[2024-01-01 00:00:00]
assert encode(~D[2024-01-01], @redactors) == ~D[2024-01-01]
assert encode(~T[00:00:00], @redactors) == ~T[00:00:00]
end

test "allows decimals" do
assert encode(Decimal.new("1.2"), @redactors) == Decimal.new("1.2")
end

test "strips Structs" do
assert encode(%IDStruct{id: "hello"}, @redactors) == %{id: "hello"}
end
Expand Down

0 comments on commit cad5979

Please sign in to comment.