thrash v0.3.1 Thrash.Enumerated

Builds an enumerated value module from thrift-generated erlang code.

Suppose you have thrift file with enumerated values:

// in thrift file
enum StatusCodes {
  OK = 0
  ERROR = 1
}

Bring these enumerated values into your Elixir app by doing

defmodule MyApp.StatusCodes do
  use Thrash.Enumerated
end

As long as the name of your module ends with ‘StatusCodes’, Thrash should find the enumerated values automatically. You can manually override the name of the source enum by passing it to the use call:

defmodule MyApp.Codes do
  use Thrash.Enumerated, source: StatusCodes
end

The generated code defines a mapping from the named values, as atoms, to their corresponding integers:

StatusCode.id(:ok) = 0
StatusCode.atom(0) = :ok

Using this module defines the following functions:

  • map/0 - Returns the map from atom to value
  • reverse_map/0 - Returns the map from value to atom
  • atoms/0 - Returns the list of valid atom values
  • values/0 - Returns the list of valid integer values
  • id/1 - Returns the integer value for a given atom
  • atom/1 - Returns the atom value for a given integer

The following types are defined:

  • atom_t/0 - Union of all valid atom values
  • value_t/0 - Union of all valid integer values

Summary

Functions

require_supported?()