Durable.Query.Opts (Durable v2.0.2)

View Source

The contract every read function of Durable keeps, and the one place that checks it.

A read function (Durable.get_execution/2, Durable.list_executions/1, everything in Durable.Query and Durable.Stats, Durable.Activity.list/1, the pending listings of Durable.Wait, the schedule lookups) returns {:ok, result} or {:error, reason}, and has a twin ending in ! that returns the result itself or raises:

{:ok, runs} = Durable.list_executions(status: :failed, limit: 20)
runs = Durable.list_executions!(status: :failed, limit: 20)

{:error, {:invalid_option, :limit}} = Durable.list_executions(limit: -1)
Durable.list_executions!(limit: -1)
#=> ** (ArgumentError) invalid value for option :limit: -1

Reasons

  • {:invalid_option, key} - the option key has a value of the wrong type or out of range: a negative or non-integer :limit or :offset, a status no run can have (alone or in a list), bucket_seconds: 0, text with a NUL byte or bytes that aren't UTF-8, a filter on an id that isn't a UUID. An option the function doesn't know is named the same way, so a misspelt filter can't match everything: {:invalid_option, :statuz}. Nothing is clamped or quietly matched against nothing. Options that aren't a keyword list are {:invalid_option, :opts}, and an argument that isn't an option is named the same way (:parent_ids, :count).
  • :not_found - the id (or schedule name) names nothing. That includes nil and an id that isn't a UUID.
  • {:too_many_buckets, count} - from Durable.Stats.timeline/1 and Durable.Stats.keys/1, for a window cut into more than 10,000 buckets.

A nil value is an option that wasn't set. :durable, the instance, is accepted everywhere.

The ! twins raise ArgumentError for each of these, with a message that names the option and the value. Both forms raise ArgumentError for a :durable instance that isn't started: that is a mistake in the app, not in the input.

Summary

Types

What a read function returns when it can't answer.

Functions

Whether id could name a run: :ok, or {:error, :not_found} for nil and anything that isn't a UUID.

Turns a read function's answer into its ! form: the result, or an ArgumentError that says what was wrong. opts are the options the caller passed and args its other arguments by name (the id under :id), so the message can show the value.

Whether value is of type.

Checks opts against schema, a keyword list of option to type. Returns the options that were set (the nils dropped), or the first that is wrong. An option that isn't in schema is wrong, bar :durable: a misspelt filter would otherwise match everything.

Types

reason()

@type reason() ::
  {:invalid_option, atom()} | :not_found | {:too_many_buckets, pos_integer()}

What a read function returns when it can't answer.

type()

@type type() ::
  :non_neg_integer
  | :pos_integer
  | :boolean
  | :datetime
  | :text
  | :texts
  | :name
  | :module
  | :uuid
  | :uuids
  | :input
  | {:one_of, [atom()]}
  | {:one_or_more_of, [atom()]}

Functions

id(id)

@spec id(term()) :: :ok | {:error, :not_found}

Whether id could name a run: :ok, or {:error, :not_found} for nil and anything that isn't a UUID.

unwrap!(result, opts, args \\ [])

@spec unwrap!({:ok, result} | {:error, term()}, term(), keyword()) :: result
when result: term()

Turns a read function's answer into its ! form: the result, or an ArgumentError that says what was wrong. opts are the options the caller passed and args its other arguments by name (the id under :id), so the message can show the value.

iex> Durable.Query.Opts.unwrap!({:ok, [1]}, limit: 1)
[1]

valid?(arg1, value)

@spec valid?(type() | nil, term()) :: boolean()

Whether value is of type.

iex> Durable.Query.Opts.valid?(:pos_integer, 0)
false

validate(opts, schema \\ [])

@spec validate(term(), [{atom(), type()}]) ::
  {:ok, keyword()} | {:error, {:invalid_option, atom()}}

Checks opts against schema, a keyword list of option to type. Returns the options that were set (the nils dropped), or the first that is wrong. An option that isn't in schema is wrong, bar :durable: a misspelt filter would otherwise match everything.

iex> Durable.Query.Opts.validate([limit: 5, tag: nil], limit: :non_neg_integer)
{:ok, [limit: 5]}
iex> Durable.Query.Opts.validate([limt: 5], limit: :non_neg_integer)
{:error, {:invalid_option, :limt}}
iex> Durable.Query.Opts.validate([limit: -1], limit: :non_neg_integer)
{:error, {:invalid_option, :limit}}