From 6678d54d6f165d01bd24bb7f34ec31cdef0644c1 Mon Sep 17 00:00:00 2001 From: Stephen Hogan Date: Fri, 19 Apr 2024 14:22:32 -0600 Subject: [PATCH] redact nested lists inside structs --- lib/let_me.ex | 22 ++++++++++++++++++ test/let_me_test.exs | 55 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/lib/let_me.ex b/lib/let_me.ex index dc43338..da8e603 100644 --- a/lib/let_me.ex +++ b/lib/let_me.ex @@ -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) @@ -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, @@ -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, diff --git a/test/let_me_test.exs b/test/let_me_test.exs index c9b7940..e8b6259 100644 --- a/test/let_me_test.exs +++ b/test/let_me_test.exs @@ -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 @@ -92,6 +107,7 @@ defmodule LetMeTest do phone_number: :redacted, age: 25, locale: "es", + all_pets: :redacted, pet: %Pet{ name: "Betty", email: "betty@pet", @@ -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, @@ -174,6 +206,7 @@ defmodule LetMeTest do phone_number: :redacted, age: 25, locale: "es", + all_pets: :redacted, pet: %Pet{ name: "Betty", email: "betty@pet", @@ -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",