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

redact nested lists inside structs #77

Merged
merged 1 commit into from
Apr 19, 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
29 changes: 29 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,22 @@ 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,
Enum.map(
list,
&replace_keys(nested_redacted_fields, subject, value, &1, opts)
)
),
opts
)

%{} = nested_map ->
replace_keys(
rest,
Expand Down Expand Up @@ -261,6 +281,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
79 changes: 75 additions & 4 deletions test/let_me_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,35 @@ 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: [:age, :email],
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 +112,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: "betty@pet",
Expand Down Expand Up @@ -126,6 +162,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 @@ -157,6 +209,7 @@ defmodule LetMeTest do
phone_number: "123",
age: 25,
locale: "es",
all_pets: [],
pet: %Pet{
name: "Betty",
email: "betty@pet",
Expand All @@ -174,6 +227,7 @@ defmodule LetMeTest do
phone_number: :redacted,
age: 25,
locale: "es",
all_pets: [],
pet: %Pet{
name: "Betty",
email: "betty@pet",
Expand All @@ -191,6 +245,7 @@ defmodule LetMeTest do
phone_number: :redacted,
age: 25,
locale: "es",
all_pets: [],
pet: %Pet{
name: "Betty",
email: :redacted,
Expand All @@ -207,7 +262,7 @@ defmodule LetMeTest do
end

test "handles nil value in nested field" do
person = %{person() | spouse: nil}
person = %{person() | spouse: nil, all_pets: nil}
assert %Person{spouse: nil} = LetMe.redact(person, :nested_fields)
assert %Person{spouse: nil} = LetMe.redact(person, :nested_schemas)
end
Expand Down Expand Up @@ -247,6 +302,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
Loading