Malla.Util (malla v0.1.0)

Copy Markdown View Source

Helpers for applications built with Malla.

The other functions of this module are used by Malla itself and are not part of its public API.

Summary

Functions

Deep merge of two keyword lists: when both values of a key are keyword lists they are merged, any other value of update (maps and plain lists included) replaces the one in base.

Deep merge of two maps: nested maps are merged, any other value of update replaces the one in base.

Hides the values of secret-looking keys (password, secret, token, credential, api_key, hmac_key, enc_key, …) anywhere inside maps, keyword lists, lists and tuples, and the password of any URL (ecto://user:pass@host), so the term can be logged. Structs are kept as they are.

The arguments of a call fun, ready to be logged: all of them hidden when the name of the function says it handles a secret (set_password, check_password, login, verify_token, …), since the secret is then a positional argument with no key to recognise it by; redact/1 otherwise.

Whether a key or function name looks like it holds a secret.

Validates the environment of application app against a NimbleOptions schema and writes the validated values back (defaults included), so the rest of the application reads them with Application.get_env/3.

Functions

keyword_merge(base, update)

@spec keyword_merge(keyword(), keyword()) :: keyword()

Deep merge of two keyword lists: when both values of a key are keyword lists they are merged, any other value of update (maps and plain lists included) replaces the one in base.

map_merge(base, update)

@spec map_merge(map(), map()) :: map()

Deep merge of two maps: nested maps are merged, any other value of update replaces the one in base.

redact(term)

@spec redact(term()) :: term()

Hides the values of secret-looking keys (password, secret, token, credential, api_key, hmac_key, enc_key, …) anywhere inside maps, keyword lists, lists and tuples, and the password of any URL (ecto://user:pass@host), so the term can be logged. Structs are kept as they are.

redact_args(fun, args)

@spec redact_args(atom() | String.t(), term()) :: term()

The arguments of a call fun, ready to be logged: all of them hidden when the name of the function says it handles a secret (set_password, check_password, login, verify_token, …), since the secret is then a positional argument with no key to recognise it by; redact/1 otherwise.

secret_name?(name)

@spec secret_name?(term()) :: boolean()

Whether a key or function name looks like it holds a secret.

validate_app!(schema, app)

@spec validate_app!(NimbleOptions.schema() | NimbleOptions.t(), atom()) :: :ok

Validates the environment of application app against a NimbleOptions schema and writes the validated values back (defaults included), so the rest of the application reads them with Application.get_env/3.

Raises if the configuration is not valid. Meant for Application.start/2. Available only when the application depends on :nimble_options, an optional dependency of Malla.