# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.Port do
  @moduledoc """
  A port exposed by a container.
  """

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

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

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

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

    Client.execute(port.client, query_builder)
  end

  @doc """
  Skip the health check when run as a service.
  """
  @spec experimental_skip_healthcheck(t()) :: {:ok, boolean()} | {:error, term()}
  def experimental_skip_healthcheck(%__MODULE__{} = port) do
    query_builder =
      port.query_builder |> QB.select("experimentalSkipHealthcheck")

    Client.execute(port.client, query_builder)
  end

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

    Client.execute(port.client, query_builder)
  end

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

    Client.execute(port.client, query_builder)
  end

  @doc """
  The transport layer protocol.
  """
  @spec protocol(t()) :: {:ok, Dagger.NetworkProtocol.t()} | {:error, term()}
  def protocol(%__MODULE__{} = port) do
    query_builder =
      port.query_builder |> QB.select("protocol")

    case Client.execute(port.client, query_builder) do
      {:ok, enum} -> {:ok, Dagger.NetworkProtocol.from_string(enum)}
      error -> error
    end
  end
end

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

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