gapic-generator 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -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 +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 +170 -0
  15. data/lib/gapic/presenters/message_presenter.rb +73 -0
  16. data/lib/gapic/presenters/method_presenter.rb +298 -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 +297 -0
  21. data/lib/gapic/resource_lookup.rb +8 -1
  22. data/lib/gapic/schema/api.rb +16 -0
  23. data/lib/gapic/schema/wrappers.rb +9 -2
  24. data/templates/default/gem/readme.erb +2 -2
  25. data/templates/default/helpers/filepath_helper.rb +2 -21
  26. data/templates/default/helpers/namespace_helper.rb +2 -27
  27. data/templates/default/layouts/_ruby.erb +1 -3
  28. data/templates/default/service/client/_client.erb +7 -1
  29. metadata +16 -14
  30. data/templates/default/helpers/presenter_helper.rb +0 -24
  31. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  32. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  33. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  34. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  35. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  36. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  37. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  38. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  39. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
@@ -0,0 +1,72 @@
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 services
49
+ @services ||= begin
50
+ files = @api.generate_files.select { |f| f.package == @package }
51
+ files.map(&:services).flatten.map { |s| ServicePresenter.new @api, s }
52
+ end
53
+ end
54
+
55
+ def address
56
+ @package.split "."
57
+ end
58
+
59
+ def package_require
60
+ ruby_file_path @api, namespace
61
+ end
62
+
63
+ def package_file_path
64
+ package_require + ".rb"
65
+ end
66
+
67
+ def empty?
68
+ services.empty?
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,99 @@
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
+ @patterns.each do |pattern|
42
+ # URI path template verification for expected proto resource usage
43
+ if named_arg_patterns? pattern.segments
44
+ raise ArgumentError, "only resources without named patterns are supported, " \
45
+ " not #{pattern.template}"
46
+ elsif positional_args? pattern.segments
47
+ raise ArgumentError, "only resources with named segments are supported, " \
48
+ " not #{pattern.template}"
49
+ end
50
+ end
51
+ end
52
+
53
+ def name
54
+ @resource.type.split("/").delete_if(&:empty?).last
55
+ end
56
+
57
+ def type
58
+ @resource.type
59
+ end
60
+
61
+ def patterns
62
+ @patterns
63
+ end
64
+
65
+ def path_helper
66
+ "#{ActiveSupport::Inflector.underscore name}_path"
67
+ end
68
+
69
+ private
70
+
71
+ def arguments_for segments
72
+ arg_segments(segments).map(&:name)
73
+ end
74
+
75
+ def arg_segments segments
76
+ segments.select { |segment| segment.is_a? Gapic::PathTemplate::Segment }
77
+ end
78
+
79
+ def path_string_for segments
80
+ segments.map do |segment|
81
+ if segment.is_a? Gapic::PathTemplate::Segment
82
+ "\#{#{segment.name}}"
83
+ else
84
+ # Should be a String
85
+ segment
86
+ end
87
+ end.join
88
+ end
89
+
90
+ def positional_args? segments
91
+ arg_segments(segments).any?(&:positional?)
92
+ end
93
+
94
+ def named_arg_patterns? segments
95
+ arg_segments(segments).any?(&:pattern)
96
+ end
97
+ end
98
+ end
99
+ 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,297 @@
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
31
+ @api = api
32
+ @service = service
33
+ end
34
+
35
+ def gem
36
+ GemPresenter.new @api
37
+ end
38
+
39
+ def file
40
+ FilePresenter.new @api, @service.parent
41
+ end
42
+
43
+ def package
44
+ PackagePresenter.new @api, @service.parent.package
45
+ end
46
+
47
+ def methods
48
+ @methods ||= begin
49
+ @service.methods.map { |m| MethodPresenter.new @api, m }
50
+ end
51
+ end
52
+
53
+ def address
54
+ @service.address
55
+ end
56
+
57
+ def namespace
58
+ return @service.ruby_package if @service.ruby_package.present?
59
+
60
+ namespace = ruby_namespace_for_address @service.address[0...-1]
61
+ fix_namespace @api, namespace
62
+ end
63
+
64
+ def version
65
+ ActiveSupport::Inflector.camelize @service.address[-2]
66
+ end
67
+
68
+ def doc_description disable_xrefs: false
69
+ @service.docs_leading_comments disable_xrefs: disable_xrefs
70
+ end
71
+
72
+ def name
73
+ @service.name
74
+ end
75
+
76
+ def proto_service_name_full
77
+ fix_namespace @api, "#{namespace}::#{name}"
78
+ end
79
+
80
+ def proto_service_file_path
81
+ @service.parent.name.sub ".proto", "_pb.rb"
82
+ end
83
+
84
+ def proto_service_file_name
85
+ proto_service_file_path.split("/").last
86
+ end
87
+
88
+ def proto_service_require
89
+ proto_service_file_path.sub ".rb", ""
90
+ end
91
+
92
+ def proto_services_file_path
93
+ @service.parent.name.sub ".proto", "_services_pb.rb"
94
+ end
95
+
96
+ def proto_services_file_name
97
+ proto_services_file_path.split("/").last
98
+ end
99
+
100
+ def proto_services_require
101
+ proto_services_file_path.sub ".rb", ""
102
+ end
103
+
104
+ def proto_service_stub_name_full
105
+ "#{proto_service_name_full}::Stub"
106
+ end
107
+
108
+ def service_file_path
109
+ service_require + ".rb"
110
+ end
111
+
112
+ def service_file_name
113
+ service_file_path.split("/").last
114
+ end
115
+
116
+ def service_require
117
+ ruby_file_path @api, proto_service_name_full
118
+ end
119
+
120
+ def client_name
121
+ "Client"
122
+ end
123
+
124
+ def client_name_full
125
+ fix_namespace @api, "#{proto_service_name_full}::#{client_name}"
126
+ end
127
+
128
+ def create_client_call
129
+ "#{client_name_full}.new"
130
+ end
131
+
132
+ def configure_client_call
133
+ "#{client_name_full}.configure"
134
+ end
135
+
136
+ def client_require
137
+ ruby_file_path @api, client_name_full
138
+ end
139
+
140
+ def client_file_path
141
+ client_require + ".rb"
142
+ end
143
+
144
+ def client_file_name
145
+ client_file_path.split("/").last
146
+ end
147
+
148
+ def client_endpoint
149
+ @service.host || default_config(:default_host) || "localhost"
150
+ end
151
+
152
+ def client_scopes
153
+ @service.scopes || default_config(:oauth_scopes)
154
+ end
155
+
156
+ def client_proto_name
157
+ @service.address.join "."
158
+ end
159
+
160
+ def credentials_name
161
+ "Credentials"
162
+ end
163
+
164
+ def credentials_name_full
165
+ fix_namespace @api, "#{proto_service_name_full}::#{credentials_name}"
166
+ end
167
+
168
+ def credentials_class_xref
169
+ "{#{credentials_name_full}}"
170
+ end
171
+
172
+ def credentials_file_path
173
+ credentials_require + ".rb"
174
+ end
175
+
176
+ def credentials_file_name
177
+ credentials_file_path.split("/").last
178
+ end
179
+
180
+ def credentials_require
181
+ ruby_file_path @api, credentials_name_full
182
+ end
183
+
184
+ def helpers_file_path
185
+ helpers_require + ".rb"
186
+ end
187
+
188
+ def helpers_file_name
189
+ "helpers.rb"
190
+ end
191
+
192
+ def helpers_require
193
+ ruby_file_path @api, "#{proto_service_name_full}::Helpers"
194
+ end
195
+
196
+ def references
197
+ @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
198
+ end
199
+
200
+ def paths?
201
+ references.any?
202
+ end
203
+
204
+ def paths_name
205
+ "Paths"
206
+ end
207
+
208
+ def paths_name_full
209
+ fix_namespace @api, "#{proto_service_name_full}::#{paths_name}"
210
+ end
211
+
212
+ def paths_file_path
213
+ paths_require + ".rb"
214
+ end
215
+
216
+ def paths_file_name
217
+ paths_file_path.split("/").last
218
+ end
219
+
220
+ def paths_require
221
+ ruby_file_path @api, "#{proto_service_name_full}::#{paths_name}"
222
+ end
223
+
224
+ def test_client_file_path
225
+ service_file_path.sub ".rb", "_test.rb"
226
+ end
227
+
228
+ def test_client_operations_file_path
229
+ service_file_path.sub ".rb", "_operations_test.rb"
230
+ end
231
+
232
+ def stub_name
233
+ "#{ActiveSupport::Inflector.underscore name}_stub"
234
+ end
235
+
236
+ def lro?
237
+ methods.find(&:lro?)
238
+ end
239
+
240
+ def lro_client_var
241
+ "operations_client"
242
+ end
243
+
244
+ def lro_client_ivar
245
+ "@#{lro_client_var}"
246
+ end
247
+
248
+ def operations_name
249
+ "Operations"
250
+ end
251
+
252
+ def operations_name_full
253
+ fix_namespace @api, "#{proto_service_name_full}::#{operations_name}"
254
+ end
255
+
256
+ def operations_file_path
257
+ operations_require + ".rb"
258
+ end
259
+
260
+ def operations_file_name
261
+ operations_file_path.split("/").last
262
+ end
263
+
264
+ def operations_require
265
+ ruby_file_path @api, "#{proto_service_name_full}::#{operations_name}"
266
+ end
267
+
268
+ def lro_service
269
+ lro = @service.parent.parent.files.find { |file| file.name == "google/longrunning/operations.proto" }
270
+
271
+ return ServicePresenter.new @api, lro.services.first unless lro.nil?
272
+ end
273
+
274
+ def config_channel_args
275
+ { "grpc.service_config_disable_resolution" => 1 }
276
+ end
277
+
278
+ def grpc_service_config
279
+ return unless @api.grpc_service_config&.service_level_configs&.key? grpc_full_name
280
+ @api.grpc_service_config.service_level_configs[grpc_full_name]
281
+ end
282
+
283
+ def grpc_full_name
284
+ @service.address.join "."
285
+ end
286
+
287
+ private
288
+
289
+ def default_config key
290
+ return unless @service.parent.parent.configuration[:defaults]
291
+ return unless @service.parent.parent.configuration[:defaults][:service]
292
+
293
+ @service.parent.parent.configuration[:defaults][:service][key]
294
+ end
295
+ end
296
+ end
297
+ end