gapic-generator 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/CHANGELOG.md +30 -0
  4. data/CODE_OF_CONDUCT.md +43 -0
  5. data/CONTRIBUTING.md +28 -0
  6. data/LICENSE +202 -0
  7. data/README.md +72 -0
  8. data/bin/gapic-generator +103 -0
  9. data/bin/protoc-gen-ruby_gapic +33 -0
  10. data/default-rubocop.yml +10 -0
  11. data/gem_templates/binary.erb +20 -0
  12. data/gem_templates/dockerfile.erb +39 -0
  13. data/gem_templates/entrypoint.erb +24 -0
  14. data/gem_templates/gapic_sh.erb +97 -0
  15. data/gem_templates/gemfile.erb +8 -0
  16. data/gem_templates/gemspec.erb +36 -0
  17. data/gem_templates/generator.erb +37 -0
  18. data/gem_templates/gitignore.erb +10 -0
  19. data/gem_templates/rakefile.erb +29 -0
  20. data/gem_templates/readme.erb +69 -0
  21. data/gem_templates/rubocop.erb +16 -0
  22. data/gem_templates/shared/_header.erb +4 -0
  23. data/gem_templates/shared/_license.erb +13 -0
  24. data/gem_templates/shared/_warning.erb +1 -0
  25. data/gem_templates/test_generator.erb +13 -0
  26. data/gem_templates/test_helper.erb +25 -0
  27. data/gem_templates/version.erb +10 -0
  28. data/lib/gapic/file_formatter.rb +62 -0
  29. data/lib/gapic/gem_builder.rb +98 -0
  30. data/lib/gapic/generator.rb +30 -0
  31. data/lib/gapic/generator/version.rb +21 -0
  32. data/lib/gapic/generators/base_generator.rb +91 -0
  33. data/lib/gapic/generators/default_generator.rb +101 -0
  34. data/lib/gapic/grpc_service_config/method_config.rb +49 -0
  35. data/lib/gapic/grpc_service_config/parser.rb +218 -0
  36. data/lib/gapic/grpc_service_config/parsing_error.rb +25 -0
  37. data/lib/gapic/grpc_service_config/retry_policy.rb +51 -0
  38. data/lib/gapic/grpc_service_config/service_config.rb +42 -0
  39. data/lib/gapic/path_template.rb +35 -0
  40. data/lib/gapic/path_template/parser.rb +83 -0
  41. data/lib/gapic/path_template/segment.rb +67 -0
  42. data/lib/gapic/resource_lookup.rb +91 -0
  43. data/lib/gapic/runner.rb +76 -0
  44. data/lib/gapic/schema.rb +17 -0
  45. data/lib/gapic/schema/api.rb +264 -0
  46. data/lib/gapic/schema/loader.rb +269 -0
  47. data/lib/gapic/schema/wrappers.rb +718 -0
  48. data/lib/google/api/annotations.pb.rb +39 -0
  49. data/lib/google/api/client.pb.rb +43 -0
  50. data/lib/google/api/field_behavior.pb.rb +51 -0
  51. data/lib/google/api/http.pb.rb +60 -0
  52. data/lib/google/api/resource.pb.rb +80 -0
  53. data/lib/google/longrunning/operations.pb.rb +115 -0
  54. data/lib/google/protobuf/any.pb.rb +40 -0
  55. data/lib/google/protobuf/compiler/plugin.pb.rb +72 -0
  56. data/lib/google/protobuf/descriptor.pb.rb +359 -0
  57. data/lib/google/protobuf/empty.pb.rb +36 -0
  58. data/lib/google/rpc/status.pb.rb +46 -0
  59. data/templates/default/gem/_version.erb +2 -0
  60. data/templates/default/gem/changelog.erb +3 -0
  61. data/templates/default/gem/gemfile.erb +4 -0
  62. data/templates/default/gem/gemspec.erb +37 -0
  63. data/templates/default/gem/gitignore.erb +18 -0
  64. data/templates/default/gem/license.erb +22 -0
  65. data/templates/default/gem/rakefile.erb +27 -0
  66. data/templates/default/gem/readme.erb +24 -0
  67. data/templates/default/gem/rubocop.erb +59 -0
  68. data/templates/default/gem/version.erb +6 -0
  69. data/templates/default/gem/yardopts.erb +12 -0
  70. data/templates/default/helpers/default_helper.rb +45 -0
  71. data/templates/default/helpers/filepath_helper.rb +38 -0
  72. data/templates/default/helpers/namespace_helper.rb +44 -0
  73. data/templates/default/helpers/presenter_helper.rb +24 -0
  74. data/templates/default/helpers/presenters/enum_presenter.rb +35 -0
  75. data/templates/default/helpers/presenters/enum_value_presenter.rb +33 -0
  76. data/templates/default/helpers/presenters/field_presenter.rb +146 -0
  77. data/templates/default/helpers/presenters/file_presenter.rb +53 -0
  78. data/templates/default/helpers/presenters/gem_presenter.rb +140 -0
  79. data/templates/default/helpers/presenters/message_presenter.rb +66 -0
  80. data/templates/default/helpers/presenters/method_presenter.rb +293 -0
  81. data/templates/default/helpers/presenters/package_presenter.rb +65 -0
  82. data/templates/default/helpers/presenters/resource_presenter.rb +92 -0
  83. data/templates/default/helpers/presenters/sample_presenter.rb +74 -0
  84. data/templates/default/helpers/presenters/service_presenter.rb +276 -0
  85. data/templates/default/layouts/_ruby.erb +20 -0
  86. data/templates/default/package.erb +6 -0
  87. data/templates/default/proto_docs/_enum.erb +13 -0
  88. data/templates/default/proto_docs/_message.erb +23 -0
  89. data/templates/default/proto_docs/_proto_file.erb +9 -0
  90. data/templates/default/proto_docs/proto_file.erb +6 -0
  91. data/templates/default/proto_docs/readme.erb +5 -0
  92. data/templates/default/service.erb +8 -0
  93. data/templates/default/service/client.erb +6 -0
  94. data/templates/default/service/client/_client.erb +137 -0
  95. data/templates/default/service/client/_config.erb +155 -0
  96. data/templates/default/service/client/_credentials.erb +21 -0
  97. data/templates/default/service/client/_helpers.erb +9 -0
  98. data/templates/default/service/client/_operations.erb +88 -0
  99. data/templates/default/service/client/_paths.erb +8 -0
  100. data/templates/default/service/client/_requires.erb +1 -0
  101. data/templates/default/service/client/_resource.erb +6 -0
  102. data/templates/default/service/client/_self_configure.erb +9 -0
  103. data/templates/default/service/client/_self_configure_defaults.erb +22 -0
  104. data/templates/default/service/client/_self_configure_retry_policy.erb +15 -0
  105. data/templates/default/service/client/method/_def.erb +21 -0
  106. data/templates/default/service/client/method/def/_options_defaults.erb +29 -0
  107. data/templates/default/service/client/method/def/_request.erb +6 -0
  108. data/templates/default/service/client/method/def/_request_normal.erb +4 -0
  109. data/templates/default/service/client/method/def/_request_streaming.erb +9 -0
  110. data/templates/default/service/client/method/def/_rescue.erb +1 -0
  111. data/templates/default/service/client/method/def/_response.erb +6 -0
  112. data/templates/default/service/client/method/def/_response_normal.erb +8 -0
  113. data/templates/default/service/client/method/def/_response_paged.erb +9 -0
  114. data/templates/default/service/client/method/docs/_error.erb +2 -0
  115. data/templates/default/service/client/method/docs/_request.erb +6 -0
  116. data/templates/default/service/client/method/docs/_request_field.erb +7 -0
  117. data/templates/default/service/client/method/docs/_request_normal.erb +20 -0
  118. data/templates/default/service/client/method/docs/_request_streaming.erb +5 -0
  119. data/templates/default/service/client/method/docs/_response.erb +6 -0
  120. data/templates/default/service/client/method/docs/_sample.erb +20 -0
  121. data/templates/default/service/client/method/docs/_sample_response.erb +24 -0
  122. data/templates/default/service/client/method/docs/_samples.erb +6 -0
  123. data/templates/default/service/client/method/docs/request_field/_arg.erb +10 -0
  124. data/templates/default/service/client/method/docs/request_field/_hash.erb +19 -0
  125. data/templates/default/service/client/method/docs/sample_response/_comment.erb +5 -0
  126. data/templates/default/service/client/method/docs/sample_response/_define.erb +2 -0
  127. data/templates/default/service/client/method/docs/sample_response/_loop.erb +12 -0
  128. data/templates/default/service/client/method/docs/sample_response/_print.erb +2 -0
  129. data/templates/default/service/client/method/docs/sample_response/_write_file.erb +2 -0
  130. data/templates/default/service/client/resource/_def.erb +6 -0
  131. data/templates/default/service/client/resource/_doc.erb +8 -0
  132. data/templates/default/service/client/resource/_multi.erb +28 -0
  133. data/templates/default/service/client/resource/_single.erb +11 -0
  134. data/templates/default/service/credentials.erb +6 -0
  135. data/templates/default/service/operations.erb +6 -0
  136. data/templates/default/service/paths.erb +6 -0
  137. data/templates/default/service/test/client.erb +24 -0
  138. data/templates/default/service/test/client_operations.erb +24 -0
  139. data/templates/default/service/test/method/_assert_response.erb +11 -0
  140. data/templates/default/service/test/method/_bidi.erb +100 -0
  141. data/templates/default/service/test/method/_client.erb +84 -0
  142. data/templates/default/service/test/method/_normal.erb +69 -0
  143. data/templates/default/service/test/method/_server.erb +85 -0
  144. data/templates/default/service/test/method/_setup.erb +21 -0
  145. data/templates/default/service/test/smoke.erb +12 -0
  146. data/templates/default/shared/_header.erb +4 -0
  147. data/templates/default/shared/_license.erb +21 -0
  148. data/templates/default/shared/_warning.erb +1 -0
  149. metadata +350 -0
@@ -0,0 +1,49 @@
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
+ module Gapic
18
+ module GrpcServiceConfig
19
+ ##
20
+ # Method config represents a combination of a timeout and
21
+ # a retry policy, both of which can be optional
22
+ # It is applied during a GRPC method call and governs the
23
+ # client-side timeout/retry policy
24
+ #
25
+ class MethodConfig
26
+ attr_reader :timeout_seconds, :retry_policy
27
+
28
+ ##
29
+ # Create new MethodConfig
30
+ #
31
+ # @param timeout_seconds [Float, nil] the value of timeout in seconds if provided
32
+ # @param retry_policy [Gapic::GrpcServiceConfig::RetryPolicy] the retry policy
33
+ #
34
+ def initialize timeout_seconds, retry_policy
35
+ @timeout_seconds = timeout_seconds
36
+ @retry_policy = retry_policy
37
+ end
38
+
39
+ ##
40
+ # Returns whether MethodConfig is empty (does not contain any values)
41
+ #
42
+ # @return [Boolean] whether MethodConfig is empty
43
+ #
44
+ def empty?
45
+ @timeout_seconds.nil? && @retry_policy.empty?
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,218 @@
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 "gapic/grpc_service_config/service_config"
18
+ require "gapic/grpc_service_config/method_config"
19
+ require "gapic/grpc_service_config/retry_policy"
20
+ require "gapic/grpc_service_config/parsing_error"
21
+
22
+ module Gapic
23
+ module GrpcServiceConfig
24
+ ##
25
+ # Takes a json of a GRPC service Config and parses it into the form
26
+ # usable by the microgenerator templates
27
+ #
28
+ module Parser
29
+ METHOD_CONFIG_JSON_KEY = "methodConfig"
30
+ RETRY_POLICY_JSON_KEY = "retryPolicy"
31
+
32
+ NAMES_JSON_KEY = "name"
33
+ SERVICE_NAME_JSON_KEY = "service"
34
+ METHOD_NAME_JSON_KEY = "method"
35
+
36
+ TIMEOUT_JSON_KEY = "timeout"
37
+
38
+ INITIAL_DELAY_JSON_KEY = "initialBackoff"
39
+ MAX_DELAY_JSON_KEY = "maxBackoff"
40
+ MULTIPLIER_JSON_KEY = "backoffMultiplier"
41
+ STATUS_CODES_JSON_KEY = "retryableStatusCodes"
42
+
43
+
44
+ ##
45
+ # Parses ServiceConfig from a json of a GRPC service config
46
+ #
47
+ # @param service_config_json [Hash] a hash that results from JSON.parse
48
+ #
49
+ # @return [Gapic::GrpcServiceConfig::ServiceConfig] parsed ServiceConfig
50
+ #
51
+ def self.parse service_config_json
52
+ service_level_result = {}
53
+ service_method_level_result = {}
54
+
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
+
58
+ method_configs_json.each do |method_config_json|
59
+ method_config = parse_config method_config_json
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
+
63
+ service_names.each do |service_name|
64
+ service_level_result[service_name] = method_config
65
+ end
66
+
67
+ service_method_names.each do |service_method_name|
68
+ service_name = get service_method_name, SERVICE_NAME_JSON_KEY
69
+ method_name = get service_method_name, METHOD_NAME_JSON_KEY
70
+
71
+ service_method_level_result[service_name] ||= {}
72
+ service_method_level_result[service_name][method_name] = method_config
73
+ end
74
+ end
75
+ end
76
+
77
+ ServiceConfig.new service_level_result, service_method_level_result
78
+ end
79
+
80
+ ##
81
+ # Parses the names of the services for which the service-level
82
+ # config is defined from the GRPC service config json.
83
+ # Within the json the names are arranged in hashes. Each hash contains a
84
+ # required "service" key and an optional "method" key. Here we select only
85
+ # the hashes WITHOUT the optional key -- meaning that the config will be
86
+ # applied on a service-level -- and return just the service names.
87
+ #
88
+ # @param method_config_json_names [Array<Hash<String, String>>] "name" hashes from
89
+ # the GRPC service config
90
+ #
91
+ # @return [Array<String>] parsed names of services
92
+ #
93
+ def self.parse_service_names method_config_json_names
94
+ service_names_jsons = method_config_json_names.select do |names_json|
95
+ names_json.size == 1 && key?(names_json, SERVICE_NAME_JSON_KEY)
96
+ end
97
+
98
+ service_names_jsons.map { |names_json| get names_json, SERVICE_NAME_JSON_KEY }
99
+ end
100
+
101
+ ##
102
+ # Filters the "name" hashes from the GRPC service config json
103
+ # to exclude service-level names
104
+ # Within the json the names are arranged in hashes. Each hash contains a
105
+ # required "service" key and an optional "method" key. Here we select only
106
+ # the hashes WITH the optional key -- meaning that the config will be
107
+ # applied on a method-level -- and return the hashes in full.
108
+ #
109
+ # @param method_config_json_names [Array<Hash<String, String>>] "name" hashes
110
+ # from the GRPC service config
111
+ #
112
+ # @return [Array<Hash<String, String>>] filtered hashes for methods
113
+ #
114
+ def self.filter_service_method_names method_config_json_names
115
+ method_config_json_names.select do |names_json|
116
+ names_json.size == 2 && key?(names_json, SERVICE_NAME_JSON_KEY) && key?(names_json, METHOD_NAME_JSON_KEY)
117
+ end
118
+ end
119
+
120
+ ##
121
+ # Parses MethodConfig from the GRPC service config json
122
+ #
123
+ # @param method_config_json [Hash] a hash of a single "method_config"
124
+ # from the GRPC service config
125
+ #
126
+ # @return [Gapic::GrpcServiceConfig::MethodConfig] parsed MethodConfig
127
+ #
128
+ def self.parse_config method_config_json
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
+
132
+ MethodConfig.new timeout_seconds, retry_policy
133
+ end
134
+
135
+ ##
136
+ # Parses RetryPolicy from the GRPC service config json
137
+ #
138
+ # @param retry_policy_json [Hash<String, String>] a hash of a single "retry_policy"
139
+ # from the GRPC service config
140
+ #
141
+ # @return [Gapic::GrpcServiceConfig::RetryPolicy] parsed RetryPolicy
142
+ #
143
+ def self.parse_retry_policy retry_policy_json
144
+ return nil if retry_policy_json.nil? || retry_policy_json.empty?
145
+
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
+
151
+ RetryPolicy.new initial_delay_seconds, max_delay_seconds, multiplier, status_codes
152
+ end
153
+
154
+ ##
155
+ # Parses time expressed in secondds from the GRPC service config json
156
+ # The time is encoded is a string as float or integer with a letter 's' afterwards
157
+ # If given a nil or an empty string returns nil for 'not set' semantic
158
+ # If a string sans the 's' cannot be converted throws a ParsingError
159
+ #
160
+ # @param timestring [String, nil] a string of a time inerval from the GRPC service config
161
+ #
162
+ # @raise [ParsingError] if the time interval string could not be converted
163
+ #
164
+ # @return [Float, nil] converted time interval or nil for 'not set'
165
+ #
166
+ def self.parse_interval_seconds timestring
167
+ return nil if timestring.empty?
168
+
169
+ timestring_nos = timestring.delete_suffix "s"
170
+ unless valid_float? timestring_nos
171
+ error_text = "Was not able to convert the string `#{timestring}` " \
172
+ "to a time interval when parsing a grpc service config"
173
+ raise ParsingError, error_text
174
+ end
175
+ Float(timestring_nos)
176
+ end
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
+
200
+ ##
201
+ # Determines if a given string can be converted to a float
202
+ #
203
+ # @param str [String, nil] a given string, can be nil
204
+ #
205
+ # @return [Boolean] true, if converstion to float is possible
206
+ #
207
+ def self.valid_float? str
208
+ Float(str)
209
+ true
210
+ rescue ::ArgumentError, ::TypeError
211
+ false
212
+ end
213
+
214
+ private_class_method :parse_service_names, :filter_service_method_names, :parse_config,
215
+ :parse_retry_policy, :parse_interval_seconds, :valid_float?
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,25 @@
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
+ module Gapic
18
+ module GrpcServiceConfig
19
+ ##
20
+ # Represents an error occuring during the GRPC config parsing
21
+ #
22
+ class ParsingError < StandardError
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,51 @@
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
+ module Gapic
18
+ module GrpcServiceConfig
19
+ ##
20
+ # RetryPolicy encapsulates the parameters governing the client-side retry
21
+ # for the GRPC method invocation. It is embedded into the MethodConfig
22
+ #
23
+ class RetryPolicy
24
+ attr_reader :initial_delay_seconds, :max_delay_seconds, :multiplier, :status_codes
25
+
26
+ ##
27
+ # Create new ParsedRetryPolicy.
28
+ #
29
+ # @param initial_delay_seconds [Float, nil] the value of initial retry delay in seconds if provided
30
+ # @param max_delay_seconds [Float, nil] the value of max retry delay in seconds if provided
31
+ # @param multiplier [Float, nil] the value of retry multiplier if provided
32
+ # @param status_codes [Array<String>, nil] the retry status codes if provided
33
+ #
34
+ def initialize initial_delay_seconds, max_delay_seconds, multiplier, status_codes
35
+ @initial_delay_seconds = initial_delay_seconds
36
+ @max_delay_seconds = max_delay_seconds
37
+ @multiplier = multiplier
38
+ @status_codes = status_codes
39
+ end
40
+
41
+ ##
42
+ # Returns whether RetryPolicy is empty (does not contain any values)
43
+ #
44
+ # @return [Boolean] whether RetryPolicy is empty
45
+ #
46
+ def empty?
47
+ @initial_delay_seconds.nil? && @max_delay_seconds.nil? && @multiplier.nil? && status_codes.to_a.empty?
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,42 @@
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
+ module Gapic
18
+ module GrpcServiceConfig
19
+ ##
20
+ # Representation of a GRPC service config split into the configurations
21
+ # applied on the service level (to all the methods) and the configurations
22
+ # applied to the specific methods
23
+ #
24
+ class ServiceConfig
25
+ attr_reader :service_level_configs, :service_method_level_configs
26
+
27
+ ##
28
+ # Create new ServiceConfig.
29
+ #
30
+ # @param service_level_configs [Hash<String, Gapic::GrpcServiceConfig::MethodConfig>] service-level configs
31
+ # in a lookup hash by the service full grpc name
32
+ #
33
+ # @param service_method_level_configs [Hash<String, Hash<String, Gapic::GrpcServiceConfig::MethodConfig>>]
34
+ # method-level configs in a double lookup hash, first by the service full grpc name then by the method name
35
+ #
36
+ def initialize service_level_configs, service_method_level_configs
37
+ @service_level_configs = service_level_configs
38
+ @service_method_level_configs = service_method_level_configs
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
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/parser"
18
+
19
+ module Gapic
20
+ # TODO: Enter docs
21
+ # Dooooooooocs!!!
22
+ module PathTemplate
23
+ # Parse a URI path template.
24
+ #
25
+ # @see https://tools.ietf.org/html/rfc6570 URI Template
26
+ #
27
+ # @param path_template [String] The URI path template to be parsed.
28
+ #
29
+ # @return [Array<PathTemplate::Segment|String>] The segments of the URI
30
+ # path template.
31
+ def self.parse path_template
32
+ Parser.new(path_template).segments
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,83 @@
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/segment"
18
+
19
+ module Gapic
20
+ module PathTemplate
21
+ # A URI path template parser.
22
+ #
23
+ # @see https://tools.ietf.org/html/rfc6570 URI Template
24
+ #
25
+ # @!attribute [r] path_template
26
+ # @return [String] The URI path template to be parsed.
27
+ # @!attribute [r] segments
28
+ # @return [Array<Segment|String>] The segments of the parsed URI path
29
+ # template.
30
+ class Parser
31
+ # @private
32
+ # /((?<positional>\*\*?)|{(?<name>[^\/]+?)(?:=(?<template>.+?))?})/
33
+ PATH_TEMPLATE = %r{
34
+ (
35
+ (?<positional>\*\*?)
36
+ |
37
+ {(?<name>[^\/]+?)(?:=(?<template>.+?))?}
38
+ )
39
+ }x.freeze
40
+
41
+ attr_reader :path_template, :segments
42
+
43
+ # Create a new URI path template parser.
44
+ #
45
+ # @param path_template [String] The URI path template to be parsed.
46
+ def initialize path_template
47
+ @path_template = path_template
48
+ @segments = parse! path_template
49
+ end
50
+
51
+ protected
52
+
53
+ def parse! path_template
54
+ # segments contain either Strings or segment objects
55
+ segments = []
56
+ segment_pos = 0
57
+
58
+ while (match = PATH_TEMPLATE.match path_template)
59
+ # The String before the match needs to be added to the segments
60
+ segments << match.pre_match unless match.pre_match.empty?
61
+
62
+ segment, segment_pos = segment_and_pos_from_match match, segment_pos
63
+ segments << segment
64
+
65
+ path_template = match.post_match
66
+ end
67
+
68
+ # Whatever String is unmatched needs to be added to the segments
69
+ segments << path_template unless path_template.empty?
70
+
71
+ segments
72
+ end
73
+
74
+ def segment_and_pos_from_match match, pos
75
+ if match[:positional]
76
+ [Segment.new(pos, match[:positional]), pos + 1]
77
+ else
78
+ [Segment.new(match[:name], match[:template]), pos]
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end