bson 4.15.0 → 5.0.2
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.
- checksums.yaml +4 -4
- data/README.md +94 -10
- data/Rakefile +68 -39
- data/ext/bson/bson-native.h +12 -4
- data/ext/bson/extconf.rb +8 -3
- data/ext/bson/init.c +11 -11
- data/ext/bson/read.c +39 -9
- data/ext/bson/util.c +171 -16
- data/ext/bson/write.c +34 -39
- data/lib/bson/active_support.rb +1 -0
- data/lib/bson/array.rb +58 -32
- data/lib/bson/big_decimal.rb +16 -6
- data/lib/bson/binary.rb +271 -129
- data/lib/bson/boolean.rb +1 -0
- data/lib/bson/code.rb +10 -12
- data/lib/bson/code_with_scope.rb +9 -11
- data/lib/bson/config.rb +1 -27
- data/lib/bson/date.rb +2 -1
- data/lib/bson/date_time.rb +2 -1
- data/lib/bson/db_pointer.rb +12 -13
- data/lib/bson/dbref.rb +11 -9
- data/lib/bson/decimal128/builder.rb +10 -9
- data/lib/bson/decimal128.rb +25 -111
- data/lib/bson/document.rb +1 -0
- data/lib/bson/environment.rb +1 -0
- data/lib/bson/error/bson_decode_error.rb +11 -0
- data/lib/bson/error/ext_json_parse_error.rb +11 -0
- data/lib/bson/error/illegal_key.rb +23 -0
- data/lib/bson/error/invalid_binary_type.rb +37 -0
- data/lib/bson/error/invalid_dbref_argument.rb +12 -0
- data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
- data/lib/bson/error/invalid_decimal128_range.rb +27 -0
- data/lib/bson/error/invalid_decimal128_string.rb +26 -0
- data/lib/bson/error/invalid_key.rb +24 -0
- data/lib/bson/error/invalid_object_id.rb +11 -0
- data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
- data/lib/bson/error/unrepresentable_precision.rb +19 -0
- data/lib/bson/error/unserializable_class.rb +13 -0
- data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
- data/lib/bson/error/unsupported_type.rb +11 -0
- data/lib/bson/error.rb +16 -28
- data/lib/bson/ext_json.rb +2 -1
- data/lib/bson/false_class.rb +2 -1
- data/lib/bson/float.rb +3 -2
- data/lib/bson/hash.rb +128 -73
- data/lib/bson/int32.rb +17 -5
- data/lib/bson/int64.rb +17 -5
- data/lib/bson/integer.rb +4 -5
- data/lib/bson/json.rb +1 -0
- data/lib/bson/max_key.rb +8 -10
- data/lib/bson/min_key.rb +8 -10
- data/lib/bson/nil_class.rb +1 -0
- data/lib/bson/object.rb +7 -27
- data/lib/bson/object_id.rb +84 -120
- data/lib/bson/open_struct.rb +3 -2
- data/lib/bson/regexp.rb +36 -65
- data/lib/bson/registry.rb +2 -6
- data/lib/bson/specialized.rb +2 -1
- data/lib/bson/string.rb +4 -27
- data/lib/bson/symbol.rb +23 -20
- data/lib/bson/time.rb +3 -2
- data/lib/bson/time_with_zone.rb +13 -1
- data/lib/bson/timestamp.rb +3 -2
- data/lib/bson/true_class.rb +2 -1
- data/lib/bson/undefined.rb +15 -1
- data/lib/bson/version.rb +3 -1
- data/lib/bson.rb +3 -2
- data/spec/bson/array_spec.rb +19 -60
- data/spec/bson/big_decimal_spec.rb +16 -4
- data/spec/bson/binary_spec.rb +129 -81
- data/spec/bson/binary_uuid_spec.rb +1 -0
- data/spec/bson/boolean_spec.rb +1 -0
- data/spec/bson/byte_buffer_read_spec.rb +1 -0
- data/spec/bson/byte_buffer_spec.rb +1 -0
- data/spec/bson/byte_buffer_write_spec.rb +1 -0
- data/spec/bson/code_spec.rb +5 -3
- data/spec/bson/code_with_scope_spec.rb +5 -3
- data/spec/bson/config_spec.rb +1 -35
- data/spec/bson/date_spec.rb +1 -0
- data/spec/bson/date_time_spec.rb +1 -0
- data/spec/bson/dbref_legacy_spec.rb +20 -3
- data/spec/bson/dbref_spec.rb +9 -9
- data/spec/bson/decimal128_spec.rb +40 -20
- data/spec/bson/document_as_spec.rb +1 -0
- data/spec/bson/document_spec.rb +1 -1
- data/spec/bson/ext_json_parse_spec.rb +1 -0
- data/spec/bson/false_class_spec.rb +8 -0
- data/spec/bson/float_spec.rb +8 -3
- data/spec/bson/hash_as_spec.rb +1 -0
- data/spec/bson/hash_spec.rb +87 -75
- data/spec/bson/int32_spec.rb +21 -6
- data/spec/bson/int64_spec.rb +21 -6
- data/spec/bson/integer_spec.rb +45 -13
- data/spec/bson/json_spec.rb +1 -0
- data/spec/bson/max_key_spec.rb +5 -3
- data/spec/bson/min_key_spec.rb +5 -3
- data/spec/bson/nil_class_spec.rb +1 -0
- data/spec/bson/object_id_spec.rb +57 -4
- data/spec/bson/object_spec.rb +2 -1
- data/spec/bson/open_struct_spec.rb +14 -71
- data/spec/bson/raw_spec.rb +9 -15
- data/spec/bson/regexp_spec.rb +4 -3
- data/spec/bson/registry_spec.rb +2 -1
- data/spec/bson/string_spec.rb +13 -38
- data/spec/bson/symbol_raw_spec.rb +25 -0
- data/spec/bson/symbol_spec.rb +15 -18
- data/spec/bson/time_spec.rb +1 -0
- data/spec/bson/time_with_zone_spec.rb +1 -0
- data/spec/bson/timestamp_spec.rb +1 -0
- data/spec/bson/true_class_spec.rb +8 -0
- data/spec/bson/undefined_spec.rb +27 -0
- data/spec/bson_spec.rb +1 -0
- data/spec/runners/common_driver.rb +6 -5
- data/spec/runners/corpus.rb +6 -0
- data/spec/runners/corpus_legacy.rb +1 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/spec_tests/common_driver_spec.rb +9 -4
- data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
- data/spec/spec_tests/corpus_spec.rb +13 -3
- data/spec/spec_tests/data/corpus/binary.json +5 -0
- data/spec/spec_tests/data/corpus/code.json +13 -13
- data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
- data/spec/spec_tests/data/corpus/document.json +20 -0
- data/spec/spec_tests/data/corpus/symbol.json +7 -7
- data/spec/spec_tests/data/corpus/top.json +18 -3
- data/spec/support/shared_examples.rb +28 -5
- data/spec/support/spec_config.rb +1 -0
- data/spec/support/utils.rb +49 -1
- metadata +114 -164
- checksums.yaml.gz.sig +0 -0
- data/spec/shared/LICENSE +0 -20
- data/spec/shared/bin/get-mongodb-download-url +0 -17
- data/spec/shared/bin/s3-copy +0 -45
- data/spec/shared/bin/s3-upload +0 -69
- data/spec/shared/lib/mrss/child_process_helper.rb +0 -80
- data/spec/shared/lib/mrss/cluster_config.rb +0 -231
- data/spec/shared/lib/mrss/constraints.rb +0 -386
- data/spec/shared/lib/mrss/docker_runner.rb +0 -271
- data/spec/shared/lib/mrss/event_subscriber.rb +0 -200
- data/spec/shared/lib/mrss/lite_constraints.rb +0 -191
- data/spec/shared/lib/mrss/server_version_registry.rb +0 -120
- data/spec/shared/lib/mrss/spec_organizer.rb +0 -179
- data/spec/shared/lib/mrss/utils.rb +0 -15
- data/spec/shared/share/Dockerfile.erb +0 -338
- data/spec/shared/share/haproxy-1.conf +0 -16
- data/spec/shared/share/haproxy-2.conf +0 -17
- data/spec/shared/shlib/distro.sh +0 -74
- data/spec/shared/shlib/server.sh +0 -367
- data/spec/shared/shlib/set_env.sh +0 -131
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -1
data/lib/bson/regexp.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -13,8 +14,8 @@
|
|
13
14
|
# See the License for the specific language governing permissions and
|
14
15
|
# limitations under the License.
|
15
16
|
|
17
|
+
# The top-level BSON module.
|
16
18
|
module BSON
|
17
|
-
|
18
19
|
# Injects behaviour for encoding and decoding regular expression values to
|
19
20
|
# and from raw bytes as specified by the BSON spec.
|
20
21
|
#
|
@@ -62,10 +63,8 @@ module BSON
|
|
62
63
|
# regexp.as_json
|
63
64
|
#
|
64
65
|
# @return [ Hash ] The regexp as a JSON hash.
|
65
|
-
|
66
|
-
|
67
|
-
def as_json(*args)
|
68
|
-
{ "$regex" => source, "$options" => bson_options }
|
66
|
+
def as_json(*)
|
67
|
+
{ '$regex' => source, '$options' => bson_options }
|
69
68
|
end
|
70
69
|
|
71
70
|
# Get the regular expression as encoded BSON.
|
@@ -83,14 +82,11 @@ module BSON
|
|
83
82
|
# and 'u' to make \w, \W, etc. match unicode.
|
84
83
|
#
|
85
84
|
# @param [ BSON::ByteBuffer ] buffer The byte buffer to append to.
|
86
|
-
# @param [ true, false ] validating_keys
|
87
85
|
#
|
88
86
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
89
87
|
#
|
90
88
|
# @see http://bsonspec.org/#/specification
|
91
|
-
|
92
|
-
# @since 2.0.0
|
93
|
-
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
89
|
+
def to_bson(buffer = ByteBuffer.new)
|
94
90
|
buffer.put_cstring(source)
|
95
91
|
buffer.put_cstring(bson_options)
|
96
92
|
end
|
@@ -103,30 +99,28 @@ module BSON
|
|
103
99
|
end
|
104
100
|
|
105
101
|
def bson_extended
|
106
|
-
(options & ::Regexp::EXTENDED
|
102
|
+
(options & ::Regexp::EXTENDED).zero? ? NO_VALUE : EXTENDED_VALUE
|
107
103
|
end
|
108
104
|
|
109
105
|
def bson_ignorecase
|
110
|
-
(options & ::Regexp::IGNORECASE
|
106
|
+
(options & ::Regexp::IGNORECASE).zero? ? NO_VALUE : IGNORECASE_VALUE
|
111
107
|
end
|
112
108
|
|
113
109
|
def bson_dotall
|
114
110
|
# Ruby Regexp's MULTILINE is equivalent to BSON's dotall value
|
115
|
-
(options & ::Regexp::MULTILINE
|
111
|
+
(options & ::Regexp::MULTILINE).zero? ? NO_VALUE : NEWLINE_VALUE
|
116
112
|
end
|
117
113
|
|
118
114
|
# Represents the raw values for the regular expression.
|
119
115
|
#
|
120
116
|
# @see https://jira.mongodb.org/browse/RUBY-698
|
121
|
-
#
|
122
|
-
# @since 3.0.0
|
123
117
|
class Raw
|
124
118
|
include JSON
|
125
119
|
|
126
120
|
# @return [ String ] pattern The regex pattern.
|
127
121
|
attr_reader :pattern
|
128
122
|
|
129
|
-
# @return [
|
123
|
+
# @return [ String ] options The options.
|
130
124
|
attr_reader :options
|
131
125
|
|
132
126
|
# Compile the Regular expression into the native type.
|
@@ -135,10 +129,8 @@ module BSON
|
|
135
129
|
# raw.compile
|
136
130
|
#
|
137
131
|
# @return [ ::Regexp ] The compiled regular expression.
|
138
|
-
#
|
139
|
-
# @since 3.0.0
|
140
132
|
def compile
|
141
|
-
@
|
133
|
+
@compile ||= ::Regexp.new(pattern, options_to_int)
|
142
134
|
end
|
143
135
|
|
144
136
|
# Initialize the new raw regular expression.
|
@@ -147,13 +139,7 @@ module BSON
|
|
147
139
|
# Raw.new(pattern, options)
|
148
140
|
#
|
149
141
|
# @param [ String ] pattern The regular expression pattern.
|
150
|
-
# @param [ String
|
151
|
-
#
|
152
|
-
# @note The ability to specify options as an Integer is deprecated.
|
153
|
-
# Please specify options as a String. The ability to pass options as
|
154
|
-
# as Integer will be removed in version 5.0.0.
|
155
|
-
#
|
156
|
-
# @since 3.0.0
|
142
|
+
# @param [ String | Symbol ] options The options.
|
157
143
|
def initialize(pattern, options = '')
|
158
144
|
if pattern.include?(NULL_BYTE)
|
159
145
|
raise Error::InvalidRegexpPattern, "Regexp pattern cannot contain a null byte: #{pattern}"
|
@@ -161,28 +147,22 @@ module BSON
|
|
161
147
|
if options.to_s.include?(NULL_BYTE)
|
162
148
|
raise Error::InvalidRegexpPattern, "Regexp options cannot contain a null byte: #{options}"
|
163
149
|
end
|
164
|
-
|
165
|
-
raise ArgumentError,
|
150
|
+
else
|
151
|
+
raise ArgumentError, 'Regexp options must be a String or Symbol'
|
166
152
|
end
|
167
153
|
|
168
154
|
@pattern = pattern
|
169
|
-
@options = options
|
155
|
+
@options = options.to_s
|
170
156
|
end
|
171
157
|
|
172
158
|
# Allow automatic delegation of methods to the Regexp object
|
173
159
|
# returned by +compile+.
|
174
160
|
#
|
175
161
|
# @param [ String] method The name of a method.
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
compile.respond_to?(method, include_private) || super
|
181
|
-
else
|
182
|
-
# YAML calls #respond_to? during deserialization, before the object
|
183
|
-
# is initialized.
|
184
|
-
super
|
185
|
-
end
|
162
|
+
def respond_to_missing?(method, include_private = false)
|
163
|
+
# YAML calls #respond_to? during deserialization, before the object
|
164
|
+
# is initialized.
|
165
|
+
defined?(@pattern) && compile.respond_to?(method, include_private)
|
186
166
|
end
|
187
167
|
|
188
168
|
# Encode the Raw Regexp object to BSON.
|
@@ -200,15 +180,11 @@ module BSON
|
|
200
180
|
# and 'u' to make \w, \W, etc. match unicode.
|
201
181
|
#
|
202
182
|
# @param [ BSON::ByteBuffer ] buffer The byte buffer to append to.
|
203
|
-
# @param [ true, false ] validating_keys
|
204
183
|
#
|
205
184
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
206
185
|
#
|
207
186
|
# @see http://bsonspec.org/#/specification
|
208
|
-
|
209
|
-
# @since 4.2.0
|
210
|
-
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
211
|
-
return compile.to_bson(buffer, validating_keys) if options.is_a?(Integer)
|
187
|
+
def to_bson(buffer = ByteBuffer.new)
|
212
188
|
buffer.put_cstring(source)
|
213
189
|
buffer.put_cstring(options.chars.sort.join)
|
214
190
|
end
|
@@ -219,14 +195,12 @@ module BSON
|
|
219
195
|
# raw_regexp.as_json
|
220
196
|
#
|
221
197
|
# @return [ Hash ] The raw regexp as a JSON hash.
|
222
|
-
|
223
|
-
# @since 4.2.0
|
224
|
-
def as_json(*args)
|
198
|
+
def as_json(*)
|
225
199
|
as_extended_json(mode: :legacy)
|
226
200
|
end
|
227
201
|
|
228
202
|
# Converts this object to a representation directly serializable to
|
229
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
203
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
230
204
|
#
|
231
205
|
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
232
206
|
# (default is canonical extended JSON)
|
@@ -234,9 +208,9 @@ module BSON
|
|
234
208
|
# @return [ Hash ] The extended json representation.
|
235
209
|
def as_extended_json(**opts)
|
236
210
|
if opts[:mode] == :legacy
|
237
|
-
{
|
211
|
+
{ '$regex' => source, '$options' => options }
|
238
212
|
else
|
239
|
-
{
|
213
|
+
{ '$regularExpression' => { 'pattern' => source, 'options' => options } }
|
240
214
|
end
|
241
215
|
end
|
242
216
|
|
@@ -248,24 +222,22 @@ module BSON
|
|
248
222
|
# @param [ Object ] other The object to check against.
|
249
223
|
#
|
250
224
|
# @return [ true, false ] If the objects are equal.
|
251
|
-
#
|
252
|
-
# @since 4.2.0
|
253
225
|
def ==(other)
|
254
226
|
return false unless other.is_a?(::Regexp::Raw)
|
255
|
-
|
256
|
-
|
227
|
+
|
228
|
+
pattern == other.pattern && options == other.options
|
257
229
|
end
|
258
|
-
alias
|
230
|
+
alias eql? ==
|
259
231
|
|
260
232
|
private
|
261
233
|
|
262
234
|
def method_missing(method, *arguments)
|
263
235
|
return super unless respond_to?(method)
|
236
|
+
|
264
237
|
compile.send(method, *arguments)
|
265
238
|
end
|
266
239
|
|
267
240
|
def options_to_int
|
268
|
-
return options if options.is_a?(Integer)
|
269
241
|
opts = 0
|
270
242
|
opts |= ::Regexp::IGNORECASE if options.include?(IGNORECASE_VALUE)
|
271
243
|
opts |= ::Regexp::MULTILINE if options.include?(NEWLINE_VALUE)
|
@@ -274,10 +246,15 @@ module BSON
|
|
274
246
|
end
|
275
247
|
end
|
276
248
|
|
249
|
+
# Class-level methods to be added to the Regexp class.
|
277
250
|
module ClassMethods
|
278
|
-
|
279
251
|
# Deserialize the regular expression from BSON.
|
280
252
|
#
|
253
|
+
# @note If the argument cannot be parsed, an exception will be raised
|
254
|
+
# and the argument will be left in an undefined state. The caller
|
255
|
+
# must explicitly call `rewind` on the buffer before trying to parse
|
256
|
+
# it again.
|
257
|
+
#
|
281
258
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
282
259
|
#
|
283
260
|
# @option opts [ nil | :bson ] :mode Decoding mode to use.
|
@@ -285,9 +262,7 @@ module BSON
|
|
285
262
|
# @return [ Regexp ] The decoded regular expression.
|
286
263
|
#
|
287
264
|
# @see http://bsonspec.org/#/specification
|
288
|
-
|
289
|
-
# @since 2.0.0
|
290
|
-
def from_bson(buffer, **opts)
|
265
|
+
def from_bson(buffer, **_)
|
291
266
|
pattern = buffer.get_cstring
|
292
267
|
options = buffer.get_cstring
|
293
268
|
Raw.new(pattern, options)
|
@@ -295,14 +270,10 @@ module BSON
|
|
295
270
|
end
|
296
271
|
|
297
272
|
# Register this type when the module is loaded.
|
298
|
-
#
|
299
|
-
# @since 2.0.0
|
300
273
|
Registry.register(BSON_TYPE, ::Regexp)
|
301
274
|
end
|
302
275
|
|
303
276
|
# Enrich the core Regexp class with this module.
|
304
|
-
|
305
|
-
|
306
|
-
::Regexp.send(:include, Regexp)
|
307
|
-
::Regexp.send(:extend, Regexp::ClassMethods)
|
277
|
+
::Regexp.include Regexp
|
278
|
+
::Regexp.extend Regexp::ClassMethods
|
308
279
|
end
|
data/lib/bson/registry.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");
|
@@ -64,11 +65,6 @@ module BSON
|
|
64
65
|
define_type_reader(type)
|
65
66
|
end
|
66
67
|
|
67
|
-
# Raised when trying to get a type from the registry that doesn't exist.
|
68
|
-
#
|
69
|
-
# @since 4.1.0
|
70
|
-
class UnsupportedType < RuntimeError; end
|
71
|
-
|
72
68
|
private
|
73
69
|
|
74
70
|
def define_type_reader(type)
|
@@ -81,7 +77,7 @@ module BSON
|
|
81
77
|
message = "Detected unknown BSON type #{byte.inspect} "
|
82
78
|
message += (field ? "for fieldname \"#{field}\". " : "in array. ")
|
83
79
|
message +="Are you using the latest BSON version?"
|
84
|
-
raise UnsupportedType.new(message)
|
80
|
+
raise Error::UnsupportedType.new(message)
|
85
81
|
end
|
86
82
|
end
|
87
83
|
end
|
data/lib/bson/specialized.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");
|
@@ -46,7 +47,7 @@ module BSON
|
|
46
47
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
47
48
|
#
|
48
49
|
# @since 2.0.0
|
49
|
-
def to_bson(buffer = ByteBuffer.new
|
50
|
+
def to_bson(buffer = ByteBuffer.new)
|
50
51
|
buffer
|
51
52
|
end
|
52
53
|
|
data/lib/bson/string.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# rubocop:todo all
|
1
2
|
# -*- coding: utf-8 -*-
|
2
3
|
# frozen_string_literal: true
|
3
4
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
@@ -46,7 +47,7 @@ module BSON
|
|
46
47
|
# @see http://bsonspec.org/#/specification
|
47
48
|
#
|
48
49
|
# @since 2.0.0
|
49
|
-
def to_bson(buffer = ByteBuffer.new
|
50
|
+
def to_bson(buffer = ByteBuffer.new)
|
50
51
|
buffer.put_string(self)
|
51
52
|
end
|
52
53
|
|
@@ -57,18 +58,12 @@ module BSON
|
|
57
58
|
#
|
58
59
|
# @raise [ EncodingError ] If the string is not UTF-8.
|
59
60
|
#
|
60
|
-
# @raise [ IllegalKey ] If validating keys and it contains a '.' or starts
|
61
|
-
# with '$'.
|
62
|
-
#
|
63
61
|
# @return [ String ] The encoded string.
|
64
62
|
#
|
65
63
|
# @see http://bsonspec.org/#/specification
|
66
64
|
#
|
67
65
|
# @since 2.0.0
|
68
|
-
def to_bson_key
|
69
|
-
if validating_keys
|
70
|
-
raise IllegalKey.new(self) if ILLEGAL_KEY =~ self
|
71
|
-
end
|
66
|
+
def to_bson_key
|
72
67
|
self
|
73
68
|
end
|
74
69
|
|
@@ -80,7 +75,7 @@ module BSON
|
|
80
75
|
#
|
81
76
|
# @note This is used for repairing legacy bson data.
|
82
77
|
#
|
83
|
-
# @raise [ BSON::
|
78
|
+
# @raise [ BSON::Error::InvalidObjectId ] If the string is not 12 elements.
|
84
79
|
#
|
85
80
|
# @return [ String ] The raw object id bytes.
|
86
81
|
#
|
@@ -101,24 +96,6 @@ module BSON
|
|
101
96
|
unpack("H*")[0]
|
102
97
|
end
|
103
98
|
|
104
|
-
# Raised when validating keys and a key is illegal in MongoDB
|
105
|
-
#
|
106
|
-
# @since 4.1.0
|
107
|
-
class IllegalKey < RuntimeError
|
108
|
-
|
109
|
-
# Instantiate the exception.
|
110
|
-
#
|
111
|
-
# @example Instantiate the exception.
|
112
|
-
# BSON::Object::IllegalKey.new(string)
|
113
|
-
#
|
114
|
-
# @param [ String ] string The illegal string.
|
115
|
-
#
|
116
|
-
# @since 4.1.0
|
117
|
-
def initialize(string)
|
118
|
-
super("'#{string}' is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.")
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
99
|
module ClassMethods
|
123
100
|
|
124
101
|
# Deserialize a string from BSON.
|
data/lib/bson/symbol.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");
|
@@ -57,7 +58,7 @@ module BSON
|
|
57
58
|
# @see http://bsonspec.org/#/specification
|
58
59
|
#
|
59
60
|
# @since 2.0.0
|
60
|
-
def to_bson(buffer = ByteBuffer.new
|
61
|
+
def to_bson(buffer = ByteBuffer.new)
|
61
62
|
buffer.put_symbol(self)
|
62
63
|
end
|
63
64
|
|
@@ -71,10 +72,7 @@ module BSON
|
|
71
72
|
# @see http://bsonspec.org/#/specification
|
72
73
|
#
|
73
74
|
# @since 2.0.0
|
74
|
-
def to_bson_key
|
75
|
-
if validating_keys
|
76
|
-
raise BSON::String::IllegalKey.new(self) if BSON::String::ILLEGAL_KEY =~ self
|
77
|
-
end
|
75
|
+
def to_bson_key
|
78
76
|
self
|
79
77
|
end
|
80
78
|
|
@@ -91,17 +89,16 @@ module BSON
|
|
91
89
|
end
|
92
90
|
|
93
91
|
# Converts this object to a representation directly serializable to
|
94
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
95
|
-
#
|
96
|
-
# @option options [ true | false ] :relaxed Whether to produce relaxed
|
97
|
-
# extended JSON representation.
|
92
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
98
93
|
#
|
99
94
|
# @return [ Hash ] The extended json representation.
|
100
|
-
def as_extended_json(**
|
95
|
+
def as_extended_json(**_options)
|
101
96
|
{ "$symbol" => to_s }
|
102
97
|
end
|
103
98
|
|
104
99
|
class Raw
|
100
|
+
include JSON
|
101
|
+
|
105
102
|
# Create a BSON Symbol
|
106
103
|
#
|
107
104
|
# @param [ String | Symbol ] str_or_sym The symbol represented by this
|
@@ -148,7 +145,7 @@ module BSON
|
|
148
145
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
149
146
|
#
|
150
147
|
# @see http://bsonspec.org/#/specification
|
151
|
-
def to_bson(buffer = ByteBuffer.new
|
148
|
+
def to_bson(buffer = ByteBuffer.new)
|
152
149
|
buffer.put_string(to_s)
|
153
150
|
end
|
154
151
|
|
@@ -156,18 +153,24 @@ module BSON
|
|
156
153
|
Symbol::BSON_TYPE
|
157
154
|
end
|
158
155
|
|
159
|
-
#
|
160
|
-
#
|
156
|
+
# Return a string representation of the raw symbol for use in
|
157
|
+
# application-level JSON serialization. This method is intentionally
|
158
|
+
# different from #as_extended_json.
|
161
159
|
#
|
162
|
-
#
|
163
|
-
#
|
160
|
+
# @example Get the raw symbol as a JSON-serializable object.
|
161
|
+
# raw_symbol.as_json
|
164
162
|
#
|
165
|
-
# @
|
166
|
-
|
163
|
+
# @return [ String ] The raw symbol as a String.
|
164
|
+
def as_json(*args)
|
165
|
+
to_s
|
166
|
+
end
|
167
|
+
|
168
|
+
# Converts this object to a representation directly serializable to
|
169
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
167
170
|
#
|
168
|
-
# @return [ Hash
|
169
|
-
def as_extended_json(**
|
170
|
-
{'$symbol' => to_s}
|
171
|
+
# @return [ Hash ] The extended json representation.
|
172
|
+
def as_extended_json(**_options)
|
173
|
+
{ '$symbol' => to_s }
|
171
174
|
end
|
172
175
|
end
|
173
176
|
|
data/lib/bson/time.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");
|
@@ -54,13 +55,13 @@ module BSON
|
|
54
55
|
# @see http://bsonspec.org/#/specification
|
55
56
|
#
|
56
57
|
# @since 2.0.0
|
57
|
-
def to_bson(buffer = ByteBuffer.new
|
58
|
+
def to_bson(buffer = ByteBuffer.new)
|
58
59
|
value = _bson_to_i * 1000 + usec.divmod(1000).first
|
59
60
|
buffer.put_int64(value)
|
60
61
|
end
|
61
62
|
|
62
63
|
# Converts this object to a representation directly serializable to
|
63
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
64
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
64
65
|
#
|
65
66
|
# @note The time is floored to the nearest millisecond.
|
66
67
|
#
|
data/lib/bson/time_with_zone.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2018-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -35,7 +36,7 @@ module BSON
|
|
35
36
|
# @see http://bsonspec.org/#/specification
|
36
37
|
#
|
37
38
|
# @since 4.4.0
|
38
|
-
def to_bson(buffer = ByteBuffer.new
|
39
|
+
def to_bson(buffer = ByteBuffer.new)
|
39
40
|
buffer.put_int64((to_i * 1000) + (usec / 1000))
|
40
41
|
end
|
41
42
|
|
@@ -46,6 +47,17 @@ module BSON
|
|
46
47
|
def bson_type
|
47
48
|
::Time::BSON_TYPE
|
48
49
|
end
|
50
|
+
|
51
|
+
# @api private
|
52
|
+
def _bson_to_i
|
53
|
+
# Workaround for JRuby's #to_i rounding negative timestamps up
|
54
|
+
# rather than down (https://github.com/jruby/jruby/issues/6104)
|
55
|
+
if BSON::Environment.jruby?
|
56
|
+
(self - usec.to_r/1000000).to_i
|
57
|
+
else
|
58
|
+
to_i
|
59
|
+
end
|
60
|
+
end
|
49
61
|
end
|
50
62
|
|
51
63
|
# Enrich the ActiveSupport::TimeWithZone class with this module.
|
data/lib/bson/timestamp.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");
|
@@ -91,7 +92,7 @@ module BSON
|
|
91
92
|
end
|
92
93
|
|
93
94
|
# Converts this object to a representation directly serializable to
|
94
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
95
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
95
96
|
#
|
96
97
|
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
97
98
|
# (default is canonical extended JSON)
|
@@ -124,7 +125,7 @@ module BSON
|
|
124
125
|
# @see http://bsonspec.org/#/specification
|
125
126
|
#
|
126
127
|
# @since 2.0.0
|
127
|
-
def to_bson(buffer = ByteBuffer.new
|
128
|
+
def to_bson(buffer = ByteBuffer.new)
|
128
129
|
buffer.put_uint32(increment)
|
129
130
|
buffer.put_uint32(seconds)
|
130
131
|
end
|
data/lib/bson/true_class.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");
|
@@ -50,7 +51,7 @@ module BSON
|
|
50
51
|
# @see http://bsonspec.org/#/specification
|
51
52
|
#
|
52
53
|
# @since 2.0.0
|
53
|
-
def to_bson(buffer = ByteBuffer.new
|
54
|
+
def to_bson(buffer = ByteBuffer.new)
|
54
55
|
buffer.put_byte(TRUE_BYTE)
|
55
56
|
end
|
56
57
|
end
|
data/lib/bson/undefined.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");
|
@@ -21,6 +22,7 @@ module BSON
|
|
21
22
|
#
|
22
23
|
# @since 2.0.0
|
23
24
|
class Undefined
|
25
|
+
include JSON
|
24
26
|
include Specialized
|
25
27
|
|
26
28
|
# Undefined is type 0x06 in the BSON spec.
|
@@ -42,8 +44,20 @@ module BSON
|
|
42
44
|
self.class == other.class
|
43
45
|
end
|
44
46
|
|
47
|
+
# Return a string representation of the BSON::Undefined for use in
|
48
|
+
# application-level JSON serialization. This method is intentionally
|
49
|
+
# different from #as_extended_json.
|
50
|
+
#
|
51
|
+
# @example Get the undefined as a JSON-serializable object.
|
52
|
+
# undefined.as_json
|
53
|
+
#
|
54
|
+
# @return [ nil ] The undefined as nil.
|
55
|
+
def as_json(*args)
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
45
59
|
# Converts this object to a representation directly serializable to
|
46
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
60
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
47
61
|
#
|
48
62
|
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
49
63
|
# (default is canonical extended JSON)
|
data/lib/bson/version.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
3
|
+
|
2
4
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
5
|
#
|
4
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -14,5 +16,5 @@
|
|
14
16
|
# limitations under the License.
|
15
17
|
|
16
18
|
module BSON
|
17
|
-
VERSION = "
|
19
|
+
VERSION = "5.0.2"
|
18
20
|
end
|
data/lib/bson.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");
|
@@ -27,7 +28,7 @@ module BSON
|
|
27
28
|
#
|
28
29
|
# @param [ String ] string The string to create the id from.
|
29
30
|
#
|
30
|
-
# @raise [ BSON::
|
31
|
+
# @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
|
31
32
|
#
|
32
33
|
# @return [ BSON::ObjectId ] The new object id.
|
33
34
|
#
|
@@ -103,7 +104,7 @@ require "bson/version"
|
|
103
104
|
begin
|
104
105
|
if BSON::Environment.jruby?
|
105
106
|
require "bson-ruby.jar"
|
106
|
-
JRuby::Util.load_ext("org.
|
107
|
+
JRuby::Util.load_ext("org.bson_ruby.NativeService")
|
107
108
|
else
|
108
109
|
require "bson_native"
|
109
110
|
end
|