gapic-generator 0.6.14 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/bin/protoc-gen-ruby_gapic +6 -6
  4. data/lib/gapic/file_formatter.rb +2 -1
  5. data/lib/gapic/formatting_utils.rb +4 -4
  6. data/lib/gapic/generator/version.rb +1 -1
  7. data/lib/gapic/generators/default_generator.rb +8 -5
  8. data/lib/gapic/generators/default_generator_parameters.rb +8 -4
  9. data/lib/gapic/grpc_service_config/method_config.rb +2 -1
  10. data/lib/gapic/grpc_service_config/retry_policy.rb +4 -1
  11. data/lib/gapic/grpc_service_config/service_config.rb +2 -1
  12. data/lib/gapic/path_pattern/parser.rb +1 -1
  13. data/lib/gapic/path_pattern/pattern.rb +2 -1
  14. data/lib/gapic/path_pattern/segment.rb +10 -3
  15. data/lib/gapic/presenters.rb +2 -0
  16. data/lib/gapic/presenters/field_presenter.rb +44 -0
  17. data/lib/gapic/presenters/gem_presenter.rb +4 -1
  18. data/lib/gapic/presenters/method_presenter.rb +32 -16
  19. data/lib/gapic/presenters/method_rest_presenter.rb +186 -0
  20. data/lib/gapic/presenters/package_presenter.rb +21 -3
  21. data/lib/gapic/presenters/resource_presenter.rb +2 -1
  22. data/lib/gapic/presenters/sample_presenter.rb +6 -1
  23. data/lib/gapic/presenters/service_presenter.rb +48 -10
  24. data/lib/gapic/presenters/service_rest_presenter.rb +157 -0
  25. data/lib/gapic/schema/api.rb +48 -19
  26. data/lib/gapic/schema/parameter_schema.rb +4 -1
  27. data/lib/gapic/schema/request_param_parser.rb +44 -10
  28. data/lib/gapic/schema/request_parameter.rb +5 -1
  29. data/lib/gapic/schema/wrappers.rb +31 -18
  30. data/lib/gapic/uri_template/parser.rb +4 -6
  31. data/templates/default/gem/gemspec.erb +2 -2
  32. data/templates/default/gem/readme.erb +2 -2
  33. data/templates/default/gem/rubocop.erb +2 -12
  34. data/templates/default/lib/_package.erb +11 -1
  35. data/templates/default/lib/_service.erb +31 -1
  36. data/templates/default/lib/rest/_rest.erb +11 -0
  37. data/templates/default/service/client/_config.erb +2 -2
  38. data/templates/default/service/rest.erb +6 -0
  39. data/templates/default/service/rest/client.erb +6 -0
  40. data/templates/default/service/rest/client/_client.erb +115 -0
  41. data/templates/default/service/rest/client/_config.erb +74 -0
  42. data/templates/default/service/rest/client/_requires.erb +1 -0
  43. data/templates/default/service/rest/client/method/_def.erb +18 -0
  44. data/templates/default/service/rest/client/method/def/_options_defaults.erb +14 -0
  45. data/templates/default/service/rest/client/method/def/_rescue.erb +3 -0
  46. data/templates/default/service/rest/client/method/def/_response_normal.erb +17 -0
  47. data/templates/default/service/rest/client/method/docs/_error.erb +2 -0
  48. data/templates/default/service/rest/client/method/docs/_request.erb +27 -0
  49. data/templates/default/service/rest/client/method/docs/_result.erb +6 -0
  50. data/templates/default/service/rest/grpc_transcoding.erb +6 -0
  51. data/templates/default/service/rest/grpc_transcoding/_grpc_transcoding.erb +9 -0
  52. data/templates/default/service/rest/grpc_transcoding/method/_def.erb +21 -0
  53. data/templates/default/service/rest/grpc_transcoding/method/def/_query_string_param.erb +8 -0
  54. data/templates/default/service/test/method/_normal.erb +1 -1
  55. data/templates/default/service/test/method/_server.erb +1 -1
  56. metadata +27 -8
@@ -39,7 +39,8 @@ module Gapic
39
39
  # @return [Array<Enum>] The top level enums seen across all files in
40
40
  # this API.
41
41
  class Api
42
- attr_accessor :request, :files
42
+ attr_accessor :request
43
+ attr_accessor :files
43
44
 
44
45
  # Initializes an API object with the file descriptors that represent the
45
46
  # API.
@@ -50,7 +51,7 @@ module Gapic
50
51
  # The request parameters schema to use
51
52
  # @param error_output [IO] An IO to write any errors/warnings to.
52
53
  # @param configuration [Hash] Optional override of configuration.
53
- def initialize request, parameter_schema: nil, error_output: STDERR, configuration: nil
54
+ def initialize request, parameter_schema: nil, error_output: $stderr, configuration: nil
54
55
  @request = request
55
56
  loader = Loader.new
56
57
  @files = request.proto_file.map do |fd|
@@ -143,11 +144,9 @@ module Gapic
143
144
  # @return [Array<Hash>]
144
145
  # An array of the sample file hashes.
145
146
  def samples
146
- @samples ||= begin
147
- protoc_options["samples"].to_s.split(";").flat_map do |sample_path|
148
- YAML.load_file sample_path
149
- end.compact
150
- end
147
+ @samples ||= protoc_options["samples"].to_s.split(";").flat_map do |sample_path|
148
+ YAML.load_file sample_path
149
+ end.compact
151
150
  end
152
151
 
153
152
  # Structured representation of the standalone samples configuration files.
@@ -173,10 +172,10 @@ module Gapic
173
172
  # An array of the standalone sample configuration hashes.
174
173
  def standalone_test_samples
175
174
  @standalone_test_samples ||= begin
176
- samples.select { |sample| sample["type"] == "test/samples" }
177
- .select { |sample| sample["schema_version"] == "1" || sample["schema_version"] == 1 }
178
- .map { |sample| sample["samples"] }
179
- .flatten.compact
175
+ test_samples = samples.select { |sample| sample["type"] == "test/samples" }
176
+ test_samples.select { |sample| sample["schema_version"] == "1" || sample["schema_version"] == 1 }
177
+ .map { |sample| sample["samples"] }
178
+ .flatten.compact
180
179
  end
181
180
  end
182
181
 
@@ -230,6 +229,24 @@ module Gapic
230
229
  configuration[:generate_path_helpers_output] ||= false
231
230
  end
232
231
 
232
+ # Whether to generate REST clients
233
+ def generate_rest_clients?
234
+ return false if configuration[:transports].nil?
235
+ configuration[:transports].include? "rest"
236
+ end
237
+
238
+ # Whether to generate GRPC clients
239
+ def generate_grpc_clients?
240
+ return true if configuration[:transports].nil?
241
+ configuration[:transports].include? "grpc"
242
+ end
243
+
244
+ # Whether to generate gapic metadata (drift manifest) file
245
+ # @return [Boolean]
246
+ def generate_metadata
247
+ configuration[:generate_metadata] ||= false
248
+ end
249
+
233
250
  # Whether the override_proto_namespaces parameter was given in the configuration
234
251
  def override_proto_namespaces_defined?
235
252
  configuration.key? :override_proto_namespaces
@@ -259,9 +276,7 @@ module Gapic
259
276
 
260
277
  # Parsed grpc service config
261
278
  def grpc_service_config
262
- @grpc_service_config ||= begin
263
- Gapic::GrpcServiceConfig::Parser.parse grpc_service_config_raw
264
- end
279
+ @grpc_service_config ||= Gapic::GrpcServiceConfig::Parser.parse grpc_service_config_raw
265
280
  end
266
281
 
267
282
  # Get a resource given its type string
@@ -271,10 +286,8 @@ module Gapic
271
286
 
272
287
  # Given a service, find all common services that use it as a delegate.
273
288
  def common_services_for delegate
274
- @delegate_to_common ||= begin
275
- (configuration[:common_services] || {}).each_with_object({}) do |(c, d), mapping|
276
- (mapping[d] ||= []) << c
277
- end
289
+ @delegate_to_common ||= (configuration[:common_services] || {}).each_with_object({}) do |(c, d), mapping|
290
+ (mapping[d] ||= []) << c
278
291
  end
279
292
  all_services = services
280
293
  @delegate_to_common.fetch(delegate.address.join("."), []).map do |addr|
@@ -291,6 +304,22 @@ module Gapic
291
304
  services.find { |s| s.address == addr }
292
305
  end
293
306
 
307
+ ##
308
+ # Whether configuration has an override for the wrapper gem name
309
+ # @return [Boolean]
310
+ def wrapper_gem_name_override?
311
+ configuration.key?(:overrides) &&
312
+ configuration[:overrides].key?(:wrapper_gem_name)
313
+ end
314
+
315
+ ##
316
+ # An override for the wrapper gem name in the configuration
317
+ # @return [String, Nil]
318
+ def wrapper_gem_name_override
319
+ return nil unless wrapper_gem_name_override?
320
+ configuration[:overrides][:wrapper_gem_name]
321
+ end
322
+
294
323
  private
295
324
 
296
325
  # Perform a variety of sanity checks on the data, and prints errors to
@@ -359,7 +388,7 @@ module Gapic
359
388
  # split the string on periods, but map backslash-escaped periods to
360
389
  # literal periods.
361
390
  def parse_key str
362
- str.scan(/\.|\\.|[^\.\\]+/)
391
+ str.scan(/\.|\\.|[^.\\]+/)
363
392
  .each_with_object([String.new]) do |tok, arr|
364
393
  if tok == "."
365
394
  arr.append String.new
@@ -19,7 +19,10 @@ module Gapic
19
19
  # Contains information about known parameter names
20
20
  # and the types expected to be parsed from the request options string
21
21
  class ParameterSchema
22
- attr_accessor :bool_params, :string_params, :array_params, :map_params
22
+ attr_accessor :bool_params
23
+ attr_accessor :string_params
24
+ attr_accessor :array_params
25
+ attr_accessor :map_params
23
26
 
24
27
  # Creates a schema with given alias-parameter maps
25
28
  # @param bool_params [Hash{String => String}]
@@ -67,14 +67,13 @@ module Gapic
67
67
  param_name_input = unescape param_name_input_esc
68
68
  param_type, param_config_name = param_schema.schema_name_type_for param_name_input
69
69
 
70
- if param_type == :bool && !["true", "false"].include?(unescape(value_str))
71
- error_str = "WARNING: parameter #{param_name_input} (recognised as bool " \
72
- "#{param_config_name}) will be discarded because of " \
73
- "invalid value. Value should be either 'true' or 'false'."
74
- error_output&.puts error_str
75
- end
76
-
77
- param_value = parse_param_value param_type, value_str
70
+ param_value = parse_validate_param_value(
71
+ param_type,
72
+ param_name_input,
73
+ param_config_name,
74
+ value_str,
75
+ error_output
76
+ )
78
77
 
79
78
  if param_value
80
79
  RequestParameter.new param_val_input_str, param_name_input_esc, value_str, param_config_name, param_value
@@ -92,13 +91,48 @@ module Gapic
92
91
 
93
92
  private
94
93
 
94
+ # Parses and validates param value depending on type and name
95
+ # @param param_type [Symbol] type of the parameter
96
+ # @param param_name_input [String] name of the parameter as given in the config
97
+ # @param param_config_name [String] canonical configuration name of the parameter
98
+ # @param value_str [String] string representation of parameter's value
99
+ # @param error_output [IO] Stream to write outputs to.
100
+ # @return [String,Array<String>,Hash{String => String}]
101
+ def parse_validate_param_value param_type, param_name_input, param_config_name, value_str, error_output
102
+ if param_type == :bool && !["true", "false"].include?(unescape(value_str))
103
+ error_str = "WARNING: parameter #{param_name_input} (recognised as bool " \
104
+ "#{param_config_name}) will be discarded because of " \
105
+ "invalid value. Value should be either 'true' or 'false'."
106
+ error_output&.puts error_str
107
+ end
108
+
109
+ param_value = parse_param_value param_type, value_str
110
+
111
+ if param_config_name == ":transports"
112
+ allowed_transports = ["grpc", "rest"]
113
+ noncompliant_values = param_value.reject { |pv| allowed_transports.include? pv }
114
+ if noncompliant_values.any?
115
+ noncompliant_values_list = noncompliant_values.join ", "
116
+ error_str = "WARNING: parameter #{param_name_input} (recognised as string array " \
117
+ "#{param_config_name}) will be discarded because "\
118
+ "it contains invalid values: #{noncompliant_values_list}. "\
119
+ "#{param_config_name} can only contain 'grpc' and/or 'rest' or be empty."
120
+ error_output&.puts error_str
121
+ param_value = nil
122
+ end
123
+ end
124
+
125
+ param_value
126
+ end
127
+
95
128
  # Parses param value depending on type
96
- # @param param_type [Symbol]
97
- # @param value_str [String]
129
+ # @param param_type [Symbol] type of the parameter
130
+ # @param value_str [String] string representation of parameter's value
98
131
  # @return [String,Array<String>,Hash{String => String}]
99
132
  def parse_param_value param_type, value_str
100
133
  case param_type
101
134
  when :array
135
+ return [] if value_str.empty?
102
136
  # elements in the arrays are concatenated by `;`
103
137
  # e.g. foo;bar;baz
104
138
  array_value_strings = split_by_unescaped value_str, ";"
@@ -19,7 +19,11 @@ module Gapic
19
19
  # Encapsulates information that is parsed from a single command line parameter
20
20
  # from the plugin_opt command line
21
21
  class RequestParameter
22
- attr_reader :input_str, :input_name, :input_value, :config_name, :config_value
22
+ attr_reader :input_str
23
+ attr_reader :input_name
24
+ attr_reader :input_value
25
+ attr_reader :config_name
26
+ attr_reader :config_value
23
27
 
24
28
  # @param input_str [String] the input string containing parameter and value
25
29
  # that the parameter and value were parsed from
@@ -80,7 +80,11 @@ module Gapic
80
80
  # of locations could be recorded in the future.
81
81
  class Proto
82
82
  extend Forwardable
83
- attr_reader :descriptor, :address, :docs
83
+
84
+ attr_reader :descriptor
85
+ attr_reader :address
86
+ attr_reader :docs
87
+
84
88
  attr_accessor :parent
85
89
 
86
90
  # Initializes a Proto object.
@@ -228,6 +232,7 @@ module Gapic
228
232
  # @ return [Enumerable<Method>] The methods of this service.
229
233
  class Service < Proto
230
234
  extend Forwardable
235
+
231
236
  attr_reader :methods
232
237
 
233
238
  # Initializes a Service object.
@@ -292,7 +297,9 @@ module Gapic
292
297
  # @ return [Message] The output message of this method.
293
298
  class Method < Proto
294
299
  extend Forwardable
295
- attr_reader :input, :output
300
+
301
+ attr_reader :input
302
+ attr_reader :output
296
303
 
297
304
  # Initializes a method object.
298
305
  # @param descriptor [Google::Protobuf::MethodDescriptorProto] the
@@ -370,7 +377,12 @@ module Gapic
370
377
  # this file.
371
378
  class File < Proto
372
379
  extend Forwardable
373
- attr_reader :messages, :enums, :services, :resources, :registry
380
+
381
+ attr_reader :messages
382
+ attr_reader :enums
383
+ attr_reader :services
384
+ attr_reader :resources
385
+ attr_reader :registry
374
386
 
375
387
  # Initializes a message object.
376
388
  # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
@@ -447,6 +459,7 @@ module Gapic
447
459
  # @ return [EnumValue] the EnumValues contained in this file.
448
460
  class Enum < Proto
449
461
  extend Forwardable
462
+
450
463
  attr_reader :values
451
464
 
452
465
  # Initializes a message object.
@@ -479,17 +492,6 @@ module Gapic
479
492
  class EnumValue < Proto
480
493
  extend Forwardable
481
494
 
482
- # Initializes a message object.
483
- # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
484
- # representation of this service.
485
- # @param address [Enumerable<String>] The address of the proto. See
486
- # #address for more info.
487
- # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
488
- # of the proto. See #docs for more info.
489
- def initialize descriptor, address, docs
490
- super descriptor, address, docs
491
- end
492
-
493
495
  # @!method name
494
496
  # @return [String] the unqualified name of the EnumValue
495
497
  # @!method number
@@ -520,7 +522,12 @@ module Gapic
520
522
  # @ return [Enumerable<Enum>] The nested enum declarations of a message.
521
523
  class Message < Proto
522
524
  extend Forwardable
523
- attr_reader :fields, :extensions, :resource, :nested_messages, :nested_enums
525
+
526
+ attr_reader :fields
527
+ attr_reader :extensions
528
+ attr_reader :resource
529
+ attr_reader :nested_messages
530
+ attr_reader :nested_enums
524
531
 
525
532
  # Initializes a message object.
526
533
  # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
@@ -583,8 +590,9 @@ module Gapic
583
590
  # otherwise.
584
591
  class Field < Proto
585
592
  extend Forwardable
586
- attr_reader :message, :enum
587
- attr_writer :message, :enum
593
+
594
+ attr_accessor :message
595
+ attr_accessor :enum
588
596
 
589
597
  # Initializes a message object.
590
598
  # @param descriptor [Google::Protobuf::FieldDescriptorProto] the
@@ -784,7 +792,12 @@ module Gapic
784
792
  # @return [Array<Gapic::Schema::Resource>] Parent resources
785
793
  class Resource
786
794
  extend Forwardable
787
- attr_reader :descriptor, :parsed_patterns, :parsed_parent_patterns, :parent_resources
795
+
796
+ attr_reader :descriptor
797
+ attr_reader :parsed_patterns
798
+ attr_reader :parsed_parent_patterns
799
+ attr_reader :parent_resources
800
+
788
801
  attr_accessor :parent
789
802
 
790
803
  # Initializes a resource object.
@@ -25,13 +25,11 @@ module Gapic
25
25
  # @return [Array<Segment|String>] The segments of the parsed path pattern.
26
26
  module Parser
27
27
  # @private
28
- # /((?<positional>\*\*?)|{(?<name>[^\/]+?)(?:=(?<template>.+?))?})/
28
+ # `/(?<positional>\*\*?)|{(?<name>[^\/]+?)(?:=(?<template>.+?))?}/`
29
29
  URI_TEMPLATE = %r{
30
- (
31
- (?<positional>\*\*?)
32
- |
33
- {(?<name>[^\/]+?)(?:=(?<template>.+?))?}
34
- )
30
+ (?<positional>\*\*?)
31
+ |
32
+ {(?<name>[^/]+?)(?:=(?<template>.+?))?}
35
33
  }x.freeze
36
34
 
37
35
  def self.parse_arguments uri_template
@@ -21,13 +21,13 @@ Gem::Specification.new do |gem|
21
21
  <%= gem.extra_files.inspect %>
22
22
  gem.require_paths = ["lib"]
23
23
 
24
- gem.required_ruby_version = ">= 2.4"
24
+ gem.required_ruby_version = ">= 2.5"
25
25
 
26
26
  <%- gem.dependency_list.each do |name, requirements| -%>
27
27
  gem.add_dependency <%= name.inspect %>, <%= requirements.map { |v| v.inspect }.join ", " %>
28
28
  <%- end -%>
29
29
 
30
- gem.add_development_dependency "google-style", "~> 1.24.0"
30
+ gem.add_development_dependency "google-style", "~> 1.25.1"
31
31
  gem.add_development_dependency "minitest", "~> 5.14"
32
32
  gem.add_development_dependency "minitest-focus", "~> 1.1"
33
33
  gem.add_development_dependency "minitest-rg", "~> 5.2"
@@ -28,11 +28,11 @@ response = client.<%= method.name %> request
28
28
 
29
29
  ## Supported Ruby Versions
30
30
 
31
- This library is supported on Ruby 2.4+.
31
+ This library is supported on Ruby 2.5+.
32
32
 
33
33
  Google provides official support for Ruby versions that are actively supported
34
34
  by Ruby Core—that is, Ruby versions that are either in normal maintenance or
35
- in security maintenance, and not end of life. Currently, this means Ruby 2.4
35
+ in security maintenance, and not end of life. Currently, this means Ruby 2.5
36
36
  and later. Older versions of Ruby _may_ still work, but are unsupported and not
37
37
  recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
38
38
  about the Ruby support schedule.
@@ -8,14 +8,14 @@ AllCops:
8
8
  - "proto_docs/**/*"
9
9
  - "test/**/*"
10
10
 
11
+ Layout/LineLength:
12
+ Enabled: false
11
13
  Metrics/AbcSize:
12
14
  Enabled: false
13
15
  Metrics/ClassLength:
14
16
  Enabled: false
15
17
  Metrics/CyclomaticComplexity:
16
18
  Enabled: false
17
- Metrics/LineLength:
18
- Enabled: false
19
19
  Metrics/MethodLength:
20
20
  Enabled: false
21
21
  Metrics/ModuleLength:
@@ -25,13 +25,3 @@ Metrics/PerceivedComplexity:
25
25
  Naming/FileName:
26
26
  Exclude:
27
27
  - "lib/<%= gem.name %>.rb"
28
- Naming/PredicateName:
29
- Enabled: false
30
- Style/AsciiComments:
31
- Enabled: false
32
- Style/CaseEquality:
33
- Enabled: false
34
- Style/IfUnlessModifier:
35
- Enabled: false
36
- Style/ModuleFunction:
37
- Enabled: false
@@ -6,13 +6,23 @@ require "<%= service.service_require %>"
6
6
  require "<%= package.gem.version_require %>"
7
7
  <% end %>
8
8
  <%- unless package.empty? -%>
9
+ <%- if package.services.first.generate_grpc_clients? -%>
9
10
  ##
10
- # To load this package, including all its services, and instantiate a client:
11
+ # To load this package, including all its services, and instantiate a <%= package.grpc_client_designation %>:
11
12
  #
12
13
  # require "<%= package.package_require %>"
13
14
  # client = <%= package.services.first.create_client_call %>
14
15
  #
15
16
  <%- end -%>
17
+ <%- if package.services.first.generate_rest_clients? -%>
18
+ ##
19
+ # To load this package, including all its services, and instantiate a REST client:
20
+ #
21
+ # require "<%= package.package_require %>"
22
+ # client = <%= package.services.first.rest.create_client_call %>
23
+ #
24
+ <%- end -%>
25
+ <%- end -%>
16
26
  module <%= package.module_name %>
17
27
  end
18
28
  <% @footer = capture do %>
@@ -1,4 +1,21 @@
1
1
  <%- assert_locals service -%>
2
+
3
+ <%- if service.generate_rest_clients? -%>
4
+ <% @requires = capture do %>
5
+ require "gapic/config"
6
+ require "gapic/config/method"
7
+
8
+ require "<%= service.gem.version_require %>"
9
+
10
+ <%- unless service.generic_endpoint? -%>
11
+ require "<%= service.credentials_require %>"
12
+ <%- end -%>
13
+ <%- if service.paths? -%>
14
+ require "<%= service.paths_require %>"
15
+ <%- end -%>
16
+ require "<%= service.rest.service_rest_require %>"
17
+ <% end %>
18
+ <%- else -%>
2
19
  <% @requires = capture do %>
3
20
  require "gapic/common"
4
21
  require "gapic/config"
@@ -22,13 +39,26 @@ require "<%= service.client_require %>"
22
39
  <%= indent service.doc_description, "# " %>
23
40
  #
24
41
  <%- end -%>
25
- # To load this service and instantiate a client:
42
+ <%- end -%>
43
+ <%- if service.generate_grpc_clients? -%>
44
+ # To load this service and instantiate a <%= service.grpc_client_designation %>:
26
45
  #
27
46
  # require "<%= service.service_require %>"
28
47
  # client = <%= service.create_client_call %>
29
48
  #
49
+ <%- end -%>
50
+ <%- if service.generate_rest_clients? -%>
51
+ # To load this service and instantiate a REST client:
52
+ #
53
+ # require "<%= service.service_require %>"
54
+ # client = <%= service.rest.create_client_call %>
55
+ #
56
+ <%- end -%>
30
57
  module <%= service.module_name %>
31
58
  end
59
+
60
+
61
+
32
62
  <% @footer = capture do %>
33
63
  <%= render partial: "service/helpers", locals: { service: service} -%>
34
64
  <% end %>