# `Commandex`
[🔗](https://github.com/codedge-llc/commandex/blob/v0.6.0/lib/commandex.ex#L1)

Defines a command struct.

Commandex is a loose implementation of the command pattern, making it easy
to wrap parameters, data, and errors into a well-defined struct.

## Example

A fully implemented command module might look like this:

    defmodule RegisterUser do
      import Commandex

      command do
        param :email, :string, required: true
        param :password, :string

        data :password_hash
        data :user

        pipeline :hash_password
        pipeline :create_user
        pipeline :send_welcome_email
      end

      def hash_password(command, %{password: nil} = _params, _data) do
        command
        |> put_error(:password, :not_given)
        |> halt()
      end

      def hash_password(command, %{password: password} = _params, _data) do
        put_data(command, :password_hash, Base.encode64(password))
      end

      def create_user(command, %{email: email} = _params, %{password_hash: phash} = _data) do
        %User{}
        |> User.changeset(%{email: email, password_hash: phash})
        |> Repo.insert()
        |> case do
          {:ok, user} -> put_data(command, :user, user)
          {:error, changeset} -> command |> put_error(:repo, changeset) |> halt()
        end
      end

      def send_welcome_email(command, _params, %{user: user}) do
        Mailer.send_welcome_email(user)
        command
      end
    end

The `command/1` macro will define a struct that looks like:

    %RegisterUser{
      __meta__: #Commandex.Metadata<2 params>,
      success: false,
      halted: false,
      errors: %{},
      params: %{email: nil, password: nil},
      data: %{password_hash: nil, user: nil},
      pipelines: [:hash_password, :create_user, :send_welcome_email]
    }

As well as two functions:

    &RegisterUser.new/1
    &RegisterUser.run/1

`&new/1` parses and casts parameters into a new struct. These can be either a
keyword list or map with atom/string keys. If a parameter has a type declared,
the value will be cast to that type. If the cast fails, the parameter keeps its
default and an `:invalid` error is added. If a parameter is marked as `required: true`
and its value is `nil` after casting, a `:required` error is added. A command with
either error is halted, so `&run/1` does not run any pipelines.

`&run/1` takes a command struct and runs it through the pipeline functions defined
in the command. **Functions are executed in the order in which they are defined**.
If a command passes through all pipelines without calling `halt/1`, `:success`
will be set to `true`. Otherwise, subsequent pipelines after the `halt/1` will
be ignored and `:success` will be set to `false`.

    %{email: "example@example.com", password: "asdf1234"}
    |> RegisterUser.new()
    |> RegisterUser.run()
    |> case do
      %{success: true, data: %{user: user}} ->
        # Success! We've got a user now

      %{success: false, errors: %{password: :not_given}} ->
        # Respond with a 400 or something

      %{success: false, errors: _errors} ->
        # I'm a lazy programmer that writes catch-all error handling
    end

## Parameter-less Commands

If a command does not have any parameters defined, a `run/0` will be generated
automatically. Useful for diagnostic jobs and internal tasks.

    iex> command = GenerateReport.run()
    iex> command.success
    true
    iex> command.data.total_valid
    183220
    iex> command.data.total_invalid
    781215

## Typed Parameters

A parameter can declare a type. `&new/1` casts the value to that type:

    param :age, :integer
    param :email, :string, required: true
    param :score, :float, default: 0.0
    param :starts_on, :date
    param :tags, {:array, :string}
    param :counts, {:map, :integer}

A parameter without a type has the `:any` type. `&new/1` does not cast
it and keeps the value as given.

### Built-in types

| Type                   | Value after the cast                     |
| ---------------------- | ---------------------------------------- |
| `:any`                 | The value as given                       |
| `:binary`              | A binary                                 |
| `:boolean`             | `true` or `false`                        |
| `:date`                | A `Date`                                 |
| `:decimal`             | A `Decimal`                              |
| `:float`               | A float                                  |
| `:integer`             | An integer                               |
| `:map`                 | A map                                    |
| `:naive_datetime`      | A `NaiveDateTime` without microseconds   |
| `:naive_datetime_usec` | A `NaiveDateTime` with microseconds      |
| `:string`              | A string                                 |
| `:time`                | A `Time` without microseconds            |
| `:time_usec`           | A `Time` with microseconds               |
| `:utc_datetime`        | A `DateTime` in UTC without microseconds |
| `:utc_datetime_usec`   | A `DateTime` in UTC with microseconds    |
| `{:array, type}`       | A list of values of the inner type       |
| `{:map, type}`         | A map with values of the inner type      |

The built-in types follow the cast rules of Ecto. For example, `:integer`
accepts `42` and `"42"`, and does not accept `42.0`.

The `:decimal` type needs the optional `:decimal` package, version 3.0
or later.

### Custom types

A module that implements the `Commandex.Type` behaviour can be the type of
a parameter. A type can also take options from the `param` line:

    param :color, MyApp.Types.Color
    param :status, MyApp.Types.Enum, values: [:active, :pending_review]

See `Commandex.Type` for how to write each kind of type.

### Empty values

For a typed parameter, a string that is empty or has only whitespace is
an empty value. `&new/1` does not cast an empty value, and the parameter
keeps its default. An array drops its empty elements. A `:binary`
parameter is empty only for `""`.

### Errors

If a cast fails, the parameter keeps its default and gets an `:invalid`
error. If a parameter with `required: true` is `nil` after the cast, it
gets a `:required` error. `&new/1` halts a command that has an error.

    command = RegisterUser.new(%{password: 42})

    command.errors
    #=> %{email: :required, password: :invalid}

    command.halted
    #=> true

# `command`

```elixir
@type command() :: %{
  __struct__: atom(),
  __meta__: Commandex.Metadata.t(),
  data: map(),
  errors: map(),
  halted: boolean(),
  params: map(),
  pipelines: [pipeline()],
  success: boolean()
}
```

Command struct.

## Attributes

- `__meta__` - Compile-time metadata with the parameter types. See `Commandex.Metadata`.
- `data` - Data generated during the pipeline, defined by `Commandex.data/1`.
- `errors` - Errors generated during the pipeline with `Commandex.put_error/3`
- `halted` - Whether or not the pipeline was halted.
- `params` - Parameters given to the command, defined by `Commandex.param/1`.
- `pipelines` - A list of pipeline functions to execute, defined by `Commandex.pipeline/1`.
- `success` - Whether or not the command was successful. This is only set to
  `true` if the command was not halted after running all of the pipelines.

# `pipeline`

```elixir
@type pipeline() ::
  atom()
  | {module(), atom()}
  | {module(), atom(), [any()]}
  | (command :: struct() -&gt; command :: struct())
  | (command :: struct(), params :: map(), data :: map() -&gt; command :: struct())
```

Command pipeline stage.

A pipeline function can be defined multiple ways:

- `pipeline :do_work` - Name of a function inside the command's module, arity three.
- `pipeline {YourModule, :do_work}` - Arity three.
- `pipeline {YourModule, :do_work, [:additional, "args"]}` - Arity three plus the
  number of additional args given.
- `pipeline &YourModule.do_work/1` - Or any anonymous function of arity one.
- `pipeline &YourModule.do_work/3` - Or any anonymous function of arity three.

# `command`
*macro* 

```elixir
@spec command([{:do, any()}]) :: no_return()
```

Defines a command struct with params, data, and pipelines.

# `data`
*macro* 

```elixir
@spec data(atom()) :: no_return()
```

Defines a command data field.

Data field values are created and set as pipelines are run. Set one with `put_data/3`.

    command do
      # ...params

      data :password_hash
      data :user

      # ...pipelines
    end

# `halt`

```elixir
@spec halt(command(), Keyword.t()) :: command()
```

Halts a command pipeline.

Any pipelines defined after the halt will be ignored. By default, if a command finishes
running through all pipelines, `:success` will be set to `true`.

    def hash_password(command, %{password: nil} = _params, _data) do
      command
      |> put_error(:password, :not_supplied)
      |> halt()
    end

### Halting Early Successfully

Pass `success: true` as an optional argument if you would like to mark the command
as successful, even if there are still pipelines left to be run. Useful for returning
early from no-op commands.

    def needs_update?(command, _params, _data) do
      command
      |> put_error(:update, :no_op)
      |> halt(success: true)
    end

# `halt_on_errors`

```elixir
@spec halt_on_errors(command()) :: command()
```

Halts the command if any errors are present.

`new/1` calls this after it casts the parameters, so a command with a
cast error or a missing required parameter does not run any pipelines.
It can also be used as a pipeline to collect custom validation errors
before the command continues.

    command do
      param :email, :string, required: true
      param :age, :integer

      pipeline :validate_age
      pipeline &Commandex.halt_on_errors/1
      pipeline :create_user
    end

# `param`
*macro* 

```elixir
@spec param(atom(), Commandex.Type.t() | Keyword.t(), Keyword.t()) :: no_return()
```

Defines a command parameter field.

Parameters are supplied at struct creation, before any pipelines are run.

## Untyped

    command do
      param :email
      param :name, default: "Anonymous"
    end

## Typed

`new/1` casts a typed parameter to its type:

    command do
      param :email, :string, required: true
      param :age, :integer
      param :score, :float, default: 0.0
      param :tags, {:array, :string}
      param :color, MyApp.Types.Color
      param :status, MyApp.Types.Enum, values: [:active, :pending_review]
    end

The type is a built-in type or a module that implements `Commandex.Type`.
See the "Typed Parameters" section of the `Commandex` module for the list
of built-in types.

## Options

- `:default` - Default value if not provided. Defaults to `nil`. A typed
  parameter casts its default at compile time.
- `:required` - If `true`, adds a `:required` error when the value is `nil`
  after casting.

A type with options also gets every option on the line. Each other
parameter raises an `ArgumentError` at compile time for an option that
is not in this list.

## Compile-time checks

`param` raises an `ArgumentError` at compile time in these cases:

- The type is not a built-in type or a module that implements
  `Commandex.Type`.
- An option is not valid.
- The default is not valid for the type.
- The type is `:decimal` and the `:decimal` package is not installed.

# `pipeline`
*macro* 

```elixir
@spec pipeline(atom()) :: no_return()
```

Defines a command pipeline.

Pipelines are functions executed against the command, *in the order in which they are defined*.

For example, two pipelines could be defined:

    pipeline :check_valid_email
    pipeline :create_user

Which could be mentally interpreted as:

    command
    |> check_valid_email()
    |> create_user()

A pipeline function can be defined multiple ways:

- `pipeline :do_work` - Name of a function inside the command's module, arity three.
- `pipeline {YourModule, :do_work}` - Arity three.
- `pipeline {YourModule, :do_work, [:additional, "args"]}` - Arity three plus the
  number of additional args given.
- `pipeline &YourModule.do_work/1` - Or any anonymous function of arity one.
- `pipeline &YourModule.do_work/3` - Or any anonymous function of arity three.

# `put_data`

```elixir
@spec put_data(command(), atom(), any()) :: command()
```

Sets a data field with given value.

Define a data field first:

    data :password_hash

Set the password hash in one of your pipeline functions:

    def hash_password(command, %{password: password} = _params, _data) do
      # Better than plaintext, I guess
      put_data(command, :password_hash, Base.encode64(password))
    end

# `put_error`

```elixir
@spec put_error(command(), any(), any()) :: command()
```

Sets error for given key and value.

`:errors` is a map. Putting an error on the same key will overwrite the previous value.

    def hash_password(command, %{password: nil} = _params, _data) do
      command
      |> put_error(:password, :not_supplied)
      |> halt()
    end

---

*Consult [api-reference.md](api-reference.md) for complete listing*
