bson 4.11.1 → 4.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +4 -7
- data/ext/bson/bson-native.h +2 -0
- data/ext/bson/init.c +9 -0
- data/ext/bson/read.c +29 -0
- data/lib/bson/active_support.rb +1 -0
- data/lib/bson/array.rb +2 -1
- data/lib/bson/big_decimal.rb +67 -0
- data/lib/bson/binary.rb +8 -5
- data/lib/bson/boolean.rb +2 -1
- data/lib/bson/code.rb +2 -1
- data/lib/bson/code_with_scope.rb +2 -1
- data/lib/bson/config.rb +1 -0
- data/lib/bson/date.rb +1 -0
- data/lib/bson/date_time.rb +1 -0
- data/lib/bson/db_pointer.rb +2 -1
- data/lib/bson/dbref.rb +125 -0
- data/lib/bson/decimal128/builder.rb +27 -20
- data/lib/bson/decimal128.rb +27 -12
- data/lib/bson/document.rb +61 -18
- data/lib/bson/environment.rb +1 -0
- data/lib/bson/error.rb +7 -0
- data/lib/bson/ext_json.rb +24 -11
- data/lib/bson/false_class.rb +2 -1
- data/lib/bson/float.rb +21 -32
- data/lib/bson/hash.rb +15 -6
- data/lib/bson/int32.rb +3 -2
- data/lib/bson/int64.rb +3 -2
- data/lib/bson/integer.rb +3 -2
- data/lib/bson/json.rb +1 -0
- data/lib/bson/max_key.rb +3 -2
- data/lib/bson/min_key.rb +3 -2
- data/lib/bson/nil_class.rb +2 -1
- data/lib/bson/object.rb +1 -0
- data/lib/bson/object_id.rb +4 -3
- data/lib/bson/open_struct.rb +1 -0
- data/lib/bson/regexp.rb +17 -6
- data/lib/bson/registry.rb +1 -0
- data/lib/bson/specialized.rb +1 -0
- data/lib/bson/string.rb +3 -2
- data/lib/bson/symbol.rb +2 -1
- data/lib/bson/time.rb +4 -3
- data/lib/bson/time_with_zone.rb +1 -0
- data/lib/bson/timestamp.rb +3 -2
- data/lib/bson/true_class.rb +2 -1
- data/lib/bson/undefined.rb +2 -1
- data/lib/bson/version.rb +2 -1
- data/lib/bson.rb +8 -5
- data/lib/bson_native.bundle +0 -0
- data/spec/README.md +14 -0
- data/spec/bson/big_decimal_spec.rb +316 -0
- data/spec/bson/binary_spec.rb +1 -1
- data/spec/bson/binary_uuid_spec.rb +12 -0
- data/spec/bson/dbref_spec.rb +461 -0
- data/spec/bson/decimal128_spec.rb +16 -0
- data/spec/bson/document_as_spec.rb +46 -0
- data/spec/bson/document_spec.rb +43 -1
- data/spec/bson/ext_json_parse_spec.rb +37 -0
- data/spec/bson/hash_as_spec.rb +57 -0
- data/spec/bson/hash_spec.rb +32 -0
- data/spec/bson/int64_spec.rb +4 -24
- data/spec/bson/raw_spec.rb +7 -1
- data/spec/bson/regexp_spec.rb +52 -0
- data/spec/runners/common_driver.rb +1 -1
- data/spec/shared/LICENSE +20 -0
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
- data/spec/shared/lib/mrss/cluster_config.rb +231 -0
- data/spec/shared/lib/mrss/constraints.rb +386 -0
- data/spec/shared/lib/mrss/docker_runner.rb +271 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
- data/spec/shared/lib/mrss/utils.rb +15 -0
- data/spec/shared/share/Dockerfile.erb +338 -0
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/distro.sh +74 -0
- data/spec/shared/shlib/server.sh +367 -0
- data/spec/shared/shlib/set_env.sh +131 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/spec_tests/common_driver_spec.rb +2 -1
- data/spec/spec_tests/data/corpus/binary.json +33 -0
- data/spec/spec_tests/data/corpus/dbref.json +21 -1
- data/spec/spec_tests/data/corpus/document.json +4 -0
- data/spec/spec_tests/data/corpus/regex.json +2 -2
- data/spec/spec_tests/data/corpus/top.json +20 -9
- data/spec/support/spec_config.rb +2 -1
- data.tar.gz.sig +0 -0
- metadata +157 -106
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 542c675ad88650d665d5e077de9a3c7bcbd566647fc1f08f1d3441948d646c1c
|
4
|
+
data.tar.gz: b9f41c2f216e982dcdcd2c83c17a4dd59f8e062406023eb6bd1932afa8338698
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a084a08fb6cd5601e6b0095b7086348e602aa42e5b52069531f107879d4a67a7a982ce1938d4ef4bedb4b559648a0139b3c85ab011b5f65fa415300dadef079
|
7
|
+
data.tar.gz: 3dde4070b315b1a3da605c11fa6571a9fb641dcdc77ed2edb7aec0e3799a2765af68342ac9a4fb839e0c0a84ad4a10f1641948a76857f649c9680a521aec90bd
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
BSON
|
2
2
|
[![Gem Version][rubygems-img]][rubygems-url]
|
3
|
-
[![Build Status][
|
4
|
-
[![Build status Windows][appveyor-img]][appveyor-url]
|
3
|
+
[![Build Status][ghactions-img]][ghactions-url]
|
5
4
|
[![Coverage Status][coveralls-img]][coveralls-url]
|
6
5
|
[![Inline docs][inch-img]][inch-url]
|
7
6
|
====
|
@@ -11,7 +10,7 @@ An implementation of the BSON specification in Ruby.
|
|
11
10
|
Compatibility
|
12
11
|
-------------
|
13
12
|
|
14
|
-
BSON is tested against MRI (2.
|
13
|
+
BSON is tested against MRI (2.5) and JRuby (9.2+).
|
15
14
|
|
16
15
|
Documentation
|
17
16
|
-------------
|
@@ -55,10 +54,8 @@ limitations under the License.
|
|
55
54
|
|
56
55
|
[rubygems-img]: https://badge.fury.io/rb/bson.svg
|
57
56
|
[rubygems-url]: http://badge.fury.io/rb/bson
|
58
|
-
[
|
59
|
-
[
|
60
|
-
[appveyor-img]: https://ci.appveyor.com/api/projects/status/p5aqko7umsx351nm?svg=true
|
61
|
-
[appveyor-url]: https://ci.appveyor.com/project/p-mongo/bson-ruby/branch/master
|
57
|
+
[ghactions-img]: https://github.com/mongodb/bson-ruby/actions/workflows/bson-ruby.yml/badge.svg?query=branch%3Amaster
|
58
|
+
[ghactions-url]: https://github.com/mongodb/bson-ruby/actions/workflows/bson-ruby.yml?query=branch%3Amaster
|
62
59
|
[coveralls-img]: https://coveralls.io/repos/mongodb/bson-ruby/badge.svg?branch=master
|
63
60
|
[coveralls-url]: https://coveralls.io/r/mongodb/bson-ruby?branch=master
|
64
61
|
[inch-img]: http://inch-ci.org/github/mongodb/bson-ruby.svg?branch=master
|
data/ext/bson/bson-native.h
CHANGED
data/ext/bson/init.c
CHANGED
@@ -30,6 +30,8 @@ const rb_data_type_t rb_byte_buffer_data_type = {
|
|
30
30
|
{ NULL, rb_bson_byte_buffer_free, rb_bson_byte_buffer_memsize }
|
31
31
|
};
|
32
32
|
|
33
|
+
VALUE _ref_str, _id_str, _db_str;
|
34
|
+
|
33
35
|
/**
|
34
36
|
* Initialize the bson_native extension.
|
35
37
|
*/
|
@@ -37,6 +39,13 @@ void Init_bson_native()
|
|
37
39
|
{
|
38
40
|
char rb_bson_machine_id[256];
|
39
41
|
|
42
|
+
_ref_str = rb_str_new_cstr("$ref");
|
43
|
+
rb_gc_register_mark_object(_ref_str);
|
44
|
+
_id_str = rb_str_new_cstr("$id");
|
45
|
+
rb_gc_register_mark_object(_id_str);
|
46
|
+
_db_str = rb_str_new_cstr("$db");
|
47
|
+
rb_gc_register_mark_object(_db_str);
|
48
|
+
|
40
49
|
VALUE rb_bson_module = rb_define_module("BSON");
|
41
50
|
|
42
51
|
/* Document-class: BSON::ByteBuffer
|
data/ext/bson/read.c
CHANGED
@@ -356,6 +356,30 @@ VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self)
|
|
356
356
|
return bytes;
|
357
357
|
}
|
358
358
|
|
359
|
+
/**
|
360
|
+
* This duplicates the DBRef validation code in DBRef constructor.
|
361
|
+
*/
|
362
|
+
static int pvt_is_dbref(VALUE doc) {
|
363
|
+
VALUE ref, id, db;
|
364
|
+
|
365
|
+
ref = rb_hash_aref(doc, _ref_str);
|
366
|
+
if (NIL_P(ref) || !RB_TYPE_P(ref, T_STRING)) {
|
367
|
+
return 0;
|
368
|
+
}
|
369
|
+
|
370
|
+
id = rb_hash_aref(doc, _id_str);
|
371
|
+
if (NIL_P(id)) {
|
372
|
+
return 0;
|
373
|
+
}
|
374
|
+
|
375
|
+
db = rb_hash_aref(doc, _db_str);
|
376
|
+
if (!NIL_P(db) && !RB_TYPE_P(db, T_STRING)) {
|
377
|
+
return 0;
|
378
|
+
}
|
379
|
+
|
380
|
+
return 1;
|
381
|
+
}
|
382
|
+
|
359
383
|
VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
|
360
384
|
VALUE doc = Qnil;
|
361
385
|
byte_buffer_t *b = NULL;
|
@@ -380,6 +404,11 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
|
|
380
404
|
if (READ_PTR(b) - start_ptr != length) {
|
381
405
|
pvt_raise_decode_error(rb_sprintf("Expected to read %d bytes for the hash but read %ld bytes", length, READ_PTR(b) - start_ptr));
|
382
406
|
}
|
407
|
+
|
408
|
+
if (pvt_is_dbref(doc)) {
|
409
|
+
VALUE cDBRef = pvt_const_get_2("BSON", "DBRef");
|
410
|
+
doc = rb_funcall(cDBRef, rb_intern("new"), 1, doc);
|
411
|
+
}
|
383
412
|
|
384
413
|
return doc;
|
385
414
|
}
|
data/lib/bson/active_support.rb
CHANGED
data/lib/bson/array.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -25,7 +26,7 @@ module BSON
|
|
25
26
|
# An array is type 0x04 in the BSON spec.
|
26
27
|
#
|
27
28
|
# @since 2.0.0
|
28
|
-
BSON_TYPE = 4.chr
|
29
|
+
BSON_TYPE = ::String.new(4.chr, encoding: BINARY).freeze
|
29
30
|
|
30
31
|
# Get the array as encoded BSON.
|
31
32
|
#
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# Copyright (C) 2009-2021 MongoDB Inc.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
module BSON
|
17
|
+
|
18
|
+
# Injects behaviour for encoding and decoding BigDecimals
|
19
|
+
# to and from raw bytes as specified by the BSON spec.
|
20
|
+
#
|
21
|
+
# @see http://bsonspec.org/#/specification
|
22
|
+
module BigDecimal
|
23
|
+
|
24
|
+
# BigDecimals are serialized as Decimal128s under the hood. A Decimal128
|
25
|
+
# is type 0x13 in the BSON spec.
|
26
|
+
BSON_TYPE = ::String.new(19.chr, encoding: BINARY).freeze
|
27
|
+
|
28
|
+
# Get the BigDecimal as encoded BSON.
|
29
|
+
#
|
30
|
+
# @example Get the BigDecimal as encoded BSON.
|
31
|
+
# BigDecimal("1").to_bson
|
32
|
+
#
|
33
|
+
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
34
|
+
#
|
35
|
+
# @see http://bsonspec.org/#/specification
|
36
|
+
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
37
|
+
BSON::Decimal128.new(to_s).to_bson(buffer, validating_keys)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get the BSON type for BigDecimal. This is the same BSON type as
|
41
|
+
# BSON::Decimal128.
|
42
|
+
def bson_type
|
43
|
+
BSON_TYPE
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
|
48
|
+
# Deserialize the BigDecimal from raw BSON bytes.
|
49
|
+
#
|
50
|
+
# @example Get the BigDecimal from BSON.
|
51
|
+
# BigDecimal.from_bson(bson)
|
52
|
+
#
|
53
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
54
|
+
#
|
55
|
+
# @option options [ nil | :bson ] :mode Decoding mode to use.
|
56
|
+
#
|
57
|
+
# @return [ BigDecimal ] The decimal object.
|
58
|
+
def from_bson(buffer, **options)
|
59
|
+
Decimal128.from_bson(buffer, **options).to_big_decimal
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Enrich the core BigDecimal class with this module.
|
65
|
+
::BigDecimal.send(:include, BigDecimal)
|
66
|
+
::BigDecimal.send(:extend, BigDecimal::ClassMethods)
|
67
|
+
end
|
data/lib/bson/binary.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -27,7 +28,7 @@ module BSON
|
|
27
28
|
# A binary is type 0x05 in the BSON spec.
|
28
29
|
#
|
29
30
|
# @since 2.0.0
|
30
|
-
BSON_TYPE = 5.chr
|
31
|
+
BSON_TYPE = ::String.new(5.chr, encoding: BINARY).freeze
|
31
32
|
|
32
33
|
# The mappings of subtypes to their single byte identifiers.
|
33
34
|
#
|
@@ -46,7 +47,8 @@ module BSON
|
|
46
47
|
:uuid => 4.chr,
|
47
48
|
:md5 => 5.chr,
|
48
49
|
:ciphertext => 6.chr,
|
49
|
-
:
|
50
|
+
:column => 7.chr,
|
51
|
+
:user => 128.chr,
|
50
52
|
}.freeze
|
51
53
|
|
52
54
|
# The mappings of single byte subtypes to their symbol counterparts.
|
@@ -169,7 +171,7 @@ module BSON
|
|
169
171
|
#
|
170
172
|
# @since 2.3.0
|
171
173
|
def inspect
|
172
|
-
"<BSON::Binary:0x#{object_id} type=#{type} data=0x#{data[0, 8].
|
174
|
+
"<BSON::Binary:0x#{object_id} type=#{type} data=0x#{data[0, 8].unpack1('H*')}...>"
|
173
175
|
end
|
174
176
|
|
175
177
|
# Returns a string representation of the UUID stored in this Binary.
|
@@ -207,7 +209,8 @@ module BSON
|
|
207
209
|
if representation && representation != :standard
|
208
210
|
raise ArgumentError, "Binary of type :uuid can only be stringified to :standard representation, requested: #{representation.inspect}"
|
209
211
|
end
|
210
|
-
|
212
|
+
|
213
|
+
data.split('').map { |n| '%02x' % n.ord }.join.sub(/\A(.{8})(.{4})(.{4})(.{4})(.{12})\z/, '\1-\2-\3-\4-\5')
|
211
214
|
when :uuid_old
|
212
215
|
if representation.nil?
|
213
216
|
raise ArgumentError, 'Representation must be specified for BSON::Binary objects of type :uuid_old'
|
@@ -229,7 +232,7 @@ module BSON
|
|
229
232
|
hex
|
230
233
|
else
|
231
234
|
raise ArgumentError, "Invalid representation: #{representation}"
|
232
|
-
end.sub(
|
235
|
+
end.sub(/\A(.{8})(.{4})(.{4})(.{4})(.{12})\z/, '\1-\2-\3-\4-\5')
|
233
236
|
else
|
234
237
|
raise TypeError, "The type of Binary must be :uuid or :uuid_old, this object is: #{type.inspect}"
|
235
238
|
end
|
data/lib/bson/boolean.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -25,7 +26,7 @@ module BSON
|
|
25
26
|
# A boolean is type 0x08 in the BSON spec.
|
26
27
|
#
|
27
28
|
# @since 2.0.0
|
28
|
-
BSON_TYPE = 8.chr
|
29
|
+
BSON_TYPE = ::String.new(8.chr, encoding: BINARY).freeze
|
29
30
|
|
30
31
|
# Deserialize a boolean from BSON.
|
31
32
|
#
|
data/lib/bson/code.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -25,7 +26,7 @@ module BSON
|
|
25
26
|
# A code is type 0x0D in the BSON spec.
|
26
27
|
#
|
27
28
|
# @since 2.0.0
|
28
|
-
BSON_TYPE = 13.chr
|
29
|
+
BSON_TYPE = ::String.new(13.chr, encoding: BINARY).freeze
|
29
30
|
|
30
31
|
# @!attribute javascript
|
31
32
|
# @return [ String ] The javascript code.
|
data/lib/bson/code_with_scope.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -26,7 +27,7 @@ module BSON
|
|
26
27
|
# A code with scope is type 0x0F in the BSON spec.
|
27
28
|
#
|
28
29
|
# @since 2.0.0
|
29
|
-
BSON_TYPE = 15.chr
|
30
|
+
BSON_TYPE = ::String.new(15.chr, encoding: BINARY).freeze
|
30
31
|
|
31
32
|
# @!attribute javascript
|
32
33
|
# @return [ String ] The javascript code.
|
data/lib/bson/config.rb
CHANGED
data/lib/bson/date.rb
CHANGED
data/lib/bson/date_time.rb
CHANGED
data/lib/bson/db_pointer.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -22,7 +23,7 @@ module BSON
|
|
22
23
|
include JSON
|
23
24
|
|
24
25
|
# A DBPointer is type 0x0C in the BSON spec.
|
25
|
-
BSON_TYPE = 0x0C.chr
|
26
|
+
BSON_TYPE = ::String.new(0x0C.chr, encoding: BINARY).freeze
|
26
27
|
|
27
28
|
# Create a new DBPointer object.
|
28
29
|
#
|
data/lib/bson/dbref.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# Copyright (C) 2015-2021 MongoDB Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the 'License');
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an 'AS IS' BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
module BSON
|
19
|
+
|
20
|
+
# Represents a DBRef document in the database.
|
21
|
+
class DBRef < Document
|
22
|
+
include JSON
|
23
|
+
|
24
|
+
# The constant for the collection reference field.
|
25
|
+
#
|
26
|
+
# @deprecated
|
27
|
+
COLLECTION = '$ref'.freeze
|
28
|
+
|
29
|
+
# The constant for the id field.
|
30
|
+
#
|
31
|
+
# @deprecated
|
32
|
+
ID = '$id'.freeze
|
33
|
+
|
34
|
+
# The constant for the database field.
|
35
|
+
#
|
36
|
+
# @deprecated
|
37
|
+
DATABASE = '$db'.freeze
|
38
|
+
|
39
|
+
# @return [ String ] collection The collection name.
|
40
|
+
def collection
|
41
|
+
self['$ref']
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [ BSON::ObjectId ] id The referenced document id.
|
45
|
+
def id
|
46
|
+
self['$id']
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [ String ] database The database name.
|
50
|
+
def database
|
51
|
+
self['$db']
|
52
|
+
end
|
53
|
+
|
54
|
+
# Get the DBRef as a JSON document
|
55
|
+
#
|
56
|
+
# @example Get the DBRef as a JSON hash.
|
57
|
+
# dbref.as_json
|
58
|
+
#
|
59
|
+
# @return [ Hash ] The max key as a JSON hash.
|
60
|
+
def as_json(*args)
|
61
|
+
{}.update(self)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Instantiate a new DBRef.
|
65
|
+
#
|
66
|
+
# @example Create the DBRef.
|
67
|
+
# BSON::DBRef.new({'$ref' => 'users', '$id' => id, '$db' => 'database'})
|
68
|
+
#
|
69
|
+
# @param [ Hash ] hash the DBRef hash. It must contain $ref and $id.
|
70
|
+
def initialize(hash)
|
71
|
+
hash = reorder_fields(hash)
|
72
|
+
%w($ref $id).each do |key|
|
73
|
+
unless hash[key]
|
74
|
+
raise ArgumentError, "DBRef must have #{key}: #{hash}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
unless hash['$ref'].is_a?(String)
|
79
|
+
raise ArgumentError, "The value for key $ref must be a string, got: #{hash['$ref']}"
|
80
|
+
end
|
81
|
+
|
82
|
+
if db = hash['$db']
|
83
|
+
unless db.is_a?(String)
|
84
|
+
raise ArgumentError, "The value for key $db must be a string, got: #{hash['$db']}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
super
|
89
|
+
end
|
90
|
+
|
91
|
+
# Converts the DBRef to raw BSON.
|
92
|
+
#
|
93
|
+
# @example Convert the DBRef to raw BSON.
|
94
|
+
# dbref.to_bson
|
95
|
+
#
|
96
|
+
# @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
|
97
|
+
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
|
98
|
+
#
|
99
|
+
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
100
|
+
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
101
|
+
as_json.to_bson(buffer, validating_keys)
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
# Reorder the fields of the given Hash to have $ref first, $id second,
|
107
|
+
# and $db third. The rest of the fields in the hash can come in any
|
108
|
+
# order after that.
|
109
|
+
#
|
110
|
+
# @param [ Hash ] hash The input hash. Must be a valid dbref.
|
111
|
+
#
|
112
|
+
# @return [ Hash ] The hash with it's fields reordered.
|
113
|
+
def reorder_fields(hash)
|
114
|
+
hash = BSON::Document.new(hash)
|
115
|
+
reordered = {}
|
116
|
+
reordered['$ref'] = hash.delete('$ref')
|
117
|
+
reordered['$id'] = hash.delete('$id')
|
118
|
+
if db = hash.delete('$db')
|
119
|
+
reordered['$db'] = db
|
120
|
+
end
|
121
|
+
|
122
|
+
reordered.update(hash)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2016-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -26,27 +27,27 @@ module BSON
|
|
26
27
|
# Infinity mask.
|
27
28
|
#
|
28
29
|
# @since 4.2.0
|
29
|
-
INFINITY_MASK = 0x7800000000000000
|
30
|
+
INFINITY_MASK = 0x7800000000000000
|
30
31
|
|
31
32
|
# NaN mask.
|
32
33
|
#
|
33
34
|
# @since 4.2.0
|
34
|
-
NAN_MASK = 0x7c00000000000000
|
35
|
+
NAN_MASK = 0x7c00000000000000
|
35
36
|
|
36
37
|
# SNaN mask.
|
37
38
|
#
|
38
39
|
# @since 4.2.0
|
39
|
-
SNAN_MASK = (1 << 57)
|
40
|
+
SNAN_MASK = (1 << 57)
|
40
41
|
|
41
42
|
# Signed bit mask.
|
42
43
|
#
|
43
44
|
# @since 4.2.0
|
44
|
-
SIGN_BIT_MASK = (1 << 63)
|
45
|
+
SIGN_BIT_MASK = (1 << 63)
|
45
46
|
|
46
47
|
# The two highest bits of the 64 high order bits.
|
47
48
|
#
|
48
49
|
# @since 4.2.0
|
49
|
-
TWO_HIGHEST_BITS_SET = (3 << 61)
|
50
|
+
TWO_HIGHEST_BITS_SET = (3 << 61)
|
50
51
|
|
51
52
|
extend self
|
52
53
|
|
@@ -84,9 +85,13 @@ module BSON
|
|
84
85
|
private
|
85
86
|
|
86
87
|
def validate_range!(exponent, significand)
|
87
|
-
unless
|
88
|
+
unless valid_exponent?(exponent)
|
88
89
|
raise Decimal128::InvalidRange.new
|
89
90
|
end
|
91
|
+
|
92
|
+
unless valid_significand?(significand)
|
93
|
+
raise Decimal128::UnrepresentablePrecision.new
|
94
|
+
end
|
90
95
|
end
|
91
96
|
|
92
97
|
def valid_significand?(significand)
|
@@ -109,14 +114,14 @@ module BSON
|
|
109
114
|
# @return [ Regex ] A regex matching a NaN string.
|
110
115
|
#
|
111
116
|
# @since 4.2.0
|
112
|
-
NAN_REGEX = /^(\-)?(S)?NaN$/i
|
117
|
+
NAN_REGEX = /^(\-)?(S)?NaN$/i
|
113
118
|
|
114
119
|
# Regex matching a string representing positive or negative Infinity.
|
115
120
|
#
|
116
121
|
# @return [ Regex ] A regex matching a positive or negative Infinity string.
|
117
122
|
#
|
118
123
|
# @since 4.2.0
|
119
|
-
INFINITY_REGEX = /^(\+|\-)?Inf(inity)?$/i
|
124
|
+
INFINITY_REGEX = /^(\+|\-)?Inf(inity)?$/i
|
120
125
|
|
121
126
|
# Regex for the fraction, including leading zeros.
|
122
127
|
#
|
@@ -124,33 +129,33 @@ module BSON
|
|
124
129
|
# including leading zeros.
|
125
130
|
#
|
126
131
|
# @since 4.2.0
|
127
|
-
SIGNIFICAND_WITH_LEADING_ZEROS_REGEX = /(0*)(\d+)
|
132
|
+
SIGNIFICAND_WITH_LEADING_ZEROS_REGEX = /(0*)(\d+)/
|
128
133
|
|
129
134
|
# Regex for separating a negative sign from the significands.
|
130
135
|
#
|
131
136
|
# @return [ Regex ] The regex for separating a sign from significands.
|
132
137
|
#
|
133
138
|
# @since 4.2.0
|
134
|
-
SIGN_AND_DIGITS_REGEX = /^(\-)?(\S+)
|
139
|
+
SIGN_AND_DIGITS_REGEX = /^(\-)?(\S+)/
|
135
140
|
|
136
141
|
# Regex matching a scientific exponent.
|
137
142
|
#
|
138
143
|
# @return [ Regex ] A regex matching E, e, E+, e+.
|
139
144
|
#
|
140
145
|
# @since 4.2.0
|
141
|
-
SCIENTIFIC_EXPONENT_REGEX = /E\+?/i
|
146
|
+
SCIENTIFIC_EXPONENT_REGEX = /E\+?/i
|
142
147
|
|
143
148
|
# Regex for capturing trailing zeros.
|
144
149
|
#
|
145
150
|
# @since 4.2.0
|
146
|
-
TRAILING_ZEROS_REGEX = /[1-9]*(0+)
|
151
|
+
TRAILING_ZEROS_REGEX = /[1-9]*(0+)$/
|
147
152
|
|
148
153
|
# Regex for a valid decimal128 string format.
|
149
154
|
#
|
150
155
|
# @return [ Regex ] The regex for a valid decimal128 string.
|
151
156
|
#
|
152
157
|
# @since 4.2.0
|
153
|
-
VALID_DECIMAL128_STRING_REGEX = /^[\-\+]?(\d+(\.\d*)?|\.\d+)(E[\-\+]?\d+)?$/i
|
158
|
+
VALID_DECIMAL128_STRING_REGEX = /^[\-\+]?(\d+(\.\d*)?|\.\d+)(E[\-\+]?\d+)?$/i
|
154
159
|
|
155
160
|
# Initialize the FromString Builder object.
|
156
161
|
#
|
@@ -301,11 +306,11 @@ module BSON
|
|
301
306
|
|
302
307
|
def to_special_bits
|
303
308
|
case @big_decimal.sign
|
304
|
-
when BigDecimal::SIGN_POSITIVE_INFINITE
|
309
|
+
when ::BigDecimal::SIGN_POSITIVE_INFINITE
|
305
310
|
high = INFINITY_MASK
|
306
|
-
when BigDecimal::SIGN_NEGATIVE_INFINITE
|
311
|
+
when ::BigDecimal::SIGN_NEGATIVE_INFINITE
|
307
312
|
high = INFINITY_MASK | SIGN_BIT_MASK
|
308
|
-
when BigDecimal::SIGN_NaN
|
313
|
+
when ::BigDecimal::SIGN_NaN
|
309
314
|
high = NAN_MASK
|
310
315
|
end
|
311
316
|
[ 0, high ]
|
@@ -314,7 +319,7 @@ module BSON
|
|
314
319
|
def to_bits
|
315
320
|
sign, significand_str, base, exp = @big_decimal.split
|
316
321
|
exponent = @big_decimal.zero? ? 0 : exp - significand_str.length
|
317
|
-
is_negative = (sign == BigDecimal::SIGN_NEGATIVE_FINITE || sign == BigDecimal::SIGN_NEGATIVE_ZERO)
|
322
|
+
is_negative = (sign == ::BigDecimal::SIGN_NEGATIVE_FINITE || sign == ::BigDecimal::SIGN_NEGATIVE_ZERO)
|
318
323
|
Builder.parts_to_bits(significand_str.to_i,
|
319
324
|
exponent,
|
320
325
|
is_negative)
|
@@ -337,14 +342,14 @@ module BSON
|
|
337
342
|
# @return [ String ] The string representing NaN.
|
338
343
|
#
|
339
344
|
# @since 4.2.0
|
340
|
-
NAN_STRING = 'NaN'
|
345
|
+
NAN_STRING = 'NaN'
|
341
346
|
|
342
347
|
# String representing an Infinity value.
|
343
348
|
#
|
344
349
|
# @return [ String ] The string representing Infinity.
|
345
350
|
#
|
346
351
|
# @since 4.2.0
|
347
|
-
INFINITY_STRING = 'Infinity'
|
352
|
+
INFINITY_STRING = 'Infinity'
|
348
353
|
|
349
354
|
# Initialize the FromBigDecimal Builder object.
|
350
355
|
#
|
@@ -366,11 +371,13 @@ module BSON
|
|
366
371
|
#
|
367
372
|
# @return [ String ] The string representing the decimal128 object.
|
368
373
|
#
|
374
|
+
# @note The returned string may be frozen.
|
375
|
+
#
|
369
376
|
# @since 4.2.0
|
370
377
|
def string
|
371
378
|
return NAN_STRING if nan?
|
372
379
|
str = infinity? ? INFINITY_STRING : create_string
|
373
|
-
negative? ?
|
380
|
+
negative? ? "-#{str}" : str
|
374
381
|
end
|
375
382
|
|
376
383
|
private
|