Skip to content

Feat: create generic strip_struct_metadata to remove surplus fields from Struct #54

@nelsonic

Description

@nelsonic

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

Metadata

Metadata

Assignees

Labels

T25mTime Estimate 25 Minuteschorea tedious but necessary task often paying technical debtdiscussShare your constructive thoughts on how to make progress with this issueenhancementNew feature or requesthelp wantedExtra attention is neededpriority-2Second highest priority, should be worked on as soon as the Priority-1 issues are finishedtechnicalA technical issue that requires understanding of the code, infrastructure or dependencies

Type

No type

Projects

Status

No status

Status

More ToDo ThanCanEver Be Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions