Unleash.Context (Unleash v5.1.2)

View Source

The Unleash Context, mostly used to evaluate conditional activation strategies. https://docs.getunleash.io/reference/unleash-context.

The Context has a predefined set of fields, but custom context properties can be set in the :properties field. Note that, for hystorical / backwards-compatibility reasons, both atom and string keys are allowed in the properties map. You are encouraged to use find/2 to lookup context values in a way that handles fallback to properties and the different style of keys correctly.

Summary

Types

t()

A looked-up context value: a standard field, a custom property, or nil.

Functions

Converts a camelCase string context key to its snake_case atom form (may create atoms).

Looks for a context value, taking care of some of the nasty details like fallback to :properties (a.k.a. custom context values) with support for both atom and string keys.

Converts a snake_case atom context key to its camelCase string form.

Types

t()

@type t() :: %{
  optional(:app_name) => String.t(),
  optional(:environment) => String.t(),
  optional(:user_id) => String.t(),
  optional(:session_id) => String.t(),
  optional(:remote_address) => String.t(),
  optional(:properties) => %{required(atom() | String.t()) => String.t()},
  optional(:current_time) => String.t()
}

value()

@type value() :: term()

A looked-up context value: a standard field, a custom property, or nil.

Functions

atomize_context_key(string_key)

@spec atomize_context_key(String.t()) :: atom()

Converts a camelCase string context key to its snake_case atom form (may create atoms).

find(context, key)

@spec find(t(), atom() | String.t()) :: value()

Looks for a context value, taking care of some of the nasty details like fallback to :properties (a.k.a. custom context values) with support for both atom and string keys.

Specifically:

  1. If the key is a standard context key (either in its atom or string form), it looks for it in the standard context fields, without fallbacking to properties.
  2. Otherwise, it looks for it in properties, trying both the atomized and the stringified version of the key, starting from the supplied one.

NOTE that this can create atom at runtimes, which is dangerous. It is highly recommended you do not call this function with values from untrusted / unbounded sources.

stringify_context_key(atom_key)

@spec stringify_context_key(atom()) :: String.t()

Converts a snake_case atom context key to its camelCase string form.