https://github.com/niku/struct_diff
Calculates the difference between two structs
https://github.com/niku/struct_diff
Last synced: 10 months ago
JSON representation
Calculates the difference between two structs
- Host: GitHub
- URL: https://github.com/niku/struct_diff
- Owner: niku
- License: mit
- Created: 2019-11-07T11:00:32.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T11:04:14.000Z (over 6 years ago)
- Last Synced: 2025-01-12T16:41:28.119Z (over 1 year ago)
- Language: Elixir
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StructDiff
Calculates the difference between two structs.
If you would like to calculate map difference and/or nested struct difference, you might use [Qqwy/elixir-map_diff](https://github.com/Qqwy/elixir-map_diff).
## Usage
```elixir
defmodule A, do: defstruct [:x, :y, :z]
defmodule B, do: defstruct [:x, :y, :z]
StructDiff.diff(%A{x: 1, y: 2, z: 3}, %A{x: 1, y: 2, z: 3})
# => {:ok, %{}}
StructDiff.diff(%A{x: 100, y: 2, z: 3}, %A{x: 1, y: 2, z: 30})
# => {:ok, %{x: {100, 1}, z: {3, 30}}}
StructDiff.diff(%A{x: 1, y: 2, z: 3}, %B{x: 1, y: 2, z: 3})
# => {:error, {:different_struct_given, A, B}}
StructDiff.diff(%{}, %A{})
# => {:error, {:no_struct_given, :the_first}}
# Limitation, it calculates only top level diffs, doesn't calculate nested structs
StructDiff.diff(%A{x: 1, y: 2, z: %B{x: 3, y: 4, z: 5}}, %A{x: 1, y: 2, z: %B{x: 30, y: 40, z: 50}})
# => {:ok, %{z: {%B{x: 3, y: 4, z: 5}, %B{x: 30, y: 40, z: 50}}}}
```
## Installation
Add `niku/struct_diff` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:struct_diff, github: "niku/struct_diff"}
]
end
```