Skip to content

Customize diffs for structs #29

@Valian

Description

@Valian

Is your feature request related to a problem? Please describe.

I want to make it possible to customize diff for structs. Right now always all fields are traversed when working with structs, when in reality often this might use some customization (eg to hide some fields). It's a similar problem as when serializing objects to JSON.

Describe the solution you'd like

I'd like an additional, optional callback used to convert struct to map. Something along the lines:

Jsonpatch.diff(source, destination, prepare_struct: fn 
  # hide some fields, eg do not expose emails
  %User{name: name} -> %{name: name},

  # change representation
  %Address{street: street, number: number} -> %{address: "#{street} #{number}"},

  # default fallback
  s -> s
end)

Describe alternatives you've considered

I can do it before running Jsonpatch.diff. Just it's suboptimal when it comes to performance - I'd be deeply converting all structs even if not necessary. I'd prefer to do it in the last possible moment, only when we know something has changed in the given struct.

It's also possible to do it via a protocol, but I don't think it's the right way here.

Additional context

It would be quite useful in my library LiveVue to send diffs of props from server to client.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions