View Source Plumbery.Request (plumbery v0.1.0)

Requests hold shared state and execution result. As the request moves through the pipeline, it can be modified and later used in next steps down the pipeline.

Requests are expected to be structs with at least the following fields:

  • command is a map or struct that contains the arguments for pipeline
  • context contains information about calling context and can be used, for example, for authorization
  • assigns a map with shared state
  • result contains pipe execution result
  • halted? true if pipeline is halted

Defining a request struct

Plumbery.Request is a struct that can in many cases be used as a request for pipelines. In cases when you need additional fields in your requests, you can use Plumbery.Request to define the struct.

To define a request struct, create a module and use Plumbery.Request, passing a keyword list with additional fields and their default values.

defmodule MyApp.Request do
  @enforce_keys [:field1]
  use Plumbery.Request, field1: nil, field2: false
end

Summary

Functions

Adds an error and sets result to {:error, errors} where errors is a list.

Assigns a value to a key in the request's shared state map.

Sets result to {:error, error}.

Halts the pipeline. No further steps will be executed.

Sets result to res. Further steps in the pipeline will be executed and can further modify the result.

Sets result to {:ok, result}. Further steps in the pipeline will be executed and can further modify the result.

Returns request's result field.

Types

@type result() :: nil | {:error, any()} | {:ok, any()}
@type t() :: %{
  :command => any(),
  :context => any(),
  :assigns => %{},
  :result => result(),
  :halted? => boolean(),
  optional(any()) => any()
}

Functions

Link to this function

add_error(request, key \\ nil, error)

View Source
@spec add_error(t(), atom(), term()) :: t()

Adds an error and sets result to {:error, errors} where errors is a list.

If key is nil, error will be added to the list, otherwise {key, error} tuple will be added.

Link to this function

assign(request, key, value)

View Source
@spec assign(t(), atom(), term()) :: t()

Assigns a value to a key in the request's shared state map.

@spec error(t(), term()) :: t()

Sets result to {:error, error}.

@spec halt(t()) :: t()

Halts the pipeline. No further steps will be executed.

Link to this function

result(res, req, strict \\ true)

View Source
@spec result(any(), t(), boolean()) :: t()

Sets result to res. Further steps in the pipeline will be executed and can further modify the result.

When strict is true (the default), only {:error, _} and {:ok, _} tuples are accepted, otherwise any value can be passed.

result, not request, is the first argument intentionally. It allows for this type of calls:

some_function(req.command)
|> result(req)
Link to this function

success(request, result)

View Source
@spec success(t(), term()) :: t()

Sets result to {:ok, result}. Further steps in the pipeline will be executed and can further modify the result.

@spec unwrap(t()) :: result()

Returns request's result field.