Module: Gapic::GrpcServiceConfig::Parser

Defined in:
lib/gapic/grpc_service_config/parser.rb

Overview

Takes a json of a GRPC service Config and parses it into the form usable by the microgenerator templates

Constant Summary collapse

METHOD_CONFIG_JSON_KEY =
"methodConfig"
RETRY_POLICY_JSON_KEY =
"retryPolicy"
NAMES_JSON_KEY =
"name"
SERVICE_NAME_JSON_KEY =
"service"
METHOD_NAME_JSON_KEY =
"method"
TIMEOUT_JSON_KEY =
"timeout"
INITIAL_DELAY_JSON_KEY =
"initialBackoff"
MAX_DELAY_JSON_KEY =
"maxBackoff"
MULTIPLIER_JSON_KEY =
"backoffMultiplier"
STATUS_CODES_JSON_KEY =
"retryableStatusCodes"
ERROR_CODE_MAPPING =
[
  "OK",
  "CANCELLED",
  "UNKNOWN",
  "INVALID_ARGUMENT",
  "DEADLINE_EXCEEDED",
  "NOT_FOUND",
  "ALREADY_EXISTS",
  "PERMISSION_DENIED",
  "RESOURCE_EXHAUSTED",
  "FAILED_PRECONDITION",
  "ABORTED",
  "OUT_OF_RANGE",
  "UNIMPLEMENTED",
  "INTERNAL",
  "UNAVAILABLE",
  "DATA_LOSS",
  "UNAUTHENTICATED"
].freeze
ERROR_STRING_MAPPING =
ERROR_CODE_MAPPING.each_with_index.with_object({}) do |(str, num), hash|
  hash[str] = num
end.freeze

Class Method Summary collapse

Class Method Details

.get(hash, key) ⇒ Object

Look up a key including checking its underscore form

Parameters:

  • hash (Hash)

    hash structure

  • key (String)

    lowerCamelCase string

Returns:

  • (Object)

    the result, or nil if not found



236
237
238
# File 'lib/gapic/grpc_service_config/parser.rb', line 236

def self.get hash, key
  hash[key] || hash[ActiveSupport::Inflector.underscore(key)]
end

.key?(hash, key) ⇒ Boolean

Determines if the key or its underscore form exists

Parameters:

  • hash (Hash)

    hash structure

  • key (String)

    lowerCamelCase string

Returns:

  • (Boolean)


225
226
227
# File 'lib/gapic/grpc_service_config/parser.rb', line 225

def self.key? hash, key
  hash.key?(key) || hash.key?(ActiveSupport::Inflector.underscore(key))
end

.parse(config_json) ⇒ Gapic::GrpcServiceConfig::Config

Parses GRPC service configuration from a json

Parameters:

  • config_json (Hash)

    a hash that results from JSON.parse

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gapic/grpc_service_config/parser.rb', line 76

def self.parse config_json
  service_level_result = {}
  service_method_level_result = {}

  if !config_json.nil? && key?(config_json, METHOD_CONFIG_JSON_KEY)
    method_configs_json = get config_json, METHOD_CONFIG_JSON_KEY

    method_configs_json.each do |method_config_json|
      method_config = parse_config method_config_json
      service_names = parse_service_names get(method_config_json, NAMES_JSON_KEY)
      service_method_names = filter_service_method_names get(method_config_json, NAMES_JSON_KEY)

      service_names.each do |service_name|
        service_level_result[service_name] = method_config
      end

      service_method_names.each do |service_method_name|
        service_name = get service_method_name, SERVICE_NAME_JSON_KEY
        method_name = get service_method_name, METHOD_NAME_JSON_KEY

        service_method_level_result[service_name] ||= {}
        service_method_level_result[service_name][method_name] = method_config
      end
    end
  end

  Config.new service_level_result, service_method_level_result
end