# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.Error do
  @moduledoc """
  Dagger.Error
  """

  use Dagger.Core.Base, kind: :object, name: "Error"

  alias Dagger.Core.Client
  alias Dagger.Core.QueryBuilder, as: QB

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  A unique identifier for this Error.
  """
  @spec id(t()) :: {:ok, Dagger.ErrorID.t()} | {:error, term()}
  def id(%__MODULE__{} = error) do
    query_builder =
      error.query_builder |> QB.select("id")

    Client.execute(error.client, query_builder)
  end

  @doc """
  A description of the error.
  """
  @spec message(t()) :: {:ok, String.t()} | {:error, term()}
  def message(%__MODULE__{} = error) do
    query_builder =
      error.query_builder |> QB.select("message")

    Client.execute(error.client, query_builder)
  end

  @doc """
  The extensions of the error.
  """
  @spec values(t()) :: {:ok, [Dagger.ErrorValue.t()]} | {:error, term()}
  def values(%__MODULE__{} = error) do
    query_builder =
      error.query_builder |> QB.select("values") |> QB.select("id")

    with {:ok, items} <- Client.execute(error.client, query_builder) do
      {:ok,
       for %{"id" => id} <- items do
         %Dagger.ErrorValue{
           query_builder:
             QB.query()
             |> QB.select("loadErrorValueFromID")
             |> QB.put_arg("id", id),
           client: error.client
         }
       end}
    end
  end

  @doc """
  Add a value to the error.
  """
  @spec with_value(t(), String.t(), Dagger.JSON.t()) :: Dagger.Error.t()
  def with_value(%__MODULE__{} = error, name, value) do
    query_builder =
      error.query_builder
      |> QB.select("withValue")
      |> QB.put_arg("name", name)
      |> QB.put_arg("value", value)

    %Dagger.Error{
      query_builder: query_builder,
      client: error.client
    }
  end
end

defimpl Jason.Encoder, for: Dagger.Error do
  def encode(error, opts) do
    {:ok, id} = Dagger.Error.id(error)
    Jason.Encode.string(id, opts)
  end
end

defimpl Nestru.Decoder, for: Dagger.Error do
  def decode_fields_hint(_struct, _context, id) do
    {:ok, Dagger.Client.load_error_from_id(Dagger.Global.dag(), id)}
  end
end
