gapic-generator 0.1.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -0
  3. data/lib/gapic/formatting_utils.rb +63 -12
  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 +13 -14
  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 +176 -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 +80 -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 +306 -0
  21. data/lib/gapic/resource_lookup.rb +23 -38
  22. data/lib/gapic/schema/api.rb +70 -0
  23. data/lib/gapic/schema/loader.rb +9 -2
  24. data/lib/gapic/schema/wrappers.rb +134 -24
  25. data/templates/default/gem/entrypoint.erb +8 -0
  26. data/templates/default/gem/gemspec.erb +2 -2
  27. data/templates/default/gem/readme.erb +17 -3
  28. data/templates/default/gem/rubocop.erb +13 -41
  29. data/templates/default/helpers/filepath_helper.rb +2 -21
  30. data/templates/default/helpers/namespace_helper.rb +2 -27
  31. data/templates/default/layouts/_ruby.erb +1 -3
  32. data/templates/default/lib/_package.erb +17 -0
  33. data/templates/default/lib/_service.erb +32 -0
  34. data/templates/default/package.erb +5 -5
  35. data/templates/default/service.erb +5 -7
  36. data/templates/default/service/_helpers.erb +3 -0
  37. data/templates/default/service/client/_client.erb +7 -17
  38. data/templates/default/service/client/_operations.erb +0 -4
  39. data/templates/default/service/client/_paths.erb +1 -0
  40. data/templates/default/service/client/method/def/_options_defaults.erb +1 -1
  41. data/templates/default/service/client/method/def/_response_normal.erb +1 -1
  42. data/templates/default/service/client/method/docs/_request_normal.erb +10 -5
  43. data/templates/default/service/client/method/docs/_request_streaming.erb +1 -1
  44. metadata +20 -15
  45. data/templates/default/helpers/presenter_helper.rb +0 -24
  46. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  47. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  48. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  49. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  50. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  51. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  52. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  53. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  54. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
  55. data/templates/default/service/client/_helpers.erb +0 -9
@@ -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,306 @@
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 module_name
82
+ proto_service_name_full.split("::").last
83
+ end
84
+
85
+ def proto_service_file_path
86
+ @service.parent.name.sub ".proto", "_pb.rb"
87
+ end
88
+
89
+ def proto_service_file_name
90
+ proto_service_file_path.split("/").last
91
+ end
92
+
93
+ def proto_service_require
94
+ proto_service_file_path.sub ".rb", ""
95
+ end
96
+
97
+ def proto_services_file_path
98
+ @service.parent.name.sub ".proto", "_services_pb.rb"
99
+ end
100
+
101
+ def proto_services_file_name
102
+ proto_services_file_path.split("/").last
103
+ end
104
+
105
+ def proto_services_require
106
+ proto_services_file_path.sub ".rb", ""
107
+ end
108
+
109
+ def proto_service_stub_name_full
110
+ "#{proto_service_name_full}::Stub"
111
+ end
112
+
113
+ def service_file_path
114
+ service_require + ".rb"
115
+ end
116
+
117
+ def service_file_name
118
+ service_file_path.split("/").last
119
+ end
120
+
121
+ def service_directory_name
122
+ service_require.split("/").last
123
+ end
124
+
125
+ def service_require
126
+ ruby_file_path @api, proto_service_name_full
127
+ end
128
+
129
+ def client_name
130
+ "Client"
131
+ end
132
+
133
+ def client_name_full
134
+ fix_namespace @api, "#{proto_service_name_full}::#{client_name}"
135
+ end
136
+
137
+ def create_client_call
138
+ "#{client_name_full}.new"
139
+ end
140
+
141
+ def configure_client_call
142
+ "#{client_name_full}.configure"
143
+ end
144
+
145
+ def client_require
146
+ ruby_file_path @api, client_name_full
147
+ end
148
+
149
+ def client_file_path
150
+ client_require + ".rb"
151
+ end
152
+
153
+ def client_file_name
154
+ client_file_path.split("/").last
155
+ end
156
+
157
+ def client_endpoint
158
+ return @parent_service.client_endpoint if @parent_service
159
+ @service.host || default_config(:default_host) || "localhost"
160
+ end
161
+
162
+ def client_scopes
163
+ @service.scopes || default_config(:oauth_scopes)
164
+ end
165
+
166
+ def client_proto_name
167
+ @service.address.join "."
168
+ end
169
+
170
+ def credentials_name
171
+ "Credentials"
172
+ end
173
+
174
+ def credentials_name_full
175
+ fix_namespace @api, "#{proto_service_name_full}::#{credentials_name}"
176
+ end
177
+
178
+ def credentials_class_xref
179
+ "{#{credentials_name_full}}"
180
+ end
181
+
182
+ def credentials_file_path
183
+ credentials_require + ".rb"
184
+ end
185
+
186
+ def credentials_file_name
187
+ credentials_file_path.split("/").last
188
+ end
189
+
190
+ def credentials_require
191
+ ruby_file_path @api, credentials_name_full
192
+ end
193
+
194
+ def helpers_file_path
195
+ helpers_require + ".rb"
196
+ end
197
+
198
+ def helpers_file_name
199
+ "helpers.rb"
200
+ end
201
+
202
+ def helpers_require
203
+ ruby_file_path @api, "#{proto_service_name_full}::Helpers"
204
+ end
205
+
206
+ def references
207
+ @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
208
+ end
209
+
210
+ def paths?
211
+ references.any?
212
+ end
213
+
214
+ def paths_name
215
+ "Paths"
216
+ end
217
+
218
+ def paths_name_full
219
+ fix_namespace @api, "#{proto_service_name_full}::#{paths_name}"
220
+ end
221
+
222
+ def paths_file_path
223
+ paths_require + ".rb"
224
+ end
225
+
226
+ def paths_file_name
227
+ paths_file_path.split("/").last
228
+ end
229
+
230
+ def paths_require
231
+ ruby_file_path @api, "#{proto_service_name_full}::#{paths_name}"
232
+ end
233
+
234
+ def test_client_file_path
235
+ service_file_path.sub ".rb", "_test.rb"
236
+ end
237
+
238
+ def test_client_operations_file_path
239
+ service_file_path.sub ".rb", "_operations_test.rb"
240
+ end
241
+
242
+ def stub_name
243
+ "#{ActiveSupport::Inflector.underscore name}_stub"
244
+ end
245
+
246
+ def lro?
247
+ methods.find(&:lro?)
248
+ end
249
+
250
+ def lro_client_var
251
+ "operations_client"
252
+ end
253
+
254
+ def lro_client_ivar
255
+ "@#{lro_client_var}"
256
+ end
257
+
258
+ def operations_name
259
+ "Operations"
260
+ end
261
+
262
+ def operations_name_full
263
+ fix_namespace @api, "#{proto_service_name_full}::#{operations_name}"
264
+ end
265
+
266
+ def operations_file_path
267
+ operations_require + ".rb"
268
+ end
269
+
270
+ def operations_file_name
271
+ operations_file_path.split("/").last
272
+ end
273
+
274
+ def operations_require
275
+ ruby_file_path @api, "#{proto_service_name_full}::#{operations_name}"
276
+ end
277
+
278
+ def lro_service
279
+ lro = @service.parent.parent.files.find { |file| file.name == "google/longrunning/operations.proto" }
280
+ return ServicePresenter.new @api, lro.services.first, parent_service: self unless lro.nil?
281
+ end
282
+
283
+ def config_channel_args
284
+ { "grpc.service_config_disable_resolution" => 1 }
285
+ end
286
+
287
+ def grpc_service_config
288
+ return unless @api.grpc_service_config&.service_level_configs&.key? grpc_full_name
289
+ @api.grpc_service_config.service_level_configs[grpc_full_name]
290
+ end
291
+
292
+ def grpc_full_name
293
+ @service.address.join "."
294
+ end
295
+
296
+ private
297
+
298
+ def default_config key
299
+ return unless @service.parent.parent.configuration[:defaults]
300
+ return unless @service.parent.parent.configuration[:defaults][:service]
301
+
302
+ @service.parent.parent.configuration[:defaults][:service][key]
303
+ end
304
+ end
305
+ end
306
+ end
@@ -26,57 +26,42 @@ module Gapic
26
26
 
27
27
  # @private
28
28
  def lookup!
29
- resources = @api.files.flat_map { |file| lookup_file_resource_descriptors file }
30
- resources.compact.uniq
31
- end
32
-
33
- # @private
34
- def lookup_file_resource_descriptors file
35
29
  resources = []
36
- resources += file.resources.select { |resource| service_resource_types.include? resource.type }
37
- resources += file.messages.flat_map { |message| lookup_message_resources_descriptors message }
38
- resources
39
- end
40
-
41
- # @private
42
- def service_resource_types
43
- @service_resource_types ||= begin
44
- @service.methods.flat_map do |method|
45
- message_resource_types method.input
46
- end.uniq
30
+ @service.methods.each do |method|
31
+ resources.concat resources_for_message(method.input)
32
+ resources.concat resources_for_message(method.output) if @api.generate_path_helpers_output?
47
33
  end
34
+ resources.uniq
48
35
  end
49
36
 
50
37
  # @private
51
- def message_resource_types message, seen_messages = []
52
- return [] if seen_messages.include? message
38
+ def resources_for_message message, seen_messages = []
39
+ resources = []
40
+ return resources if seen_messages.include? message
53
41
  seen_messages << message
54
- resource_types = []
55
- resource_types << message.resource.type if message.resource
56
- resource_types += message.nested_messages.map do |nested_message|
57
- message_resource_types nested_message, seen_messages
42
+ resources << message.resource if message.resource
43
+ message.nested_messages.each do |nested_message|
44
+ resources.concat resources_for_message(nested_message, seen_messages)
58
45
  end
59
46
  message.fields.each do |field|
60
- resource_types << field.resource_reference.type if field.resource_reference
61
- resource_types += message_resource_types field.message, seen_messages if field.message?
47
+ resources.concat resources_for_reference(field.resource_reference) if field.resource_reference
48
+ resources.concat resources_for_message(field.message, seen_messages) if field.message?
62
49
  end
63
- resource_types.flatten
50
+ resources
64
51
  end
65
52
 
66
53
  # @private
67
- def lookup_message_resources_descriptors message
68
- resources = []
69
-
70
- # We don't expect service_resource_types to iclude nil, so we can use message.resource&.type
71
- resources << message.resource if service_resource_types.include? message.resource&.type
72
-
73
- if message.nested_messages
74
- resources += message.nested_messages.flat_map do |nested_message|
75
- lookup_message_resources_descriptors nested_message
76
- end
54
+ # Given a reference (either a type or child type), return the corresponding
55
+ # resources.
56
+ def resources_for_reference reference
57
+ if (type = reference.type) && !type.empty?
58
+ Array(@api.lookup_resource_type(type))
59
+ elsif (child_type = reference.child_type) && !child_type.empty?
60
+ child_resource = @api.lookup_resource_type child_type
61
+ child_resource ? child_resource.parent_resources : []
62
+ else
63
+ []
77
64
  end
78
-
79
- resources
80
65
  end
81
66
 
82
67
  # Lookup all resources for a given service.