Class: Mongo::URI::OptionsMapper Private
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::Mongo::Loggable
|
|
Inherits: | Object |
Defined in: | lib/mongo/uri/options_mapper.rb |
Overview
Performs mapping between ::Mongo::URI
options and Ruby options.
This class contains:
-
The mapping defining how
::Mongo::URI
options are converted to Ruby options. -
The mapping from downcased
::Mongo::URI
option names to canonical-cased::Mongo::URI
option names. -
Methods to perform conversion of
::Mongo::URI
option values to Ruby option values (the convert_* methods). These generally warn and return nil when input given is invalid. -
Methods to perform conversion of Ruby option values to standardized MongoClient options (revert_* methods). These assume the input is valid and generally do not perform validation.
::Mongo::URI
option names are case insensitive. Ruby options are specified as symbols (though in ::Mongo::Client
options use indifferent access).
Constant Summary
-
URI_OPTION_CANONICAL_NAMES =
{}
-
URI_OPTION_MAP =
Hash for storing map of
::Mongo::URI
option parameters to conversion strategies{}
::Mongo::Loggable
- Included
Class Method Summary
-
.new(**opts) ⇒ OptionsMapper
constructor
Internal use only
Instantates the options mapper.
-
.uri_option(uri_key, name, **extra)
private
Internal use only
Simple internal dsl to register a MongoDB URI option in the URI_OPTION_MAP.
Instance Attribute Summary
- #options ⇒ Hash readonly Internal use only
Instance Method Summary
-
#add_uri_option(key, value, uri_options)
Internal use only
Adds an option to the uri options hash.
-
#ruby_to_smc(opts) ⇒ Hash
Internal use only
Converts Ruby options provided to “standardized MongoClient options”.
-
#ruby_to_string(opts) ⇒ Hash
Internal use only
Converts Ruby options provided to their representation in a
::Mongo::URI
string. - #smc_to_ruby(opts) Internal use only
-
#apply_transform(key, value, type)
private
Internal use only
Applies URI value transformation by either using the default cast or a transformation appropriate for the given type.
-
#convert_array(name, value) ⇒ Array<String>
private
Internal use only
Extract values from the string and put them into an array.
-
#convert_auth_mech(name, value) ⇒ Symbol
private
Internal use only
Authentication mechanism transformation.
-
#convert_auth_mech_props(name, value) ⇒ Hash | nil
private
Internal use only
::Mongo::Auth
mechanism properties extractor. -
#convert_bool(name, value) ⇒ true | false | nil
private
Internal use only
Converts
value
to a boolean. -
#convert_integer(name, value) ⇒ nil | Integer
private
Internal use only
Converts
value
into an integer. -
#convert_inverse_bool(name, value) ⇒ true | false | nil
private
Internal use only
Parses a boolean value and returns its inverse.
-
#convert_max_staleness(name, value) ⇒ Integer | nil
private
Internal use only
Parses the max staleness value, which must be either “0” or an integer greater or equal to 90.
-
#convert_ms(name, value) ⇒ Float
private
Internal use only
Ruby’s convention is to provide timeouts in seconds, not milliseconds and to use fractions where more precision is necessary.
-
#convert_read_mode(name, value) ⇒ Symbol | String
private
Internal use only
Read preference mode transformation.
-
#convert_read_set(name, value) ⇒ Hash
private
Internal use only
Read preference tag set extractor.
-
#convert_read_tags(name, value) ⇒ Array<Hash> | nil
private
Internal use only
Read preference tags transformation.
-
#convert_repeated_bool(name, value) ⇒ Array<true | false> | nil
private
Internal use only
Converts the value into a boolean and returns it wrapped in an array.
-
#convert_symbol(name, value) ⇒ Symbol
private
Internal use only
Converts
value
into a symbol. -
#convert_w(name, value) ⇒ Integer | Symbol | String
private
Internal use only
Converts
value
as a write concern. -
#convert_zlib_compression_level(name, value) ⇒ Integer | nil
private
Internal use only
Parses the zlib compression level.
-
#hash_extractor(name, value) ⇒ Hash
private
Internal use only
Extract values from the string and put them into a nested hash.
-
#merge_uri_option(target, value, name)
private
Internal use only
Merges a new option into the target.
-
#revert_array(value) ⇒ Array<String>
private
Internal use only
Reverts an array.
-
#revert_auth_mech(value) ⇒ String
private
Internal use only
Reverts auth mechanism.
-
#revert_auth_mech_props(value) ⇒ Hash | nil
private
Internal use only
Reverts auth mechanism properties.
-
#revert_bool(value) ⇒ true | false | nil
private
Internal use only
Reverts a boolean type.
-
#revert_integer(value) ⇒ Integer | nil
private
Internal use only
Reverts an integer.
-
#revert_inverse_bool(value) ⇒ true | false | nil
private
Internal use only
Reverts and inverts a boolean type.
-
#revert_max_staleness(value) ⇒ Integer | nil
private
Internal use only
Reverts max staleness.
-
#revert_ms(value) ⇒ Integer
private
Internal use only
Reverts an ms.
-
#revert_read_mode(value) ⇒ String
(also: #stringify_read_mode)
private
Internal use only
Reverts read mode.
-
#revert_read_tags(value) ⇒ Array<Hash> | nil
private
Internal use only
Reverts read tags.
-
#revert_repeated_bool(value) ⇒ Array<true | false> | true | false | nil
private
Internal use only
Reverts a repeated boolean type.
-
#revert_symbol(value) ⇒ String
(also: #stringify_symbol)
private
Internal use only
Reverts a symbol.
-
#revert_w(value) ⇒ Integer | String
private
Internal use only
Reverts write concern.
-
#revert_zlib_compression_level(value) ⇒ Integer | nil
private
Internal use only
Reverts zlib compression level.
-
#stringify_array(value) ⇒ String
private
Internal use only
Stringifies an array.
-
#stringify_auth_mech(value) ⇒ String | nil
private
Internal use only
Stringifies auth mechanism.
-
#stringify_auth_mech_props(value) ⇒ String | nil
private
Internal use only
Stringifies auth mechanism properties.
-
#stringify_bool(value) ⇒ String | nil
private
Internal use only
Stringifies a boolean type.
-
#stringify_integer(value) ⇒ String | nil
private
Internal use only
Stringifies an integer.
-
#stringify_inverse_bool(value) ⇒ String | nil
private
Internal use only
Inverts and stringifies a boolean.
-
#stringify_max_staleness(value) ⇒ String | nil
private
Internal use only
Stringifies max staleness.
-
#stringify_ms(value) ⇒ String
private
Internal use only
Stringifies an ms.
-
#stringify_read_mode(value)
private
Alias for #revert_read_mode.
-
#stringify_read_tags(value) ⇒ String | nil
private
Internal use only
Stringifies read tags.
-
#stringify_repeated_bool(value) ⇒ Array<true | false> | nil
private
Internal use only
Stringifies a repeated boolean type.
-
#stringify_symbol(value)
private
Alias for #revert_symbol.
-
#stringify_w(value) ⇒ String
private
Internal use only
Stringifies write concern.
-
#stringify_zlib_compression_level(value) ⇒ String | nil
private
Internal use only
Stringifies zlib compression level.
::Mongo::Loggable
- Included
#log_debug | Convenience method to log debug messages with the standard prefix. |
#log_error | Convenience method to log error messages with the standard prefix. |
#log_fatal | Convenience method to log fatal messages with the standard prefix. |
#log_info | Convenience method to log info messages with the standard prefix. |
#log_warn | Convenience method to log warn messages with the standard prefix. |
#logger | Get the logger instance. |
#_mongo_log_prefix, #format_message |
Class Method Details
.uri_option(uri_key, name, **extra) (private)
Simple internal dsl to register a MongoDB URI option in the URI_OPTION_MAP.
# File 'lib/mongo/uri/options_mapper.rb', line 260
def self.uri_option(uri_key, name, **extra) URI_OPTION_MAP[uri_key.downcase] = { name: name }.update(extra) URI_OPTION_CANONICAL_NAMES[uri_key.downcase] = uri_key end
Instance Attribute Details
#options ⇒ Hash
(readonly)
# File 'lib/mongo/uri/options_mapper.rb', line 51
attr_reader :
Instance Method Details
#add_uri_option(key, value, uri_options)
Adds an option to the uri options hash.
Acquires a target for the option based on group.
Transforms the value.
Merges the option into the target.
# File 'lib/mongo/uri/options_mapper.rb', line 62
def add_uri_option(key, value, ) strategy = URI_OPTION_MAP[key.downcase] if strategy.nil? log_warn("Unsupported URI option '#{key}' on URI '#{@string}'. It will be ignored.") return end group = strategy[:group] target = if group [group] || {} else end value = apply_transform(key, value, strategy[:type]) # Sometimes the value here would be nil, for example if we are processing # read preference tags or auth mechanism properties and all of the # data within is invalid. Ignore such options. unless value.nil? merge_uri_option(target, value, strategy[:name]) end if group && !target.empty? && ! .key?(group) [group] = target end end
#apply_transform(key, value, type) (private)
Applies URI value transformation by either using the default cast or a transformation appropriate for the given type.
# File 'lib/mongo/uri/options_mapper.rb', line 215
def apply_transform(key, value, type) if type send("convert_#{type}", key, value) else value end end
#convert_array(name, value) ⇒ Array
<String
> (private)
Extract values from the string and put them into an array.
# File 'lib/mongo/uri/options_mapper.rb', line 547
def convert_array(name, value) value.split(',') end
#convert_auth_mech(name, value) ⇒ Symbol (private)
Authentication mechanism transformation.
# File 'lib/mongo/uri/options_mapper.rb', line 575
def convert_auth_mech(name, value) auth_mech = AUTH_MECH_MAP[value.upcase] (auth_mech || value).tap do |mech| log_warn("#{value} is not a valid auth mechanism") unless auth_mech end end
#convert_auth_mech_props(name, value) ⇒ Hash
| nil
(private)
::Mongo::Auth
mechanism properties extractor.
# File 'lib/mongo/uri/options_mapper.rb', line 615
def convert_auth_mech_props(name, value) properties = hash_extractor('authMechanismProperties', value) if properties properties.each do |k, v| if k.to_s.downcase == 'canonicalize_host_name' && v properties[k] = (v.downcase == 'true') end end end properties end
#convert_bool(name, value) ⇒ true
| false
| nil
(private)
Converts value
to a boolean.
Returns true for ‘true’, false for ‘false’, otherwise nil.
# File 'lib/mongo/uri/options_mapper.rb', line 336
def convert_bool(name, value) case value when true, "true", 'TRUE' true when false, "false", 'FALSE' false else log_warn("invalid boolean option for #{name}: #{value}") nil end end
#convert_integer(name, value) ⇒ nil
| Integer
(private)
Converts value
into an integer. Only converts positive integers.
If the value is not a valid integer, warns and returns nil.
# File 'lib/mongo/uri/options_mapper.rb', line 443
def convert_integer(name, value) if value.is_a?(String) && /\A\d+\z/ !~ value log_warn("#{value} is not a valid integer for #{name}") return nil end value.to_i end
#convert_inverse_bool(name, value) ⇒ true
| false
| nil
(private)
Parses a boolean value and returns its inverse.
# File 'lib/mongo/uri/options_mapper.rb', line 407
def convert_inverse_bool(name, value) b = convert_bool(name, value) if b.nil? nil else !b end end
#convert_max_staleness(name, value) ⇒ Integer
| nil
(private)
Parses the max staleness value, which must be either “0” or an integer greater or equal to 90.
# File 'lib/mongo/uri/options_mapper.rb', line 654
def convert_max_staleness(name, value) int = if value.is_a?(String) && /\A-?\d+\z/ =~ value value.to_i elsif value.is_a?(Integer) value end if int.nil? log_warn("Invalid max staleness value: #{value}") return nil end if int == -1 int = nil end if int && (int > 0 && int < 90 || int < 0) log_warn("max staleness should be either 0 or greater than 90: #{value}") int = nil end int end
#convert_ms(name, value) ⇒ Float
(private)
Ruby’s convention is to provide timeouts in seconds, not milliseconds and to use fractions where more precision is necessary. The connection string options are always in MS so we provide an easy conversion type.
# File 'lib/mongo/uri/options_mapper.rb', line 480
def convert_ms(name, value) case value when String if /\A-?\d+(\.\d+)?\z/ !~ value log_warn("Invalid ms value for #{name}: #{value}") return nil end if value.to_s[0] == '-' log_warn("#{name} cannot be a negative number") return nil end when Integer, Float if value < 0 log_warn("#{name} cannot be a negative number") return nil end else raise ArgumentError, "Can only convert Strings, Integers, or Floats to ms. Given: #{value.class}" end value.to_f / 1000 end
#convert_read_mode(name, value) ⇒ Symbol | String
(private)
Read preference mode transformation.
# File 'lib/mongo/uri/options_mapper.rb', line 702
def convert_read_mode(name, value) READ_MODE_MAP[value.downcase] || value end
#convert_read_set(name, value) ⇒ Hash
(private)
Read preference tag set extractor.
# File 'lib/mongo/uri/options_mapper.rb', line 755
def convert_read_set(name, value) hash_extractor('readPreferenceTags', value) end
#convert_read_tags(name, value) ⇒ Array
<Hash
> | nil
(private)
Read preference tags transformation.
# File 'lib/mongo/uri/options_mapper.rb', line 722
def (name, value) converted = convert_read_set(name, value) if converted [converted] else nil end end
#convert_repeated_bool(name, value) ⇒ Array
<true
| false
> | nil
(private)
Converts the value into a boolean and returns it wrapped in an array.
# File 'lib/mongo/uri/options_mapper.rb', line 373
def convert_repeated_bool(name, value) [convert_bool(name, value)] end
#convert_symbol(name, value) ⇒ Symbol (private)
Converts value
into a symbol.
# File 'lib/mongo/uri/options_mapper.rb', line 527
def convert_symbol(name, value) value.to_sym end
#convert_w(name, value) ⇒ Integer
| Symbol | String
(private)
Converts value
as a write concern.
If value
is the word “majority”, returns the symbol :majority
. If value
is a number, returns the number as an integer. Otherwise returns the string value
unchanged.
# File 'lib/mongo/uri/options_mapper.rb', line 769
def convert_w(name, value) case value when 'majority' :majority when /\A[0-9]+\z/ value.to_i else value end end
#convert_zlib_compression_level(name, value) ⇒ Integer
| nil
(private)
Parses the zlib compression level.
# File 'lib/mongo/uri/options_mapper.rb', line 810
def convert_zlib_compression_level(name, value) i = if value.is_a?(String) && /\A-?\d+\z/ =~ value value.to_i elsif value.is_a?(Integer) value end if i && (i >= -1 && i <= 9) i else log_warn("#{value} is not a valid zlibCompressionLevel") nil end end
#hash_extractor(name, value) ⇒ Hash
(private)
Extract values from the string and put them into a nested hash.
# File 'lib/mongo/uri/options_mapper.rb', line 849
def hash_extractor(name, value) h = {} value.split(',').each do |tag| k, v = tag.split(':') if v.nil? log_warn("Invalid hash value for #{name}: key `#{k}` does not have a value: #{value}") next end h[k.to_sym] = v end if h.empty? nil else h end end
#merge_uri_option(target, value, name) (private)
Merges a new option into the target.
If the option exists at the target destination the merge will be an addition.
Specifically required to append an additional tag set to the array of tag sets without overwriting the original.
# File 'lib/mongo/uri/options_mapper.rb', line 234
def merge_uri_option(target, value, name) if target.key?(name) if REPEATABLE_OPTIONS.include?(name) target[name] += value else log_warn("Repeated option key: #{name}.") end else target.merge!(name => value) end end
#revert_array(value) ⇒ Array
<String
> (private)
Reverts an array.
# File 'lib/mongo/uri/options_mapper.rb', line 556
def revert_array(value) value end
#revert_auth_mech(value) ⇒ String
(private)
Reverts auth mechanism.
# File 'lib/mongo/uri/options_mapper.rb', line 589
def revert_auth_mech(value) found = AUTH_MECH_MAP.detect do |k, v| v == value end if found found.first else raise ArgumentError, "Unknown auth mechanism #{value}" end end
#revert_auth_mech_props(value) ⇒ Hash
| nil
(private)
Reverts auth mechanism properties.
# File 'lib/mongo/uri/options_mapper.rb', line 632
def revert_auth_mech_props(value) value end
#revert_bool(value) ⇒ true
| false
| nil
(private)
Reverts a boolean type.
# File 'lib/mongo/uri/options_mapper.rb', line 353
def revert_bool(value) value end
#revert_integer(value) ⇒ Integer
| nil
(private)
Reverts an integer.
# File 'lib/mongo/uri/options_mapper.rb', line 457
def revert_integer(value) value end
#revert_inverse_bool(value) ⇒ true
| false
| nil
(private)
Reverts and inverts a boolean type.
# File 'lib/mongo/uri/options_mapper.rb', line 422
def revert_inverse_bool(value) value.nil? ? nil : !value end
#revert_max_staleness(value) ⇒ Integer
| nil
(private)
Reverts max staleness.
# File 'lib/mongo/uri/options_mapper.rb', line 683
def revert_max_staleness(value) value end
#revert_ms(value) ⇒ Integer
(private)
Reverts an ms.
# File 'lib/mongo/uri/options_mapper.rb', line 508
def revert_ms(value) (value * 1000).round end
#revert_read_mode(value) ⇒ String
(private)
Also known as: #stringify_read_mode
Reverts read mode.
# File 'lib/mongo/uri/options_mapper.rb', line 711
def revert_read_mode(value) value.to_s.gsub(/_(\w)/) { $1.upcase } end
#revert_read_tags(value) ⇒ Array
<Hash
> | nil
(private)
Reverts read tags.
# File 'lib/mongo/uri/options_mapper.rb', line 736
def (value) value end
#revert_repeated_bool(value) ⇒ Array
<true
| false
> | true
| false
| nil
(private)
Reverts a repeated boolean type.
# File 'lib/mongo/uri/options_mapper.rb', line 382
def revert_repeated_bool(value) value end
#revert_symbol(value) ⇒ String
(private)
Also known as: #stringify_symbol
Reverts a symbol.
# File 'lib/mongo/uri/options_mapper.rb', line 536
def revert_symbol(value) value.to_s end
#revert_w(value) ⇒ Integer
| String
(private)
Reverts write concern.
#revert_zlib_compression_level(value) ⇒ Integer
| nil
(private)
Reverts zlib compression level
# File 'lib/mongo/uri/options_mapper.rb', line 830
def revert_zlib_compression_level(value) value end
#ruby_to_smc(opts) ⇒ Hash
Converts Ruby options provided to “standardized MongoClient options”.
# File 'lib/mongo/uri/options_mapper.rb', line 126
def ruby_to_smc(opts) rv = {} URI_OPTION_MAP.each do |uri_key, spec| if spec[:group] v = opts[spec[:group]] v = v && v[spec[:name]] else v = opts[spec[:name]] end unless v.nil? if type = spec[:type] v = send("revert_#{type}", v) end canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key] unless canonical_key raise ArgumentError, "Option #{uri_key} is not known" end rv[canonical_key] = v end end # For options that default to true, remove the value if it is true. %w(retryReads retryWrites).each do |k| if rv[k] rv.delete(k) end end # Remove auth source when it is $external for mechanisms that default # (or require) that auth source. if %w(MONGODB-AWS).include?(rv['authMechanism']) && rv['authSource'] == '$external' rv.delete('authSource') end # ssl and tls are aliases, remove ssl ones rv.delete('ssl') # TODO remove authSource if it is the same as the database, # requires this method to know the database specified in the client. rv end
#ruby_to_string(opts) ⇒ Hash
Converts Ruby options provided to their representation in a ::Mongo::URI
string.
# File 'lib/mongo/uri/options_mapper.rb', line 169
def ruby_to_string(opts) rv = {} URI_OPTION_MAP.each do |uri_key, spec| if spec[:group] v = opts[spec[:group]] v = v && v[spec[:name]] else v = opts[spec[:name]] end unless v.nil? if type = spec[:type] v = send("stringify_#{type}", v) end canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key] unless canonical_key raise ArgumentError, "Option #{uri_key} is not known" end rv[canonical_key] = v end end # For options that default to true, remove the value if it is true. %w(retryReads retryWrites).each do |k| if rv[k] rv.delete(k) end end # Remove auth source when it is $external for mechanisms that default # (or require) that auth source. if %w(MONGODB-AWS).include?(rv['authMechanism']) && rv['authSource'] == '$external' rv.delete('authSource') end # ssl and tls are aliases, remove ssl ones rv.delete('ssl') # TODO remove authSource if it is the same as the database, # requires this method to know the database specified in the client. rv end
#smc_to_ruby(opts)
# File 'lib/mongo/uri/options_mapper.rb', line 88
def smc_to_ruby(opts) = {} opts.each do |key, value| strategy = URI_OPTION_MAP[key.downcase] if strategy.nil? log_warn("Unsupported URI option '#{key}' on URI '#{@string}'. It will be ignored.") return end group = strategy[:group] target = if group [group] || {} else end value = apply_transform(key, value, strategy[:type]) # Sometimes the value here would be nil, for example if we are processing # read preference tags or auth mechanism properties and all of the # data within is invalid. Ignore such options. unless value.nil? merge_uri_option(target, value, strategy[:name]) end if group && !target.empty? && ! .key?(group) [group] = target end end end
#stringify_array(value) ⇒ String
(private)
Stringifies an array.
# File 'lib/mongo/uri/options_mapper.rb', line 565
def stringify_array(value) value.join(',') end
#stringify_auth_mech(value) ⇒ String
| nil
(private)
Stringifies auth mechanism.
# File 'lib/mongo/uri/options_mapper.rb', line 605
def stringify_auth_mech(value) revert_auth_mech(value) rescue nil end
#stringify_auth_mech_props(value) ⇒ String
| nil
(private)
Stringifies auth mechanism properties.
# File 'lib/mongo/uri/options_mapper.rb', line 641
def stringify_auth_mech_props(value) return if value.nil? value.map { |k, v| "#{k}:#{v}" }.join(',') end
#stringify_bool(value) ⇒ String
| nil
(private)
Stringifies a boolean type.
# File 'lib/mongo/uri/options_mapper.rb', line 362
def stringify_bool(value) revert_bool(value)&.to_s end
#stringify_integer(value) ⇒ String
| nil
(private)
Stringifies an integer.
# File 'lib/mongo/uri/options_mapper.rb', line 466
def stringify_integer(value) revert_integer(value)&.to_s end
#stringify_inverse_bool(value) ⇒ String
| nil
(private)
Inverts and stringifies a boolean.
# File 'lib/mongo/uri/options_mapper.rb', line 431
def stringify_inverse_bool(value) revert_inverse_bool(value)&.to_s end
#stringify_max_staleness(value) ⇒ String
| nil
(private)
Stringifies max staleness.
# File 'lib/mongo/uri/options_mapper.rb', line 692
def stringify_max_staleness(value) revert_max_staleness(value)&.to_s end
#stringify_ms(value) ⇒ String
(private)
Stringifies an ms.
#stringify_read_mode(value) (private)
Alias for #revert_read_mode.
# File 'lib/mongo/uri/options_mapper.rb', line 714
alias :stringify_read_mode :revert_read_mode
#stringify_read_tags(value) ⇒ String
| nil
(private)
Stringifies read tags.
# File 'lib/mongo/uri/options_mapper.rb', line 745
def (value) value&.map { |ar| ar.map { |k, v| "#{k}:#{v}" }.join(',') } end
#stringify_repeated_bool(value) ⇒ Array
<true
| false
> | nil
(private)
Stringifies a repeated boolean type.
# File 'lib/mongo/uri/options_mapper.rb', line 391
def stringify_repeated_bool(value) rep = revert_repeated_bool(value) if rep&.is_a?(Array) rep.join(",") else rep end end
#stringify_symbol(value) (private)
Alias for #revert_symbol.
# File 'lib/mongo/uri/options_mapper.rb', line 539
alias :stringify_symbol :revert_symbol
#stringify_w(value) ⇒ String
(private)
Stringifies write concern.
#stringify_zlib_compression_level(value) ⇒ String
| nil
(private)
Stringifies zlib compression level
# File 'lib/mongo/uri/options_mapper.rb', line 839
def stringify_zlib_compression_level(value) revert_zlib_compression_level(value)&.to_s end