View Source Unleash.Macros (Unleash v3.0.0)
This module offers a macro API over the Unleash
behaviour,
and should be used as the main entrypoint over the Unleash SDK.
Doing so has a few advantages:
Delegates the actual check to a configurable runtime implementation. This allows, for example, to use a mock implementation in tests, without having to change anything at the call site.
Requires the options to be specified at compile-time, making feature checks more clear and explicit (see chapter below for details).
Checks which options have been passed, and generates a compilation warning in case of unknown ones:
require Unleash.Macros, as: Unleash # generates a compilation warning: unknown option `:falback`. Unleash.enabled?(:flag, falback: true)
More compile-time checks might be added in the future
Options as named arguments
Whenever you call enabled?/2
or get_variant/2
,
option keys must be provided statically at compile-time.
Basically, you should treat the options as named function arguments.
This requirement does not extend to the feature name or option values, which can be dynamic runtime terms -- however, for readability and clarity of feature flags checks, it is recommended to also provide those statically whenever possible, particularly for feature names.
For instance, rather than writing (which would raise a compile time error if using the enabled?/2
macro):
iex> opts = [allow_overrides: true, fallback: true]
...> {name, opts} = case some_condition do
...> true -> {:flag1, Keyword.put(opts, :context, %{user_id: "u-42"})}
...> false -> {:flag2, opts}
...> end
...> Unleash.enabled?(name, opts)
You should write:
iex> case some_condition do
...> true -> Unleash.enabled?(:flag1, allow_overrides: true, fallback: true, context: %{user_id: "u-42"})
...> false -> Unleash.enabled?(:flag2, allow_overrides: true, fallback: true)
...> end
In the second example, it is much clearer to see which feature flags are being checked and the parameters being used.
Summary
Functions
Checks if the given feature flag is enabled.
Returns the variant for the given feature flag.
Functions
Checks if the given feature flag is enabled.
This is a macro that does some compile-time checks
and delegates to the configured Runtime's Unleash.enabled?/2
at runtime.
See Unleash.Runtime.enabled?/2
for the full documentation.
Examples
iex> require Unleash.Macros, as: Unleash
...> Unleash.enabled?(:flag_a)
...> Unleash.enabled?(:flag_b, context: get_context(), fallback: true, allow_overrides: false)
Returns the variant for the given feature flag.
This is a macro that does some compile-time checks
and delegates to the configured Runtime's Unleash.get_variant/2
at runtime.
See Unleash.Runtime.get_variant/2
for the full documentation.
Examples
iex> require Unleash.Macros, as: Unleash
...> Unleash.get_variant(:feature_a)
...> Unleash.get_variant(:test, fallback: %{enabled: true, name: "variant_two"})