gapic-generator 0.1.4
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 +7 -0
- data/.yardopts +13 -0
- data/CHANGELOG.md +43 -0
- data/CODE_OF_CONDUCT.md +43 -0
- data/CONTRIBUTING.md +28 -0
- data/LICENSE +202 -0
- data/README.md +72 -0
- data/bin/gapic-generator +103 -0
- data/bin/protoc-gen-ruby_gapic +33 -0
- data/default-rubocop.yml +10 -0
- data/gem_templates/binary.erb +20 -0
- data/gem_templates/dockerfile.erb +39 -0
- data/gem_templates/entrypoint.erb +24 -0
- data/gem_templates/gapic_sh.erb +97 -0
- data/gem_templates/gemfile.erb +8 -0
- data/gem_templates/gemspec.erb +36 -0
- data/gem_templates/generator.erb +37 -0
- data/gem_templates/gitignore.erb +10 -0
- data/gem_templates/rakefile.erb +29 -0
- data/gem_templates/readme.erb +69 -0
- data/gem_templates/rubocop.erb +16 -0
- data/gem_templates/shared/_header.erb +4 -0
- data/gem_templates/shared/_license.erb +13 -0
- data/gem_templates/shared/_warning.erb +1 -0
- data/gem_templates/test_generator.erb +13 -0
- data/gem_templates/test_helper.erb +25 -0
- data/gem_templates/version.erb +10 -0
- data/lib/gapic/file_formatter.rb +62 -0
- data/lib/gapic/formatting_utils.rb +109 -0
- data/lib/gapic/gem_builder.rb +98 -0
- data/lib/gapic/generator.rb +30 -0
- data/lib/gapic/generator/version.rb +21 -0
- data/lib/gapic/generators/base_generator.rb +91 -0
- data/lib/gapic/generators/default_generator.rb +101 -0
- data/lib/gapic/grpc_service_config/method_config.rb +49 -0
- data/lib/gapic/grpc_service_config/parser.rb +218 -0
- data/lib/gapic/grpc_service_config/parsing_error.rb +25 -0
- data/lib/gapic/grpc_service_config/retry_policy.rb +51 -0
- data/lib/gapic/grpc_service_config/service_config.rb +42 -0
- data/lib/gapic/path_template.rb +35 -0
- data/lib/gapic/path_template/parser.rb +83 -0
- data/lib/gapic/path_template/segment.rb +67 -0
- data/lib/gapic/resource_lookup.rb +91 -0
- data/lib/gapic/runner.rb +76 -0
- data/lib/gapic/schema.rb +17 -0
- data/lib/gapic/schema/api.rb +264 -0
- data/lib/gapic/schema/loader.rb +269 -0
- data/lib/gapic/schema/wrappers.rb +717 -0
- data/lib/google/api/annotations.pb.rb +39 -0
- data/lib/google/api/client.pb.rb +43 -0
- data/lib/google/api/field_behavior.pb.rb +51 -0
- data/lib/google/api/http.pb.rb +60 -0
- data/lib/google/api/resource.pb.rb +80 -0
- data/lib/google/longrunning/operations.pb.rb +115 -0
- data/lib/google/protobuf/any.pb.rb +40 -0
- data/lib/google/protobuf/compiler/plugin.pb.rb +72 -0
- data/lib/google/protobuf/descriptor.pb.rb +359 -0
- data/lib/google/protobuf/empty.pb.rb +36 -0
- data/lib/google/rpc/status.pb.rb +46 -0
- data/templates/default/gem/_version.erb +2 -0
- data/templates/default/gem/changelog.erb +3 -0
- data/templates/default/gem/gemfile.erb +4 -0
- data/templates/default/gem/gemspec.erb +37 -0
- data/templates/default/gem/gitignore.erb +20 -0
- data/templates/default/gem/license.erb +22 -0
- data/templates/default/gem/rakefile.erb +27 -0
- data/templates/default/gem/readme.erb +24 -0
- data/templates/default/gem/rubocop.erb +59 -0
- data/templates/default/gem/version.erb +6 -0
- data/templates/default/gem/yardopts.erb +12 -0
- data/templates/default/helpers/default_helper.rb +50 -0
- data/templates/default/helpers/filepath_helper.rb +38 -0
- data/templates/default/helpers/namespace_helper.rb +44 -0
- data/templates/default/helpers/presenter_helper.rb +24 -0
- data/templates/default/helpers/presenters/enum_presenter.rb +35 -0
- data/templates/default/helpers/presenters/enum_value_presenter.rb +33 -0
- data/templates/default/helpers/presenters/field_presenter.rb +146 -0
- data/templates/default/helpers/presenters/file_presenter.rb +53 -0
- data/templates/default/helpers/presenters/gem_presenter.rb +140 -0
- data/templates/default/helpers/presenters/message_presenter.rb +66 -0
- data/templates/default/helpers/presenters/method_presenter.rb +293 -0
- data/templates/default/helpers/presenters/package_presenter.rb +65 -0
- data/templates/default/helpers/presenters/resource_presenter.rb +92 -0
- data/templates/default/helpers/presenters/sample_presenter.rb +74 -0
- data/templates/default/helpers/presenters/service_presenter.rb +276 -0
- data/templates/default/layouts/_ruby.erb +20 -0
- data/templates/default/package.erb +6 -0
- data/templates/default/proto_docs/_enum.erb +13 -0
- data/templates/default/proto_docs/_message.erb +23 -0
- data/templates/default/proto_docs/_proto_file.erb +9 -0
- data/templates/default/proto_docs/proto_file.erb +6 -0
- data/templates/default/proto_docs/readme.erb +5 -0
- data/templates/default/service.erb +8 -0
- data/templates/default/service/client.erb +6 -0
- data/templates/default/service/client/_client.erb +137 -0
- data/templates/default/service/client/_config.erb +155 -0
- data/templates/default/service/client/_credentials.erb +21 -0
- data/templates/default/service/client/_helpers.erb +9 -0
- data/templates/default/service/client/_operations.erb +88 -0
- data/templates/default/service/client/_paths.erb +8 -0
- data/templates/default/service/client/_requires.erb +1 -0
- data/templates/default/service/client/_resource.erb +6 -0
- data/templates/default/service/client/_self_configure.erb +9 -0
- data/templates/default/service/client/_self_configure_defaults.erb +22 -0
- data/templates/default/service/client/_self_configure_retry_policy.erb +15 -0
- data/templates/default/service/client/method/_def.erb +21 -0
- data/templates/default/service/client/method/def/_options_defaults.erb +29 -0
- data/templates/default/service/client/method/def/_request.erb +6 -0
- data/templates/default/service/client/method/def/_request_normal.erb +4 -0
- data/templates/default/service/client/method/def/_request_streaming.erb +9 -0
- data/templates/default/service/client/method/def/_rescue.erb +1 -0
- data/templates/default/service/client/method/def/_response.erb +6 -0
- data/templates/default/service/client/method/def/_response_normal.erb +8 -0
- data/templates/default/service/client/method/def/_response_paged.erb +9 -0
- data/templates/default/service/client/method/docs/_error.erb +2 -0
- data/templates/default/service/client/method/docs/_request.erb +6 -0
- data/templates/default/service/client/method/docs/_request_field.erb +7 -0
- data/templates/default/service/client/method/docs/_request_normal.erb +20 -0
- data/templates/default/service/client/method/docs/_request_streaming.erb +5 -0
- data/templates/default/service/client/method/docs/_response.erb +6 -0
- data/templates/default/service/client/method/docs/_sample.erb +20 -0
- data/templates/default/service/client/method/docs/_sample_response.erb +24 -0
- data/templates/default/service/client/method/docs/_samples.erb +6 -0
- data/templates/default/service/client/method/docs/request_field/_arg.erb +10 -0
- data/templates/default/service/client/method/docs/request_field/_hash.erb +19 -0
- data/templates/default/service/client/method/docs/sample_response/_comment.erb +5 -0
- data/templates/default/service/client/method/docs/sample_response/_define.erb +2 -0
- data/templates/default/service/client/method/docs/sample_response/_loop.erb +12 -0
- data/templates/default/service/client/method/docs/sample_response/_print.erb +2 -0
- data/templates/default/service/client/method/docs/sample_response/_write_file.erb +2 -0
- data/templates/default/service/client/resource/_def.erb +6 -0
- data/templates/default/service/client/resource/_doc.erb +8 -0
- data/templates/default/service/client/resource/_multi.erb +28 -0
- data/templates/default/service/client/resource/_single.erb +11 -0
- data/templates/default/service/credentials.erb +6 -0
- data/templates/default/service/operations.erb +6 -0
- data/templates/default/service/paths.erb +6 -0
- data/templates/default/service/test/client.erb +24 -0
- data/templates/default/service/test/client_operations.erb +24 -0
- data/templates/default/service/test/method/_assert_response.erb +11 -0
- data/templates/default/service/test/method/_bidi.erb +100 -0
- data/templates/default/service/test/method/_client.erb +84 -0
- data/templates/default/service/test/method/_normal.erb +69 -0
- data/templates/default/service/test/method/_server.erb +85 -0
- data/templates/default/service/test/method/_setup.erb +21 -0
- data/templates/default/service/test/smoke.erb +12 -0
- data/templates/default/shared/_header.erb +4 -0
- data/templates/default/shared/_license.erb +21 -0
- data/templates/default/shared/_warning.erb +1 -0
- metadata +351 -0
|
@@ -0,0 +1,101 @@
|
|
|
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 "gapic/generators/base_generator"
|
|
18
|
+
|
|
19
|
+
module Gapic
|
|
20
|
+
module Generators
|
|
21
|
+
# The generator orchestrates the rendering of templates.
|
|
22
|
+
class DefaultGenerator < BaseGenerator
|
|
23
|
+
# Initializes the generator.
|
|
24
|
+
#
|
|
25
|
+
# @param api [Gapic::Schema::Api] The API model/context to
|
|
26
|
+
# generate.
|
|
27
|
+
def initialize api
|
|
28
|
+
super
|
|
29
|
+
|
|
30
|
+
# Configure to use a custom templates directory
|
|
31
|
+
use_templates! File.join __dir__, "../../../templates/default"
|
|
32
|
+
|
|
33
|
+
# Configure these helper method to be used by the generator
|
|
34
|
+
use_helpers! :gem_presenter
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Disable Rubocop because we expect generate to grow and violate more
|
|
38
|
+
# and more style rules.
|
|
39
|
+
# rubocop:disable all
|
|
40
|
+
|
|
41
|
+
# Generates all the files for the API.
|
|
42
|
+
#
|
|
43
|
+
# @return [Array<
|
|
44
|
+
# Google::Protobuf::Compiler::CodeGeneratorResponse::File>]
|
|
45
|
+
# The files that were generated for the API.
|
|
46
|
+
def generate
|
|
47
|
+
files = []
|
|
48
|
+
|
|
49
|
+
gem = gem_presenter @api
|
|
50
|
+
|
|
51
|
+
gem.packages.each do |package|
|
|
52
|
+
# Package level files
|
|
53
|
+
files << g("package.erb", "lib/#{package.package_file_path}", package: package)
|
|
54
|
+
|
|
55
|
+
package.services.each do |service|
|
|
56
|
+
# Service level files
|
|
57
|
+
files << g("service.erb", "lib/#{service.service_file_path}", service: service)
|
|
58
|
+
files << g("service/client.erb", "lib/#{service.client_file_path}", service: service)
|
|
59
|
+
files << g("service/credentials.erb", "lib/#{service.credentials_file_path}", service: service)
|
|
60
|
+
files << g("service/paths.erb", "lib/#{service.paths_file_path}", service: service) if service.paths?
|
|
61
|
+
files << g("service/operations.erb", "lib/#{service.operations_file_path}", service: service) if service.lro?
|
|
62
|
+
files << g("service/test/client.erb", "test/#{service.test_client_file_path}", service: service)
|
|
63
|
+
files << g("service/test/client_operations.erb", "test/#{service.test_client_operations_file_path}", service: service) if service.lro?
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Gem level files
|
|
68
|
+
files << g("gem/gitignore.erb", ".gitignore", gem: gem)
|
|
69
|
+
files << g("gem/version.erb", "lib/#{gem.version_file_path}", gem: gem)
|
|
70
|
+
files << g("gem/gemspec.erb", "#{gem.name}.gemspec", gem: gem)
|
|
71
|
+
files << g("gem/gemfile.erb", "Gemfile", gem: gem)
|
|
72
|
+
files << g("gem/rakefile.erb", "Rakefile", gem: gem)
|
|
73
|
+
files << g("gem/readme.erb", "README.md", gem: gem)
|
|
74
|
+
files << g("gem/changelog.erb", "CHANGELOG.md", gem: gem)
|
|
75
|
+
files << g("gem/rubocop.erb", ".rubocop.yml", gem: gem)
|
|
76
|
+
files << g("gem/yardopts.erb", ".yardopts", gem: gem)
|
|
77
|
+
files << g("gem/license.erb", "LICENSE.md", gem: gem)
|
|
78
|
+
|
|
79
|
+
gem.proto_files.each do |proto_file|
|
|
80
|
+
files << g("proto_docs/proto_file.erb", "proto_docs/#{proto_file.docs_file_path}", file: proto_file)
|
|
81
|
+
end
|
|
82
|
+
files << g("proto_docs/readme.erb", "proto_docs/README.md", gem: gem)
|
|
83
|
+
|
|
84
|
+
format_files files
|
|
85
|
+
|
|
86
|
+
files
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# rubocop:enable all
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
##
|
|
94
|
+
# Override the default rubocop config file to be used.
|
|
95
|
+
def format_config
|
|
96
|
+
@api.configuration[:format_config] ||
|
|
97
|
+
super
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -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
|