Skip to content

Commit e9d8dc1

Browse files
authored
RUBY-3152 Linter appeasement for #2722 (#2723)
* add environment detection logic * add environment info, and updated truncation logic * rubocop pass for new files * prose tests from handshake spec * updated truncation logic, and AWS detection logic * minor cleanups * linter fixes for AppMetadata * fix the low-hanging linter fruit * linting updates * linting updates * fix missed linter complaint * fix broken tests caused by refactoring for linter
1 parent 3c5cc83 commit e9d8dc1

File tree

5 files changed

+852
-935
lines changed

5 files changed

+852
-935
lines changed

lib/mongo/server/app_metadata.rb

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
# rubocop:todo all
32

43
# Copyright (C) 2016-2020 MongoDB Inc.
54
#
@@ -16,6 +15,7 @@
1615
# limitations under the License.
1716

1817
require 'mongo/server/app_metadata/environment'
18+
require 'mongo/server/app_metadata/platform'
1919
require 'mongo/server/app_metadata/truncator'
2020

2121
module Mongo
@@ -24,35 +24,23 @@ class Server
2424
# when a new connection is established.
2525
#
2626
# @api private
27-
#
28-
# @since 2.4.0
2927
class AppMetadata
3028
extend Forwardable
3129

3230
# The max application name byte size.
33-
#
34-
# @since 2.4.0
35-
MAX_APP_NAME_SIZE = 128.freeze
31+
MAX_APP_NAME_SIZE = 128
3632

3733
# The driver name.
38-
#
39-
# @since 2.4.0
4034
DRIVER_NAME = 'mongo-ruby-driver'
4135

4236
# Option keys that affect auth mechanism negotiation.
43-
#
44-
# @api private
45-
AUTH_OPTION_KEYS = [:user, :auth_source, :auth_mech].freeze
37+
AUTH_OPTION_KEYS = %i[ user auth_source auth_mech].freeze
4638

4739
# Possible connection purposes.
48-
#
49-
# @api private
50-
PURPOSES = %i(application monitor push_monitor).freeze
40+
PURPOSES = %i[ application monitor push_monitor ].freeze
5141

5242
# Instantiate the new AppMetadata object.
5343
#
54-
# @api private
55-
#
5644
# @example Instantiate the app metadata.
5745
# Mongo::Server::AppMetadata.new(options)
5846
#
@@ -86,35 +74,33 @@ class AppMetadata
8674
def initialize(options = {})
8775
@app_name = options[:app_name].to_s if options[:app_name]
8876
@platform = options[:platform]
89-
if @purpose = options[:purpose]
90-
unless PURPOSES.include?(@purpose)
91-
raise ArgumentError, "Invalid purpose: #{@purpose}"
92-
end
93-
end
77+
78+
@purpose = check_purpose!(options[:purpose])
79+
9480
@compressors = options[:compressors] || []
9581
@wrapping_libraries = options[:wrapping_libraries]
9682
@server_api = options[:server_api]
9783

98-
if options[:user] && !options[:auth_mech]
99-
auth_db = options[:auth_source] || 'admin'
100-
@request_auth_mech = "#{auth_db}.#{options[:user]}"
101-
end
84+
return unless options[:user] && !options[:auth_mech]
85+
86+
auth_db = options[:auth_source] || 'admin'
87+
@request_auth_mech = "#{auth_db}.#{options[:user]}"
10288
end
10389

10490
# @return [ Symbol ] The purpose of the connection for which this
10591
# app metadata is created.
106-
#
107-
# @api private
10892
attr_reader :purpose
10993

94+
# @return [ String ] The platform information given when the object was
95+
# instantiated.
96+
attr_reader :platform
97+
11098
# @return [ Hash | nil ] The requested server API version.
11199
#
112100
# Thes hash can have the following items:
113101
# - *:version* -- string
114102
# - *:strict* -- boolean
115103
# - *:deprecation_errors* -- boolean
116-
#
117-
# @api private
118104
attr_reader :server_api
119105

120106
# @return [ Array<Hash> | nil ] Information about libraries wrapping
@@ -130,8 +116,6 @@ def initialize(options = {})
130116
# @return [BSON::Document] Valid document for connection's handshake.
131117
#
132118
# @raise [ Error::InvalidApplicationName ] When the metadata are invalid.
133-
#
134-
# @api private
135119
def validated_document
136120
validate!
137121
document
@@ -141,15 +125,13 @@ def validated_document
141125
# handshake document.
142126
#
143127
# @return [BSON::Document] Document describing client for handshake.
144-
#
145-
# @api private
146128
def client_document
147129
@client_document ||=
148130
BSON::Document.new.tap do |doc|
149131
doc[:application] = { name: @app_name } if @app_name
150132
doc[:driver] = driver_doc
151133
doc[:os] = os_doc
152-
doc[:platform] = platform
134+
doc[:platform] = platform_string
153135
env_doc.tap { |env| doc[:env] = env if env }
154136
end
155137
end
@@ -164,6 +146,7 @@ def validate!
164146
if @app_name && @app_name.bytesize > MAX_APP_NAME_SIZE
165147
raise Error::InvalidApplicationName.new(@app_name, MAX_APP_NAME_SIZE)
166148
end
149+
167150
true
168151
end
169152

@@ -183,14 +166,13 @@ def document
183166
end
184167

185168
def driver_doc
186-
names = [DRIVER_NAME]
187-
versions = [Mongo::VERSION]
188-
if wrapping_libraries
189-
wrapping_libraries.each do |library|
190-
names << library[:name] || ''
191-
versions << library[:version] || ''
192-
end
169+
names = [ DRIVER_NAME ]
170+
versions = [ Mongo::VERSION ]
171+
wrapping_libraries&.each do |library|
172+
names << (library[:name] || '')
173+
versions << (library[:version] || '')
193174
end
175+
194176
{
195177
name: names.join('|'),
196178
version: versions.join('|'),
@@ -201,18 +183,25 @@ def os_doc
201183
{
202184
type: type,
203185
name: name,
204-
architecture: architecture
186+
architecture: architecture,
205187
}
206188
end
207189

190+
# Returns the environment doc describing the current FaaS environment.
191+
#
192+
# @return [ Hash | nil ] the environment doc (or nil if not in a FaaS
193+
# environment).
208194
def env_doc
209195
env = Environment.new
210196
env.faas? ? env.to_h : nil
211197
end
212198

213199
def type
214-
(RbConfig::CONFIG && RbConfig::CONFIG['host_os']) ?
215-
RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase : 'unknown'
200+
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
201+
RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase
202+
else
203+
'unknown'
204+
end
216205
end
217206

218207
def name
@@ -223,31 +212,22 @@ def architecture
223212
RbConfig::CONFIG['target_cpu']
224213
end
225214

226-
def platform
227-
if BSON::Environment.jruby?
228-
ruby_versions = ["JRuby #{JRUBY_VERSION}", "like Ruby #{RUBY_VERSION}"]
229-
platforms = [RUBY_PLATFORM, "JVM #{java.lang.System.get_property('java.version')}"]
230-
else
231-
ruby_versions = ["Ruby #{RUBY_VERSION}"]
232-
platforms = [RUBY_PLATFORM]
233-
end
234-
platforms = [
235-
@platform,
236-
*ruby_versions,
237-
*platforms,
238-
RbConfig::CONFIG['build'],
239-
]
240-
if @purpose
241-
platforms << @purpose.to_s[0].upcase
242-
end
243-
platform = platforms.compact.join(', ')
244-
platforms = [platform]
245-
if wrapping_libraries
246-
wrapping_libraries.each do |library|
247-
platforms << library[:platform] || ''
248-
end
249-
end
250-
platforms.join('|')
215+
def platform_string
216+
Platform.new(self).to_s
217+
end
218+
219+
# Verifies that the given purpose is either nil, or is one of the
220+
# allowed purposes.
221+
#
222+
# @param [ String | nil ] purpose The purpose to validate
223+
#
224+
# @return [ String | nil ] the {{purpose}} argument
225+
#
226+
# @raise [ ArgumentError ] if the purpose is invalid
227+
def check_purpose!(purpose)
228+
return purpose unless purpose && !PURPOSES.include?(purpose)
229+
230+
raise ArgumentError, "Invalid purpose: #{purpose}"
251231
end
252232
end
253233
end
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (C) 2016-2023 MongoDB Inc.
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+
# http://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 Mongo
18+
class Server
19+
class AppMetadata
20+
# Implements the logic for building the platform string for the
21+
# handshake.
22+
#
23+
# @api private
24+
class Platform
25+
# @return [ Mongo::Server::AppMetadata ] the metadata object to
26+
# reference when building the platform string.
27+
attr_reader :metadata
28+
29+
# Create a new Platform object, referencing the given metadata object.
30+
#
31+
# @param [ Mongo::Server::AppMetadata ] metadata the metadata object
32+
# the reference when building the platform string.
33+
def initialize(metadata)
34+
@metadata = metadata
35+
end
36+
37+
# Queries whether the current runtime is JRuby or not.
38+
#
39+
# @return [ true | false ] whether the runtime is JRuby or not.
40+
def jruby?
41+
BSON::Environment.jruby?
42+
end
43+
44+
# Returns the list of Ruby versions that identify this runtime.
45+
#
46+
# @return [ Array<String> ] the list of ruby versions
47+
def ruby_versions
48+
if jruby?
49+
[ "JRuby #{JRUBY_VERSION}", "like Ruby #{RUBY_VERSION}" ]
50+
else
51+
[ "Ruby #{RUBY_VERSION}" ]
52+
end
53+
end
54+
55+
# Returns the list of platform identifiers that identify this runtime.
56+
#
57+
# @return [ Array<String> ] the list of platform identifiers.
58+
def platforms
59+
[ RUBY_PLATFORM ].tap do |list|
60+
list.push "JVM #{java_version}" if jruby?
61+
end
62+
end
63+
64+
# Returns the version of the current Java environment, or nil if not
65+
# invoked with JRuby.
66+
#
67+
# @return [ String | nil ] the current Java version
68+
def java_version
69+
return nil unless jruby?
70+
71+
java.lang.System.get_property('java.version')
72+
end
73+
74+
# Builds and returns the default platform list, for use when building
75+
# the platform string.
76+
#
77+
# @return [ Array<String> ] the list of platform identifiers
78+
def default_platform_list
79+
[
80+
metadata.platform,
81+
*ruby_versions,
82+
*platforms,
83+
RbConfig::CONFIG['build']
84+
]
85+
end
86+
87+
# Returns a single letter representing the purpose reported to the
88+
# metadata, or nil if no purpose was specified.
89+
#
90+
# @return [ String | nil ] the code representing the purpose
91+
def purpose
92+
return nil unless metadata.purpose
93+
94+
metadata.purpose.to_s[0].upcase
95+
end
96+
97+
# Builds and returns the platform string by concatenating relevant
98+
# values together.
99+
#
100+
# @return [ String ] the platform string
101+
def to_s
102+
primary = [ *default_platform_list, purpose ].compact.join(', ')
103+
list = [ primary ]
104+
105+
metadata.wrapping_libraries&.each do |library|
106+
list << (library[:platform] || '')
107+
end
108+
109+
list.join('|')
110+
end
111+
end
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)