# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.Address do
  @moduledoc """
  A standardized address to load containers, directories, secrets, and other object types. Address format depends on the type, and is validated at type selection.
  """

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

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

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  Load a container from the address.
  """
  @spec container(t()) :: Dagger.Container.t()
  def container(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("container")

    %Dagger.Container{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  Load a directory from the address.
  """
  @spec directory(t(), [
          {:exclude, [String.t()]},
          {:include, [String.t()]},
          {:no_cache, boolean() | nil}
        ]) :: Dagger.Directory.t()
  def directory(%__MODULE__{} = address, optional_args \\ []) do
    query_builder =
      address.query_builder
      |> QB.select("directory")
      |> QB.maybe_put_arg("exclude", optional_args[:exclude])
      |> QB.maybe_put_arg("include", optional_args[:include])
      |> QB.maybe_put_arg("noCache", optional_args[:no_cache])

    %Dagger.Directory{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  Load a file from the address.
  """
  @spec file(t(), [
          {:exclude, [String.t()]},
          {:include, [String.t()]},
          {:no_cache, boolean() | nil}
        ]) :: Dagger.File.t()
  def file(%__MODULE__{} = address, optional_args \\ []) do
    query_builder =
      address.query_builder
      |> QB.select("file")
      |> QB.maybe_put_arg("exclude", optional_args[:exclude])
      |> QB.maybe_put_arg("include", optional_args[:include])
      |> QB.maybe_put_arg("noCache", optional_args[:no_cache])

    %Dagger.File{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  Load a git ref (branch, tag or commit) from the address.
  """
  @spec git_ref(t()) :: Dagger.GitRef.t()
  def git_ref(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("gitRef")

    %Dagger.GitRef{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  Load a git repository from the address.
  """
  @spec git_repository(t()) :: Dagger.GitRepository.t()
  def git_repository(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("gitRepository")

    %Dagger.GitRepository{
      query_builder: query_builder,
      client: address.client
    }
  end

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

    Client.execute(address.client, query_builder)
  end

  @doc """
  Load a secret from the address.
  """
  @spec secret(t()) :: Dagger.Secret.t()
  def secret(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("secret")

    %Dagger.Secret{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  Load a service from the address.
  """
  @spec service(t()) :: Dagger.Service.t()
  def service(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("service")

    %Dagger.Service{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  Load a local socket from the address.
  """
  @spec socket(t()) :: Dagger.Socket.t()
  def socket(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("socket")

    %Dagger.Socket{
      query_builder: query_builder,
      client: address.client
    }
  end

  @doc """
  The address value
  """
  @spec value(t()) :: {:ok, String.t()} | {:error, term()}
  def value(%__MODULE__{} = address) do
    query_builder =
      address.query_builder |> QB.select("value")

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

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

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