bson 4.2.2 → 4.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.md +25 -7
- data/Rakefile +16 -9
- data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
- data/ext/bson/bson-native.h +125 -0
- data/ext/bson/bytebuf.c +133 -0
- data/ext/bson/endian.c +117 -0
- data/ext/bson/init.c +355 -0
- data/ext/bson/libbson-utf8.c +230 -0
- data/ext/bson/read.c +411 -0
- data/ext/bson/util.c +95 -0
- data/ext/bson/write.c +680 -0
- data/lib/bson.rb +6 -3
- data/lib/bson/active_support.rb +17 -0
- data/lib/bson/array.rb +57 -17
- data/lib/bson/binary.rb +185 -13
- data/lib/bson/boolean.rb +12 -3
- data/lib/bson/code.rb +16 -2
- data/lib/bson/code_with_scope.rb +32 -5
- data/lib/bson/config.rb +1 -1
- data/lib/bson/date.rb +12 -2
- data/lib/bson/date_time.rb +2 -2
- data/lib/bson/db_pointer.rb +110 -0
- data/lib/bson/decimal128.rb +17 -3
- data/lib/bson/decimal128/builder.rb +1 -1
- data/lib/bson/document.rb +152 -5
- data/lib/bson/environment.rb +2 -1
- data/lib/bson/error.rb +27 -0
- data/lib/bson/ext_json.rb +383 -0
- data/lib/bson/false_class.rb +1 -1
- data/lib/bson/float.rb +48 -2
- data/lib/bson/hash.rb +68 -17
- data/lib/bson/int32.rb +52 -13
- data/lib/bson/int64.rb +59 -15
- data/lib/bson/integer.rb +36 -2
- data/lib/bson/json.rb +1 -1
- data/lib/bson/max_key.rb +13 -1
- data/lib/bson/min_key.rb +13 -1
- data/lib/bson/nil_class.rb +4 -2
- data/lib/bson/object.rb +28 -1
- data/lib/bson/object_id.rb +16 -2
- data/lib/bson/open_struct.rb +1 -1
- data/lib/bson/regexp.rb +27 -4
- data/lib/bson/registry.rb +3 -3
- data/lib/bson/specialized.rb +4 -2
- data/lib/bson/string.rb +5 -3
- data/lib/bson/symbol.rb +99 -7
- data/lib/bson/time.rb +63 -4
- data/lib/bson/time_with_zone.rb +54 -0
- data/lib/bson/timestamp.rb +44 -6
- data/lib/bson/true_class.rb +1 -1
- data/lib/bson/undefined.rb +12 -1
- data/lib/bson/version.rb +2 -2
- data/spec/bson/array_spec.rb +18 -1
- data/spec/bson/binary_spec.rb +100 -3
- data/spec/bson/binary_uuid_spec.rb +189 -0
- data/spec/bson/boolean_spec.rb +1 -1
- data/spec/bson/byte_buffer_read_spec.rb +197 -0
- data/spec/bson/byte_buffer_spec.rb +121 -381
- data/spec/bson/byte_buffer_write_spec.rb +854 -0
- data/spec/bson/code_spec.rb +1 -1
- data/spec/bson/code_with_scope_spec.rb +1 -1
- data/spec/bson/date_spec.rb +1 -1
- data/spec/bson/date_time_spec.rb +54 -1
- data/spec/bson/decimal128_spec.rb +35 -35
- data/spec/bson/document_as_spec.rb +46 -0
- data/spec/bson/document_spec.rb +197 -30
- data/spec/bson/ext_json_parse_spec.rb +308 -0
- data/spec/bson/false_class_spec.rb +1 -1
- data/spec/bson/float_spec.rb +37 -1
- data/spec/bson/hash_as_spec.rb +57 -0
- data/spec/bson/hash_spec.rb +209 -1
- data/spec/bson/int32_spec.rb +180 -6
- data/spec/bson/int64_spec.rb +199 -6
- data/spec/bson/integer_spec.rb +29 -3
- data/spec/bson/json_spec.rb +1 -1
- data/spec/bson/max_key_spec.rb +1 -1
- data/spec/bson/min_key_spec.rb +1 -1
- data/spec/bson/nil_class_spec.rb +1 -1
- data/spec/bson/object_id_spec.rb +1 -1
- data/spec/bson/object_spec.rb +1 -1
- data/spec/bson/open_struct_spec.rb +1 -1
- data/spec/bson/raw_spec.rb +34 -2
- data/spec/bson/regexp_spec.rb +1 -1
- data/spec/bson/registry_spec.rb +1 -1
- data/spec/bson/string_spec.rb +19 -1
- data/spec/bson/symbol_raw_spec.rb +45 -0
- data/spec/bson/symbol_spec.rb +63 -3
- data/spec/bson/time_spec.rb +205 -2
- data/spec/bson/time_with_zone_spec.rb +68 -0
- data/spec/bson/timestamp_spec.rb +56 -1
- data/spec/bson/true_class_spec.rb +1 -1
- data/spec/bson/undefined_spec.rb +1 -1
- data/spec/bson_spec.rb +1 -1
- data/spec/{support → runners}/common_driver.rb +1 -1
- data/spec/runners/corpus.rb +185 -0
- data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
- data/spec/spec_helper.rb +40 -3
- data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
- data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +10 -7
- data/spec/spec_tests/corpus_spec.rb +124 -0
- data/spec/spec_tests/data/corpus/README.md +15 -0
- data/spec/spec_tests/data/corpus/array.json +49 -0
- data/spec/spec_tests/data/corpus/binary.json +113 -0
- data/spec/spec_tests/data/corpus/boolean.json +27 -0
- data/spec/spec_tests/data/corpus/code.json +67 -0
- data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
- data/spec/spec_tests/data/corpus/datetime.json +42 -0
- data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
- data/spec/spec_tests/data/corpus/dbref.json +31 -0
- data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
- data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
- data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
- data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
- data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
- data/spec/spec_tests/data/corpus/document.json +36 -0
- data/spec/spec_tests/data/corpus/double.json +87 -0
- data/spec/spec_tests/data/corpus/int32.json +43 -0
- data/spec/spec_tests/data/corpus/int64.json +43 -0
- data/spec/spec_tests/data/corpus/maxkey.json +12 -0
- data/spec/spec_tests/data/corpus/minkey.json +12 -0
- data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
- data/spec/spec_tests/data/corpus/multi-type.json +11 -0
- data/spec/spec_tests/data/corpus/null.json +12 -0
- data/spec/spec_tests/data/corpus/oid.json +28 -0
- data/spec/spec_tests/data/corpus/regex.json +65 -0
- data/spec/spec_tests/data/corpus/string.json +72 -0
- data/spec/spec_tests/data/corpus/symbol.json +80 -0
- data/spec/spec_tests/data/corpus/timestamp.json +34 -0
- data/spec/spec_tests/data/corpus/top.json +236 -0
- data/spec/spec_tests/data/corpus/undefined.json +15 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +8 -2
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
- data/spec/support/shared_examples.rb +3 -5
- data/spec/support/spec_config.rb +16 -0
- data/spec/support/utils.rb +10 -0
- metadata +227 -124
- metadata.gz.sig +0 -0
- data/ext/bson/bson_native.c +0 -762
data/lib/bson/hash.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -22,7 +22,7 @@ module BSON
|
|
22
22
|
# @since 2.0.0
|
23
23
|
module Hash
|
24
24
|
|
25
|
-
#
|
25
|
+
# A hash, also called an embedded document, is type 0x03 in the BSON spec.
|
26
26
|
#
|
27
27
|
# @since 2.0.0
|
28
28
|
BSON_TYPE = 3.chr.force_encoding(BINARY).freeze
|
@@ -38,15 +38,31 @@ module BSON
|
|
38
38
|
#
|
39
39
|
# @since 2.0.0
|
40
40
|
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
buffer.
|
45
|
-
buffer.
|
46
|
-
|
41
|
+
if buffer.respond_to?(:put_hash)
|
42
|
+
buffer.put_hash(self, validating_keys)
|
43
|
+
else
|
44
|
+
position = buffer.length
|
45
|
+
buffer.put_int32(0)
|
46
|
+
each do |field, value|
|
47
|
+
unless value.respond_to?(:bson_type)
|
48
|
+
raise Error::UnserializableClass, "Hash value for key '#{field}' does not define its BSON serialized type: #{value}"
|
49
|
+
end
|
50
|
+
buffer.put_byte(value.bson_type)
|
51
|
+
key = field.to_bson_key(validating_keys)
|
52
|
+
begin
|
53
|
+
buffer.put_cstring(key)
|
54
|
+
rescue ArgumentError => e
|
55
|
+
raise ArgumentError, "Error serializing key #{key}: #{e.class}: #{e}"
|
56
|
+
rescue EncodingError => e
|
57
|
+
# Note this may convert exception class from a subclass of
|
58
|
+
# EncodingError to EncodingError itself
|
59
|
+
raise EncodingError, "Error serializing key #{key}: #{e.class}: #{e}"
|
60
|
+
end
|
61
|
+
value.to_bson(buffer, validating_keys)
|
62
|
+
end
|
63
|
+
buffer.put_byte(NULL_BYTE)
|
64
|
+
buffer.replace_int32(position, buffer.length - position)
|
47
65
|
end
|
48
|
-
buffer.put_byte(NULL_BYTE)
|
49
|
-
buffer.replace_int32(position, buffer.length - position)
|
50
66
|
end
|
51
67
|
|
52
68
|
# Converts the hash to a normalized value in a BSON document.
|
@@ -61,25 +77,60 @@ module BSON
|
|
61
77
|
Document.new(self)
|
62
78
|
end
|
63
79
|
|
80
|
+
# Converts this object to a representation directly serializable to
|
81
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
82
|
+
#
|
83
|
+
# This method recursively invokes +as_extended_json+ with the provided
|
84
|
+
# options on each hash value.
|
85
|
+
#
|
86
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
87
|
+
# (default is canonical extended JSON)
|
88
|
+
#
|
89
|
+
# @return [ Hash ] This hash converted to extended json representation.
|
90
|
+
def as_extended_json(**options)
|
91
|
+
::Hash[map do |key, value|
|
92
|
+
[key, value.as_extended_json(**options)]
|
93
|
+
end]
|
94
|
+
end
|
95
|
+
|
64
96
|
module ClassMethods
|
65
97
|
|
66
98
|
# Deserialize the hash from BSON.
|
67
99
|
#
|
68
100
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
69
101
|
#
|
102
|
+
# @option options [ nil | :bson ] :mode Decoding mode to use.
|
103
|
+
#
|
70
104
|
# @return [ Array ] The decoded hash.
|
71
105
|
#
|
72
106
|
# @see http://bsonspec.org/#/specification
|
73
107
|
#
|
74
108
|
# @since 2.0.0
|
75
|
-
def from_bson(buffer)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
109
|
+
def from_bson(buffer, **options)
|
110
|
+
if buffer.respond_to?(:get_hash)
|
111
|
+
buffer.get_hash(**options)
|
112
|
+
else
|
113
|
+
hash = Document.allocate
|
114
|
+
start_position = buffer.read_position
|
115
|
+
expected_byte_size = buffer.get_int32
|
116
|
+
while (type = buffer.get_byte) != NULL_BYTE
|
117
|
+
field = buffer.get_cstring
|
118
|
+
cls = BSON::Registry.get(type, field)
|
119
|
+
value = if options.empty?
|
120
|
+
# Compatibility with the older Ruby driver versions which define
|
121
|
+
# a DBRef class with from_bson accepting a single argument.
|
122
|
+
cls.from_bson(buffer)
|
123
|
+
else
|
124
|
+
cls.from_bson(buffer, **options)
|
125
|
+
end
|
126
|
+
hash.store(field, value)
|
127
|
+
end
|
128
|
+
actual_byte_size = buffer.read_position - start_position
|
129
|
+
if actual_byte_size != expected_byte_size
|
130
|
+
raise Error::BSONDecodeError, "Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
|
131
|
+
end
|
132
|
+
hash
|
81
133
|
end
|
82
|
-
hash
|
83
134
|
end
|
84
135
|
end
|
85
136
|
|
data/lib/bson/int32.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -14,8 +14,7 @@
|
|
14
14
|
|
15
15
|
module BSON
|
16
16
|
|
17
|
-
# Represents
|
18
|
-
# specification.
|
17
|
+
# Represents int32 type.
|
19
18
|
#
|
20
19
|
# @see http://bsonspec.org/#/specification
|
21
20
|
#
|
@@ -41,27 +40,41 @@ module BSON
|
|
41
40
|
#
|
42
41
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
43
42
|
#
|
43
|
+
# @option options [ nil | :bson ] :mode Decoding mode to use.
|
44
|
+
#
|
44
45
|
# @return [ Integer ] The decoded Integer.
|
45
46
|
#
|
46
47
|
# @see http://bsonspec.org/#/specification
|
47
48
|
#
|
48
49
|
# @since 2.0.0
|
49
|
-
def self.from_bson(buffer)
|
50
|
+
def self.from_bson(buffer, **options)
|
50
51
|
buffer.get_int32
|
51
52
|
end
|
52
53
|
|
53
54
|
# Instantiate a BSON Int32.
|
54
55
|
#
|
55
|
-
# @param [ Integer ]
|
56
|
+
# @param [ Integer ] value The 32-bit integer.
|
56
57
|
#
|
57
58
|
# @see http://bsonspec.org/#/specification
|
58
59
|
#
|
59
60
|
# @since 4.2.0
|
60
|
-
def initialize(
|
61
|
-
|
62
|
-
|
61
|
+
def initialize(value)
|
62
|
+
if value.is_a?(self.class)
|
63
|
+
@value = value.value
|
64
|
+
return
|
65
|
+
end
|
66
|
+
|
67
|
+
unless value.bson_int32?
|
68
|
+
raise RangeError.new("#{value} cannot be stored in 32 bits")
|
69
|
+
end
|
70
|
+
@value = value.freeze
|
63
71
|
end
|
64
72
|
|
73
|
+
# Returns the value of this Int32.
|
74
|
+
#
|
75
|
+
# @return [ Integer ] The integer value.
|
76
|
+
attr_reader :value
|
77
|
+
|
65
78
|
# Append the integer as encoded BSON to a ByteBuffer.
|
66
79
|
#
|
67
80
|
# @example Encoded the integer and append to a ByteBuffer.
|
@@ -73,7 +86,7 @@ module BSON
|
|
73
86
|
#
|
74
87
|
# @since 4.2.0
|
75
88
|
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
76
|
-
buffer.put_int32(
|
89
|
+
buffer.put_int32(value)
|
77
90
|
end
|
78
91
|
|
79
92
|
# Convert the integer to a BSON string key.
|
@@ -87,13 +100,39 @@ module BSON
|
|
87
100
|
#
|
88
101
|
# @since 4.2.0
|
89
102
|
def to_bson_key(validating_keys = Config.validating_keys?)
|
90
|
-
|
103
|
+
value
|
91
104
|
end
|
92
105
|
|
93
|
-
|
106
|
+
# Check equality of the int32 with another object.
|
107
|
+
#
|
108
|
+
# @param [ Object ] other The object to check against.
|
109
|
+
#
|
110
|
+
# @return [ true, false ] If the objects are equal.
|
111
|
+
#
|
112
|
+
# @since 4.4.0
|
113
|
+
def ==(other)
|
114
|
+
return false unless other.is_a?(Int32)
|
115
|
+
value == other.value
|
116
|
+
end
|
117
|
+
alias :eql? :==
|
118
|
+
alias :=== :==
|
94
119
|
|
95
|
-
|
96
|
-
|
120
|
+
# Converts this object to a representation directly serializable to
|
121
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
122
|
+
#
|
123
|
+
# This method returns the integer value if relaxed representation is
|
124
|
+
# requested, otherwise a $numberInt hash.
|
125
|
+
#
|
126
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
127
|
+
# (default is canonical extended JSON)
|
128
|
+
#
|
129
|
+
# @return [ Hash | Integer ] The extended json representation.
|
130
|
+
def as_extended_json(**options)
|
131
|
+
if options[:mode] == :relaxed || options[:mode] == :legacy
|
132
|
+
value
|
133
|
+
else
|
134
|
+
{'$numberInt' => value.to_s}
|
135
|
+
end
|
97
136
|
end
|
98
137
|
|
99
138
|
# Register this type when the module is loaded.
|
data/lib/bson/int64.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -14,8 +14,7 @@
|
|
14
14
|
|
15
15
|
module BSON
|
16
16
|
|
17
|
-
# Represents
|
18
|
-
# specification.
|
17
|
+
# Represents int64 type.
|
19
18
|
#
|
20
19
|
# @see http://bsonspec.org/#/specification
|
21
20
|
#
|
@@ -36,27 +35,46 @@ module BSON
|
|
36
35
|
#
|
37
36
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
38
37
|
#
|
39
|
-
# @
|
38
|
+
# @option options [ nil | :bson ] :mode Decoding mode to use.
|
39
|
+
#
|
40
|
+
# @return [ Integer | BSON::Int64 ] The decoded Integer.
|
40
41
|
#
|
41
42
|
# @see http://bsonspec.org/#/specification
|
42
43
|
#
|
43
44
|
# @since 2.0.0
|
44
|
-
def self.from_bson(buffer)
|
45
|
-
buffer.get_int64
|
45
|
+
def self.from_bson(buffer, **options)
|
46
|
+
value = buffer.get_int64
|
47
|
+
if options[:mode] == :bson
|
48
|
+
new(value)
|
49
|
+
else
|
50
|
+
value
|
51
|
+
end
|
46
52
|
end
|
47
53
|
|
48
54
|
# Instantiate a BSON Int64.
|
49
55
|
#
|
50
|
-
# @param [ Integer ]
|
56
|
+
# @param [ Integer ] value The 64-bit integer.
|
51
57
|
#
|
52
58
|
# @see http://bsonspec.org/#/specification
|
53
59
|
#
|
54
60
|
# @since 4.2.0
|
55
|
-
def initialize(
|
56
|
-
|
57
|
-
|
61
|
+
def initialize(value)
|
62
|
+
if value.is_a?(self.class)
|
63
|
+
@value = value.value
|
64
|
+
return
|
65
|
+
end
|
66
|
+
|
67
|
+
unless value.bson_int64?
|
68
|
+
raise RangeError.new("#{value} cannot be stored in 64 bits")
|
69
|
+
end
|
70
|
+
@value = value.freeze
|
58
71
|
end
|
59
72
|
|
73
|
+
# Returns the value of this Int64.
|
74
|
+
#
|
75
|
+
# @return [ Integer ] The integer value.
|
76
|
+
attr_reader :value
|
77
|
+
|
60
78
|
# Append the integer as encoded BSON to a ByteBuffer.
|
61
79
|
#
|
62
80
|
# @example Encoded the integer and append to a ByteBuffer.
|
@@ -68,7 +86,7 @@ module BSON
|
|
68
86
|
#
|
69
87
|
# @since 4.2.0
|
70
88
|
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
71
|
-
buffer.put_int64(
|
89
|
+
buffer.put_int64(value)
|
72
90
|
end
|
73
91
|
|
74
92
|
# Convert the integer to a BSON string key.
|
@@ -82,13 +100,39 @@ module BSON
|
|
82
100
|
#
|
83
101
|
# @since 4.2.0
|
84
102
|
def to_bson_key(validating_keys = Config.validating_keys?)
|
85
|
-
|
103
|
+
value
|
86
104
|
end
|
87
105
|
|
88
|
-
|
106
|
+
# Check equality of the int64 with another object.
|
107
|
+
#
|
108
|
+
# @param [ Object ] other The object to check against.
|
109
|
+
#
|
110
|
+
# @return [ true, false ] If the objects are equal.
|
111
|
+
#
|
112
|
+
# @since 4.4.0
|
113
|
+
def ==(other)
|
114
|
+
return false unless other.is_a?(Int64)
|
115
|
+
value == other.value
|
116
|
+
end
|
117
|
+
alias :eql? :==
|
118
|
+
alias :=== :==
|
89
119
|
|
90
|
-
|
91
|
-
|
120
|
+
# Converts this object to a representation directly serializable to
|
121
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
122
|
+
#
|
123
|
+
# This method returns the integer value if relaxed representation is
|
124
|
+
# requested, otherwise a $numberLong hash.
|
125
|
+
#
|
126
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
127
|
+
# (default is canonical extended JSON)
|
128
|
+
#
|
129
|
+
# @return [ Hash | Integer ] The extended json representation.
|
130
|
+
def as_extended_json(**options)
|
131
|
+
if options[:mode] == :relaxed || options[:mode] == :legacy
|
132
|
+
value
|
133
|
+
else
|
134
|
+
{'$numberLong' => value.to_s}
|
135
|
+
end
|
92
136
|
end
|
93
137
|
|
94
138
|
# Register this type when the module is loaded.
|
data/lib/bson/integer.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -156,7 +156,41 @@ module BSON
|
|
156
156
|
#
|
157
157
|
# @since 2.0.0
|
158
158
|
def to_bson_key(validating_keys = Config.validating_keys?)
|
159
|
-
|
159
|
+
self
|
160
|
+
end
|
161
|
+
|
162
|
+
# Converts this object to a representation directly serializable to
|
163
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
164
|
+
#
|
165
|
+
# This method returns the integer itself if relaxed representation is
|
166
|
+
# requested, otherwise a $numberInt hash if the value fits in 32 bits
|
167
|
+
# and a $numberLong otherwise. Regardless of which representation is
|
168
|
+
# requested, a value that does not fit in 64 bits raises RangeError.
|
169
|
+
#
|
170
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
171
|
+
# (default is canonical extended JSON)
|
172
|
+
#
|
173
|
+
# @return [ Hash | Integer ] The extended json representation.
|
174
|
+
def as_extended_json(**options)
|
175
|
+
# The behavior of native integers' serialization to extended json is
|
176
|
+
# not specified. Following our bson serialization logic in this file,
|
177
|
+
# produce explicit $numberInt or $numberLong, choosing $numberInt if
|
178
|
+
# the integer fits in 32 bits. In Ruby integers can be arbitrarily
|
179
|
+
# big; integers that do not fit into 64 bits raise an error as we do not
|
180
|
+
# want to silently perform an effective type conversion of integer ->
|
181
|
+
# decimal.
|
182
|
+
|
183
|
+
unless bson_int64?
|
184
|
+
raise RangeError, "Integer #{self} is too big to be represented as a MongoDB integer"
|
185
|
+
end
|
186
|
+
|
187
|
+
if options[:mode] == :relaxed || options[:mode] == :legacy
|
188
|
+
self
|
189
|
+
elsif bson_int32?
|
190
|
+
{'$numberInt' => to_s}
|
191
|
+
else
|
192
|
+
{'$numberLong' => to_s}
|
193
|
+
end
|
160
194
|
end
|
161
195
|
|
162
196
|
private
|
data/lib/bson/json.rb
CHANGED
data/lib/bson/max_key.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -58,7 +58,19 @@ module BSON
|
|
58
58
|
# @return [ Hash ] The max key as a JSON hash.
|
59
59
|
#
|
60
60
|
# @since 2.0.0
|
61
|
+
# @deprecated Use as_extended_json instead.
|
61
62
|
def as_json(*args)
|
63
|
+
as_extended_json
|
64
|
+
end
|
65
|
+
|
66
|
+
# Converts this object to a representation directly serializable to
|
67
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
68
|
+
#
|
69
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
70
|
+
# (default is canonical extended JSON)
|
71
|
+
#
|
72
|
+
# @return [ Hash ] The extended json representation.
|
73
|
+
def as_extended_json(**options)
|
62
74
|
{ "$maxKey" => 1 }
|
63
75
|
end
|
64
76
|
|
data/lib/bson/min_key.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -58,7 +58,19 @@ module BSON
|
|
58
58
|
# @return [ Hash ] The min key as a JSON hash.
|
59
59
|
#
|
60
60
|
# @since 2.0.0
|
61
|
+
# @deprecated Use as_extended_json instead.
|
61
62
|
def as_json(*args)
|
63
|
+
as_extended_json
|
64
|
+
end
|
65
|
+
|
66
|
+
# Converts this object to a representation directly serializable to
|
67
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
68
|
+
#
|
69
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
70
|
+
# (default is canonical extended JSON)
|
71
|
+
#
|
72
|
+
# @return [ Hash ] The extended json representation.
|
73
|
+
def as_extended_json(**options)
|
62
74
|
{ "$minKey" => 1 }
|
63
75
|
end
|
64
76
|
|