How can i map the values of enums where enums has {key,value} and need to use the values?

Hello Folks :waving_hand:,

I declare the enums like this,

defmodule Enums.Country do 
     use Ash.Type.Enum,
        values: [
            af: "Afghanistan",
            al: "Albania",
            dz: "Algeria",
                    ]

and i am using this in my live-view like this,

 countries =
   Enums.Country.values()

but it gave me the output of this → [af, al, dz] and i need both the values and its keys to be used in the form while the field value is af, al, dz while submit the form but to be displayed the value “Afghanistan”, “Albania”, “Algeria”.

i read the Ash.Type.Enums documentation. Please help…

You added description.
Which means you could get what you want with description call:
Enum.map(Enums.Country.values(), &Enums.Country.description(&1))

or, since you need both values you could do:

Enum.map(Enums.Country.values(), fn value ->
   {value, Enums.Country.description(value)}
end)

:thinking: @zachdaniel I’m looking at the code now and values returns:
def values, do: @values where @values Map.keys(@values_map)
Should there be a values_map/0 call or opts for values/0 to get the whole map, would that make sense?

I think values/0 should stay as it is, but values_map wouldn’t necessarily hurt to expose. Your original solution of building the list with Enum.map is generally the recommended way. You could put that tin your enum module and call it values_for_select for example.

2 Likes