Laurent

Laurent

What is a good way to compare structs?

In some tests, we need to compare 2 structs between each other. However when we build

  • the test struct: this is done manually
%{
  data_actions: %{A, B}
}
  • the core struct, we go through a
data_actions |> Map.from_struct() |> Enum.reduce( ...)

Since OTP 26, Maps aren’t ordered anymore (Taking Control of Map Sort Order in Elixir · The Phoenix Files) so the result is random (in the resulting struct, the data_actions have a random order).

Is there any way to compare a struct recursively, without considering the values order? What is the elixir way to do it clean?

Most Liked

christhekeele

christhekeele

The == operator does not care about order when comparing maps. This applies for structs too.

%{key1: :equal, key2: :values} == %{key2: :values, key1: :equal}
#=> true
%{key1: :unequal, key2: :values} == %{key2: :values, key1: :equal}
#=> false

For tests, you can just assert map1 == map2 as you would expect.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Again, what is wrong with a simple map_a == map_b ?

cevado

cevado

just adding to that, if you’re talking about tests… if you pattern match you can just look for the stuff you care for in a particular test run… so that works

assert %{field: :i_care_for} = %MyStruct{field: :i_care_for, other_field: :dont_care_about}

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement