gapic-generator 0.1.4 → 0.2.3

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +48 -0
  3. data/lib/gapic/formatting_utils.rb +65 -14
  4. data/lib/gapic/generator/version.rb +1 -1
  5. data/lib/gapic/generators/base_generator.rb +0 -8
  6. data/lib/gapic/generators/default_generator.rb +2 -4
  7. data/lib/gapic/helpers/filepath_helper.rb +45 -0
  8. data/lib/gapic/helpers/namespace_helper.rb +51 -0
  9. data/lib/gapic/presenters.rb +44 -0
  10. data/{templates/default/helpers → lib/gapic}/presenters/enum_presenter.rb +19 -14
  11. data/{templates/default/helpers → lib/gapic}/presenters/enum_value_presenter.rb +19 -12
  12. data/lib/gapic/presenters/field_presenter.rb +154 -0
  13. data/lib/gapic/presenters/file_presenter.rb +59 -0
  14. data/lib/gapic/presenters/gem_presenter.rb +172 -0
  15. data/lib/gapic/presenters/message_presenter.rb +73 -0
  16. data/lib/gapic/presenters/method_presenter.rb +307 -0
  17. data/lib/gapic/presenters/package_presenter.rb +72 -0
  18. data/lib/gapic/presenters/resource_presenter.rb +99 -0
  19. data/lib/gapic/presenters/sample_presenter.rb +84 -0
  20. data/lib/gapic/presenters/service_presenter.rb +298 -0
  21. data/lib/gapic/resource_lookup.rb +8 -1
  22. data/lib/gapic/schema/api.rb +24 -0
  23. data/lib/gapic/schema/wrappers.rb +25 -2
  24. data/templates/default/gem/gemspec.erb +1 -1
  25. data/templates/default/gem/readme.erb +2 -2
  26. data/templates/default/gem/rubocop.erb +2 -0
  27. data/templates/default/helpers/filepath_helper.rb +2 -21
  28. data/templates/default/helpers/namespace_helper.rb +2 -27
  29. data/templates/default/layouts/_ruby.erb +1 -3
  30. data/templates/default/service/client/_client.erb +7 -1
  31. data/templates/default/service/client/_paths.erb +1 -0
  32. data/templates/default/service/client/method/def/_options_defaults.erb +1 -1
  33. data/templates/default/service/client/method/def/_response_normal.erb +1 -1
  34. metadata +16 -14
  35. data/templates/default/helpers/presenter_helper.rb +0 -24
  36. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  37. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  38. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  39. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  40. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  41. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  42. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  43. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  44. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 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
+ require "active_support/inflector"
18
+
19
+ module Gapic
20
+ module Presenters
21
+ ##
22
+ # A presenter for samples.
23
+ #
24
+ class SamplePresenter
25
+ def initialize api, sample_config
26
+ @api = api
27
+ @sample_config = sample_config
28
+ end
29
+
30
+ def description
31
+ @sample_config["description"]
32
+ end
33
+
34
+ def input_parameters
35
+ @sample_config["request"].select { |f| f.key? "input_parameter" }.map { |f| RequestField.new f }
36
+ end
37
+
38
+ def fields
39
+ dotted_hash = @sample_config["request"].each_with_object({}) do |f, memo|
40
+ memo[f["field"]] = f
41
+ end
42
+ dotted_hash.each_with_object({}) do |(path, f), memo|
43
+ parts = path.split "."
44
+ leaf_hash = parts[0...-1].inject(memo) { |h, k| h[k] ||= {} }
45
+ leaf_hash[parts.last] = RequestField.new f
46
+ end
47
+ end
48
+
49
+ def kwargs
50
+ fields.keys.map { |k| "#{k}: #{k}" }.join ", "
51
+ end
52
+
53
+ def response_raw
54
+ @sample_config["response"]
55
+ end
56
+
57
+ ##
58
+ # Representation of a request field.
59
+ #
60
+ class RequestField
61
+ attr_reader :field, :value, :input_parameter, :comment, :value_is_file
62
+ def initialize field
63
+ @field = field["field"]
64
+ @value = convert field["value"]
65
+ @input_parameter = field["input_parameter"]
66
+ @comment = field["comment"]
67
+ @comment = nil if @comment&.empty?
68
+ @value_is_file = field["value_is_file"]
69
+ end
70
+
71
+ protected
72
+
73
+ def convert val
74
+ if val.is_a? String
75
+ return ":#{val}" if val =~ /\A[A-Z_0-9]+\z/ # print constants as symbols
76
+ val.inspect
77
+ else
78
+ val.to_s
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,298 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 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
+ require "active_support/inflector"
18
+ require "gapic/helpers/filepath_helper"
19
+ require "gapic/helpers/namespace_helper"
20
+
21
+ module Gapic
22
+ module Presenters
23
+ ##
24
+ # A presenter for proto services.
25
+ #
26
+ class ServicePresenter
27
+ include Gapic::Helpers::FilepathHelper
28
+ include Gapic::Helpers::NamespaceHelper
29
+
30
+ def initialize api, service, parent_service: nil
31
+ @api = api
32
+ @service = service
33
+ @parent_service = parent_service
34
+ end
35
+
36
+ def gem
37
+ GemPresenter.new @api
38
+ end
39
+
40
+ def file
41
+ FilePresenter.new @api, @service.parent
42
+ end
43
+
44
+ def package
45
+ PackagePresenter.new @api, @service.parent.package
46
+ end
47
+
48
+ def methods
49
+ @methods ||= begin
50
+ @service.methods.map { |m| MethodPresenter.new @api, m }
51
+ end
52
+ end
53
+
54
+ def address
55
+ @service.address
56
+ end
57
+
58
+ def namespace
59
+ return @service.ruby_package if @service.ruby_package.present?
60
+
61
+ namespace = ruby_namespace_for_address @service.address[0...-1]
62
+ fix_namespace @api, namespace
63
+ end
64
+
65
+ def version
66
+ ActiveSupport::Inflector.camelize @service.address[-2]
67
+ end
68
+
69
+ def doc_description disable_xrefs: false
70
+ @service.docs_leading_comments disable_xrefs: disable_xrefs
71
+ end
72
+
73
+ def name
74
+ @service.name
75
+ end
76
+
77
+ def proto_service_name_full
78
+ fix_namespace @api, "#{namespace}::#{name}"
79
+ end
80
+
81
+ def proto_service_file_path
82
+ @service.parent.name.sub ".proto", "_pb.rb"
83
+ end
84
+
85
+ def proto_service_file_name
86
+ proto_service_file_path.split("/").last
87
+ end
88
+
89
+ def proto_service_require
90
+ proto_service_file_path.sub ".rb", ""
91
+ end
92
+
93
+ def proto_services_file_path
94
+ @service.parent.name.sub ".proto", "_services_pb.rb"
95
+ end
96
+
97
+ def proto_services_file_name
98
+ proto_services_file_path.split("/").last
99
+ end
100
+
101
+ def proto_services_require
102
+ proto_services_file_path.sub ".rb", ""
103
+ end
104
+
105
+ def proto_service_stub_name_full
106
+ "#{proto_service_name_full}::Stub"
107
+ end
108
+
109
+ def service_file_path
110
+ service_require + ".rb"
111
+ end
112
+
113
+ def service_file_name
114
+ service_file_path.split("/").last
115
+ end
116
+
117
+ def service_require
118
+ ruby_file_path @api, proto_service_name_full
119
+ end
120
+
121
+ def client_name
122
+ "Client"
123
+ end
124
+
125
+ def client_name_full
126
+ fix_namespace @api, "#{proto_service_name_full}::#{client_name}"
127
+ end
128
+
129
+ def create_client_call
130
+ "#{client_name_full}.new"
131
+ end
132
+
133
+ def configure_client_call
134
+ "#{client_name_full}.configure"
135
+ end
136
+
137
+ def client_require
138
+ ruby_file_path @api, client_name_full
139
+ end
140
+
141
+ def client_file_path
142
+ client_require + ".rb"
143
+ end
144
+
145
+ def client_file_name
146
+ client_file_path.split("/").last
147
+ end
148
+
149
+ def client_endpoint
150
+ return @parent_service.client_endpoint if @parent_service
151
+ @service.host || default_config(:default_host) || "localhost"
152
+ end
153
+
154
+ def client_scopes
155
+ @service.scopes || default_config(:oauth_scopes)
156
+ end
157
+
158
+ def client_proto_name
159
+ @service.address.join "."
160
+ end
161
+
162
+ def credentials_name
163
+ "Credentials"
164
+ end
165
+
166
+ def credentials_name_full
167
+ fix_namespace @api, "#{proto_service_name_full}::#{credentials_name}"
168
+ end
169
+
170
+ def credentials_class_xref
171
+ "{#{credentials_name_full}}"
172
+ end
173
+
174
+ def credentials_file_path
175
+ credentials_require + ".rb"
176
+ end
177
+
178
+ def credentials_file_name
179
+ credentials_file_path.split("/").last
180
+ end
181
+
182
+ def credentials_require
183
+ ruby_file_path @api, credentials_name_full
184
+ end
185
+
186
+ def helpers_file_path
187
+ helpers_require + ".rb"
188
+ end
189
+
190
+ def helpers_file_name
191
+ "helpers.rb"
192
+ end
193
+
194
+ def helpers_require
195
+ ruby_file_path @api, "#{proto_service_name_full}::Helpers"
196
+ end
197
+
198
+ def references
199
+ @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
200
+ end
201
+
202
+ def paths?
203
+ references.any?
204
+ end
205
+
206
+ def paths_name
207
+ "Paths"
208
+ end
209
+
210
+ def paths_name_full
211
+ fix_namespace @api, "#{proto_service_name_full}::#{paths_name}"
212
+ end
213
+
214
+ def paths_file_path
215
+ paths_require + ".rb"
216
+ end
217
+
218
+ def paths_file_name
219
+ paths_file_path.split("/").last
220
+ end
221
+
222
+ def paths_require
223
+ ruby_file_path @api, "#{proto_service_name_full}::#{paths_name}"
224
+ end
225
+
226
+ def test_client_file_path
227
+ service_file_path.sub ".rb", "_test.rb"
228
+ end
229
+
230
+ def test_client_operations_file_path
231
+ service_file_path.sub ".rb", "_operations_test.rb"
232
+ end
233
+
234
+ def stub_name
235
+ "#{ActiveSupport::Inflector.underscore name}_stub"
236
+ end
237
+
238
+ def lro?
239
+ methods.find(&:lro?)
240
+ end
241
+
242
+ def lro_client_var
243
+ "operations_client"
244
+ end
245
+
246
+ def lro_client_ivar
247
+ "@#{lro_client_var}"
248
+ end
249
+
250
+ def operations_name
251
+ "Operations"
252
+ end
253
+
254
+ def operations_name_full
255
+ fix_namespace @api, "#{proto_service_name_full}::#{operations_name}"
256
+ end
257
+
258
+ def operations_file_path
259
+ operations_require + ".rb"
260
+ end
261
+
262
+ def operations_file_name
263
+ operations_file_path.split("/").last
264
+ end
265
+
266
+ def operations_require
267
+ ruby_file_path @api, "#{proto_service_name_full}::#{operations_name}"
268
+ end
269
+
270
+ def lro_service
271
+ lro = @service.parent.parent.files.find { |file| file.name == "google/longrunning/operations.proto" }
272
+ return ServicePresenter.new @api, lro.services.first, parent_service: self unless lro.nil?
273
+ end
274
+
275
+ def config_channel_args
276
+ { "grpc.service_config_disable_resolution" => 1 }
277
+ end
278
+
279
+ def grpc_service_config
280
+ return unless @api.grpc_service_config&.service_level_configs&.key? grpc_full_name
281
+ @api.grpc_service_config.service_level_configs[grpc_full_name]
282
+ end
283
+
284
+ def grpc_full_name
285
+ @service.address.join "."
286
+ end
287
+
288
+ private
289
+
290
+ def default_config key
291
+ return unless @service.parent.parent.configuration[:defaults]
292
+ return unless @service.parent.parent.configuration[:defaults][:service]
293
+
294
+ @service.parent.parent.configuration[:defaults][:service][key]
295
+ end
296
+ end
297
+ end
298
+ end
@@ -42,7 +42,14 @@ module Gapic
42
42
  def service_resource_types
43
43
  @service_resource_types ||= begin
44
44
  @service.methods.flat_map do |method|
45
- message_resource_types method.input
45
+ input_resource_types = message_resource_types method.input
46
+
47
+ if @api.generate_path_helpers_output?
48
+ output_resource_types = message_resource_types method.output
49
+ input_resource_types + output_resource_types
50
+ else
51
+ input_resource_types
52
+ end
46
53
  end.uniq
47
54
  end
48
55
  end
@@ -53,6 +53,14 @@ module Gapic
53
53
  @files.each { |f| f.parent = self }
54
54
  end
55
55
 
56
+ def containing_api
57
+ self
58
+ end
59
+
60
+ def containing_file
61
+ nil
62
+ end
63
+
56
64
  def lookup address
57
65
  address = address.join "." if address.is_a? Array
58
66
  @files.each do |f|
@@ -200,6 +208,22 @@ module Gapic
200
208
  end
201
209
  end
202
210
 
211
+ # Whether the generate_path_helpers_output parameter was given in the configuration
212
+ def generate_path_helpers_output_defined?
213
+ configuration.key? :generate_path_helpers_output
214
+ end
215
+
216
+ # Sets the generate_path_helpers_output parameter in the configuration
217
+ def generate_path_helpers_output= value
218
+ configuration[:generate_path_helpers_output] = value
219
+ end
220
+
221
+ # Whether to generate path helpers for output as well as input messages
222
+ def generate_path_helpers_output?
223
+ # if not set in configuration, false by default
224
+ configuration[:generate_path_helpers_output] ||= false
225
+ end
226
+
203
227
  # Raw parsed json of the combined grpc service config files if provided
204
228
  # or an empty hash if no config was provided
205
229
  def grpc_service_config_raw
@@ -95,14 +95,33 @@ module Gapic
95
95
  @docs = docs
96
96
  end
97
97
 
98
+ # Returns the "root" of this schema.
99
+ # @return [Gapic::Schema::Api]
100
+ def containing_api
101
+ parent&.containing_api
102
+ end
103
+
104
+ # Returns the file containing this proto entity
105
+ # @return [Gapic::Schema::File]
106
+ def containing_file
107
+ parent&.containing_file
108
+ end
109
+
110
+ ##
98
111
  # Gets the cleaned up leading comments documentation
99
- def docs_leading_comments
112
+ #
113
+ # @param disable_xrefs [Boolean] (default is `false`) Disable linking to
114
+ # cross-references, and render them simply as text. This can be used if
115
+ # it is known that the targets are not present in the current library.
116
+ # @return [String]
117
+ #
118
+ def docs_leading_comments disable_xrefs: false
100
119
  return nil if @docs.nil?
101
120
  return nil if @docs.leading_comments.empty?
102
121
 
103
122
  lines = @docs.leading_comments.each_line.to_a
104
123
  lines.map! { |line| line.start_with?(" ") ? line[1..-1] : line }
105
- lines = FormattingUtils.escape_braces lines
124
+ lines = FormattingUtils.format_doc_lines containing_api, lines, disable_xrefs: disable_xrefs
106
125
  lines.join
107
126
  end
108
127
 
@@ -376,6 +395,10 @@ module Gapic
376
395
  @services.each { |m| m.parent = self }
377
396
  end
378
397
 
398
+ def containing_file
399
+ self
400
+ end
401
+
379
402
  def lookup address
380
403
  address = address.split(".").reject(&:empty?).join(".")
381
404
  @registry[address]