Class: Gapic::Presenters::Method::ComputePaginationInfo

Inherits:
Object
  • Object
show all
Includes:
Helpers::NamespaceHelper
Defined in:
lib/gapic/presenters/method/compute_pagination_info.rb

Overview

Compute-specific pagination info determined from the proto method

This implements the Compute-specific pagination heuristic

The following steps are followed for this heuristic:

  1. The method should not be server-streamed
  2. The request should have a page token and page size fields
  3. The response should have a next page token and contain a valid results field

Now determining the valid results field is its own complicated sub-heuristic that should be run last. This sub-heuristic cannot end in "not paginated". It should either determine the results field or throw an error

The following steps are followed for this sub-heuristic:

  1. Check the exception dictionary. If the method is there as a key, use the value as the results field.
  2. If there is exactly one map field, that field is the results field.

NB: at this point the response contains either 0 or 2+ map fields

  1. If there are no repeated fields there is no results field and we shall throw an error
  2. If there is exactly one repeated field, that field is the results field.
  3. If there are exactly 2 repeated fields, one is of message type, and the other is of type "String", the field of message type is the results field.

At this point there are either 2 repeated fields in wrong configuration, or 3 or more repeated fields. The method should have been in the exception dictionary (see step 0). Since the method is NOT in the exception dictionary we should throw an error to prevent accidentally releasing a Compute library with incorrect pagination.

Instance Method Summary collapse

Methods included from Helpers::NamespaceHelper

#ensure_absolute_namespace, #fix_namespace, #ruby_namespace, #ruby_namespace_for_address

Constructor Details

#initialize(proto_method, api) ⇒ ComputePaginationInfo

Returns a new instance of ComputePaginationInfo.

Parameters:



88
89
90
91
92
93
94
# File 'lib/gapic/presenters/method/compute_pagination_info.rb', line 88

def initialize proto_method, api
  @api = api
  @method_full_name = proto_method.full_name
  @request = proto_method.input
  @response = proto_method.output
  @server_streaming = proto_method.server_streaming
end

Instance Method Details

#paged?Boolean

Whether the method should be generated as paged

Returns:

  • (Boolean)


100
101
102
103
104
105
# File 'lib/gapic/presenters/method/compute_pagination_info.rb', line 100

def paged?
  paged_candidate = !server_streaming? && paged_request? && paged_response_candidate?

  # `paged_response?` can raise and should be evaluated last
  paged_candidate && paged_response?
end

#paged_element_doc_typeString?

Proto type of the repeated field in the response message

Returns:

  • (String, nil)


137
138
139
140
# File 'lib/gapic/presenters/method/compute_pagination_info.rb', line 137

def paged_element_doc_type
  return nil if response_results_field.nil?
  field_paginated_elem_doc_type response_results_field
end

#repeated_field_is_a_map?Boolean?

Whether the repeated field in the response message is a map

Returns:

  • (Boolean, nil)


129
130
131
# File 'lib/gapic/presenters/method/compute_pagination_info.rb', line 129

def repeated_field_is_a_map?
  response_results_field&.map?
end

#request_page_size_nameString?

Name of the request's field used for page size For Regapic can be either page_size or max_results

Returns:

  • (String, nil)


112
113
114
# File 'lib/gapic/presenters/method/compute_pagination_info.rb', line 112

def request_page_size_name
  request_page_size_field&.name
end

#response_repeated_field_nameString?

Name of the repeated field in the response message For REST gapics can be either a vanilla repeated field or a map

Returns:

  • (String, nil)


121
122
123
# File 'lib/gapic/presenters/method/compute_pagination_info.rb', line 121

def response_repeated_field_name
  response_results_field&.name
end