gapic-generator 0.1.7 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +48 -0
  3. data/lib/gapic/formatting_utils.rb +9 -4
  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 +93 -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 +62 -0
  23. data/lib/gapic/schema/loader.rb +9 -2
  24. data/lib/gapic/schema/wrappers.rb +118 -24
  25. data/templates/default/gem/entrypoint.erb +8 -0
  26. data/templates/default/gem/gemspec.erb +1 -1
  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/_resource.erb +1 -1
  40. data/templates/default/service/client/method/docs/_request_normal.erb +10 -5
  41. data/templates/default/service/client/method/docs/_request_streaming.erb +1 -1
  42. metadata +20 -15
  43. data/templates/default/helpers/presenter_helper.rb +0 -24
  44. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  45. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  46. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  47. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  48. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  49. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  50. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  51. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  52. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
  53. data/templates/default/service/client/_helpers.erb +0 -9
@@ -0,0 +1,80 @@
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 packages.
25
+ #
26
+ class PackagePresenter
27
+ include Gapic::Helpers::FilepathHelper
28
+ include Gapic::Helpers::NamespaceHelper
29
+
30
+ def initialize api, package
31
+ @api = api
32
+ @package = package
33
+ end
34
+
35
+ def gem
36
+ GemPresenter.new @api
37
+ end
38
+
39
+ def name
40
+ @package
41
+ end
42
+
43
+ def namespace
44
+ return services.first&.namespace if services.first&.namespace
45
+ ruby_namespace_for_address address
46
+ end
47
+
48
+ def parent_namespace
49
+ namespace.split("::")[0...-1].join("::")
50
+ end
51
+
52
+ def module_name
53
+ namespace.split("::").last
54
+ end
55
+
56
+ def services
57
+ @services ||= begin
58
+ files = @api.generate_files.select { |f| f.package == @package }
59
+ files.map(&:services).flatten.map { |s| ServicePresenter.new @api, s }
60
+ end
61
+ end
62
+
63
+ def address
64
+ @package.split "."
65
+ end
66
+
67
+ def package_require
68
+ ruby_file_path @api, namespace
69
+ end
70
+
71
+ def package_file_path
72
+ package_require + ".rb"
73
+ end
74
+
75
+ def empty?
76
+ services.empty?
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,93 @@
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 "gapic/path_template"
18
+ require "ostruct"
19
+ require "active_support/inflector"
20
+
21
+ module Gapic
22
+ module Presenters
23
+ ##
24
+ # A presenter for proto resources.
25
+ #
26
+ class ResourcePresenter
27
+ # @param resource [Google::Api::ResourceDescriptor]
28
+ def initialize resource
29
+ @resource = resource
30
+
31
+ @patterns = resource.pattern.map do |template|
32
+ segments = Gapic::PathTemplate.parse template
33
+ OpenStruct.new(
34
+ template: template,
35
+ segments: segments,
36
+ arguments: arguments_for(segments),
37
+ path_string: path_string_for(segments)
38
+ )
39
+ end
40
+
41
+ # Keep only patterns that can be used to create path helpers
42
+ @patterns.reject! do |pattern|
43
+ named_arg_patterns?(pattern.segments) || positional_args?(pattern.segments)
44
+ end
45
+ end
46
+
47
+ def name
48
+ @resource.type.split("/").delete_if(&:empty?).last
49
+ end
50
+
51
+ def type
52
+ @resource.type
53
+ end
54
+
55
+ def patterns
56
+ @patterns
57
+ end
58
+
59
+ def path_helper
60
+ "#{ActiveSupport::Inflector.underscore name}_path"
61
+ end
62
+
63
+ private
64
+
65
+ def arguments_for segments
66
+ arg_segments(segments).map(&:name)
67
+ end
68
+
69
+ def arg_segments segments
70
+ segments.select { |segment| segment.is_a? Gapic::PathTemplate::Segment }
71
+ end
72
+
73
+ def path_string_for segments
74
+ segments.map do |segment|
75
+ if segment.is_a? Gapic::PathTemplate::Segment
76
+ "\#{#{segment.name}}"
77
+ else
78
+ # Should be a String
79
+ segment
80
+ end
81
+ end.join
82
+ end
83
+
84
+ def positional_args? segments
85
+ arg_segments(segments).any?(&:positional?)
86
+ end
87
+
88
+ def named_arg_patterns? segments
89
+ arg_segments(segments).any?(&:pattern)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -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