Construct.Types v0.1.0 Construct.Types.CommaList View Source

Extracts list of separated by comma values from string. You can use it alone, just for splitting values:

defmodule Structure do
  use Construct

  structure do
    field :values, Construct.Types.CommaList
  end
end

> Structure.make!(values: "foo,bar,baz,42")
%Structure{values: ["foo", "bar", "baz", "42"]}

> Structure.make!(values: ["foo", 42])
%Structure{values: ["foo", 42]}

Also you can compose it with other types:

defmodule UserInfoRequest do
  use Construct

  structure do
    field :user_ids, [Construct.Types.CommaList, {:array, :integer}]
  end
end

> UserInfoRequest.make!(%{user_ids: "1,2,42"})
%UserInfoRequest{user_ids: [1, 2, 42]}

> UserInfoRequest.make(%{user_ids: "1,foo"})
{:error, %{user_ids: :invalid}}

Link to this section Summary

Functions

Casts the given input to the custom type

Link to this section Functions

Casts the given input to the custom type.

Callback implementation for Construct.Type.cast/1.