Skip to content

Commit

Permalink
feat(gapic-generator): Change REGAPIC pagination algorithm to the sta…
Browse files Browse the repository at this point in the history
…ndard algorithm for non-compute clients (#1143)
  • Loading branch information
dazuma authored Jan 30, 2025
1 parent acad6ab commit 95d1add
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def test_compute_addresses_aggregatedlist
assert_equal "::Google::Cloud::Compute::V1::AddressAggregatedList", presenter.return_type

assert presenter.rest.paged?
assert presenter.rest.pagination.repeated_field_is_a_map?
assert presenter.rest.compute_pagination.repeated_field_is_a_map?
end
end
12 changes: 12 additions & 0 deletions gapic-generator/lib/gapic/presenters/method_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ def paged_response_type
message_ruby_type repeated_field.message
end

##
# @return [String] The name of the repeated field in paginated responses
#
def paged_response_repeated_field_name
return nil unless paged_response? @method.output

repeated_field = @method.output.fields.find do |f|
f.label == :LABEL_REPEATED && f.type == :TYPE_MESSAGE
end
repeated_field.name
end

##
# @return [Array<String>] The segment key names.
#
Expand Down
25 changes: 21 additions & 4 deletions gapic-generator/lib/gapic/presenters/method_rest_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Presenters
#
class MethodRestPresenter
# @return [Gapic::Presenters::Method::RestPaginationInfo]
attr_reader :pagination
attr_reader :compute_pagination

attr_reader :http_bindings

Expand All @@ -37,7 +37,10 @@ class MethodRestPresenter
def initialize main_method, api
@main_method = main_method
@http_bindings = main_method.http_bindings
@pagination = Gapic::Presenters::Method::RestPaginationInfo.new main_method.method, api
@compute_pagination =
if @main_method.service.special_compute_behavior?
Gapic::Presenters::Method::RestPaginationInfo.new main_method.method, api
end
@type = "method"
end

Expand Down Expand Up @@ -91,18 +94,32 @@ def return_type
#
def doc_response_type
return "::Gapic::Operation" if lro?
return "::Gapic::Rest::PagedEnumerable<#{pagination.paged_element_doc_type}>" if paged?
if paged?
elem_type = compute_pagination&.paged_element_doc_type || @main_method.paged_response_type
return "::Gapic::Rest::PagedEnumerable<#{elem_type}>"
end
return "::Gapic::GenericLRO::Operation" if nonstandard_lro?
return_type
end

##
# @return [String] The name of the repeated field in paginated responses
#
def paged_response_repeated_field_name
if compute_pagination
compute_pagination.response_repeated_field_name
else
@main_method.paged_response_repeated_field_name
end
end

##
# Whether the REGAPIC method should be rendered as paged
#
# @return [Boolean]
#
def paged?
@pagination.paged?
compute_pagination ? compute_pagination.paged? : @main_method.paged?
end

def lro?
Expand Down
4 changes: 4 additions & 0 deletions gapic-generator/lib/gapic/presenters/service_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def package
PackagePresenter.new @gem_presenter, @api, @service.parent.package
end

def special_compute_behavior?
address[0] == "google" && address[1] == "cloud" && address[2] == "compute" && !address[3].to_s.empty?
end

##
# @return [Boolean] Whether the service is marked as deprecated.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%- assert_locals method -%>
<%- boverr_str = method.service.rest.is_main_mixin_service? ? ", bindings_override: bindings_override" : "" -%>
@<%= method.service.stub_name %>.<%= method.name %> request, options<%= boverr_str %> do |result, operation|
result = ::Gapic::Rest::PagedEnumerable.new @<%= method.service.stub_name %>, :<%= method.name %>, "<%= method.rest.pagination.response_repeated_field_name %>", request, result, options
result = ::Gapic::Rest::PagedEnumerable.new @<%= method.service.stub_name %>, :<%= method.name %>, "<%= method.rest.paged_response_repeated_field_name %>", request, result, options
yield result, operation if block_given?
throw :response, result
end
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,10 @@ def paged_expand request, options = nil
# @param page_token [::String]
# The position of the page to be returned.
# @yield [result, operation] Access the result along with the TransportOperation object
# @yieldparam result [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::EchoResponse>]
# @yieldparam result [::Google::Showcase::V1beta1::PagedExpandResponse]
# @yieldparam operation [::Gapic::Rest::TransportOperation]
#
# @return [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::EchoResponse>]
# @return [::Google::Showcase::V1beta1::PagedExpandResponse]
#
# @raise [::Gapic::Rest::Error] if the REST call is aborted.
#
Expand Down Expand Up @@ -684,10 +684,7 @@ def paged_expand_legacy request, options = nil
retry_policy: @config.retry_policy

@echo_stub.paged_expand_legacy request, options do |result, operation|
result = ::Gapic::Rest::PagedEnumerable.new @echo_stub, :paged_expand_legacy, "responses", request,
result, options
yield result, operation if block_given?
throw :response, result
end
rescue ::Faraday::Error => e
raise ::Gapic::Rest::Error.wrap_faraday_error e
Expand Down Expand Up @@ -722,10 +719,10 @@ def paged_expand_legacy request, options = nil
# @param page_token [::String]
# The position of the page to be returned.
# @yield [result, operation] Access the result along with the TransportOperation object
# @yieldparam result [::Gapic::Rest::PagedEnumerable<::String, ::Google::Showcase::V1beta1::PagedExpandResponseList>]
# @yieldparam result [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::PagedExpandLegacyMappedResponse::AlphabetizedEntry>]
# @yieldparam operation [::Gapic::Rest::TransportOperation]
#
# @return [::Gapic::Rest::PagedEnumerable<::String, ::Google::Showcase::V1beta1::PagedExpandResponseList>]
# @return [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::PagedExpandLegacyMappedResponse::AlphabetizedEntry>]
#
# @raise [::Gapic::Rest::Error] if the REST call is aborted.
#
Expand Down

0 comments on commit 95d1add

Please sign in to comment.