gapic-generator 0.1.0 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7438b616f56c0f6c150b04d5facd7b77dea1ad988f4892bf2143f455a236653d
4
- data.tar.gz: d3d836af88d5bc7ebc433c1a5da15690bd62ba2aabee75b8a647f2aa6e729287
3
+ metadata.gz: 87900df01c425c43c7c7ac1a46965b2324af78eb6c56b91e6cfe36eb0abf3d56
4
+ data.tar.gz: 3f72d4b99665d5e235332854e4bbd4cc69f181e08e66fff9a2444cdbd9f44693
5
5
  SHA512:
6
- metadata.gz: b8a752648197f7b2065bdcde4d9d5462660e8f073adcdd8687b1d9d8c55de34d86dfa69f8e118d51fb8d9a21d3a5c65a95586f633ed33b6db17529443eb429a8
7
- data.tar.gz: 68dda5a00441de0c2e8645f9c3c0862d68bf8d96b5e379990c076edde6ea607466bdf95aeb9178e4e45c7be4b0384a94438745abd7596e9a90cd56eea4a5d289
6
+ metadata.gz: 89550d2af9728e7efdb9a3813fa50bf237dc8322057fce25653fba88aae3871672e50af88d0844e4c2ff34d725734236500e5c17cf0db889e0d517a67852760e
7
+ data.tar.gz: 653271d51b4f3142d99b21cee2dff0146ddea8768a3d2d128cad0fc3914a498cac532223b53773145a33c57d5167059d1d2c8fd29b17b66d51bf7cb7c1d792d8
@@ -1,5 +1,26 @@
1
1
  # Release History for gapic-generator
2
2
 
3
+ ### 0.1.5 / 2020-03-13
4
+
5
+ * More improvements to escaping of curly braces.
6
+
7
+ ### 0.1.4 / 2020-03-12
8
+
9
+ * Fixed over-escaping of curly braces in preformatted blocks in the yardocs.
10
+ * Fixed typo in authentication document.
11
+
12
+ ### 0.1.3 / 2020-03-11
13
+
14
+ * Generated numeric constants are formatted with underscores if necessary.
15
+
16
+ ### 0.1.2 / 2020-03-09
17
+
18
+ * Support multiple grpc service config files
19
+
20
+ ### 0.1.1 / 2020-03-09
21
+
22
+ * Relax rake dependency.
23
+
3
24
  ### 0.1.0 / 2020-03-07
4
25
 
5
26
  * Generate default timeout and retry configs based on GRPC service configs.
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 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 "bigdecimal"
18
+
19
+ module Gapic
20
+ ##
21
+ # Various string formatting utils
22
+ #
23
+ module FormattingUtils
24
+ @brace_detector = /\A(?<pre>[^`]*(`[^`]*`[^`]*)*[^`\\])?\{(?<inside>[^\s][^}]*)\}(?<post>.*)\z/m
25
+ @list_element_detector = /\A\s*(\*|\+|-|[0-9a-zA-Z]+\.)\s/
26
+
27
+ class << self
28
+ ##
29
+ # Given an enumerable of lines, escape braces that look like yardoc type
30
+ # links. Tries to be smart about handling only braces that would be
31
+ # interpreted by yard (i.e. those that are not part of preformatted text
32
+ # blocks).
33
+ #
34
+ # @param lines [Enumerable<String>]
35
+ # @return [Enumerable<String>]
36
+ #
37
+ def escape_braces lines
38
+ # This looks for braces that:
39
+ # * Are opened and closed on the same line
40
+ # * Are not nested
41
+ # * Are not located between backticks
42
+ # * Are not in a preformatted block
43
+ #
44
+ # To detect preformatted blocks, this tracks the "expected" base indent
45
+ # according to Markdown. Specifically, this is the effective indent of
46
+ # previous block, which is normally 0 except if we're in a list item.
47
+ # Then, if a block is indented at least 4 spaces past that expected
48
+ # indent (and as long as it remains so), those lines are considered
49
+ # preformatted.
50
+ in_block = nil
51
+ base_indent = 0
52
+ lines.map do |line|
53
+ indent = line_indent line
54
+ if indent.nil?
55
+ in_block = nil
56
+ else
57
+ in_block, base_indent = update_indent_state in_block, base_indent, line, indent
58
+ line = escape_line_braces line if in_block == false
59
+ end
60
+ line
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Given a number, format it in such a way that Rubocop will be happy.
66
+ # Specifically, we add underscores if the magnitude is at least 10_000.
67
+ # This works for both integers and floats.
68
+ #
69
+ # @param value [Numeric]
70
+ # @return [String]
71
+ #
72
+ def format_number value
73
+ return value.to_s if value.abs < 10_000
74
+ str = value.is_a?(Integer) ? value.to_s : BigDecimal(value.to_f.to_s).to_s("F")
75
+ re = /^(-?\d+)(\d\d\d)([_\.][_\.\d]+)?$/
76
+ while (m = re.match str)
77
+ str = "#{m[1]}_#{m[2]}#{m[3]}"
78
+ end
79
+ str
80
+ end
81
+
82
+ private
83
+
84
+ def update_indent_state in_block, base_indent, line, indent
85
+ if in_block != true && @list_element_detector =~ line
86
+ in_block = false
87
+ indent = base_indent if indent > base_indent
88
+ base_indent = (indent + 7) / 4 * 4
89
+ else
90
+ in_block = indent >= base_indent + 4 unless in_block == false
91
+ base_indent = indent / 4 * 4 if in_block == false && indent < base_indent
92
+ end
93
+ [in_block, base_indent]
94
+ end
95
+
96
+ def line_indent line
97
+ m = /^( *)\S/.match line
98
+ m.nil? ? nil : m[1].length
99
+ end
100
+
101
+ def escape_line_braces line
102
+ while (m = @brace_detector.match line)
103
+ line = "#{m[:pre]}\\\\{#{m[:inside]}}#{m[:post]}"
104
+ end
105
+ line
106
+ end
107
+ end
108
+ end
109
+ end
@@ -16,6 +16,6 @@
16
16
 
17
17
  module Gapic
18
18
  module Generator
19
- VERSION = "0.1.0"
19
+ VERSION = "0.1.5"
20
20
  end
21
21
  end
@@ -26,8 +26,8 @@ module Gapic
26
26
  # usable by the microgenerator templates
27
27
  #
28
28
  module Parser
29
- METHOD_CONFIG_JSON_KEY = "method_config"
30
- RETRY_POLICY_JSON_KEY = "retry_policy"
29
+ METHOD_CONFIG_JSON_KEY = "methodConfig"
30
+ RETRY_POLICY_JSON_KEY = "retryPolicy"
31
31
 
32
32
  NAMES_JSON_KEY = "name"
33
33
  SERVICE_NAME_JSON_KEY = "service"
@@ -35,10 +35,10 @@ module Gapic
35
35
 
36
36
  TIMEOUT_JSON_KEY = "timeout"
37
37
 
38
- INITIAL_DELAY_JSON_KEY = "initial_backoff"
39
- MAX_DELAY_JSON_KEY = "max_backoff"
40
- MULTIPLIER_JSON_KEY = "backoff_multiplier"
41
- STATUS_CODES_JSON_KEY = "retryable_status_codes"
38
+ INITIAL_DELAY_JSON_KEY = "initialBackoff"
39
+ MAX_DELAY_JSON_KEY = "maxBackoff"
40
+ MULTIPLIER_JSON_KEY = "backoffMultiplier"
41
+ STATUS_CODES_JSON_KEY = "retryableStatusCodes"
42
42
 
43
43
 
44
44
  ##
@@ -52,21 +52,21 @@ module Gapic
52
52
  service_level_result = {}
53
53
  service_method_level_result = {}
54
54
 
55
- if !service_config_json.nil? && service_config_json.key?(METHOD_CONFIG_JSON_KEY)
56
- method_configs_json = service_config_json[METHOD_CONFIG_JSON_KEY]
55
+ if !service_config_json.nil? && key?(service_config_json, METHOD_CONFIG_JSON_KEY)
56
+ method_configs_json = get service_config_json, METHOD_CONFIG_JSON_KEY
57
57
 
58
58
  method_configs_json.each do |method_config_json|
59
59
  method_config = parse_config method_config_json
60
- service_names = parse_service_names method_config_json[NAMES_JSON_KEY]
61
- service_method_names = filter_service_method_names method_config_json[NAMES_JSON_KEY]
60
+ service_names = parse_service_names get(method_config_json, NAMES_JSON_KEY)
61
+ service_method_names = filter_service_method_names get(method_config_json, NAMES_JSON_KEY)
62
62
 
63
63
  service_names.each do |service_name|
64
64
  service_level_result[service_name] = method_config
65
65
  end
66
66
 
67
67
  service_method_names.each do |service_method_name|
68
- service_name = service_method_name[SERVICE_NAME_JSON_KEY]
69
- method_name = service_method_name[METHOD_NAME_JSON_KEY]
68
+ service_name = get service_method_name, SERVICE_NAME_JSON_KEY
69
+ method_name = get service_method_name, METHOD_NAME_JSON_KEY
70
70
 
71
71
  service_method_level_result[service_name] ||= {}
72
72
  service_method_level_result[service_name][method_name] = method_config
@@ -92,10 +92,10 @@ module Gapic
92
92
  #
93
93
  def self.parse_service_names method_config_json_names
94
94
  service_names_jsons = method_config_json_names.select do |names_json|
95
- names_json.size == 1 && names_json.key?(SERVICE_NAME_JSON_KEY)
95
+ names_json.size == 1 && key?(names_json, SERVICE_NAME_JSON_KEY)
96
96
  end
97
97
 
98
- service_names_jsons.map { |names_json| names_json[SERVICE_NAME_JSON_KEY] }
98
+ service_names_jsons.map { |names_json| get names_json, SERVICE_NAME_JSON_KEY }
99
99
  end
100
100
 
101
101
  ##
@@ -113,7 +113,7 @@ module Gapic
113
113
  #
114
114
  def self.filter_service_method_names method_config_json_names
115
115
  method_config_json_names.select do |names_json|
116
- names_json.size == 2 && names_json.key?(SERVICE_NAME_JSON_KEY) && names_json.key?(METHOD_NAME_JSON_KEY)
116
+ names_json.size == 2 && key?(names_json, SERVICE_NAME_JSON_KEY) && key?(names_json, METHOD_NAME_JSON_KEY)
117
117
  end
118
118
  end
119
119
 
@@ -126,8 +126,8 @@ module Gapic
126
126
  # @return [Gapic::GrpcServiceConfig::MethodConfig] parsed MethodConfig
127
127
  #
128
128
  def self.parse_config method_config_json
129
- timeout_seconds = parse_interval_seconds method_config_json[TIMEOUT_JSON_KEY]
130
- retry_policy = parse_retry_policy method_config_json[RETRY_POLICY_JSON_KEY]
129
+ timeout_seconds = parse_interval_seconds get(method_config_json, TIMEOUT_JSON_KEY)
130
+ retry_policy = parse_retry_policy get(method_config_json, RETRY_POLICY_JSON_KEY)
131
131
 
132
132
  MethodConfig.new timeout_seconds, retry_policy
133
133
  end
@@ -143,10 +143,10 @@ module Gapic
143
143
  def self.parse_retry_policy retry_policy_json
144
144
  return nil if retry_policy_json.nil? || retry_policy_json.empty?
145
145
 
146
- initial_delay_seconds = parse_interval_seconds retry_policy_json[INITIAL_DELAY_JSON_KEY]
147
- max_delay_seconds = parse_interval_seconds retry_policy_json[MAX_DELAY_JSON_KEY]
148
- multiplier = retry_policy_json[MULTIPLIER_JSON_KEY]
149
- status_codes = retry_policy_json[STATUS_CODES_JSON_KEY]
146
+ initial_delay_seconds = parse_interval_seconds get(retry_policy_json, INITIAL_DELAY_JSON_KEY)
147
+ max_delay_seconds = parse_interval_seconds get(retry_policy_json, MAX_DELAY_JSON_KEY)
148
+ multiplier = get retry_policy_json, MULTIPLIER_JSON_KEY
149
+ status_codes = get retry_policy_json, STATUS_CODES_JSON_KEY
150
150
 
151
151
  RetryPolicy.new initial_delay_seconds, max_delay_seconds, multiplier, status_codes
152
152
  end
@@ -175,6 +175,28 @@ module Gapic
175
175
  Float(timestring_nos)
176
176
  end
177
177
 
178
+ ##
179
+ # Determines if the key or its underscore form exists
180
+ #
181
+ # @param hash [Hash] hash structure
182
+ # @param key [String] lowerCamelCase string
183
+ # @return [Boolean]
184
+ #
185
+ def self.key? hash, key
186
+ hash.key?(key) || hash.key?(ActiveSupport::Inflector.underscore(key))
187
+ end
188
+
189
+ ##
190
+ # Look up a key including checking its underscore form
191
+ #
192
+ # @param hash [Hash] hash structure
193
+ # @param key [String] lowerCamelCase string
194
+ # @return [Object] the result, or `nil` if not found
195
+ #
196
+ def self.get hash, key
197
+ hash[key] || hash[ActiveSupport::Inflector.underscore(key)]
198
+ end
199
+
178
200
  ##
179
201
  # Determines if a given string can be converted to a float
180
202
  #
@@ -200,16 +200,14 @@ module Gapic
200
200
  end
201
201
  end
202
202
 
203
- # Raw parsed json of the grpc service config if provided
204
- # or an empty hash if config was not provided
203
+ # Raw parsed json of the combined grpc service config files if provided
204
+ # or an empty hash if no config was provided
205
205
  def grpc_service_config_raw
206
206
  @grpc_service_config_raw ||= begin
207
- grpc_service_config_filename = protoc_options["grpc_service_config"]
208
- if grpc_service_config_filename
209
- file = ::File.read grpc_service_config_filename
210
- JSON.parse file
211
- else
212
- {}
207
+ filenames = protoc_options["grpc_service_config"].to_s.split ";"
208
+ filenames.inject({}) do |running_hash, filename|
209
+ file_hash = JSON.parse ::File.read filename
210
+ deep_merge running_hash, file_hash
213
211
  end
214
212
  end
215
213
  end
@@ -252,6 +250,8 @@ module Gapic
252
250
  left.merge right do |_k, lt, rt|
253
251
  if lt.is_a?(Hash) && rt.is_a?(Hash)
254
252
  deep_merge lt, rt
253
+ elsif lt.is_a?(Array) && rt.is_a?(Array)
254
+ lt + rt
255
255
  else
256
256
  rt
257
257
  end
@@ -14,6 +14,8 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
+ require "gapic/formatting_utils"
18
+
17
19
  module Gapic
18
20
  module Schema
19
21
  # Base class for all generic proto types including: enums, messages,
@@ -98,13 +100,10 @@ module Gapic
98
100
  return nil if @docs.nil?
99
101
  return nil if @docs.leading_comments.empty?
100
102
 
101
- @docs
102
- .leading_comments
103
- .each_line
104
- .map { |line| line.start_with?(" ") ? line[1..-1] : line }
105
- .join
106
- .split("{").join("\\\\\\{") # The only safe way to replace with \ characters...
107
- .split("}").join("\\}")
103
+ lines = @docs.leading_comments.each_line.to_a
104
+ lines.map! { |line| line.start_with?(" ") ? line[1..-1] : line }
105
+ lines = FormattingUtils.escape_braces lines
106
+ lines.join
108
107
  end
109
108
 
110
109
  # @!method path
@@ -30,7 +30,7 @@ Gem::Specification.new do |gem|
30
30
 
31
31
  gem.add_development_dependency "google-style", "~> 1.24.0"
32
32
  gem.add_development_dependency "minitest", "~> 5.10"
33
- gem.add_development_dependency "rake", "~> 12.0"
33
+ gem.add_development_dependency "rake", ">= 12.0"
34
34
  gem.add_development_dependency "redcarpet", "~> 3.0"
35
35
  gem.add_development_dependency "simplecov", "~> 0.18"
36
36
  gem.add_development_dependency "yard", "~> 0.9"
@@ -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
@@ -15,6 +15,7 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require "active_support/inflector"
18
+ require "gapic/formatting_utils"
18
19
 
19
20
  module DefaultHelper
20
21
  def prepend_with input, prepend
@@ -39,6 +40,10 @@ module DefaultHelper
39
40
  input.lines[0] + indent(input.lines[1..-1].join, spacing)
40
41
  end
41
42
 
43
+ def format_number value
44
+ Gapic::FormattingUtils.format_number value
45
+ end
46
+
42
47
  def assert_locals *locals
43
48
  locals.each { |local| raise "missing local in template" if local.nil? }
44
49
  end
@@ -2,27 +2,7 @@
2
2
  <%- method_service ||= service -%>
3
3
  @configure ||= begin
4
4
  default_config = <%= service.client_name %>::Configuration.new
5
- <%- if service.grpc_service_config && !service.grpc_service_config.empty? -%>
6
-
7
- <%- if service.grpc_service_config.timeout_seconds -%>
8
- default_config.timeout = <%= service.grpc_service_config.timeout_seconds %>
9
- <%- end -%>
10
- <%- if service.grpc_service_config.retry_policy -%>
11
- default_config.retry_policy = <%= indent_tail render(partial: "service/client/self_configure_retry_policy", locals: { retry_policy: service.grpc_service_config.retry_policy }), 2 %>
12
- <%- end -%>
13
- <%- end -%>
14
- <%- method_service.methods.each do |method| -%>
15
- <%- if method.grpc_service_config && !method.grpc_service_config.empty? -%>
16
-
17
- <%- if method.grpc_service_config.timeout_seconds -%>
18
- default_config.rpcs.<%= method.name %>.timeout = <%= method.grpc_service_config.timeout_seconds %>
19
- <%- end -%>
20
- <%- if method.grpc_service_config.retry_policy -%>
21
- default_config.rpcs.<%= method.name %>.retry_policy =<%= indent_tail render(partial: "service/client/self_configure_retry_policy", locals: { retry_policy: method.grpc_service_config.retry_policy }), 2 %>
22
- <%- end -%>
23
- <%- end -%>
24
- <%- end -%>
25
-
5
+ <%= render partial: "service/client/self_configure_defaults", locals: {service: service} %>
26
6
  default_config
27
7
  end
28
8
  yield @configure if block_given?
@@ -0,0 +1,22 @@
1
+ <%- assert_locals service -%>
2
+ <%- method_service ||= service -%>
3
+ <%- if service.grpc_service_config && !service.grpc_service_config.empty? -%>
4
+
5
+ <%- if service.grpc_service_config.timeout_seconds -%>
6
+ default_config.timeout = <%= format_number service.grpc_service_config.timeout_seconds %>
7
+ <%- end -%>
8
+ <%- if service.grpc_service_config.retry_policy -%>
9
+ default_config.retry_policy = <%= indent_tail render(partial: "service/client/self_configure_retry_policy", locals: { retry_policy: service.grpc_service_config.retry_policy }), 2 %>
10
+ <%- end -%>
11
+ <%- end -%>
12
+ <%- method_service.methods.each do |method| -%>
13
+ <%- if method.grpc_service_config && !method.grpc_service_config.empty? -%>
14
+
15
+ <%- if method.grpc_service_config.timeout_seconds -%>
16
+ default_config.rpcs.<%= method.name %>.timeout = <%= format_number method.grpc_service_config.timeout_seconds %>
17
+ <%- end -%>
18
+ <%- if method.grpc_service_config.retry_policy -%>
19
+ default_config.rpcs.<%= method.name %>.retry_policy =<%= indent_tail render(partial: "service/client/self_configure_retry_policy", locals: { retry_policy: method.grpc_service_config.retry_policy }), 2 %>
20
+ <%- end -%>
21
+ <%- end -%>
22
+ <%- end -%>
@@ -1,13 +1,13 @@
1
1
  <%- assert_locals retry_policy -%>
2
2
  {
3
3
  <%- if retry_policy.initial_delay_seconds -%>
4
- initial_delay: <%= retry_policy.initial_delay_seconds %>,
4
+ initial_delay: <%= format_number retry_policy.initial_delay_seconds %>,
5
5
  <%- end -%>
6
6
  <%- if retry_policy.max_delay_seconds -%>
7
- max_delay: <%= retry_policy.max_delay_seconds %>,
7
+ max_delay: <%= format_number retry_policy.max_delay_seconds %>,
8
8
  <%- end -%>
9
9
  <%- if retry_policy.multiplier -%>
10
- multiplier: <%= retry_policy.multiplier %>,
10
+ multiplier: <%= format_number retry_policy.multiplier %>,
11
11
  <%- end-%>
12
12
  <%- if retry_policy.status_codes -%>
13
13
  retry_codes: <%= retry_policy.status_codes %>
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.0
4
+ version: 0.1.5
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-08 00:00:00.000000000 Z
13
+ date: 2020-03-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -128,14 +128,14 @@ dependencies:
128
128
  name: rake
129
129
  requirement: !ruby/object:Gem::Requirement
130
130
  requirements:
131
- - - "~>"
131
+ - - ">="
132
132
  - !ruby/object:Gem::Version
133
133
  version: '12.0'
134
134
  type: :development
135
135
  prerelease: false
136
136
  version_requirements: !ruby/object:Gem::Requirement
137
137
  requirements:
138
- - - "~>"
138
+ - - ">="
139
139
  - !ruby/object:Gem::Version
140
140
  version: '12.0'
141
141
  - !ruby/object:Gem::Dependency
@@ -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
@@ -278,6 +279,7 @@ files:
278
279
  - templates/default/service/client/_requires.erb
279
280
  - templates/default/service/client/_resource.erb
280
281
  - templates/default/service/client/_self_configure.erb
282
+ - templates/default/service/client/_self_configure_defaults.erb
281
283
  - templates/default/service/client/_self_configure_retry_policy.erb
282
284
  - templates/default/service/client/method/_def.erb
283
285
  - templates/default/service/client/method/def/_options_defaults.erb