View Source Craftgate.Enum (Craftgate v1.0.42)
Allows modules to declare enumerations of certain values, with serialization/deserialization support.
To do this, simply use
this module and list the individual values as a list of atoms, e.g.
defmodule MyEnum do
use Craftgate.Enum, [
:FOO,
:BAR
]
end
With this, the MyEnum
module will gain access to the following convenience methods which can be useful
to compare incoming values with.
...
def foo(), do: :FOO
def bar(), do: :BAR
def values(), do: [:FOO, :BAR]
...
The module will also adopt the Construct.TypeC
behavior which will allow it to cast back and forth between
values and their string representations. For more info, see Construct.TypeC
MyEnum.cast("FOO") # returns :FOO
MyEnum.cast(:FOO) # returns :FOO
Please also note that strings or atoms that are not in the actual enumaration list will still be accepted so as not to regressions when a remote data source decides to add more values to the list.
When this occurs, an error message will be logged into the console with the warn
level to let the developers
know that a version bump might be necessary.
MyEnum.cast("BAZ") # returns :BAZ but also logs the following message:
# An unknown enum value was received but accepted for backwards compatibility: BAZ. Known values are: BAR, BAZ
Link to this section Summary
Functions
Callback implementation for Construct.TypeC.castc/2
.
Link to this section Functions
Callback implementation for Construct.TypeC.castc/2
.