<commit_before># frozen_string_literal: true
# encoding: utf-8

# Copyright (C) 2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
  class URI

    # Performs mapping between URI options and Ruby options.
    #
    # This class contains:
    #
    # - The mapping defining how URI options are converted to Ruby options.
    # - The mapping from downcased URI option names to canonical-cased URI
    #   option names.
    # - Methods to perform conversion of 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.
    #
    # URI option names are case insensitive. Ruby options are specified as
    # symbols (though in Client options use indifferent access).
    #
    # @api private
    class OptionsMapper

      include Loggable

      # Instantates the options mapper.
      #
      # @option opts [ Logger ] :logger A custom logger to use.
      def initialize(**opts)
        @options = opts
      end

      # @return [ Hash ] The options.
      attr_reader :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.
      #
      # @param [ String ] key URI option name.
      # @param [ String ] value The value of the option.
      # @param [ Hash ] uri_options The base option target.
      def add_uri_option(key, value, uri_options)
        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
          uri_options[group] || {}
        else
          uri_options
        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? && !uri_options.key?(group)
          uri_options[group] = target
        end
      end

      def smc_to_ruby(opts)
        uri_options = {}

        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
            uri_options[group] || {}
          else
            uri_options
          end

          if key == 'readConcernLevel'
            value = value.to_sym
          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? && !uri_options.key?(group)
            uri_options[group] = target
          end
        end

        #p uri_options
        uri_options
      end

      # Converts Ruby options provided to "standardized MongoClient options".
      #
      # @param [ Hash ] opts Ruby options to convert.
      #
      # @return [ Hash ] Standardized MongoClient options.
      def ruby_to_smc(opts)
        rv = {}
        URI_OPTION_MAP.each do |uri_key, spec|
          if spec[:group]<commit_msg>RUBY-3095 remove readConcernLevel special case (#2609)<commit_after># frozen_string_literal: true
# encoding: utf-8

# Copyright (C) 2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
  class URI

    # Performs mapping between URI options and Ruby options.
    #
    # This class contains:
    #
    # - The mapping defining how URI options are converted to Ruby options.
    # - The mapping from downcased URI option names to canonical-cased URI
    #   option names.
    # - Methods to perform conversion of 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.
    #
    # URI option names are case insensitive. Ruby options are specified as
    # symbols (though in Client options use indifferent access).
    #
    # @api private
    class OptionsMapper

      include Loggable

      # Instantates the options mapper.
      #
      # @option opts [ Logger ] :logger A custom logger to use.
      def initialize(**opts)
        @options = opts
      end

      # @return [ Hash ] The options.
      attr_reader :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.
      #
      # @param [ String ] key URI option name.
      # @param [ String ] value The value of the option.
      # @param [ Hash ] uri_options The base option target.
      def add_uri_option(key, value, uri_options)
        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
          uri_options[group] || {}
        else
          uri_options
        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? && !uri_options.key?(group)
          uri_options[group] = target
        end
      end

      def smc_to_ruby(opts)
        uri_options = {}

        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
            uri_options[group] || {}
          else
            uri_options
          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? && !uri_options.key?(group)
            uri_options[group] = target
          end
        end

        #p uri_options
        uri_options
      end

      # Converts Ruby options provided to "standardized MongoClient options".
      #
      # @param [ Hash ] opts Ruby options to convert.
      #
      # @return [ Hash ] Standardized MongoClient options.
      def ruby_to_smc(opts)
        rv = {}
        URI_OPTION_MAP.each do |uri_key, spec|
          if spec[:group]
{'fraction_non_alphanumeric': 0.07325268817204302, 'fraction_numerical': 0.0029121863799283156, 'mean_word_length': 2.1900678813862093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5372467', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2296, 'n_words': 946}