gapic-generator 0.1.7 → 0.2.0

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 (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
@@ -1,65 +0,0 @@
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_relative "gem_presenter"
19
- require_relative "service_presenter"
20
-
21
- class PackagePresenter
22
- include FilepathHelper
23
- include NamespaceHelper
24
-
25
- def initialize api, package
26
- @api = api
27
- @package = package
28
- end
29
-
30
- def gem
31
- GemPresenter.new @api
32
- end
33
-
34
- def name
35
- @package
36
- end
37
-
38
- def namespace
39
- return services.first&.namespace if services.first&.namespace
40
- ruby_namespace_for_address address
41
- end
42
-
43
- def services
44
- @services ||= begin
45
- files = @api.generate_files.select { |f| f.package == @package }
46
- files.map(&:services).flatten.map { |s| ServicePresenter.new @api, s }
47
- end
48
- end
49
-
50
- def address
51
- @package.split "."
52
- end
53
-
54
- def package_require
55
- ruby_file_path @api, namespace
56
- end
57
-
58
- def package_file_path
59
- package_require + ".rb"
60
- end
61
-
62
- def empty?
63
- services.empty?
64
- end
65
- end
@@ -1,92 +0,0 @@
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
- class ResourcePresenter
22
- # @param resource [Google::Api::ResourceDescriptor]
23
- def initialize resource
24
- @resource = resource
25
-
26
- @patterns = resource.pattern.map do |template|
27
- segments = Gapic::PathTemplate.parse template
28
- OpenStruct.new(
29
- template: template,
30
- segments: segments,
31
- arguments: arguments_for(segments),
32
- path_string: path_string_for(segments)
33
- )
34
- end
35
-
36
- @patterns.each do |pattern|
37
- # URI path template verification for expected proto resource usage
38
- if named_arg_patterns? pattern.segments
39
- raise ArgumentError, "only resources without named patterns are supported, " \
40
- " not #{pattern.template}"
41
- elsif positional_args? pattern.segments
42
- raise ArgumentError, "only resources with named segments are supported, " \
43
- " not #{pattern.template}"
44
- end
45
- end
46
- end
47
-
48
- def name
49
- @resource.type.split("/").delete_if(&:empty?).last
50
- end
51
-
52
- def type
53
- @resource.type
54
- end
55
-
56
- def patterns
57
- @patterns
58
- end
59
-
60
- def path_helper
61
- "#{ActiveSupport::Inflector.underscore name}_path"
62
- end
63
-
64
- private
65
-
66
- def arguments_for segments
67
- arg_segments(segments).map(&:name)
68
- end
69
-
70
- def arg_segments segments
71
- segments.select { |segment| segment.is_a? Gapic::PathTemplate::Segment }
72
- end
73
-
74
- def path_string_for segments
75
- segments.map do |segment|
76
- if segment.is_a? Gapic::PathTemplate::Segment
77
- "\#{#{segment.name}}"
78
- else
79
- # Should be a String
80
- segment
81
- end
82
- end.join
83
- end
84
-
85
- def positional_args? segments
86
- arg_segments(segments).any?(&:positional?)
87
- end
88
-
89
- def named_arg_patterns? segments
90
- arg_segments(segments).any?(&:pattern)
91
- end
92
- end
@@ -1,74 +0,0 @@
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
- class SamplePresenter
20
- def initialize api, sample_config
21
- @api = api
22
- @sample_config = sample_config
23
- end
24
-
25
- def description
26
- @sample_config["description"]
27
- end
28
-
29
- def input_parameters
30
- @sample_config["request"].select { |f| f.key? "input_parameter" }.map { |f| RequestField.new f }
31
- end
32
-
33
- def fields
34
- dotted_hash = @sample_config["request"].each_with_object({}) do |f, memo|
35
- memo[f["field"]] = f
36
- end
37
- dotted_hash.each_with_object({}) do |(path, f), memo|
38
- parts = path.split "."
39
- leaf_hash = parts[0...-1].inject(memo) { |h, k| h[k] ||= {} }
40
- leaf_hash[parts.last] = RequestField.new f
41
- end
42
- end
43
-
44
- def kwargs
45
- fields.keys.map { |k| "#{k}: #{k}" }.join ", "
46
- end
47
-
48
- def response_raw
49
- @sample_config["response"]
50
- end
51
-
52
- class RequestField
53
- attr_reader :field, :value, :input_parameter, :comment, :value_is_file
54
- def initialize field
55
- @field = field["field"]
56
- @value = convert field["value"]
57
- @input_parameter = field["input_parameter"]
58
- @comment = field["comment"]
59
- @comment = nil if @comment&.empty?
60
- @value_is_file = field["value_is_file"]
61
- end
62
-
63
- protected
64
-
65
- def convert val
66
- if val.is_a? String
67
- return ":#{val}" if val =~ /\A[A-Z_0-9]+\z/ # print constants as symbols
68
- val.inspect
69
- else
70
- val.to_s
71
- end
72
- end
73
- end
74
- end
@@ -1,276 +0,0 @@
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_relative "gem_presenter"
19
- require_relative "package_presenter"
20
- require_relative "resource_presenter"
21
- require_relative "method_presenter"
22
-
23
- class ServicePresenter
24
- include FilepathHelper
25
- include NamespaceHelper
26
-
27
- def initialize api, service
28
- @api = api
29
- @service = service
30
- end
31
-
32
- def gem
33
- GemPresenter.new @api
34
- end
35
-
36
- def file
37
- FilePresenter.new @api, @service.parent
38
- end
39
-
40
- def package
41
- PackagePresenter.new @api, @service.parent.package
42
- end
43
-
44
- def methods
45
- @methods ||= begin
46
- @service.methods.map { |m| MethodPresenter.new @api, m }
47
- end
48
- end
49
-
50
- def address
51
- @service.address
52
- end
53
-
54
- def namespace
55
- return @service.ruby_package if @service.ruby_package.present?
56
-
57
- namespace = ruby_namespace_for_address @service.address[0...-1]
58
- fix_namespace @api, namespace
59
- end
60
-
61
- def version
62
- ActiveSupport::Inflector.camelize @service.address[-2]
63
- end
64
-
65
- def name
66
- @service.name
67
- end
68
-
69
- def proto_service_name_full
70
- fix_namespace @api, "#{namespace}::#{name}"
71
- end
72
-
73
- def proto_service_file_path
74
- @service.parent.name.sub ".proto", "_pb.rb"
75
- end
76
-
77
- def proto_service_file_name
78
- proto_service_file_path.split("/").last
79
- end
80
-
81
- def proto_service_require
82
- proto_service_file_path.sub ".rb", ""
83
- end
84
-
85
- def proto_services_file_path
86
- @service.parent.name.sub ".proto", "_services_pb.rb"
87
- end
88
-
89
- def proto_services_file_name
90
- proto_services_file_path.split("/").last
91
- end
92
-
93
- def proto_services_require
94
- proto_services_file_path.sub ".rb", ""
95
- end
96
-
97
- def proto_service_stub_name_full
98
- "#{proto_service_name_full}::Stub"
99
- end
100
-
101
- def service_file_path
102
- service_require + ".rb"
103
- end
104
-
105
- def service_file_name
106
- service_file_path.split("/").last
107
- end
108
-
109
- def service_require
110
- ruby_file_path @api, proto_service_name_full
111
- end
112
-
113
- def client_name
114
- "Client"
115
- end
116
-
117
- def client_name_full
118
- fix_namespace @api, "#{proto_service_name_full}::#{client_name}"
119
- end
120
-
121
- def client_require
122
- ruby_file_path @api, client_name_full
123
- end
124
-
125
- def client_file_path
126
- client_require + ".rb"
127
- end
128
-
129
- def client_file_name
130
- client_file_path.split("/").last
131
- end
132
-
133
- def client_endpoint
134
- @service.host || default_config(:default_host) || "localhost"
135
- end
136
-
137
- def client_scopes
138
- @service.scopes || default_config(:oauth_scopes)
139
- end
140
-
141
- def client_proto_name
142
- @service.address.join "."
143
- end
144
-
145
- def credentials_name
146
- "Credentials"
147
- end
148
-
149
- def credentials_name_full
150
- fix_namespace @api, "#{proto_service_name_full}::#{credentials_name}"
151
- end
152
-
153
- def credentials_file_path
154
- credentials_require + ".rb"
155
- end
156
-
157
- def credentials_file_name
158
- credentials_file_path.split("/").last
159
- end
160
-
161
- def credentials_require
162
- ruby_file_path @api, credentials_name_full
163
- end
164
-
165
- def helpers_file_path
166
- helpers_require + ".rb"
167
- end
168
-
169
- def helpers_file_name
170
- "helpers.rb"
171
- end
172
-
173
- def helpers_require
174
- ruby_file_path @api, "#{proto_service_name_full}::Helpers"
175
- end
176
-
177
- def references
178
- @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
179
- end
180
-
181
- def paths?
182
- references.any?
183
- end
184
-
185
- def paths_name
186
- "Paths"
187
- end
188
-
189
- def paths_name_full
190
- fix_namespace @api, "#{proto_service_name_full}::#{paths_name}"
191
- end
192
-
193
- def paths_file_path
194
- paths_require + ".rb"
195
- end
196
-
197
- def paths_file_name
198
- paths_file_path.split("/").last
199
- end
200
-
201
- def paths_require
202
- ruby_file_path @api, "#{proto_service_name_full}::#{paths_name}"
203
- end
204
-
205
- def test_client_file_path
206
- service_file_path.sub ".rb", "_test.rb"
207
- end
208
-
209
- def test_client_operations_file_path
210
- service_file_path.sub ".rb", "_operations_test.rb"
211
- end
212
-
213
- def stub_name
214
- "#{ActiveSupport::Inflector.underscore name}_stub"
215
- end
216
-
217
- def lro?
218
- methods.find(&:lro?)
219
- end
220
-
221
- def lro_client_var
222
- "operations_client"
223
- end
224
-
225
- def lro_client_ivar
226
- "@#{lro_client_var}"
227
- end
228
-
229
- def operations_name
230
- "Operations"
231
- end
232
-
233
- def operations_name_full
234
- fix_namespace @api, "#{proto_service_name_full}::#{operations_name}"
235
- end
236
-
237
- def operations_file_path
238
- operations_require + ".rb"
239
- end
240
-
241
- def operations_file_name
242
- operations_file_path.split("/").last
243
- end
244
-
245
- def operations_require
246
- ruby_file_path @api, "#{proto_service_name_full}::#{operations_name}"
247
- end
248
-
249
- def lro_service
250
- lro = @service.parent.parent.files.find { |file| file.name == "google/longrunning/operations.proto" }
251
-
252
- return ServicePresenter.new @api, lro.services.first unless lro.nil?
253
- end
254
-
255
- def config_channel_args
256
- { "grpc.service_config_disable_resolution" => 1 }
257
- end
258
-
259
- def grpc_service_config
260
- return unless @api.grpc_service_config&.service_level_configs&.key? grpc_full_name
261
- @api.grpc_service_config.service_level_configs[grpc_full_name]
262
- end
263
-
264
- def grpc_full_name
265
- @service.address.join "."
266
- end
267
-
268
- private
269
-
270
- def default_config key
271
- return unless @service.parent.parent.configuration[:defaults]
272
- return unless @service.parent.parent.configuration[:defaults][:service]
273
-
274
- @service.parent.parent.configuration[:defaults][:service][key]
275
- end
276
- end