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}
hubertlepicki

hubertlepicki

can you give me some example of map comparison that is failing? It should not be failing even if the order of fields looks different for some reason.

stefan_z

stefan_z

Absolutely nothing :+1: == operator is the best way to do it. For some reason my brain went for a more complex approach :man_shrugging:

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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 Postg...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement