Skip to content

Commit

Permalink
metadata_matches supports array
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Zhou authored and Edward Zhou committed Oct 17, 2021
1 parent af8ac21 commit 435f498
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/logger_file_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ defmodule LoggerFileBackend do
# all of the filter keys are present
def metadata_matches?(_md, []), do: true

def metadata_matches?(md, [{key, [_ | _] = val} | rest]) do
case Keyword.fetch(md, key) do
{:ok, md_val} ->
(md_val in val) && metadata_matches?(md, rest)

# fail on first mismatch
_ ->
false
end
end

def metadata_matches?(md, [{key, val} | rest]) do
case Keyword.fetch(md, key) do
{:ok, ^val} ->
Expand Down
2 changes: 2 additions & 0 deletions test/logger_file_backend_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule LoggerFileBackendTest do
test "metadata_matches?" do
# exact match
assert metadata_matches?([a: 1], a: 1) == true
# included in array match
assert metadata_matches?([a: 1], a: [1, 2]) == true
# total mismatch
assert metadata_matches?([b: 1], a: 1) == false
# default to allow
Expand Down

0 comments on commit 435f498

Please sign in to comment.