defmodule CustomerService.Aggregates.CustomerId do @type t :: %__MODULE__{ org_id: String.t(), customer_type: String.t(), customer_id: String.t() } @derive Jason.Encoder defstruct [:org_id, :customer_type, :customer_id] def new(org_id, customer_type, customer_id) when is_binary(org_id) and is_binary(customer_type) and is_binary(customer_id) do %__MODULE__{ org_id: org_id, customer_type: customer_type, customer_id: customer_id } end def parse(<<_::binary>> = string) do case String.split(string, ":", parts: 3) do [org_id, customer_type, customer_id] -> {:ok, %__MODULE__{org_id: org_id, customer_type: customer_type, customer_id: customer_id}} _ -> {:error, :invalid_customer_id} end end def parse! do {:error, :invalid_customer_id} end defimpl String.Chars do def to_string(%CustomerService.Aggregates.CustomerId{ org_id: org_id, customer_type: type, customer_id: customer_id }) do org_id <> ":" <> type <> ":" <> customer_id end end defimpl Commanded.Serialization.JsonDecoder do def decode(%{org_id: org_id, customer_type: customer_type, customer_id: customer_id}) do CustomerService.Aggregates.CustomerId.new(org_id, customer_type, customer_id) end def decode(id), do: id end end