Skip to content

Commit 95d1add

Browse files
authored
feat(gapic-generator): Change REGAPIC pagination algorithm to the standard algorithm for non-compute clients (#1143)
1 parent acad6ab commit 95d1add

File tree

6 files changed

+43
-13
lines changed

6 files changed

+43
-13
lines changed

gapic-generator-cloud/test/gapic/presenters/method/regapic_paged_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ def test_compute_addresses_aggregatedlist
5353
assert_equal "::Google::Cloud::Compute::V1::AddressAggregatedList", presenter.return_type
5454

5555
assert presenter.rest.paged?
56-
assert presenter.rest.pagination.repeated_field_is_a_map?
56+
assert presenter.rest.compute_pagination.repeated_field_is_a_map?
5757
end
5858
end

gapic-generator/lib/gapic/presenters/method_presenter.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,18 @@ def paged_response_type
311311
message_ruby_type repeated_field.message
312312
end
313313

314+
##
315+
# @return [String] The name of the repeated field in paginated responses
316+
#
317+
def paged_response_repeated_field_name
318+
return nil unless paged_response? @method.output
319+
320+
repeated_field = @method.output.fields.find do |f|
321+
f.label == :LABEL_REPEATED && f.type == :TYPE_MESSAGE
322+
end
323+
repeated_field.name
324+
end
325+
314326
##
315327
# @return [Array<String>] The segment key names.
316328
#

gapic-generator/lib/gapic/presenters/method_rest_presenter.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Presenters
2323
#
2424
class MethodRestPresenter
2525
# @return [Gapic::Presenters::Method::RestPaginationInfo]
26-
attr_reader :pagination
26+
attr_reader :compute_pagination
2727

2828
attr_reader :http_bindings
2929

@@ -37,7 +37,10 @@ class MethodRestPresenter
3737
def initialize main_method, api
3838
@main_method = main_method
3939
@http_bindings = main_method.http_bindings
40-
@pagination = Gapic::Presenters::Method::RestPaginationInfo.new main_method.method, api
40+
@compute_pagination =
41+
if @main_method.service.special_compute_behavior?
42+
Gapic::Presenters::Method::RestPaginationInfo.new main_method.method, api
43+
end
4144
@type = "method"
4245
end
4346

@@ -91,18 +94,32 @@ def return_type
9194
#
9295
def doc_response_type
9396
return "::Gapic::Operation" if lro?
94-
return "::Gapic::Rest::PagedEnumerable<#{pagination.paged_element_doc_type}>" if paged?
97+
if paged?
98+
elem_type = compute_pagination&.paged_element_doc_type || @main_method.paged_response_type
99+
return "::Gapic::Rest::PagedEnumerable<#{elem_type}>"
100+
end
95101
return "::Gapic::GenericLRO::Operation" if nonstandard_lro?
96102
return_type
97103
end
98104

105+
##
106+
# @return [String] The name of the repeated field in paginated responses
107+
#
108+
def paged_response_repeated_field_name
109+
if compute_pagination
110+
compute_pagination.response_repeated_field_name
111+
else
112+
@main_method.paged_response_repeated_field_name
113+
end
114+
end
115+
99116
##
100117
# Whether the REGAPIC method should be rendered as paged
101118
#
102119
# @return [Boolean]
103120
#
104121
def paged?
105-
@pagination.paged?
122+
compute_pagination ? compute_pagination.paged? : @main_method.paged?
106123
end
107124

108125
def lro?

gapic-generator/lib/gapic/presenters/service_presenter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def package
6161
PackagePresenter.new @gem_presenter, @api, @service.parent.package
6262
end
6363

64+
def special_compute_behavior?
65+
address[0] == "google" && address[1] == "cloud" && address[2] == "compute" && !address[3].to_s.empty?
66+
end
67+
6468
##
6569
# @return [Boolean] Whether the service is marked as deprecated.
6670
#
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<%- assert_locals method -%>
22
<%- boverr_str = method.service.rest.is_main_mixin_service? ? ", bindings_override: bindings_override" : "" -%>
33
@<%= method.service.stub_name %>.<%= method.name %> request, options<%= boverr_str %> do |result, operation|
4-
result = ::Gapic::Rest::PagedEnumerable.new @<%= method.service.stub_name %>, :<%= method.name %>, "<%= method.rest.pagination.response_repeated_field_name %>", request, result, options
4+
result = ::Gapic::Rest::PagedEnumerable.new @<%= method.service.stub_name %>, :<%= method.name %>, "<%= method.rest.paged_response_repeated_field_name %>", request, result, options
55
yield result, operation if block_given?
66
throw :response, result
77
end

shared/output/gapic/templates/showcase/lib/google/showcase/v1beta1/echo/rest/client.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,10 @@ def paged_expand request, options = nil
633633
# @param page_token [::String]
634634
# The position of the page to be returned.
635635
# @yield [result, operation] Access the result along with the TransportOperation object
636-
# @yieldparam result [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::EchoResponse>]
636+
# @yieldparam result [::Google::Showcase::V1beta1::PagedExpandResponse]
637637
# @yieldparam operation [::Gapic::Rest::TransportOperation]
638638
#
639-
# @return [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::EchoResponse>]
639+
# @return [::Google::Showcase::V1beta1::PagedExpandResponse]
640640
#
641641
# @raise [::Gapic::Rest::Error] if the REST call is aborted.
642642
#
@@ -684,10 +684,7 @@ def paged_expand_legacy request, options = nil
684684
retry_policy: @config.retry_policy
685685

686686
@echo_stub.paged_expand_legacy request, options do |result, operation|
687-
result = ::Gapic::Rest::PagedEnumerable.new @echo_stub, :paged_expand_legacy, "responses", request,
688-
result, options
689687
yield result, operation if block_given?
690-
throw :response, result
691688
end
692689
rescue ::Faraday::Error => e
693690
raise ::Gapic::Rest::Error.wrap_faraday_error e
@@ -722,10 +719,10 @@ def paged_expand_legacy request, options = nil
722719
# @param page_token [::String]
723720
# The position of the page to be returned.
724721
# @yield [result, operation] Access the result along with the TransportOperation object
725-
# @yieldparam result [::Gapic::Rest::PagedEnumerable<::String, ::Google::Showcase::V1beta1::PagedExpandResponseList>]
722+
# @yieldparam result [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::PagedExpandLegacyMappedResponse::AlphabetizedEntry>]
726723
# @yieldparam operation [::Gapic::Rest::TransportOperation]
727724
#
728-
# @return [::Gapic::Rest::PagedEnumerable<::String, ::Google::Showcase::V1beta1::PagedExpandResponseList>]
725+
# @return [::Gapic::Rest::PagedEnumerable<::Google::Showcase::V1beta1::PagedExpandLegacyMappedResponse::AlphabetizedEntry>]
729726
#
730727
# @raise [::Gapic::Rest::Error] if the REST call is aborted.
731728
#

0 commit comments

Comments
 (0)