gapic-generator 0.6.15 → 0.7.5

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 (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/bin/protoc-gen-ruby_gapic +5 -5
  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/runner.rb +2 -1
  26. data/lib/gapic/schema/api.rb +48 -19
  27. data/lib/gapic/schema/parameter_schema.rb +4 -1
  28. data/lib/gapic/schema/request_param_parser.rb +44 -10
  29. data/lib/gapic/schema/request_parameter.rb +5 -1
  30. data/lib/gapic/schema/wrappers.rb +31 -18
  31. data/lib/gapic/uri_template/parser.rb +4 -6
  32. data/lib/google/protobuf/any.pb.rb +1 -1
  33. data/lib/google/protobuf/compiler/plugin.pb.rb +9 -6
  34. data/lib/google/protobuf/descriptor.pb.rb +2 -2
  35. data/lib/google/protobuf/empty.pb.rb +1 -1
  36. data/templates/default/gem/gemspec.erb +2 -2
  37. data/templates/default/gem/readme.erb +2 -2
  38. data/templates/default/gem/rubocop.erb +2 -12
  39. data/templates/default/lib/_package.erb +11 -1
  40. data/templates/default/lib/_service.erb +31 -1
  41. data/templates/default/lib/rest/_rest.erb +11 -0
  42. data/templates/default/service/client/_config.erb +2 -2
  43. data/templates/default/service/rest.erb +6 -0
  44. data/templates/default/service/rest/client.erb +6 -0
  45. data/templates/default/service/rest/client/_client.erb +115 -0
  46. data/templates/default/service/rest/client/_config.erb +74 -0
  47. data/templates/default/service/rest/client/_requires.erb +1 -0
  48. data/templates/default/service/rest/client/method/_def.erb +18 -0
  49. data/templates/default/service/rest/client/method/def/_options_defaults.erb +14 -0
  50. data/templates/default/service/rest/client/method/def/_rescue.erb +3 -0
  51. data/templates/default/service/rest/client/method/def/_response_normal.erb +17 -0
  52. data/templates/default/service/rest/client/method/docs/_error.erb +2 -0
  53. data/templates/default/service/rest/client/method/docs/_request.erb +27 -0
  54. data/templates/default/service/rest/client/method/docs/_result.erb +6 -0
  55. data/templates/default/service/rest/grpc_transcoding.erb +6 -0
  56. data/templates/default/service/rest/grpc_transcoding/_grpc_transcoding.erb +9 -0
  57. data/templates/default/service/rest/grpc_transcoding/method/_def.erb +21 -0
  58. data/templates/default/service/rest/grpc_transcoding/method/def/_query_string_param.erb +8 -0
  59. data/templates/default/service/test/method/_normal.erb +1 -1
  60. data/templates/default/service/test/method/_server.erb +1 -1
  61. metadata +27 -8
data/lib/gapic/runner.rb CHANGED
@@ -65,7 +65,8 @@ module Gapic
65
65
 
66
66
  # Create and write the response
67
67
  response = Google::Protobuf::Compiler::CodeGeneratorResponse.new file: output_files
68
- response.supported_features = Google::Protobuf::Compiler::CodeGeneratorResponse::FEATURE_PROTO3_OPTIONAL
68
+ feature_set = Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL.to_i
69
+ response.supported_features = feature_set
69
70
  response
70
71
  end
71
72
 
@@ -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,7 +21,7 @@ module Google
21
21
  set_option :java_package, "com.google.protobuf"
22
22
  set_option :java_outer_classname, "AnyProto"
23
23
  set_option :java_multiple_files, true
24
- set_option :go_package, "github.com/golang/protobuf/ptypes/any"
24
+ set_option :go_package, "google.golang.org/protobuf/types/known/anypb"
25
25
  set_option :objc_class_prefix, "GPB"
26
26
  set_option :csharp_namespace, "Google.Protobuf.WellKnownTypes"
27
27
 
@@ -1,8 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  ##
4
- # This file was auto-generated initially but now is hand-edited to add support
5
- # for response features and proto3_optional
4
+ # This file is auto-generated. DO NOT EDIT!
6
5
  #
7
6
  require 'protobuf'
8
7
 
@@ -23,6 +22,11 @@ module Google
23
22
  class Version < ::Protobuf::Message; end
24
23
  class CodeGeneratorRequest < ::Protobuf::Message; end
25
24
  class CodeGeneratorResponse < ::Protobuf::Message
25
+ class Feature < ::Protobuf::Enum
26
+ define :FEATURE_NONE, 0
27
+ define :FEATURE_PROTO3_OPTIONAL, 1
28
+ end
29
+
26
30
  class File < ::Protobuf::Message; end
27
31
 
28
32
  end
@@ -34,7 +38,7 @@ module Google
34
38
  #
35
39
  set_option :java_package, "com.google.protobuf.compiler"
36
40
  set_option :java_outer_classname, "PluginProtos"
37
- set_option :go_package, "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go"
41
+ set_option :go_package, "google.golang.org/protobuf/types/pluginpb"
38
42
 
39
43
 
40
44
  ##
@@ -55,16 +59,15 @@ module Google
55
59
  end
56
60
 
57
61
  class CodeGeneratorResponse
58
- FEATURE_PROTO3_OPTIONAL = 1
59
-
60
62
  class File
61
63
  optional :string, :name, 1
62
64
  optional :string, :insertion_point, 2
63
65
  optional :string, :content, 15
66
+ optional ::Google::Protobuf::GeneratedCodeInfo, :generated_code_info, 16
64
67
  end
65
68
 
66
69
  optional :string, :error, 1
67
- optional :int32, :supported_features, 2
70
+ optional :uint64, :supported_features, 2
68
71
  repeated ::Google::Protobuf::Compiler::CodeGeneratorResponse::File, :file, 15
69
72
  end
70
73
 
@@ -121,7 +121,7 @@ module Google
121
121
  set_option :java_package, "com.google.protobuf"
122
122
  set_option :java_outer_classname, "DescriptorProtos"
123
123
  set_option :optimize_for, ::Google::Protobuf::FileOptions::OptimizeMode::SPEED
124
- set_option :go_package, "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor"
124
+ set_option :go_package, "google.golang.org/protobuf/types/descriptorpb"
125
125
  set_option :cc_enable_arenas, true
126
126
  set_option :objc_class_prefix, "GPB"
127
127
  set_option :csharp_namespace, "Google.Protobuf.Reflection"
@@ -245,7 +245,7 @@ module Google
245
245
  optional :bool, :py_generic_services, 18, :default => false
246
246
  optional :bool, :php_generic_services, 42, :default => false
247
247
  optional :bool, :deprecated, 23, :default => false
248
- optional :bool, :cc_enable_arenas, 31, :default => false
248
+ optional :bool, :cc_enable_arenas, 31, :default => true
249
249
  optional :string, :objc_class_prefix, 36
250
250
  optional :string, :csharp_namespace, 37
251
251
  optional :string, :swift_prefix, 39
@@ -21,7 +21,7 @@ module Google
21
21
  set_option :java_package, "com.google.protobuf"
22
22
  set_option :java_outer_classname, "EmptyProto"
23
23
  set_option :java_multiple_files, true
24
- set_option :go_package, "github.com/golang/protobuf/ptypes/empty"
24
+ set_option :go_package, "google.golang.org/protobuf/types/known/emptypb"
25
25
  set_option :cc_enable_arenas, true
26
26
  set_option :objc_class_prefix, "GPB"
27
27
  set_option :csharp_namespace, "Google.Protobuf.WellKnownTypes"