gapic-generator 0.1.3 → 0.2.2

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/lib/gapic/formatting_utils.rb +160 -0
  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 +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 +30 -8
  24. data/templates/default/gem/gemspec.erb +1 -1
  25. data/templates/default/gem/gitignore.erb +2 -0
  26. data/templates/default/gem/readme.erb +2 -2
  27. data/templates/default/gem/rubocop.erb +2 -0
  28. data/templates/default/helpers/default_helper.rb +2 -8
  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/service/client/_client.erb +7 -1
  33. data/templates/default/service/client/_paths.erb +1 -0
  34. data/templates/default/service/client/method/def/_options_defaults.erb +1 -1
  35. data/templates/default/service/client/method/def/_response_normal.erb +1 -1
  36. metadata +17 -14
  37. data/templates/default/helpers/presenter_helper.rb +0 -24
  38. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  39. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  40. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  41. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  42. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  43. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  44. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  45. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  46. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
@@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
23
23
 
24
24
  gem.required_ruby_version = ">= 2.4"
25
25
 
26
- gem.add_dependency "gapic-common", "~> 0.1.0"
26
+ gem.add_dependency "gapic-common", "~> 0.2"
27
27
  <%- if gem.iam_dependency? -%>
28
28
  gem.add_dependency "grpc-google-iam-v1", "~> 0.6.9"
29
29
  <%- end -%>
@@ -16,3 +16,5 @@ pkg/*
16
16
  # Ignore files commonly present in certain dev environments
17
17
  .vagrant
18
18
  .DS_STORE
19
+ .idea
20
+ *.iml
@@ -1,9 +1,9 @@
1
1
  # <%= gem.title %>
2
2
 
3
- <%= gem.description %>
4
-
5
3
  <%= gem.summary %>
6
4
 
5
+ <%= gem.description %>
6
+
7
7
  <%= gem.homepage %>
8
8
 
9
9
  ## Installation
@@ -57,3 +57,5 @@ Style/CaseEquality:
57
57
  - "lib/<%= service.client_file_path.sub "client.rb", "*.rb" %>"
58
58
  <%- end -%>
59
59
  <%- end -%>
60
+ Style/ModuleFunction:
61
+ Enabled: false
@@ -14,8 +14,8 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "bigdecimal"
18
17
  require "active_support/inflector"
18
+ require "gapic/formatting_utils"
19
19
 
20
20
  module DefaultHelper
21
21
  def prepend_with input, prepend
@@ -41,13 +41,7 @@ module DefaultHelper
41
41
  end
42
42
 
43
43
  def format_number value
44
- return value.to_s if value.abs < 10_000
45
- str = value.is_a?(Integer) ? value.to_s : BigDecimal(value.to_f.to_s).to_s("F")
46
- re = /^(-?\d+)(\d\d\d)([_\.][_\.\d]+)?$/
47
- while (m = re.match str)
48
- str = "#{m[1]}_#{m[2]}#{m[3]}"
49
- end
50
- str
44
+ Gapic::FormattingUtils.format_number value
51
45
  end
52
46
 
53
47
  def assert_locals *locals
@@ -14,25 +14,6 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "active_support/inflector"
17
+ require "gapic/helpers/filepath_helper"
18
18
 
19
- module FilepathHelper
20
- ##
21
- # Converts a ruby namespace string to a file path string.
22
- def ruby_file_path api, namespace
23
- file_path = ruby_file_path_for_namespace namespace
24
- fix_file_path api, file_path
25
- end
26
-
27
- ##
28
- # Converts a ruby namespace string to a file path string.
29
- def ruby_file_path_for_namespace namespace
30
- ActiveSupport::Inflector.underscore namespace
31
- end
32
-
33
- ##
34
- # Corrects a namespace by replacing known bad values with good values.
35
- def fix_file_path api, file_path
36
- file_path.split("/").map { |node| api.fix_file_path node }.join("/")
37
- end
38
- end
19
+ FilepathHelper = Gapic::Helpers::FilepathHelper
@@ -14,31 +14,6 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "active_support/inflector"
17
+ require "gapic/helpers/namespace_helper"
18
18
 
19
- module NamespaceHelper
20
- ##
21
- # Looks up the ruby_package for a dot-separated address string to a new string
22
- # and creates the corrected Ruby namespace
23
- def ruby_namespace api, address
24
- file = api.file_for address
25
- address = address.dup
26
- address[file.package] = file.ruby_package if file.ruby_package.present?
27
- namespace = ruby_namespace_for_address address
28
- fix_namespace api, namespace
29
- end
30
-
31
- ##
32
- # Converts an array or dot-separated address string to a new string with
33
- # Ruby double-semicolon separators.
34
- def ruby_namespace_for_address address
35
- address = address.split "." if address.is_a? String
36
- address.reject(&:empty?).map(&:camelize).join "::"
37
- end
38
-
39
- ##
40
- # Corrects a namespace by replacing known bad values with good values.
41
- def fix_namespace api, namespace
42
- namespace.split("::").map { |node| api.fix_namespace node }.join("::")
43
- end
44
- end
19
+ NamespaceHelper = Gapic::Helpers::NamespaceHelper
@@ -1,11 +1,9 @@
1
1
  <%- assert_locals namespace -%>
2
2
  <%= render partial: "shared/header" -%>
3
- <%- if instance_variable_defined? :@requires -%>
4
3
 
4
+ <%- if instance_variable_defined? :@requires -%>
5
5
  <%= @requires -%>
6
6
  <%- end -%>
7
- <%- # Two newlines here so YARD won't use the header as documentation -%>
8
-
9
7
 
10
8
  <%- namespace.split("::").each_with_index do |ns, i| -%>
11
9
  <%= indent "module #{ns}", i*2 %>
@@ -15,7 +15,13 @@ require "<%= service.paths_require %>"
15
15
  require "<%= service.operations_require %>"
16
16
  <%- end -%>
17
17
  <% end %>
18
- # Service that implements <%= service.name %> API.
18
+ ##
19
+ # Client for the <%= service.name %> service.
20
+ #
21
+ <%- if service.doc_description -%>
22
+ <%= indent service.doc_description, "# " %>
23
+ #
24
+ <%- end -%>
19
25
  class <%= service.client_name %>
20
26
  <%- if service.paths? -%>
21
27
  include <%= service.paths_name %>
@@ -5,4 +5,5 @@ module Paths
5
5
  <%= indent render(partial: "service/client/resource", locals: { resource: resource }), 2 %>
6
6
 
7
7
  <%- end %>
8
+ extend self
8
9
  end
@@ -1,6 +1,6 @@
1
1
  <%- assert_locals method -%>
2
2
  # Converts hash and nil to an options object
3
- options = Gapic::CallOptions.new options.to_h if options.respond_to? :to_h
3
+ options = Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
4
4
 
5
5
  # Customize the options with defaults
6
6
  metadata = @config.rpcs.<%= method.name %>.metadata.to_h
@@ -1,7 +1,7 @@
1
1
  <%- assert_locals method -%>
2
2
  @<%= method.service.stub_name %>.call_rpc :<%= method.name %>, request, options: options do |response, operation|
3
3
  <%- if method.lro? -%>
4
- response = Gapic::Operation.new response, <%= method.service.lro_client_ivar %>
4
+ response = Gapic::Operation.new response, <%= method.service.lro_client_ivar %>, options: options
5
5
  <%- end -%>
6
6
  yield response, operation if block_given?
7
7
  return response
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gapic-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernest Landrito
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-03-11 00:00:00.000000000 Z
13
+ date: 2020-03-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -204,6 +204,7 @@ files:
204
204
  - gem_templates/test_helper.erb
205
205
  - gem_templates/version.erb
206
206
  - lib/gapic/file_formatter.rb
207
+ - lib/gapic/formatting_utils.rb
207
208
  - lib/gapic/gem_builder.rb
208
209
  - lib/gapic/generator.rb
209
210
  - lib/gapic/generator/version.rb
@@ -214,9 +215,23 @@ files:
214
215
  - lib/gapic/grpc_service_config/parsing_error.rb
215
216
  - lib/gapic/grpc_service_config/retry_policy.rb
216
217
  - lib/gapic/grpc_service_config/service_config.rb
218
+ - lib/gapic/helpers/filepath_helper.rb
219
+ - lib/gapic/helpers/namespace_helper.rb
217
220
  - lib/gapic/path_template.rb
218
221
  - lib/gapic/path_template/parser.rb
219
222
  - lib/gapic/path_template/segment.rb
223
+ - lib/gapic/presenters.rb
224
+ - lib/gapic/presenters/enum_presenter.rb
225
+ - lib/gapic/presenters/enum_value_presenter.rb
226
+ - lib/gapic/presenters/field_presenter.rb
227
+ - lib/gapic/presenters/file_presenter.rb
228
+ - lib/gapic/presenters/gem_presenter.rb
229
+ - lib/gapic/presenters/message_presenter.rb
230
+ - lib/gapic/presenters/method_presenter.rb
231
+ - lib/gapic/presenters/package_presenter.rb
232
+ - lib/gapic/presenters/resource_presenter.rb
233
+ - lib/gapic/presenters/sample_presenter.rb
234
+ - lib/gapic/presenters/service_presenter.rb
220
235
  - lib/gapic/resource_lookup.rb
221
236
  - lib/gapic/runner.rb
222
237
  - lib/gapic/schema.rb
@@ -248,18 +263,6 @@ files:
248
263
  - templates/default/helpers/default_helper.rb
249
264
  - templates/default/helpers/filepath_helper.rb
250
265
  - templates/default/helpers/namespace_helper.rb
251
- - templates/default/helpers/presenter_helper.rb
252
- - templates/default/helpers/presenters/enum_presenter.rb
253
- - templates/default/helpers/presenters/enum_value_presenter.rb
254
- - templates/default/helpers/presenters/field_presenter.rb
255
- - templates/default/helpers/presenters/file_presenter.rb
256
- - templates/default/helpers/presenters/gem_presenter.rb
257
- - templates/default/helpers/presenters/message_presenter.rb
258
- - templates/default/helpers/presenters/method_presenter.rb
259
- - templates/default/helpers/presenters/package_presenter.rb
260
- - templates/default/helpers/presenters/resource_presenter.rb
261
- - templates/default/helpers/presenters/sample_presenter.rb
262
- - templates/default/helpers/presenters/service_presenter.rb
263
266
  - templates/default/layouts/_ruby.erb
264
267
  - templates/default/package.erb
265
268
  - templates/default/proto_docs/_enum.erb
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2018 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 "presenters/gem_presenter"
19
-
20
- module PresenterHelper
21
- def gem_presenter api
22
- GemPresenter.new api
23
- end
24
- end
@@ -1,146 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2018 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 FieldPresenter
20
- include NamespaceHelper
21
-
22
- def initialize api, message, field
23
- @api = api
24
- @message = message
25
- @field = field
26
- end
27
-
28
- def name
29
- @field.name
30
- end
31
-
32
- def doc_types
33
- field_doc_types @field, false
34
- end
35
-
36
- def doc_attribute_type
37
- mode = @field.output_only? ? "r" : "rw"
38
- "@!attribute [#{mode}] #{@field.name}"
39
- end
40
-
41
- def output_doc_types
42
- field_doc_types @field, true
43
- end
44
-
45
- def doc_description
46
- @field.docs_leading_comments
47
- end
48
-
49
- def default_value
50
- single = default_singular_value
51
- return "[#{single}]" if @field.repeated? && !@field.map?
52
- single
53
- end
54
-
55
- def as_kwarg value: nil
56
- "#{name}: #{value || name}"
57
- end
58
-
59
- # TODO: remove, only used in tests
60
- def type_name
61
- @field.type_name
62
- end
63
-
64
- def type_name_full
65
- return nil if type_name.blank?
66
- ruby_namespace @api, type_name
67
- end
68
-
69
- def message?
70
- @field.message?
71
- end
72
-
73
- def enum?
74
- @field.enum?
75
- end
76
-
77
- def repeated?
78
- @field.repeated?
79
- end
80
-
81
- def map?
82
- @field.map?
83
- end
84
-
85
- protected
86
-
87
- def field_doc_types field, output
88
- return field_map_type field.message, output if field.map?
89
- base_type =
90
- if field.message?
91
- type = message_ruby_type field.message
92
- output ? type : "#{type} | Hash"
93
- elsif field.enum?
94
- # TODO: handle when arg message is nil and enum is the type
95
- message_ruby_type field.enum
96
- else
97
- case field.type
98
- when 1, 2 then "Float"
99
- when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "Integer"
100
- when 9, 12 then "String"
101
- when 8 then "Boolean"
102
- else
103
- "Object"
104
- end
105
- end
106
- field.repeated? ? "Array<#{base_type}>" : base_type
107
- end
108
-
109
- def field_map_type entry_message, output
110
- key_field = value_field = nil
111
- entry_message.fields.each do |field|
112
- key_field = field if field.name == "key"
113
- value_field = field if field.name == "value"
114
- end
115
- class_name = output ? "Google::Protobuf::Map" : "Hash"
116
- if key_field && value_field
117
- key_type = field_doc_types key_field, output
118
- value_type = field_doc_types value_field, output
119
- "#{class_name}{#{key_type} => #{value_type}}"
120
- else
121
- class_name
122
- end
123
- end
124
-
125
- def default_singular_value
126
- if @field.message?
127
- "{}"
128
- elsif @field.enum?
129
- # TODO: select the first non-0 enum value
130
- ":#{@field.enum.values.first.name}"
131
- else
132
- case @field.type
133
- when 1, 2 then "3.5"
134
- when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "42"
135
- when 9, 12 then "\"hello world\""
136
- when 8 then "true"
137
- else
138
- "Object"
139
- end
140
- end
141
- end
142
-
143
- def message_ruby_type message
144
- ruby_namespace @api, message.address.join(".")
145
- end
146
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2018 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_relative "enum_presenter"
18
- require_relative "message_presenter"
19
-
20
- class FilePresenter
21
- include NamespaceHelper
22
-
23
- # @param file [Gapic::Schema::File] the file to present
24
- def initialize api, file
25
- @api = api
26
- @file = file
27
- end
28
-
29
- def name
30
- @file.name
31
- end
32
-
33
- def address
34
- @file.address
35
- end
36
-
37
- def namespace
38
- return @file.ruby_package if @file.ruby_package.present?
39
- ruby_namespace_for_address address
40
- end
41
-
42
- def messages
43
- @messages ||= @file.messages.map { |m| MessagePresenter.new @api, m }
44
- end
45
-
46
- def enums
47
- @enums ||= @file.enums.map { |e| EnumPresenter.new e }
48
- end
49
-
50
- def docs_file_path
51
- @file.name.gsub ".proto", ".rb"
52
- end
53
- end