# Copyright 2019-2020 OMG Network Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule OMG.WatcherInfo.DB.Types.AtomType do
  @moduledoc """
  Custom Ecto type that converts DB's string value into atom.
  """
  @behaviour Ecto.Type
  def type(), do: :string

  def cast(value), do: {:ok, value}

  def load(value), do: {:ok, String.to_existing_atom(value)}

  def dump(value) when is_atom(value), do: {:ok, Atom.to_string(value)}

  def dump(_), do: :error
  # https://hexdocs.pm/ecto/Ecto.Type.html#c:embed_as/1
  def embed_as(_), do: :self

  def equal?(value1, value2), do: value1 == value2
end
