bson 4.15.0-java → 5.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -4
  4. data/Rakefile +5 -0
  5. data/lib/bson/active_support.rb +1 -0
  6. data/lib/bson/array.rb +57 -31
  7. data/lib/bson/big_decimal.rb +16 -6
  8. data/lib/bson/binary.rb +255 -128
  9. data/lib/bson/boolean.rb +1 -0
  10. data/lib/bson/code.rb +9 -11
  11. data/lib/bson/code_with_scope.rb +8 -10
  12. data/lib/bson/config.rb +1 -27
  13. data/lib/bson/date.rb +2 -1
  14. data/lib/bson/date_time.rb +2 -1
  15. data/lib/bson/db_pointer.rb +11 -12
  16. data/lib/bson/dbref.rb +11 -9
  17. data/lib/bson/decimal128/builder.rb +9 -8
  18. data/lib/bson/decimal128.rb +24 -110
  19. data/lib/bson/document.rb +1 -0
  20. data/lib/bson/environment.rb +1 -0
  21. data/lib/bson/error/bson_decode_error.rb +11 -0
  22. data/lib/bson/error/ext_json_parse_error.rb +11 -0
  23. data/lib/bson/error/illegal_key.rb +23 -0
  24. data/lib/bson/error/invalid_binary_type.rb +37 -0
  25. data/lib/bson/error/invalid_dbref_argument.rb +12 -0
  26. data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
  27. data/lib/bson/error/invalid_decimal128_range.rb +27 -0
  28. data/lib/bson/error/invalid_decimal128_string.rb +26 -0
  29. data/lib/bson/error/invalid_key.rb +24 -0
  30. data/lib/bson/error/invalid_object_id.rb +11 -0
  31. data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
  32. data/lib/bson/error/unrepresentable_precision.rb +19 -0
  33. data/lib/bson/error/unserializable_class.rb +13 -0
  34. data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
  35. data/lib/bson/error/unsupported_type.rb +11 -0
  36. data/lib/bson/error.rb +16 -28
  37. data/lib/bson/ext_json.rb +1 -0
  38. data/lib/bson/false_class.rb +2 -1
  39. data/lib/bson/float.rb +2 -1
  40. data/lib/bson/hash.rb +127 -72
  41. data/lib/bson/int32.rb +16 -4
  42. data/lib/bson/int64.rb +16 -4
  43. data/lib/bson/integer.rb +3 -4
  44. data/lib/bson/json.rb +1 -0
  45. data/lib/bson/max_key.rb +7 -9
  46. data/lib/bson/min_key.rb +7 -9
  47. data/lib/bson/nil_class.rb +1 -0
  48. data/lib/bson/object.rb +5 -25
  49. data/lib/bson/object_id.rb +75 -121
  50. data/lib/bson/open_struct.rb +3 -2
  51. data/lib/bson/regexp.rb +35 -64
  52. data/lib/bson/registry.rb +2 -6
  53. data/lib/bson/specialized.rb +2 -1
  54. data/lib/bson/string.rb +4 -27
  55. data/lib/bson/symbol.rb +22 -19
  56. data/lib/bson/time.rb +2 -1
  57. data/lib/bson/time_with_zone.rb +13 -1
  58. data/lib/bson/timestamp.rb +2 -1
  59. data/lib/bson/true_class.rb +2 -1
  60. data/lib/bson/undefined.rb +14 -0
  61. data/lib/bson/version.rb +2 -1
  62. data/lib/bson-ruby.jar +0 -0
  63. data/lib/bson.rb +3 -2
  64. data/spec/bson/array_spec.rb +19 -60
  65. data/spec/bson/big_decimal_spec.rb +16 -4
  66. data/spec/bson/binary_spec.rb +83 -74
  67. data/spec/bson/binary_uuid_spec.rb +1 -0
  68. data/spec/bson/boolean_spec.rb +1 -0
  69. data/spec/bson/byte_buffer_read_spec.rb +1 -0
  70. data/spec/bson/byte_buffer_spec.rb +1 -0
  71. data/spec/bson/byte_buffer_write_spec.rb +1 -0
  72. data/spec/bson/code_spec.rb +5 -3
  73. data/spec/bson/code_with_scope_spec.rb +5 -3
  74. data/spec/bson/config_spec.rb +1 -35
  75. data/spec/bson/date_spec.rb +1 -0
  76. data/spec/bson/date_time_spec.rb +1 -0
  77. data/spec/bson/dbref_legacy_spec.rb +20 -3
  78. data/spec/bson/dbref_spec.rb +9 -9
  79. data/spec/bson/decimal128_spec.rb +40 -20
  80. data/spec/bson/document_as_spec.rb +1 -0
  81. data/spec/bson/document_spec.rb +1 -1
  82. data/spec/bson/ext_json_parse_spec.rb +1 -0
  83. data/spec/bson/false_class_spec.rb +8 -0
  84. data/spec/bson/float_spec.rb +8 -3
  85. data/spec/bson/hash_as_spec.rb +1 -0
  86. data/spec/bson/hash_spec.rb +87 -75
  87. data/spec/bson/int32_spec.rb +21 -6
  88. data/spec/bson/int64_spec.rb +21 -6
  89. data/spec/bson/integer_spec.rb +45 -13
  90. data/spec/bson/json_spec.rb +1 -0
  91. data/spec/bson/max_key_spec.rb +5 -3
  92. data/spec/bson/min_key_spec.rb +5 -3
  93. data/spec/bson/nil_class_spec.rb +1 -0
  94. data/spec/bson/object_id_spec.rb +43 -4
  95. data/spec/bson/object_spec.rb +2 -1
  96. data/spec/bson/open_struct_spec.rb +14 -71
  97. data/spec/bson/raw_spec.rb +9 -15
  98. data/spec/bson/regexp_spec.rb +4 -3
  99. data/spec/bson/registry_spec.rb +2 -1
  100. data/spec/bson/string_spec.rb +13 -38
  101. data/spec/bson/symbol_raw_spec.rb +25 -0
  102. data/spec/bson/symbol_spec.rb +15 -18
  103. data/spec/bson/time_spec.rb +1 -0
  104. data/spec/bson/time_with_zone_spec.rb +1 -0
  105. data/spec/bson/timestamp_spec.rb +1 -0
  106. data/spec/bson/true_class_spec.rb +8 -0
  107. data/spec/bson/undefined_spec.rb +27 -0
  108. data/spec/bson_spec.rb +1 -0
  109. data/spec/runners/common_driver.rb +6 -5
  110. data/spec/runners/corpus.rb +6 -0
  111. data/spec/runners/corpus_legacy.rb +1 -0
  112. data/spec/shared/lib/mrss/constraints.rb +8 -16
  113. data/spec/shared/lib/mrss/docker_runner.rb +30 -3
  114. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  115. data/spec/shared/lib/mrss/event_subscriber.rb +15 -5
  116. data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
  117. data/spec/shared/lib/mrss/server_version_registry.rb +16 -23
  118. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  119. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  120. data/spec/shared/lib/mrss/utils.rb +28 -6
  121. data/spec/shared/share/Dockerfile.erb +68 -85
  122. data/spec/shared/shlib/config.sh +27 -0
  123. data/spec/shared/shlib/server.sh +73 -24
  124. data/spec/shared/shlib/set_env.sh +39 -1
  125. data/spec/spec_helper.rb +1 -0
  126. data/spec/spec_tests/common_driver_spec.rb +9 -4
  127. data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
  128. data/spec/spec_tests/corpus_spec.rb +13 -3
  129. data/spec/spec_tests/data/corpus/binary.json +5 -0
  130. data/spec/spec_tests/data/corpus/code.json +13 -13
  131. data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
  132. data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
  133. data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
  134. data/spec/spec_tests/data/corpus/document.json +20 -0
  135. data/spec/spec_tests/data/corpus/symbol.json +7 -7
  136. data/spec/spec_tests/data/corpus/top.json +18 -3
  137. data/spec/support/shared_examples.rb +28 -5
  138. data/spec/support/spec_config.rb +1 -0
  139. data/spec/support/utils.rb +49 -1
  140. data.tar.gz.sig +0 -0
  141. metadata +166 -142
  142. metadata.gz.sig +0 -0
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -54,24 +55,22 @@ module BSON
54
55
  ref == other.ref && id == other.id
55
56
  end
56
57
 
57
- # Get the DBPointer as JSON hash data
58
+ # Return a representation of the object for use in
59
+ # application-level JSON serialization. Since BSON::DbPointer
60
+ # is used exclusively in BSON-related contexts, this
61
+ # method returns the canonical Extended JSON representation.
58
62
  #
59
- # @return [ Hash ] The DBPointer as a JSON hash.
60
- #
61
- # @deprecated Use as_extended_json instead.
62
- def as_json(*args)
63
+ # @return [ Hash ] The extended json representation.
64
+ def as_json(*_args)
63
65
  as_extended_json
64
66
  end
65
67
 
66
68
  # Converts this object to a representation directly serializable to
67
69
  # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
68
70
  #
69
- # @option options [ true | false ] :relaxed Whether to produce relaxed
70
- # extended JSON representation.
71
- #
72
71
  # @return [ Hash ] The extended json representation.
73
- def as_extended_json(**options)
74
- {'$dbPointer' => { "$ref" => ref, '$id' => id.as_extended_json }}
72
+ def as_extended_json(**_options)
73
+ { '$dbPointer' => { "$ref" => ref, '$id' => id.as_extended_json } }
75
74
  end
76
75
 
77
76
  # Encode the DBPointer.
@@ -79,9 +78,9 @@ module BSON
79
78
  # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
80
79
  #
81
80
  # @see http://bsonspec.org/#/specification
82
- def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
81
+ def to_bson(buffer = ByteBuffer.new)
83
82
  buffer.put_string(ref)
84
- id.to_bson(buffer, validating_keys)
83
+ id.to_bson(buffer)
85
84
  buffer
86
85
  end
87
86
 
data/lib/bson/dbref.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # encoding: utf-8
2
+ # rubocop:todo all
3
3
 
4
4
  # Copyright (C) 2015-2021 MongoDB Inc.
5
5
  #
@@ -74,18 +74,21 @@ module BSON
74
74
  # this parameter must be a String containing the collection name.
75
75
  # @param [ Object ] id The object id, when using the legacy API.
76
76
  # @param [ String ] database The database name, when using the legacy API.
77
+ #
78
+ # @raise [ BSON::Error::InvalidDBRefArgument ] if giving invalid arguments
79
+ # to the constructor.
77
80
  def initialize(hash_or_collection, id = nil, database = nil)
78
81
  if hash_or_collection.is_a?(Hash)
79
82
  hash = hash_or_collection
80
83
 
81
84
  unless id.nil? && database.nil?
82
- raise ArgumentError, 'When using the hash API, DBRef constructor accepts only one argument'
85
+ raise Error::InvalidDBRefArgument, 'When using the hash API, DBRef constructor accepts only one argument'
83
86
  end
84
87
  else
85
88
  warn("BSON::DBRef constructor called with the legacy API - please use the hash API instead")
86
89
 
87
90
  if id.nil?
88
- raise ArgumentError, 'When using the legacy constructor API, id must be provided'
91
+ raise Error::InvalidDBRefArgument, 'When using the legacy constructor API, id must be provided'
89
92
  end
90
93
 
91
94
  hash = {
@@ -98,17 +101,17 @@ module BSON
98
101
  hash = reorder_fields(hash)
99
102
  %w($ref $id).each do |key|
100
103
  unless hash[key]
101
- raise ArgumentError, "DBRef must have #{key}: #{hash}"
104
+ raise Error::InvalidDBRefArgument, "DBRef must have #{key}: #{hash}"
102
105
  end
103
106
  end
104
107
 
105
108
  unless hash['$ref'].is_a?(String)
106
- raise ArgumentError, "The value for key $ref must be a string, got: #{hash['$ref']}"
109
+ raise Error::InvalidDBRefArgument, "The value for key $ref must be a string, got: #{hash['$ref']}"
107
110
  end
108
111
 
109
112
  if db = hash['$db']
110
113
  unless db.is_a?(String)
111
- raise ArgumentError, "The value for key $db must be a string, got: #{hash['$db']}"
114
+ raise Error::InvalidDBRefArgument, "The value for key $db must be a string, got: #{hash['$db']}"
112
115
  end
113
116
  end
114
117
 
@@ -121,11 +124,10 @@ module BSON
121
124
  # dbref.to_bson
122
125
  #
123
126
  # @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
124
- # @param [ true, false ] validating_keys Whether keys should be validated when serializing.
125
127
  #
126
128
  # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
127
- def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
128
- as_json.to_bson(buffer, validating_keys)
129
+ def to_bson(buffer = ByteBuffer.new)
130
+ as_json.to_bson(buffer)
129
131
  end
130
132
 
131
133
  private
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2016-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -86,11 +87,11 @@ module BSON
86
87
 
87
88
  def validate_range!(exponent, significand)
88
89
  unless valid_exponent?(exponent)
89
- raise Decimal128::InvalidRange.new
90
+ raise Error::InvalidDecimal128Range.new
90
91
  end
91
92
 
92
93
  unless valid_significand?(significand)
93
- raise Decimal128::UnrepresentablePrecision.new
94
+ raise Error::UnrepresentablePrecision.new
94
95
  end
95
96
  end
96
97
 
@@ -189,9 +190,9 @@ module BSON
189
190
  private
190
191
 
191
192
  def to_bits
192
- original, sign, digits_str = SIGN_AND_DIGITS_REGEX.match(@string).to_a
193
- digits, e, scientific_exp = digits_str.partition(SCIENTIFIC_EXPONENT_REGEX)
194
- before_decimal, decimal, after_decimal = digits.partition('.')
193
+ _, sign, digits_str = SIGN_AND_DIGITS_REGEX.match(@string).to_a
194
+ digits, _, scientific_exp = digits_str.partition(SCIENTIFIC_EXPONENT_REGEX)
195
+ before_decimal, _, after_decimal = digits.partition('.')
195
196
 
196
197
  significand_str = before_decimal << after_decimal
197
198
  significand_str = SIGNIFICAND_WITH_LEADING_ZEROS_REGEX.match(significand_str).to_a[2]
@@ -262,7 +263,7 @@ module BSON
262
263
  end
263
264
 
264
265
  def validate_format!
265
- raise BSON::Decimal128::InvalidString.new unless @string =~ VALID_DECIMAL128_STRING_REGEX
266
+ raise Error::InvalidDecimal128String.new unless @string =~ VALID_DECIMAL128_STRING_REGEX
266
267
  end
267
268
  end
268
269
 
@@ -317,7 +318,7 @@ module BSON
317
318
  end
318
319
 
319
320
  def to_bits
320
- sign, significand_str, base, exp = @big_decimal.split
321
+ sign, significand_str, _, exp = @big_decimal.split
321
322
  exponent = @big_decimal.zero? ? 0 : exp - significand_str.length
322
323
  is_negative = (sign == ::BigDecimal::SIGN_NEGATIVE_FINITE || sign == ::BigDecimal::SIGN_NEGATIVE_ZERO)
323
324
  Builder.parts_to_bits(significand_str.to_i,
@@ -452,4 +453,4 @@ module BSON
452
453
  end
453
454
  end
454
455
  end
455
- end
456
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2016-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -57,17 +58,18 @@ module BSON
57
58
  # @since 4.2.0
58
59
  NATIVE_TYPE = BigDecimal
59
60
 
60
- # Get the Decimal128 as JSON hash data.
61
+ # Return a string representation of the Decimal128 use in standard
62
+ # application-level JSON serialization. Returns nil for non-real
63
+ # numbers such as NaN and Infinity to be compatible with ActiveSupport.
64
+ # This method is intentionally different from #as_extended_json.
61
65
  #
62
- # @example Get the Decimal128 as a JSON hash.
66
+ # @example Get the Decimal128 as a JSON-serializable object.
63
67
  # decimal.as_json
64
68
  #
65
- # @return [ Hash ] The number as a JSON hash.
66
- #
67
- # @since 4.2.0
68
- # @deprecated Use as_extended_json instead.
69
+ # @return [ String | nil ] The decimal128 as a String or nil for non-representable numbers.
69
70
  def as_json(*args)
70
- as_extended_json
71
+ value = to_s
72
+ value unless %w[NaN Infinity -Infinity].include?(value)
71
73
  end
72
74
 
73
75
  # Converts this object to a representation directly serializable to
@@ -77,7 +79,7 @@ module BSON
77
79
  # (default is canonical extended JSON)
78
80
  #
79
81
  # @return [ Hash ] The extended json representation.
80
- def as_extended_json(**options)
82
+ def as_extended_json(**_options)
81
83
  { EXTENDED_JSON_KEY => to_s }
82
84
  end
83
85
 
@@ -99,9 +101,9 @@ module BSON
99
101
  alias :eql? :==
100
102
 
101
103
  def <=>(other)
102
- to_big_decimal <=> case other
104
+ to_d <=> case other
103
105
  when Decimal128
104
- other.to_big_decimal
106
+ other.to_d
105
107
  else
106
108
  other
107
109
  end
@@ -115,7 +117,7 @@ module BSON
115
117
  # @param [ String, BigDecimal ] object The BigDecimal or String to use for
116
118
  # instantiating a Decimal128.
117
119
  #
118
- # @raise [ InvalidArgument ] When argument is not a String or BigDecimal.
120
+ # @raise [ BSON::Error::InvalidDecimal128Argument ] When argument is not a String or BigDecimal.
119
121
  #
120
122
  # @since 4.2.0
121
123
  def initialize(object)
@@ -124,10 +126,15 @@ module BSON
124
126
  elsif object.is_a?(BigDecimal)
125
127
  set_bits(*Builder::FromBigDecimal.new(object).bits)
126
128
  else
127
- raise InvalidArgument.new
129
+ raise Error::InvalidDecimal128Argument.new
128
130
  end
129
131
  end
130
132
 
133
+ # Get the BSON type for Decimal128.
134
+ def bson_type
135
+ BSON_TYPE
136
+ end
137
+
131
138
  # Get the decimal128 as its raw BSON data.
132
139
  #
133
140
  # @example Get the raw bson bytes in a buffer.
@@ -138,7 +145,7 @@ module BSON
138
145
  # @see http://bsonspec.org/#/specification
139
146
  #
140
147
  # @since 4.2.0
141
- def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
148
+ def to_bson(buffer = ByteBuffer.new)
142
149
  buffer.put_decimal128(@low, @high)
143
150
  end
144
151
 
@@ -189,7 +196,7 @@ module BSON
189
196
  # @example
190
197
  # decimal128 = BSON::Decimal128.new("0.200")
191
198
  # => BSON::Decimal128('0.200')
192
- # big_decimal = decimal128.to_big_decimal
199
+ # big_decimal = decimal128.to_d
193
200
  # => #<BigDecimal:7fc619c95388,'0.2E0',9(18)>
194
201
  # big_decimal.to_s
195
202
  # => "0.2E0"
@@ -198,11 +205,10 @@ module BSON
198
205
  # and -sNaN while Ruby's BigDecimal cannot.
199
206
  #
200
207
  # @return [ BigDecimal ] The decimal as a BigDecimal.
201
- #
202
- # @since 4.2.0
203
- def to_big_decimal
208
+ def to_d
204
209
  @big_decimal ||= BigDecimal(to_s)
205
210
  end
211
+ alias :to_big_decimal :to_d
206
212
 
207
213
  private
208
214
 
@@ -236,7 +242,7 @@ module BSON
236
242
  #
237
243
  # @param [ String ] string The string to parse.
238
244
  #
239
- # @raise [ BSON::Decimal128::InvalidString ] If the provided string is invalid.
245
+ # @raise [ BSON::Error:InvalidDecimal128String ] If the provided string is invalid.
240
246
  #
241
247
  # @return [ BSON::Decimal128 ] The new decimal128.
242
248
  #
@@ -262,97 +268,5 @@ module BSON
262
268
  decimal
263
269
  end
264
270
  end
265
-
266
- # Raised when trying to create a Decimal128 from an object that is neither a String nor a BigDecimal.
267
- #
268
- # @api private
269
- #
270
- # @since 4.2.0
271
- class InvalidArgument < ArgumentError
272
-
273
- # The custom error message for this error.
274
- #
275
- # @since 4.2.0
276
- MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'
277
-
278
- # Get the custom error message for the exception.
279
- #
280
- # @example Get the message.
281
- # error.message
282
- #
283
- # @return [ String ] The error message.
284
- #
285
- # @since 4.2.0
286
- def message
287
- MESSAGE
288
- end
289
- end
290
-
291
- # Raised when trying to create a Decimal128 from a string with
292
- # an invalid format.
293
- #
294
- # @api private
295
- #
296
- # @since 4.2.0
297
- class InvalidString < RuntimeError
298
-
299
- # The custom error message for this error.
300
- #
301
- # @since 4.2.0
302
- MESSAGE = 'Invalid string format for creating a Decimal128 object.'
303
-
304
- # Get the custom error message for the exception.
305
- #
306
- # @example Get the message.
307
- # error.message
308
- #
309
- # @return [ String ] The error message.
310
- #
311
- # @since 4.2.0
312
- def message
313
- MESSAGE
314
- end
315
- end
316
-
317
- # Raised when the exponent is outside the valid range.
318
- #
319
- # @since 4.2.0
320
- class InvalidRange < RuntimeError
321
-
322
- # The custom error message for this error.
323
- #
324
- # @since 4.2.0
325
- # @deprecated
326
- MESSAGE = 'Value out of range for Decimal128 representation.'
327
-
328
- # Get the custom error message for the exception.
329
- #
330
- # @example Get the message.
331
- # error.message
332
- #
333
- # @return [ String ] The error message.
334
- #
335
- # @since 4.2.0
336
- def message
337
- MESSAGE
338
- end
339
- end
340
-
341
- # Raised when the significand provided is outside the valid range.
342
- #
343
- # @note This class derives from InvalidRange for backwards compatibility,
344
- # however when RUBY-1806 is implemented it should be changed to derive
345
- # from the base BSON exception class.
346
- class UnrepresentablePrecision < InvalidRange
347
-
348
- # Get the custom error message for the exception.
349
- #
350
- # @return [ String ] The error message.
351
- def message
352
- 'The value contains too much precision for Decimal128 representation'
353
- end
354
- end
355
-
356
- Registry.register(BSON_TYPE, self)
357
271
  end
358
272
  end
data/lib/bson/document.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2009-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2009-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Exception raised when BSON decoding fails.
8
+ class BSONDecodeError < Error
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Exception raised when Extended JSON parsing fails.
8
+ class ExtJSONParseError < Error
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when validating keys and a key is illegal in MongoDB
8
+ class IllegalKey < Error
9
+
10
+ # Instantiate the exception.
11
+ #
12
+ # @example Instantiate the exception.
13
+ # BSON::Error::IllegalKey.new(string)
14
+ #
15
+ # @param [ String ] string The illegal string.
16
+ #
17
+ # @api private
18
+ def initialize(string)
19
+ super("'#{string}' is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when providing an invalid type to the Binary.
8
+ class InvalidBinaryType < Error
9
+
10
+ # @return [ Object ] The invalid type.
11
+ attr_reader :type
12
+
13
+ # Instantiate the new error.
14
+ #
15
+ # @example Instantiate the error.
16
+ # InvalidBinaryType.new(:error)
17
+ #
18
+ # @param [ Object ] type The invalid type.
19
+ #
20
+ # @api private
21
+ def initialize(type)
22
+ @type = type
23
+ end
24
+
25
+ # Get the custom error message for the exception.
26
+ #
27
+ # @example Get the message.
28
+ # error.message
29
+ #
30
+ # @return [ String ] The error message.
31
+ def message
32
+ "#{type.inspect} is not a valid binary type. " +
33
+ "Please use one of #{BSON::Binary::SUBTYPES.keys.map(&:inspect).join(", ")}."
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when trying to create a BSON::DBRef from an object that is an invalid DBRef.
8
+ class InvalidDBRefArgument < Error
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when trying to create a Decimal128 from an object that is neither a String nor a BigDecimal.
8
+ class InvalidDecimal128Argument < Error
9
+
10
+ # The custom error message for this error.
11
+ MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'
12
+
13
+ # Get the custom error message for the exception.
14
+ #
15
+ # @example Get the message.
16
+ # error.message
17
+ #
18
+ # @return [ String ] The error message.
19
+ def message
20
+ MESSAGE
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when the exponent is outside the valid range.
8
+ class InvalidDecimal128Range < Error
9
+
10
+ # The custom error message for this error.
11
+ #
12
+ # @deprecated
13
+ MESSAGE = 'Value out of range for Decimal128 representation.'
14
+
15
+ # Get the custom error message for the exception.
16
+ #
17
+ # @example Get the message.
18
+ # error.message
19
+ #
20
+ # @return [ String ] The error message.
21
+ def message
22
+ MESSAGE
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when trying to create a Decimal128 from a string with
8
+ # an invalid format.
9
+ class InvalidDecimal128String < Error
10
+
11
+ # The custom error message for this error.
12
+ MESSAGE = 'Invalid string format for creating a Decimal128 object.'
13
+
14
+ # Get the custom error message for the exception.
15
+ #
16
+ # @example Get the message.
17
+ # error.message
18
+ #
19
+ # @return [ String ] The error message.
20
+ def message
21
+ MESSAGE
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when trying to serialize an object into a key.
8
+ class InvalidKey < Error
9
+
10
+ # Instantiate the exception.
11
+ #
12
+ # @example Instantiate the exception.
13
+ # BSON::Object::InvalidKey.new(object)
14
+ #
15
+ # @param [ Object ] object The object that was meant for the key.
16
+ #
17
+ # @api private
18
+ def initialize(object)
19
+ super("#{object.class} instances are not allowed as keys in a BSON document.")
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when trying to create an object id with invalid data.
8
+ class InvalidObjectId < Error; end
9
+ end
10
+ end
11
+
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Exception raised when there is an invalid argument passed into the
8
+ # constructor of regexp object. This includes when the argument contains
9
+ # a null byte.
10
+ class InvalidRegexpPattern < Error
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Raised when the significand provided is outside the valid range.
8
+ class UnrepresentablePrecision < Error
9
+
10
+ # Get the custom error message for the exception.
11
+ #
12
+ # @return [ String ] The error message.
13
+ def message
14
+ 'The value contains too much precision for Decimal128 representation'
15
+ end
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Exception raised when serializing an Array or Hash to BSON and an
8
+ # array or hash element is of a class that does not define how to serialize
9
+ # itself to BSON.
10
+ class UnserializableClass < Error
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module BSON
5
+ class Error
6
+
7
+ # Exception raised when decoding BSON and the data contains an
8
+ # unsupported binary subtype.
9
+ class UnsupportedBinarySubtype < Error
10
+ end
11
+ end
12
+ end