Get value from a list inside a map

I have this map stored in a customer variable.

   customer = %Hai.Customer{
  __meta__: #Ecto.Schema.Metadata<:loaded, "customers">,
   billing_contact: #Ecto.Association.NotLoaded<association :billing_contact is not loaded>,
   billing_contact_id: nil,
   business_contact: #Ecto.Association.NotLoaded<association :business_contact is not loaded>,
   business_contact_id: nil,
   disabled_message: "",
   end_date: nil,
users: [
%Hai.User{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
  customer: #Ecto.Association.NotLoaded<association :customer is not loaded>,
  customer_id: 690,
  disable_login: false,
  email_address: "test@example.com",
  first_name: "Jane"    
  }
 ]
}

I want to get the values inside the users list like email or first_name. something like this

   customer.users[:email_address]

Thanks

Of which?

There are between zero and infinite users in the list.

users:[] list. after end_date: nil

Yes, I have seen that.

:users is a list, so there might be zero users, one user, two users, a googol users, and of which of those do you want to have additional information?

only one user

And which?

The first, the second, the 10th?

What if that user doesn’t exist?

In general you can access a single element in the list, as you would with any other list. Culprits included.

currently I am doing this:

    user_struct = hd(customer.users)
    user_map.email_address

I think may be there is some better method to access it directly from map.

That method will fail when there are no users. But at least it tells meyou want the first…

And no, I do not think there is a more direct way.

But if you really want only one element of the list, are you sure you actually need a list?

Actually this is the result returning from a query. And I use it in test cases so there will be always only one user on zero index of the list.

If you are sure there will always be exactly one user in the list you can do:

[%{email_address: email_address}] = customer.users

This does a pattern match on a list with exactly one element. Know that if users is empty or has more elements this will fail!

The name of the key (users) sounds like there might be 0, 1 or more users, so I wouldn’t write my code like I did.

Thanks for your suggesstion
yes in dev envthere might be more than one users. but In test env I only inserting one user so it always be one.