lib/klife_protocol/generated/messages/describe_groups.ex

# DO NOT EDIT THIS FILE MANUALLY  
# This module is automatically generated by running mix task generate_file
# every change must be done inside the mix task directly

defmodule KlifeProtocol.Messages.DescribeGroups do
  @moduledoc """
  Kafka protocol DescribeGroups message

  Request versions summary:   
  - Versions 1 and 2 are the same as version 0.
  - Starting in version 3, authorized operations can be requested.
  - Starting in version 4, the response will include group.instance.id info for members.
  - Version 5 is the first flexible version.

  Response versions summary:
  - Version 1 added throttle time.
  - Starting in version 2, on quota violation, brokers send out responses before throttling.
  - Starting in version 3, brokers can send authorized operations.
  - Starting in version 4, the response will optionally include group.instance.id info for members.
  - Version 5 is the first flexible version.

  """

  alias KlifeProtocol.Deserializer
  alias KlifeProtocol.Serializer
  alias KlifeProtocol.Header

  @api_key 15
  @min_flexible_version_req 5
  @min_flexible_version_res 5

  @doc """
  Receives a map and serialize it to kafka wire format of the given version.

  Input content fields:
  - groups: The names of the groups to describe ([]string | versions 0+)
  - include_authorized_operations: Whether to include authorized operations. (bool | versions 3+)

  """
  def serialize_request(%{headers: headers, content: content}, version) do
    headers
    |> Map.put(:request_api_key, @api_key)
    |> Map.put(:request_api_version, version)
    |> Header.serialize_request(req_header_version(version))
    |> then(&Serializer.execute(content, request_schema(version), &1))
  end

  @doc """
  Receive a binary in the kafka wire format and deserialize it into a map.

  Response content fields:

  - throttle_time_ms: The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota. (int32 | versions 1+)
  - groups: Each described group. ([]DescribedGroup | versions 0+)
      - error_code: The describe error, or 0 if there was no error. (int16 | versions 0+)
      - group_id: The group ID string. (string | versions 0+)
      - group_state: The group state string, or the empty string. (string | versions 0+)
      - protocol_type: The group protocol type, or the empty string. (string | versions 0+)
      - protocol_data: The group protocol data, or the empty string. (string | versions 0+)
      - members: The group members. ([]DescribedGroupMember | versions 0+)
          - member_id: The member ID assigned by the group coordinator. (string | versions 0+)
          - group_instance_id: The unique identifier of the consumer instance provided by end user. (string | versions 4+)
          - client_id: The client ID used in the member's latest join group request. (string | versions 0+)
          - client_host: The client host. (string | versions 0+)
          - member_metadata: The metadata corresponding to the current group protocol in use. (bytes | versions 0+)
          - member_assignment: The current assignment provided by the group leader. (bytes | versions 0+)
      - authorized_operations: 32-bit bitfield to represent authorized operations for this group. (int32 | versions 3+)

  """
  def deserialize_response(data, version, with_header? \\ true)

  def deserialize_response(data, version, true) do
    {:ok, {headers, rest_data}} = Header.deserialize_response(data, res_header_version(version))

    case Deserializer.execute(rest_data, response_schema(version)) do
      {:ok, {content, <<>>}} ->
        {:ok, %{headers: headers, content: content}}

      {:error, _reason} = err ->
        err
    end
  end

  def deserialize_response(data, version, false) do
    case Deserializer.execute(data, response_schema(version)) do
      {:ok, {content, <<>>}} ->
        {:ok, %{content: content}}

      {:error, _reason} = err ->
        err
    end
  end

  @doc """
  Returns the message api key number.
  """
  def api_key(), do: @api_key

  @doc """
  Returns the current max supported version of this message.
  """
  def max_supported_version(), do: 5

  @doc """
  Returns the current min supported version of this message.
  """
  def min_supported_version(), do: 0

  defp req_header_version(msg_version),
    do: if(msg_version >= @min_flexible_version_req, do: 2, else: 1)

  defp res_header_version(msg_version),
    do: if(msg_version >= @min_flexible_version_res, do: 1, else: 0)

  defp request_schema(0), do: [groups: {{:array, :string}, %{is_nullable?: false}}]
  defp request_schema(1), do: [groups: {{:array, :string}, %{is_nullable?: false}}]
  defp request_schema(2), do: [groups: {{:array, :string}, %{is_nullable?: false}}]

  defp request_schema(3),
    do: [
      groups: {{:array, :string}, %{is_nullable?: false}},
      include_authorized_operations: {:boolean, %{is_nullable?: false}}
    ]

  defp request_schema(4),
    do: [
      groups: {{:array, :string}, %{is_nullable?: false}},
      include_authorized_operations: {:boolean, %{is_nullable?: false}}
    ]

  defp request_schema(5),
    do: [
      groups: {{:compact_array, :compact_string}, %{is_nullable?: false}},
      include_authorized_operations: {:boolean, %{is_nullable?: false}},
      tag_buffer: {:tag_buffer, []}
    ]

  defp request_schema(unkown_version),
    do: raise("Unknown version #{unkown_version} for message DescribeGroups")

  defp response_schema(0),
    do: [
      groups:
        {{:array,
          [
            error_code: {:int16, %{is_nullable?: false}},
            group_id: {:string, %{is_nullable?: false}},
            group_state: {:string, %{is_nullable?: false}},
            protocol_type: {:string, %{is_nullable?: false}},
            protocol_data: {:string, %{is_nullable?: false}},
            members:
              {{:array,
                [
                  member_id: {:string, %{is_nullable?: false}},
                  client_id: {:string, %{is_nullable?: false}},
                  client_host: {:string, %{is_nullable?: false}},
                  member_metadata: {:bytes, %{is_nullable?: false}},
                  member_assignment: {:bytes, %{is_nullable?: false}}
                ]}, %{is_nullable?: false}}
          ]}, %{is_nullable?: false}}
    ]

  defp response_schema(1),
    do: [
      throttle_time_ms: {:int32, %{is_nullable?: false}},
      groups:
        {{:array,
          [
            error_code: {:int16, %{is_nullable?: false}},
            group_id: {:string, %{is_nullable?: false}},
            group_state: {:string, %{is_nullable?: false}},
            protocol_type: {:string, %{is_nullable?: false}},
            protocol_data: {:string, %{is_nullable?: false}},
            members:
              {{:array,
                [
                  member_id: {:string, %{is_nullable?: false}},
                  client_id: {:string, %{is_nullable?: false}},
                  client_host: {:string, %{is_nullable?: false}},
                  member_metadata: {:bytes, %{is_nullable?: false}},
                  member_assignment: {:bytes, %{is_nullable?: false}}
                ]}, %{is_nullable?: false}}
          ]}, %{is_nullable?: false}}
    ]

  defp response_schema(2),
    do: [
      throttle_time_ms: {:int32, %{is_nullable?: false}},
      groups:
        {{:array,
          [
            error_code: {:int16, %{is_nullable?: false}},
            group_id: {:string, %{is_nullable?: false}},
            group_state: {:string, %{is_nullable?: false}},
            protocol_type: {:string, %{is_nullable?: false}},
            protocol_data: {:string, %{is_nullable?: false}},
            members:
              {{:array,
                [
                  member_id: {:string, %{is_nullable?: false}},
                  client_id: {:string, %{is_nullable?: false}},
                  client_host: {:string, %{is_nullable?: false}},
                  member_metadata: {:bytes, %{is_nullable?: false}},
                  member_assignment: {:bytes, %{is_nullable?: false}}
                ]}, %{is_nullable?: false}}
          ]}, %{is_nullable?: false}}
    ]

  defp response_schema(3),
    do: [
      throttle_time_ms: {:int32, %{is_nullable?: false}},
      groups:
        {{:array,
          [
            error_code: {:int16, %{is_nullable?: false}},
            group_id: {:string, %{is_nullable?: false}},
            group_state: {:string, %{is_nullable?: false}},
            protocol_type: {:string, %{is_nullable?: false}},
            protocol_data: {:string, %{is_nullable?: false}},
            members:
              {{:array,
                [
                  member_id: {:string, %{is_nullable?: false}},
                  client_id: {:string, %{is_nullable?: false}},
                  client_host: {:string, %{is_nullable?: false}},
                  member_metadata: {:bytes, %{is_nullable?: false}},
                  member_assignment: {:bytes, %{is_nullable?: false}}
                ]}, %{is_nullable?: false}},
            authorized_operations: {:int32, %{is_nullable?: false}}
          ]}, %{is_nullable?: false}}
    ]

  defp response_schema(4),
    do: [
      throttle_time_ms: {:int32, %{is_nullable?: false}},
      groups:
        {{:array,
          [
            error_code: {:int16, %{is_nullable?: false}},
            group_id: {:string, %{is_nullable?: false}},
            group_state: {:string, %{is_nullable?: false}},
            protocol_type: {:string, %{is_nullable?: false}},
            protocol_data: {:string, %{is_nullable?: false}},
            members:
              {{:array,
                [
                  member_id: {:string, %{is_nullable?: false}},
                  group_instance_id: {:string, %{is_nullable?: true}},
                  client_id: {:string, %{is_nullable?: false}},
                  client_host: {:string, %{is_nullable?: false}},
                  member_metadata: {:bytes, %{is_nullable?: false}},
                  member_assignment: {:bytes, %{is_nullable?: false}}
                ]}, %{is_nullable?: false}},
            authorized_operations: {:int32, %{is_nullable?: false}}
          ]}, %{is_nullable?: false}}
    ]

  defp response_schema(5),
    do: [
      throttle_time_ms: {:int32, %{is_nullable?: false}},
      groups:
        {{:compact_array,
          [
            error_code: {:int16, %{is_nullable?: false}},
            group_id: {:compact_string, %{is_nullable?: false}},
            group_state: {:compact_string, %{is_nullable?: false}},
            protocol_type: {:compact_string, %{is_nullable?: false}},
            protocol_data: {:compact_string, %{is_nullable?: false}},
            members:
              {{:compact_array,
                [
                  member_id: {:compact_string, %{is_nullable?: false}},
                  group_instance_id: {:compact_string, %{is_nullable?: true}},
                  client_id: {:compact_string, %{is_nullable?: false}},
                  client_host: {:compact_string, %{is_nullable?: false}},
                  member_metadata: {:compact_bytes, %{is_nullable?: false}},
                  member_assignment: {:compact_bytes, %{is_nullable?: false}},
                  tag_buffer: {:tag_buffer, %{}}
                ]}, %{is_nullable?: false}},
            authorized_operations: {:int32, %{is_nullable?: false}},
            tag_buffer: {:tag_buffer, %{}}
          ]}, %{is_nullable?: false}},
      tag_buffer: {:tag_buffer, %{}}
    ]

  defp response_schema(unkown_version),
    do: raise("Unknown version #{unkown_version} for message DescribeGroups")
end