BattleStandard (Battle Standard v0.1.0) View Source

Link to this section Summary

Functions

Battle Standard will implement two helpers for dealing with bitfield flag arrays delivered as integers.

Link to this section Functions

Link to this macro

__using__(opts)

View Source (macro)

Battle Standard will implement two helpers for dealing with bitfield flag arrays delivered as integers.

Lets run through an example.

Your receive an integer from an external API and you were expecting an array of boolean values.

Examples

iex> #some_response
user = %{
  first_name: "Stevejones",
  last_name: "Benson,
  flags: 123
}
123

But I expected:

iex> #some_response
user = %{
  first_name: "Stevejones",
  last_name: "Benson,
  flags: %{
  CROSSPOSTED: true,
  EPHEMERAL: true,
  HAS_THREAD: true,
  IS_CROSSPOST: true,
  LOADING: false,
  SOURCE_MESSAGE_DELETED: true,
  SUPPRESS_EMBEDS: false,
  URGENT: true
  }
}

Use this package and set a module attribute describing your field. The example uses the discord message flags from Here

iex> use BattleStandard

@flag_bits [
  {:CROSSPOSTED, 1 <<< 0},
  {:IS_CROSSPOST, 1 <<< 1},
  {:SUPPRESS_EMBEDS, 1 <<< 2},
  {:SOURCE_MESSAGE_DELETED, 1 <<< 3},
  {:URGENT, 1 <<< 4},
  {:HAS_THREAD, 1 <<< 5},
  {:EPHEMERAL, 1 <<< 6},
  {:LOADING, 1 <<< 7}
]

You can use the two callbacks from this this package that have been automatically inserted into your module. Put them into wherever you want, be it a changeset, or some other generic casting operation.

Examples

iex> MessageFlags.from_integer(123)
%{
  CROSSPOSTED: true,
  EPHEMERAL: true,
  HAS_THREAD: true,
  IS_CROSSPOST: true,
  LOADING: false,
  SOURCE_MESSAGE_DELETED: true,
  SUPPRESS_EMBEDS: false,
  URGENT: true
}