Skip to content

Commit

Permalink
redact nested lists inside structs
Browse files Browse the repository at this point in the history
  • Loading branch information
hogiyogi597 committed Apr 19, 2024
1 parent 2c0c3c3 commit 6678d54
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/let_me.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ defmodule LetMe do
do_redact(object, subject, redact_value, opts)
end

defp do_redact(objects, subject, redact_value, opts) when is_list(objects) do
Enum.map(objects, &do_redact(&1, subject, redact_value, opts))
end

defp do_redact(%module{} = object, subject, redact_value, opts) do
redacted_fields = module.redacted_fields(object, subject, opts)
replace_keys(redacted_fields, subject, redact_value, object, opts)
Expand All @@ -231,6 +235,15 @@ defmodule LetMe do
%{__struct__: Ecto.Association.NotLoaded} ->
replace_keys(rest, subject, value, acc, opts)

list when is_list(list) ->
replace_keys(
rest,
subject,
value,
Map.put(acc, key, do_redact(list, subject, value, opts)),
opts
)

%{} = nested_map ->
replace_keys(
rest,
Expand Down Expand Up @@ -261,6 +274,15 @@ defmodule LetMe do
%{__struct__: Ecto.Association.NotLoaded} ->
replace_keys(rest, subject, value, acc, opts)

list when is_list(list) ->
replace_keys(
rest,
subject,
value,
Map.put(acc, key, do_redact(list, subject, value, opts)),
opts
)

%^module{} = nested_map ->
replace_keys(
rest,
Expand Down
55 changes: 52 additions & 3 deletions test/let_me_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,30 @@ defmodule LetMeTest do
defmodule Person do
use LetMe.Schema

defstruct [:name, :email, :phone_number, :pet, :spouse, :age, :locale]
defstruct [
:name,
:email,
:phone_number,
:all_pets,
:pet,
:spouse,
:age,
:locale
]

@impl LetMe.Schema
def redacted_fields(_, :nested_fields, _) do
[:name, :phone_number, spouse: [:email, pet: [:weight]]]
[:name, :phone_number, :all_pets, spouse: [:email, pet: [:weight]]]
end

def redacted_fields(_, :nested_schemas, _) do
[:name, :phone_number, spouse: __MODULE__, pet: LetMeTest.Pet]
[
:name,
:phone_number,
spouse: __MODULE__,
pet: LetMeTest.Pet,
all_pets: LetMeTest.Pet
]
end
end

Expand Down Expand Up @@ -92,6 +107,7 @@ defmodule LetMeTest do
phone_number: :redacted,
age: 25,
locale: "es",
all_pets: :redacted,
pet: %Pet{
name: "Betty",
email: "betty@pet",
Expand Down Expand Up @@ -126,6 +142,22 @@ defmodule LetMeTest do
phone_number: :redacted,
age: 25,
locale: "es",
all_pets: [
%Pet{
name: "Jad",
email: :redacted,
phone_number: "597",
weight: 6,
age: :redacted
},
%Pet{
name: "Betty",
email: :redacted,
phone_number: "987",
weight: 8,
age: :redacted
}
],
pet: %Pet{
name: "Betty",
email: :redacted,
Expand Down Expand Up @@ -174,6 +206,7 @@ defmodule LetMeTest do
phone_number: :redacted,
age: 25,
locale: "es",
all_pets: :redacted,
pet: %Pet{
name: "Betty",
email: "betty@pet",
Expand Down Expand Up @@ -247,6 +280,22 @@ defmodule LetMeTest do
phone_number: "123",
age: 25,
locale: "es",
all_pets: [
%Pet{
name: "Jad",
email: "jad@pet",
phone_number: "597",
weight: 6,
age: 4
},
%Pet{
name: "Betty",
email: "betty@pet",
phone_number: "987",
weight: 8,
age: 7
}
],
pet: %Pet{
name: "Betty",
email: "betty@pet",
Expand Down

0 comments on commit 6678d54

Please sign in to comment.