Durable.Helpers (Durable v2.0.2)

View Source

Helper functions for working with workflow data in the pipeline model.

These functions provide a convenient, pipe-friendly API for updating the data that flows through workflow steps.

Usage

use Durable.Helpers

This imports all helper functions into your workflow module.

Keys

A workflow's data has string keys at every depth, as it is stored. Each helper takes a key as an atom or a string and reads or writes the string: assign(data, :status, "paid") sets data["status"].

Best practice

Read and write a workflow's data through these helpers. They let a step name its keys with atoms, as Elixir code usually does, over data that is stored with strings:

step :charge, fn order ->
  total = get(order, :total)                  # order["total"]
  city = get(order, [:customer, :city])       # order["customer"]["city"]
  {:ok, assign(order, :charged, total)}       # sets order["charged"]
end

order.total, order[:total] and %{total: total} = order don't read the data: its keys are strings. Map.put(order, :total, 1) in the middle of a step leaves :total beside the stored "total" until the step returns, so a later get(order, :total) in that step still reads the old one; assign/3 writes the string and has no such gap.

Examples

step :process, fn data ->
  {:ok, data
  |> assign(:status, :processing)
  |> assign(%{started_at: DateTime.utc_now()})
  |> increment(:step_count)}
end

Summary

Functions

Injects helper functions into the calling module.

Appends a value to a list in the data.

Merges a map into the data.

Assigns a value to a key in the data map.

Deletes a key from the data.

Gets a value from the data.

Gets a value from the data with a default.

Checks if a key exists in the data.

Increments a numeric value in the data.

Updates an existing value in the data using a function.

Updates a value in the data using a function.

Functions

__using__(opts)

(macro)

Injects helper functions into the calling module.

append(data, key, value)

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

Appends a value to a list in the data.

If the key doesn't exist, creates a new list with the value.

Examples

append(data, :items, new_item)
append(data, :events, %{type: :clicked, time: DateTime.utc_now()})

assign(data, map)

@spec assign(map(), map()) :: map()

Merges a map into the data.

Examples

assign(data, %{order_id: 123, status: :pending})

assign(data, key, value)

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

Assigns a value to a key in the data map.

Examples

assign(data, :order_id, 123)
assign(data, "order_id", 123)

delete(data, key)

@spec delete(map(), atom() | String.t()) :: map()

Deletes a key from the data.

Examples

delete(data, :temporary_value)

get(data, key)

@spec get(map(), atom() | String.t() | [atom() | String.t()]) :: term()

Gets a value from the data.

Returns nil if the key doesn't exist.

Examples

order_id = get(data, :order_id)

A list is a path into the maps inside the data:

total = get(data, [:invoice, :total])

get(data, path, default)

@spec get(map(), atom() | String.t() | [atom() | String.t()], term()) :: term()

Gets a value from the data with a default.

Examples

count = get(data, :retry_count, 0)

has_key?(data, key)

@spec has_key?(map(), atom() | String.t()) :: boolean()

Checks if a key exists in the data.

Examples

if has_key?(data, :order_id) do
  # ...
end

increment(data, key, amount \\ 1)

@spec increment(map(), atom() | String.t(), number()) :: map()

Increments a numeric value in the data.

If the key doesn't exist, starts from 0.

Examples

increment(data, :retry_count)
increment(data, :total, 100)

update(data, key, fun)

@spec update(map(), atom() | String.t(), (term() -> term())) :: map()

Updates an existing value in the data using a function.

Raises if the key doesn't exist.

Examples

update(data, :count, &(&1 + 1))

update(data, key, default, fun)

@spec update(map(), atom() | String.t(), term(), (term() -> term())) :: map()

Updates a value in the data using a function.

If the key doesn't exist, uses the default value.

Examples

update(data, :count, 0, &(&1 + 1))
update(data, :items, [], &[new_item | &1])