Skip to content

RUBY-3152 Linter appeasement for #2722 #2723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 1, 2023
Merged
118 changes: 49 additions & 69 deletions lib/mongo/server/app_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2016-2020 MongoDB Inc.
#
Expand All @@ -16,6 +15,7 @@
# limitations under the License.

require 'mongo/server/app_metadata/environment'
require 'mongo/server/app_metadata/platform'
require 'mongo/server/app_metadata/truncator'

module Mongo
Expand All @@ -24,35 +24,23 @@ class Server
# when a new connection is established.
#
# @api private
#
# @since 2.4.0
class AppMetadata
extend Forwardable

# The max application name byte size.
#
# @since 2.4.0
MAX_APP_NAME_SIZE = 128.freeze
MAX_APP_NAME_SIZE = 128

# The driver name.
#
# @since 2.4.0
DRIVER_NAME = 'mongo-ruby-driver'

# Option keys that affect auth mechanism negotiation.
#
# @api private
AUTH_OPTION_KEYS = [:user, :auth_source, :auth_mech].freeze
AUTH_OPTION_KEYS = %i[ user auth_source auth_mech].freeze

# Possible connection purposes.
#
# @api private
PURPOSES = %i(application monitor push_monitor).freeze
PURPOSES = %i[ application monitor push_monitor ].freeze

# Instantiate the new AppMetadata object.
#
# @api private
#
# @example Instantiate the app metadata.
# Mongo::Server::AppMetadata.new(options)
#
Expand Down Expand Up @@ -86,35 +74,33 @@ class AppMetadata
def initialize(options = {})
@app_name = options[:app_name].to_s if options[:app_name]
@platform = options[:platform]
if @purpose = options[:purpose]
unless PURPOSES.include?(@purpose)
raise ArgumentError, "Invalid purpose: #{@purpose}"
end
end

@purpose = check_purpose!(options[:purpose])

@compressors = options[:compressors] || []
@wrapping_libraries = options[:wrapping_libraries]
@server_api = options[:server_api]

if options[:user] && !options[:auth_mech]
auth_db = options[:auth_source] || 'admin'
@request_auth_mech = "#{auth_db}.#{options[:user]}"
end
return unless options[:user] && !options[:auth_mech]

auth_db = options[:auth_source] || 'admin'
@request_auth_mech = "#{auth_db}.#{options[:user]}"
end

# @return [ Symbol ] The purpose of the connection for which this
# app metadata is created.
#
# @api private
attr_reader :purpose

# @return [ String ] The platform information given when the object was
# instantiated.
attr_reader :platform

# @return [ Hash | nil ] The requested server API version.
#
# Thes hash can have the following items:
# - *:version* -- string
# - *:strict* -- boolean
# - *:deprecation_errors* -- boolean
#
# @api private
attr_reader :server_api

# @return [ Array<Hash> | nil ] Information about libraries wrapping
Expand All @@ -130,8 +116,6 @@ def initialize(options = {})
# @return [BSON::Document] Valid document for connection's handshake.
#
# @raise [ Error::InvalidApplicationName ] When the metadata are invalid.
#
# @api private
def validated_document
validate!
document
Expand All @@ -141,15 +125,13 @@ def validated_document
# handshake document.
#
# @return [BSON::Document] Document describing client for handshake.
#
# @api private
def client_document
@client_document ||=
BSON::Document.new.tap do |doc|
doc[:application] = { name: @app_name } if @app_name
doc[:driver] = driver_doc
doc[:os] = os_doc
doc[:platform] = platform
doc[:platform] = platform_string
env_doc.tap { |env| doc[:env] = env if env }
end
end
Expand All @@ -164,6 +146,7 @@ def validate!
if @app_name && @app_name.bytesize > MAX_APP_NAME_SIZE
raise Error::InvalidApplicationName.new(@app_name, MAX_APP_NAME_SIZE)
end

true
end

Expand All @@ -183,14 +166,13 @@ def document
end

def driver_doc
names = [DRIVER_NAME]
versions = [Mongo::VERSION]
if wrapping_libraries
wrapping_libraries.each do |library|
names << library[:name] || ''
versions << library[:version] || ''
end
names = [ DRIVER_NAME ]
versions = [ Mongo::VERSION ]
wrapping_libraries&.each do |library|
names << (library[:name] || '')
versions << (library[:version] || '')
end

{
name: names.join('|'),
version: versions.join('|'),
Expand All @@ -201,18 +183,25 @@ def os_doc
{
type: type,
name: name,
architecture: architecture
architecture: architecture,
}
end

# Returns the environment doc describing the current FaaS environment.
#
# @return [ Hash | nil ] the environment doc (or nil if not in a FaaS
# environment).
def env_doc
env = Environment.new
env.faas? ? env.to_h : nil
end

def type
(RbConfig::CONFIG && RbConfig::CONFIG['host_os']) ?
RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase : 'unknown'
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase
else
'unknown'
end
end

def name
Expand All @@ -223,31 +212,22 @@ def architecture
RbConfig::CONFIG['target_cpu']
end

def platform
if BSON::Environment.jruby?
ruby_versions = ["JRuby #{JRUBY_VERSION}", "like Ruby #{RUBY_VERSION}"]
platforms = [RUBY_PLATFORM, "JVM #{java.lang.System.get_property('java.version')}"]
else
ruby_versions = ["Ruby #{RUBY_VERSION}"]
platforms = [RUBY_PLATFORM]
end
platforms = [
@platform,
*ruby_versions,
*platforms,
RbConfig::CONFIG['build'],
]
if @purpose
platforms << @purpose.to_s[0].upcase
end
platform = platforms.compact.join(', ')
platforms = [platform]
if wrapping_libraries
wrapping_libraries.each do |library|
platforms << library[:platform] || ''
end
end
platforms.join('|')
def platform_string
Platform.new(self).to_s
end

# Verifies that the given purpose is either nil, or is one of the
# allowed purposes.
#
# @param [ String | nil ] purpose The purpose to validate
#
# @return [ String | nil ] the {{purpose}} argument
#
# @raise [ ArgumentError ] if the purpose is invalid
def check_purpose!(purpose)
return purpose unless purpose && !PURPOSES.include?(purpose)

raise ArgumentError, "Invalid purpose: #{purpose}"
end
end
end
Expand Down
114 changes: 114 additions & 0 deletions lib/mongo/server/app_metadata/platform.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true

# Copyright (C) 2016-2023 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 Server
class AppMetadata
# Implements the logic for building the platform string for the
# handshake.
#
# @api private
class Platform
# @return [ Mongo::Server::AppMetadata ] the metadata object to
# reference when building the platform string.
attr_reader :metadata

# Create a new Platform object, referencing the given metadata object.
#
# @param [ Mongo::Server::AppMetadata ] metadata the metadata object
# the reference when building the platform string.
def initialize(metadata)
@metadata = metadata
end

# Queries whether the current runtime is JRuby or not.
#
# @return [ true | false ] whether the runtime is JRuby or not.
def jruby?
BSON::Environment.jruby?
end

# Returns the list of Ruby versions that identify this runtime.
#
# @return [ Array<String> ] the list of ruby versions
def ruby_versions
if jruby?
[ "JRuby #{JRUBY_VERSION}", "like Ruby #{RUBY_VERSION}" ]
else
[ "Ruby #{RUBY_VERSION}" ]
end
end

# Returns the list of platform identifiers that identify this runtime.
#
# @return [ Array<String> ] the list of platform identifiers.
def platforms
[ RUBY_PLATFORM ].tap do |list|
list.push "JVM #{java_version}" if jruby?
end
end

# Returns the version of the current Java environment, or nil if not
# invoked with JRuby.
#
# @return [ String | nil ] the current Java version
def java_version
return nil unless jruby?

java.lang.System.get_property('java.version')
end

# Builds and returns the default platform list, for use when building
# the platform string.
#
# @return [ Array<String> ] the list of platform identifiers
def default_platform_list
[
metadata.platform,
*ruby_versions,
*platforms,
RbConfig::CONFIG['build']
]
end

# Returns a single letter representing the purpose reported to the
# metadata, or nil if no purpose was specified.
#
# @return [ String | nil ] the code representing the purpose
def purpose
return nil unless metadata.purpose

metadata.purpose.to_s[0].upcase
end

# Builds and returns the platform string by concatenating relevant
# values together.
#
# @return [ String ] the platform string
def to_s
primary = [ *default_platform_list, purpose ].compact.join(', ')
list = [ primary ]

metadata.wrapping_libraries&.each do |library|
list << (library[:platform] || '')
end

list.join('|')
end
end
end
end
end
Loading