Skip to content

Commit f57decb

Browse files
committed
code review feedback
1 parent 7910476 commit f57decb

File tree

2 files changed

+76
-76
lines changed

2 files changed

+76
-76
lines changed

lib/mongo/uri.rb

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ def self.uri_option(uri_key, name, extra = {})
435435

436436
# Write Options
437437
uri_option 'w', :w, :group => :write
438-
uri_option 'journal', :j, :group => :write, :type => :bool
438+
uri_option 'journal', :j, :group => :write, :type => :journal
439439
uri_option 'fsync', :fsync, :group => :write
440-
uri_option 'wtimeoutms', :timeout, :group => :write, :type => :unsigned_int
440+
uri_option 'wtimeoutms', :timeout, :group => :write, :type => :wtimeout
441441

442442
# Read Options
443443
uri_option 'readpreference', :mode, :group => :read, :type => :read_mode
@@ -452,7 +452,7 @@ def self.uri_option(uri_key, name, extra = {})
452452
# Security Options
453453
uri_option 'ssl', :ssl
454454
uri_option 'tls', :ssl
455-
uri_option 'tlsallowinvalidcertificates', :ssl_verify, :type => :inverse_bool
455+
uri_option 'tlsallowinvalidcertificates', :ssl_verify, :type => :ssl_verify
456456
uri_option 'tlscafilepath', :ssl_ca_cert
457457
uri_option 'tlsclientcertfilepath', :ssl_cert
458458
uri_option 'tlsclientkeyfilepath', :ssl_key
@@ -471,7 +471,7 @@ def self.uri_option(uri_key, name, extra = {})
471471
uri_option 'appname', :app_name
472472
uri_option 'compressors', :compressors, :type => :array
473473
uri_option 'readconcernlevel', :read_concern
474-
uri_option 'retrywrites', :retry_writes, :type => :bool
474+
uri_option 'retrywrites', :retry_writes, :type => :retry_writes
475475
uri_option 'zlibcompressionlevel', :zlib_compression_level, :type => :zlib_compression_level
476476

477477
# Casts option values that do not have a specifically provided
@@ -582,7 +582,9 @@ def auth_source(value)
582582
#
583583
# @return [Symbol] The transformed authentication mechanism.
584584
def auth_mech(value)
585-
AUTH_MECH_MAP[value.upcase] || log_warn("#{value} is not a valid auth mechanism")
585+
AUTH_MECH_MAP[value.upcase].tap do |mech|
586+
log_warn("#{value} is not a valid auth mechanism") unless mech
587+
end
586588
end
587589

588590
# Read preference mode transformation.
@@ -631,9 +633,9 @@ def auth_mech_props(value)
631633
# @param value [ String ] The zlib compression level string.
632634
#
633635
# @return [ Integer | nil ] The compression level value if it is between -1 and 9 (inclusive),
634-
# otherwise nil (and a warning will be raised).
636+
# otherwise nil (and a warning will be logged).
635637
def zlib_compression_level(value)
636-
if /^-?\d+$/ =~ value
638+
if /\A-?\d+\z/ =~ value
637639
i = value.to_i
638640

639641
if i >= -1 && i <= 9
@@ -645,47 +647,69 @@ def zlib_compression_level(value)
645647
nil
646648
end
647649

650+
# Parses the journal value.
651+
#
652+
# @param value [ String ] The journal value.
653+
#
654+
# @return [ true | false | nil ] The journal value parsed out, otherwise nil (and a warning
655+
# will be raised).
656+
def journal(value)
657+
bool('journal', value)
658+
end
659+
660+
# Parses the ssl_verify value. Note that this will be the inverse of the value of
661+
# tlsAllowInvalidCertificates (if present).
662+
#
663+
# @param value [ String ] The tlsAllowInvalidCertificates value.
664+
#
665+
# @return [ true | false | nil ] The ssl_verify value parsed out, otherwise nil (and a warning
666+
# will be raised).
667+
def ssl_verify(value)
668+
b = bool('tlsAllowInvalidCertificates', value)
669+
670+
if b.nil?
671+
nil
672+
else
673+
!b
674+
end
675+
end
676+
677+
# Parses the retryWrites value.
678+
#
679+
# @param value [ String ] The retryWrites value.
680+
#
681+
# @return [ true | false | nil ] The boolean value parsed out, otherwise nil (and a warning
682+
# will be raised).
683+
def retry_writes(value)
684+
bool('retryWrites', value)
685+
end
648686

649687
# Parses a boolean value.
650688
#
651689
# @param value [ String ] The URI option value.
652690
#
653691
# @return [ true | false | nil ] The boolean value parsed out, otherwise nil (and a warning
654692
# will be raised).
655-
def bool(value)
693+
def bool(name, value)
656694
case value
657695
when "true"
658696
true
659697
when "false"
660698
false
661699
else
662-
log_warn("invalid boolean: #{value}")
700+
log_warn("invalid boolean option for #{name}: #{value}")
663701
nil
664702
end
665703
end
666704

667-
# Parses a boolean value and returns its inverse. This is used for options where the spec
668-
# defines the boolean value semantics of enabling/disabling something in the reverse way that
669-
# the driver does. For instance, the client option `ssl_verify` will disable certificate
670-
# checking when the value is false, but the spec defines the option
671-
# `tlsAllowInvalidCertificates`, which disables certificate checking when the value is true.
672-
#
673-
# @param value [ String ] The URI option.
674-
#
675-
# @return [ true | false | nil ] The inverse of boolean value parsed out, otherwise nil (and a
676-
# warning will be raised).
677-
def inverse_bool(value)
678-
!bool(value)
679-
end
680-
681705
# Parses the max staleness value, which must be either "0" or an integer greater or equal to 90.
682706
#
683707
# @param value [ String ] The max staleness string.
684708
#
685709
# @return [ Integer | nil ] The max staleness integer parsed out if it is valid, otherwise nil
686-
# (and a warning will be raised).
710+
# (and a warning will be logged).
687711
def max_staleness(value)
688-
if /^\d+$/ =~ value
712+
if /\A\d+\z/ =~ value
689713
int = value.to_i
690714

691715
if int >= 0 && int < 90
@@ -705,9 +729,9 @@ def max_staleness(value)
705729
#
706730
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
707731
# raised).
708-
def unsigned_int(value)
709-
unless /^?\d+$/ =~ value
710-
log_warn("Invalid unsigned integer value: #{value}")
732+
def wtimeout(value)
733+
unless /\A\d+\z/ =~ value
734+
log_warn("Invalid wtimeoutMS value: #{value}")
711735
return nil
712736
end
713737

@@ -724,7 +748,7 @@ def unsigned_int(value)
724748
#
725749
# @since 2.0.0
726750
def ms_convert(value)
727-
unless /^-?\d+(\.\d+)?$/ =~ value
751+
unless /\A-?\d+(\.\d+)?\z/ =~ value
728752
log_warn("Invalid ms value: #{value}")
729753
return nil
730754
end

spec/spec_tests/uri_options_spec.rb

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,6 @@ def create_resolver(timeout, ssl_options)
2323
FAMILY_MAP[info.first[4]].new(info[3], port, host)
2424
end
2525
end
26-
27-
class Server
28-
29-
# The constructor keeps the same API, but does not instantiate a
30-
# monitor and run it.
31-
alias :original_initialize :initialize
32-
def initialize(address, cluster, monitoring, event_listeners, options = {})
33-
@address = address
34-
@cluster = cluster
35-
@monitoring = monitoring
36-
@options = options.freeze
37-
@monitor = Monitor.new(address, event_listeners, Monitoring.new, options)
38-
end
39-
40-
# Disconnect simply needs to return true since we have no monitor and
41-
# no connection.
42-
alias :original_disconnect! :disconnect!
43-
def disconnect!; true; end
44-
end
4526
end
4627
end
4728

@@ -54,14 +35,6 @@ class Address
5435
alias :create_resolver :original_create_resolver
5536
remove_method(:original_create_resolver)
5637
end
57-
58-
class Server
59-
alias :initialize :original_initialize
60-
remove_method(:original_initialize)
61-
62-
alias :disconnect! :original_disconnect!
63-
remove_method(:original_disconnect!)
64-
end
6538
end
6639
end
6740

@@ -83,26 +56,7 @@ class Server
8356
end
8457
end
8558

86-
context 'when the uri should warn', if: test.warn? do
87-
88-
before do
89-
expect(Mongo::Logger.logger).to receive(:warn)
90-
end
91-
92-
it 'warns' do
93-
expect(test.client).to be_a(Mongo::Client)
94-
end
95-
end
96-
97-
context 'when the uri is valid', if: test.valid? && !test.warn? do
98-
99-
before do
100-
expect(Mongo::Logger.logger).not_to receive(:warn)
101-
end
102-
103-
it 'does not raise an exception or warning' do
104-
expect(test.uri).to be_a(Mongo::URI)
105-
end
59+
context 'when the uri is valid', if: test.valid? do
10660

10761
it 'creates a client with the correct hosts' do
10862
expect(test.client).to have_hosts(test)
@@ -115,6 +69,28 @@ class Server
11569
it 'creates a client with the correct options' do
11670
expect(test.client).to match_options(test)
11771
end
72+
73+
context 'when the uri should not warn', if: !test.warn? do
74+
75+
before do
76+
expect(Mongo::Logger.logger).not_to receive(:warn)
77+
end
78+
79+
it 'does not raise an exception or warning' do
80+
expect(test.uri).to be_a(Mongo::URI)
81+
end
82+
end
83+
84+
context 'when the uri should warn', if: test.warn? do
85+
86+
before do
87+
expect(Mongo::Logger.logger).to receive(:warn)
88+
end
89+
90+
it 'warns' do
91+
expect(test.client).to be_a(Mongo::Client)
92+
end
93+
end
11894
end
11995
end
12096
end

0 commit comments

Comments
 (0)