useful icon indicating copy to clipboard operation
useful copied to clipboard

Feat: create generic `strip_struct_metadata` to remove surplus fields from `Struct`

Open nelsonic opened this issue 1 year ago • 0 comments

At present we have a strip_struct_metadata/1 function is hidden in: https://github.com/dwyl/auth_plug/blob/da40aca75d5ecc0fc016023f4a61eb6f4b7cfb6f/lib/auth_plug_helpers.ex#L18C1-L33C6

@doc """
`strip_struct_metadata/1` removes the Ecto Struct metadata from a struct.
This is essential before attempting to create a JWT as `Jason.encode/2`
chokes on any invalid data. See: github.com/dwyl/auth_plug/issues/16
"""
def strip_struct_metadata(struct) do
  struct
  |> Map.delete(:__meta__)
  |> Map.delete(:__struct__)
  # association
  |> Map.delete(:statuses)
  # association
  |> Map.delete(:login_logs)
  # binary
  |> Map.delete(:email_hash)
end

We can easily make this function generic and reusable by accepting a second argument field_list.

Todo

  • [ ] Create the tests (borrow from auth_plug and extend with field_list)
  • [ ] " function definition
  • [ ] " PR
  • [ ] Publish new version of package to hex.pm

nelsonic avatar Aug 26 '23 04:08 nelsonic