gapic-generator 0.8.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/lib/gapic/generator/version.rb +1 -1
  4. data/lib/gapic/generators/default_generator.rb +1 -1
  5. data/lib/gapic/generators/default_generator_parameters.rb +3 -1
  6. data/lib/gapic/presenters/gem_presenter.rb +25 -6
  7. data/lib/gapic/presenters/method/rest_pagination_info.rb +246 -0
  8. data/lib/gapic/presenters/method_presenter.rb +17 -2
  9. data/lib/gapic/presenters/method_rest_presenter.rb +75 -2
  10. data/lib/gapic/presenters/resource_presenter.rb +8 -0
  11. data/lib/gapic/presenters/service_config_presenter.rb +48 -0
  12. data/lib/gapic/presenters/service_presenter.rb +69 -0
  13. data/lib/gapic/presenters/service_rest_presenter.rb +42 -16
  14. data/lib/gapic/presenters/snippet_presenter.rb +1 -1
  15. data/lib/gapic/presenters.rb +1 -0
  16. data/lib/gapic/schema/api.rb +17 -7
  17. data/lib/gapic/schema/wrappers.rb +26 -0
  18. data/templates/default/gem/readme.erb +3 -3
  19. data/templates/default/lib/rest/_rest.erb +0 -2
  20. data/templates/default/service/client/_client.erb +18 -20
  21. data/templates/default/service/client/_config.erb +13 -14
  22. data/templates/default/service/client/_credentials.erb +2 -0
  23. data/templates/default/service/client/_operations.erb +1 -1
  24. data/templates/default/service/client/_paths.erb +1 -1
  25. data/templates/default/service/client/_self_configure_defaults.erb +2 -2
  26. data/templates/default/service/client/method/_def.erb +2 -0
  27. data/templates/default/service/client/method/def/_options_defaults.erb +3 -1
  28. data/templates/default/service/client/method/docs/_deprecated.erb +5 -0
  29. data/templates/default/service/client/method/docs/_snippets.erb +6 -0
  30. data/templates/default/service/rest/client/_client.erb +15 -24
  31. data/templates/default/service/rest/client/_config.erb +48 -0
  32. data/templates/default/service/rest/client/method/_def.erb +1 -1
  33. data/templates/default/service/rest/client/method/def/_options_defaults.erb +5 -2
  34. data/templates/default/service/rest/client/method/def/_response.erb +8 -0
  35. data/templates/default/service/rest/client/method/def/_response_lro.erb +7 -0
  36. data/templates/default/service/rest/client/method/def/_response_normal.erb +4 -15
  37. data/templates/default/service/rest/client/method/def/_response_paged.erb +7 -0
  38. data/templates/default/service/rest/client/method/docs/_result.erb +3 -3
  39. data/templates/default/service/rest/service_stub/_service_stub.erb +25 -0
  40. data/templates/default/service/rest/{grpc_transcoding/method → service_stub/grpc_transcoding_method}/_def.erb +4 -1
  41. data/templates/default/service/rest/{grpc_transcoding/method → service_stub/grpc_transcoding_method}/def/_query_string_param.erb +0 -0
  42. data/templates/default/service/rest/service_stub/method/_def.erb +20 -0
  43. data/templates/default/service/rest/service_stub/method/def/_request.erb +2 -0
  44. data/templates/default/service/rest/service_stub/method/def/_response.erb +17 -0
  45. data/templates/default/service/rest/service_stub.erb +6 -0
  46. data/templates/default/service/rest/test/client.erb +1 -1
  47. data/templates/default/service/test/client_paths.erb +1 -1
  48. metadata +17 -8
  49. data/templates/default/service/client/_self_configure_retry_policy.erb +0 -15
  50. data/templates/default/service/rest/client/_requires.erb +0 -1
  51. data/templates/default/service/rest/grpc_transcoding/_grpc_transcoding.erb +0 -9
@@ -33,6 +33,10 @@ module Gapic
33
33
  @patterns.filter!(&:useful_for_helpers?)
34
34
  end
35
35
 
36
+ def dup
37
+ ResourcePresenter.new @resource
38
+ end
39
+
36
40
  def name
37
41
  @resource.type.split("/").delete_if(&:empty?).last
38
42
  end
@@ -62,6 +66,10 @@ module Gapic
62
66
  attr_reader :pattern
63
67
  attr_reader :path_string
64
68
 
69
+ def pattern_template
70
+ @parsed_pattern.template
71
+ end
72
+
65
73
  def useful_for_helpers?
66
74
  !@parsed_pattern.positional_segments? && !@parsed_pattern.nontrivial_pattern_segments?
67
75
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2021 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Gapic
18
+ module Presenters
19
+ ##
20
+ # A presenter for grpc service config.
21
+ #
22
+ class ServiceConfigPresenter
23
+ def initialize grpc_service_config
24
+ @grpc_service_config = grpc_service_config
25
+ end
26
+
27
+ attr_reader :grpc_service_config
28
+
29
+ def retry_policy_fields
30
+ elems = []
31
+ retry_policy = grpc_service_config&.retry_policy
32
+ if retry_policy&.initial_delay_seconds
33
+ elems << "initial_delay: #{Gapic::FormattingUtils.format_number retry_policy.initial_delay_seconds}"
34
+ end
35
+ if retry_policy&.max_delay_seconds
36
+ elems << "max_delay: #{Gapic::FormattingUtils.format_number retry_policy.max_delay_seconds}"
37
+ end
38
+ if retry_policy&.multiplier
39
+ elems << "multiplier: #{Gapic::FormattingUtils.format_number retry_policy.multiplier}"
40
+ end
41
+ if retry_policy&.status_codes
42
+ elems << "retry_codes: #{retry_policy.status_codes}"
43
+ end
44
+ elems.empty? ? "{}" : "{\n #{elems.join ', '}\n}"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -50,6 +50,13 @@ module Gapic
50
50
  PackagePresenter.new @gem_presenter, @api, @service.parent.package
51
51
  end
52
52
 
53
+ ##
54
+ # @return [Boolean]
55
+ #
56
+ def is_deprecated?
57
+ @service.is_deprecated?
58
+ end
59
+
53
60
  ##
54
61
  # @return [Enumerable<Gapic::Presenters::MethodPresenter>]
55
62
  #
@@ -252,6 +259,34 @@ module Gapic
252
259
  @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
253
260
  end
254
261
 
262
+ ##
263
+ # Deduplicate resource presenters by combining resources with the same
264
+ # name. If multiple resources have the same name (though possibly
265
+ # different namespaces, e.g. `location.googleapis.com/Location` vs
266
+ # `documentai.googleapis.com/Location`), this combines (and dedups) their
267
+ # patterns into a single resource presenter.
268
+ #
269
+ # Used for generating path helpers while avoiding duplicate method names.
270
+ #
271
+ def deduped_references
272
+ @deduped_references ||= begin
273
+ hash = {}
274
+ references.each do |resource|
275
+ if hash.key? resource.name
276
+ existing = hash[resource.name]
277
+ resource.patterns.each do |pat|
278
+ unless existing.patterns.any? { |epat| epat.pattern_template == pat.pattern_template }
279
+ existing.patterns << pat
280
+ end
281
+ end
282
+ else
283
+ hash[resource.name] = resource.dup
284
+ end
285
+ end
286
+ hash.values
287
+ end
288
+ end
289
+
255
290
  def paths?
256
291
  references.any?
257
292
  end
@@ -361,6 +396,10 @@ module Gapic
361
396
  @api.grpc_service_config.service_level_configs[grpc_full_name]
362
397
  end
363
398
 
399
+ def service_config_presenter
400
+ ServiceConfigPresenter.new grpc_service_config
401
+ end
402
+
364
403
  ##
365
404
  # The short proto name for this service
366
405
  #
@@ -405,6 +444,36 @@ module Gapic
405
444
  generate_rest_clients? ? "GRPC client" : "client"
406
445
  end
407
446
 
447
+ ##
448
+ # The method to use for quick start samples. Normally this is simply the
449
+ # first non-client-streaming method defined, but it can be overridden via
450
+ # a gem config.
451
+ #
452
+ # @return [Gapic::Presenters::MethodPresenter]
453
+ #
454
+ def quick_start_method
455
+ gem_config = @api.configuration[:gem]
456
+ preferred_method = gem_config[:quick_start_method] if gem_config
457
+ result = methods.find { |meth| meth.name == preferred_method } if preferred_method
458
+ result || methods.find { |meth| !meth.client_streaming? }
459
+ end
460
+
461
+ ##
462
+ # Returns this service presenter if there is a grpc client. Otherwise,
463
+ # returns the corresponding rest service presenter if there isn't a grpc
464
+ # client but there is a rest client. Otherwise, returns nil if there is
465
+ # neither client.
466
+ #
467
+ # @return [ServicePresenter,ServiceRestPresenter,nil]
468
+ #
469
+ def usable_service_presenter
470
+ if @api.generate_grpc_clients?
471
+ self
472
+ elsif @api.generate_rest_clients? && methods_rest_bindings?
473
+ rest
474
+ end
475
+ end
476
+
408
477
  private
409
478
 
410
479
  def default_config key
@@ -43,6 +43,34 @@ module Gapic
43
43
  fix_namespace api, "#{main_service.service_name_full}::Rest"
44
44
  end
45
45
 
46
+ ##
47
+ # @return [String]
48
+ #
49
+ def service_stub_name
50
+ "ServiceStub"
51
+ end
52
+
53
+ ##
54
+ # @return [String]
55
+ #
56
+ def service_stub_name_full
57
+ fix_namespace api, "#{service_name_full}::#{service_stub_name}"
58
+ end
59
+
60
+ ##
61
+ # @return [String]
62
+ #
63
+ def service_stub_require
64
+ ruby_file_path api, service_stub_name_full
65
+ end
66
+
67
+ ##
68
+ # @return [String]
69
+ #
70
+ def service_stub_file_path
71
+ "#{service_stub_require}.rb"
72
+ end
73
+
46
74
  ##
47
75
  # @return [String]
48
76
  #
@@ -95,39 +123,37 @@ module Gapic
95
123
  ##
96
124
  # @return [String]
97
125
  #
98
- def transcoding_helper_name
99
- "GrpcTranscoding"
126
+ def test_client_file_path
127
+ main_service.service_file_path.sub ".rb", "_test.rb"
100
128
  end
101
129
 
102
130
  ##
103
131
  # @return [String]
104
132
  #
105
- def transcoding_helper_name_full
106
- fix_namespace api, "#{service_name_full}::#{transcoding_helper_name}"
133
+ def credentials_class_xref
134
+ main_service.credentials_class_xref
107
135
  end
108
136
 
109
137
  ##
110
138
  # @return [String]
111
139
  #
112
- def transcoding_helper_require
113
- ruby_file_path api, transcoding_helper_name_full
140
+ def configure_client_call
141
+ "#{client_name_full}.configure"
114
142
  end
115
143
 
116
144
  ##
117
- # @return [String]
145
+ # The method to use for quick start samples. Normally this is simply the
146
+ # first non-client-streaming method defined, but it can be overridden via
147
+ # a gem config.
118
148
  #
119
- def transcoding_helper_file_path
120
- "#{transcoding_helper_require}.rb"
121
- end
122
-
123
- ##
124
- # @return [String]
149
+ # @return [Gapic::Presenters::MethodRestPresenter] if there is a method
150
+ # appropriatke for quick start
151
+ # @return [nil] if there is no method appropriate for quick start
125
152
  #
126
- def test_client_file_path
127
- main_service.service_file_path.sub ".rb", "_test.rb"
153
+ def quick_start_method
154
+ main_service.quick_start_method&.rest
128
155
  end
129
156
 
130
-
131
157
  private
132
158
 
133
159
  # @return [Gapic::Presenters::ServicePresenter]
@@ -52,7 +52,7 @@ module Gapic
52
52
  end
53
53
 
54
54
  def require_path
55
- @method_presenter.service.service_require
55
+ @method_presenter.service.package.package_require
56
56
  end
57
57
 
58
58
  def client_type
@@ -26,6 +26,7 @@ require "gapic/presenters/package_presenter"
26
26
  require "gapic/presenters/resource_presenter"
27
27
  require "gapic/presenters/sample_presenter"
28
28
  require "gapic/presenters/service_presenter"
29
+ require "gapic/presenters/service_config_presenter"
29
30
  require "gapic/presenters/service_rest_presenter"
30
31
  require "gapic/presenters/snippet_presenter"
31
32
 
@@ -250,6 +250,11 @@ module Gapic
250
250
  configuration[:generate_standalone_snippets] ||= false
251
251
  end
252
252
 
253
+ # Whether to generate inline documentation snippets
254
+ def generate_yardoc_snippets?
255
+ configuration[:generate_yardoc_snippets] ||= false
256
+ end
257
+
253
258
  # Whether to generate gapic metadata (drift manifest) file
254
259
  # @return [Boolean]
255
260
  def generate_metadata
@@ -323,7 +328,7 @@ module Gapic
323
328
 
324
329
  ##
325
330
  # An override for the wrapper gem name in the configuration
326
- # @return [String, Nil]
331
+ # @return [String, nil]
327
332
  def wrapper_gem_name_override
328
333
  return nil unless wrapper_gem_name_override?
329
334
  return nil if configuration[:overrides][:wrapper_gem_name].nil?
@@ -354,9 +359,14 @@ module Gapic
354
359
  # * A mapping from resource type to resource wrapper is returned.
355
360
  def analyze_resources
356
361
  # In order to set parent_resources, we first populate a mapping from
357
- # parsed pattern to resource mapping (in the patterns variable). This
358
- # is done in one pass along with populating the resource type mapping.
359
- # Then, we go through all resources again, get its expected parent
362
+ # parsed pattern to resources that use it (in the patterns variable).
363
+ # Note that there may be multiple resources associated with a pattern.
364
+ # (This is uncommon, but one example is monitoring v3 which uses
365
+ # "projects/*" for its workspace type as well as inheriting the common
366
+ # project type. We thus map each pattern to an array of resources.)
367
+ # Constructing the patterns mapping is done in one pass along with
368
+ # populating the type mapping (which maps only to single resources.)
369
+ # Then, we go through all resources again, get each's expected parent
360
370
  # patterns, and anything that shows up in the patterns mapping is taken
361
371
  # to be a parent.
362
372
  types = {}
@@ -367,8 +377,8 @@ module Gapic
367
377
  end
368
378
  types.each do |_type, resource|
369
379
  parents = resource.parsed_parent_patterns
370
- .map { |pat| patterns[pat] }
371
- .compact.uniq
380
+ .flat_map { |pat| Array(patterns[pat]) }
381
+ .uniq
372
382
  resource.parent_resources.replace parents
373
383
  end
374
384
  types
@@ -377,7 +387,7 @@ module Gapic
377
387
  def populate_resource_lookups resource, types, patterns
378
388
  types[resource.type] = resource
379
389
  resource.parsed_patterns.each do |pat|
380
- patterns[pat] = resource
390
+ ((patterns[pat] ||= []) << resource).uniq!
381
391
  end
382
392
  end
383
393
 
@@ -268,6 +268,12 @@ module Gapic
268
268
  parent.ruby_package
269
269
  end
270
270
 
271
+ # @return [Boolean] True if this service is marked as deprecated, false
272
+ # otherwise.
273
+ def is_deprecated?
274
+ options[:deprecated] if options
275
+ end
276
+
271
277
  # @return [Array<Google::Api::ResourceDescriptor>] A representation of the resource.
272
278
  # This is generally intended to be attached to the "name" field.
273
279
  # See `google/api/resource.proto`.
@@ -336,6 +342,12 @@ module Gapic
336
342
  options[:".google.longrunning.operation_info"] if options
337
343
  end
338
344
 
345
+ # @return [Boolean] True if this method is marked as deprecated, false
346
+ # otherwise.
347
+ def is_deprecated?
348
+ options[:deprecated] if options
349
+ end
350
+
339
351
  # @return [Google::Api::HttpRule] The HTTP bindings for this method. See
340
352
  # `google/api/http.proto`.
341
353
  def http
@@ -641,6 +653,20 @@ module Gapic
641
653
  false
642
654
  end
643
655
 
656
+ # @return [Field, nil] a key field for this map
657
+ # or nil if this field is not a map
658
+ def map_key_field
659
+ return nil? unless map?
660
+ @message.fields.find { |f| f.name == "key" }
661
+ end
662
+
663
+ # @return [Field, nil] a value field for this map
664
+ # or nil if this field is not a map
665
+ def map_val_field
666
+ return nil? unless map?
667
+ @message.fields.find { |f| f.name == "value" }
668
+ end
669
+
644
670
  # @return [String] A reference to another resource message or resource
645
671
  # definition. See `google/api/resource.proto`.
646
672
  def resource_reference
@@ -16,12 +16,12 @@ $ gem install <%= gem.name %>
16
16
 
17
17
  ```ruby
18
18
  require "<%= gem.entrypoint_require %>"
19
- <%- service = gem.first_non_common_service -%>
20
- <%- method = service&.methods.first -%>
19
+ <%- service = gem.quick_start_service -%>
20
+ <%- method = service&.quick_start_method -%>
21
21
  <%- if service && method -%>
22
22
 
23
23
  client = <%= service.create_client_call %>
24
- request = my_create_request
24
+ request = <%= method.request_type %>.new # (request fields as keyword arguments...)
25
25
  response = client.<%= method.name %> request
26
26
  <%- end -%>
27
27
  ```
@@ -1,7 +1,5 @@
1
1
  <%- assert_locals service -%>
2
2
  <% @requires = capture do %>
3
- require "gapic/rest"
4
- require "<%= service.rest.transcoding_helper_require %>"
5
3
  require "<%= service.rest.client_require %>"
6
4
  <% end %>
7
5
  module <%= service.module_name %>
@@ -23,14 +23,17 @@ class <%= service.client_name %>
23
23
  #
24
24
  # See {<%= service.client_name_full %>::Configuration}
25
25
  # for a description of the configuration fields.
26
+ <%- if service.is_deprecated? -%>
26
27
  #
27
- # ## Example
28
+ # @deprecated This service is deprecated and may be removed in the next major version update.
29
+ <%- end -%>
28
30
  #
29
- # To modify the configuration for all <%= service.name %> clients:
31
+ # @example
30
32
  #
31
- # <%= service.client_name_full %>.configure do |config|
32
- # config.timeout = 10.0
33
- # end
33
+ # # Modify the configuration for all <%= service.name %> clients
34
+ # <%= service.client_name_full %>.configure do |config|
35
+ # config.timeout = 10.0
36
+ # end
34
37
  #
35
38
  # @yield [config] Configure the <%= service.client_name %> client.
36
39
  # @yieldparam config [<%= service.client_name %>::Configuration]
@@ -64,19 +67,15 @@ class <%= service.client_name %>
64
67
  ##
65
68
  # Create a new <%= service.name %> client object.
66
69
  #
67
- # ## Examples
68
- #
69
- # To create a new <%= service.name %> client with the default
70
- # configuration:
71
- #
72
- # client = <%= service.client_name_full %>.new
70
+ # @example
73
71
  #
74
- # To create a new <%= service.name %> client with a custom
75
- # configuration:
72
+ # # Create a client using the default configuration
73
+ # client = <%= service.client_name_full %>.new
76
74
  #
77
- # client = <%= service.client_name_full %>.new do |config|
78
- # config.timeout = 10.0
79
- # end
75
+ # # Create a client using a custom configuration
76
+ # client = <%= service.client_name_full %>.new do |config|
77
+ # config.timeout = 10.0
78
+ # end
80
79
  #
81
80
  # @yield [config] Configure the <%= service.name %> client.
82
81
  # @yieldparam config [<%= service.client_name %>::Configuration]
@@ -97,14 +96,13 @@ class <%= service.client_name %>
97
96
  # Create credentials
98
97
  credentials = @config.credentials
99
98
  <%- unless service.generic_endpoint? -%>
100
- # Use self-signed JWT if the scope and endpoint are unchanged from default,
99
+ # Use self-signed JWT if the endpoint is unchanged from default,
101
100
  # but only if the default endpoint does not have a region prefix.
102
- enable_self_signed_jwt = @config.scope == <%= service.client_name %>.configure.scope &&
103
- @config.endpoint == <%= service.client_name %>.configure.endpoint &&
101
+ enable_self_signed_jwt = @config.endpoint == <%= service.client_name %>.configure.endpoint &&
104
102
  !@config.endpoint.split(".").first.include?("-")
105
103
  credentials ||= Credentials.default scope: @config.scope,
106
104
  enable_self_signed_jwt: enable_self_signed_jwt
107
- if credentials.is_a?(String) || credentials.is_a?(Hash)
105
+ if credentials.is_a?(::String) || credentials.is_a?(::Hash)
108
106
  credentials = Credentials.new credentials, scope: @config.scope
109
107
  end
110
108
  <%- end -%>