# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.EngineCacheEntrySet do
  @moduledoc """
  A set of cache entries returned by a query to a cache
  """

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

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

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  The total disk space used by the cache entries in this set.
  """
  @spec disk_space_bytes(t()) :: {:ok, integer()} | {:error, term()}
  def disk_space_bytes(%__MODULE__{} = engine_cache_entry_set) do
    query_builder =
      engine_cache_entry_set.query_builder |> QB.select("diskSpaceBytes")

    Client.execute(engine_cache_entry_set.client, query_builder)
  end

  @doc """
  The list of individual cache entries in the set
  """
  @spec entries(t()) :: {:ok, [Dagger.EngineCacheEntry.t()]} | {:error, term()}
  def entries(%__MODULE__{} = engine_cache_entry_set) do
    query_builder =
      engine_cache_entry_set.query_builder |> QB.select("entries") |> QB.select("id")

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

  @doc """
  The number of cache entries in this set.
  """
  @spec entry_count(t()) :: {:ok, integer()} | {:error, term()}
  def entry_count(%__MODULE__{} = engine_cache_entry_set) do
    query_builder =
      engine_cache_entry_set.query_builder |> QB.select("entryCount")

    Client.execute(engine_cache_entry_set.client, query_builder)
  end

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

    Client.execute(engine_cache_entry_set.client, query_builder)
  end
end

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

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