bson 4.2.2 → 4.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +25 -7
  5. data/Rakefile +16 -9
  6. data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
  7. data/ext/bson/bson-native.h +125 -0
  8. data/ext/bson/bytebuf.c +133 -0
  9. data/ext/bson/endian.c +117 -0
  10. data/ext/bson/init.c +355 -0
  11. data/ext/bson/libbson-utf8.c +230 -0
  12. data/ext/bson/read.c +411 -0
  13. data/ext/bson/util.c +95 -0
  14. data/ext/bson/write.c +680 -0
  15. data/lib/bson.rb +6 -3
  16. data/lib/bson/active_support.rb +17 -0
  17. data/lib/bson/array.rb +57 -17
  18. data/lib/bson/binary.rb +185 -13
  19. data/lib/bson/boolean.rb +12 -3
  20. data/lib/bson/code.rb +16 -2
  21. data/lib/bson/code_with_scope.rb +32 -5
  22. data/lib/bson/config.rb +1 -1
  23. data/lib/bson/date.rb +12 -2
  24. data/lib/bson/date_time.rb +2 -2
  25. data/lib/bson/db_pointer.rb +110 -0
  26. data/lib/bson/decimal128.rb +17 -3
  27. data/lib/bson/decimal128/builder.rb +1 -1
  28. data/lib/bson/document.rb +152 -5
  29. data/lib/bson/environment.rb +2 -1
  30. data/lib/bson/error.rb +27 -0
  31. data/lib/bson/ext_json.rb +383 -0
  32. data/lib/bson/false_class.rb +1 -1
  33. data/lib/bson/float.rb +48 -2
  34. data/lib/bson/hash.rb +68 -17
  35. data/lib/bson/int32.rb +52 -13
  36. data/lib/bson/int64.rb +59 -15
  37. data/lib/bson/integer.rb +36 -2
  38. data/lib/bson/json.rb +1 -1
  39. data/lib/bson/max_key.rb +13 -1
  40. data/lib/bson/min_key.rb +13 -1
  41. data/lib/bson/nil_class.rb +4 -2
  42. data/lib/bson/object.rb +28 -1
  43. data/lib/bson/object_id.rb +16 -2
  44. data/lib/bson/open_struct.rb +1 -1
  45. data/lib/bson/regexp.rb +27 -4
  46. data/lib/bson/registry.rb +3 -3
  47. data/lib/bson/specialized.rb +4 -2
  48. data/lib/bson/string.rb +5 -3
  49. data/lib/bson/symbol.rb +99 -7
  50. data/lib/bson/time.rb +63 -4
  51. data/lib/bson/time_with_zone.rb +54 -0
  52. data/lib/bson/timestamp.rb +44 -6
  53. data/lib/bson/true_class.rb +1 -1
  54. data/lib/bson/undefined.rb +12 -1
  55. data/lib/bson/version.rb +2 -2
  56. data/spec/bson/array_spec.rb +18 -1
  57. data/spec/bson/binary_spec.rb +100 -3
  58. data/spec/bson/binary_uuid_spec.rb +189 -0
  59. data/spec/bson/boolean_spec.rb +1 -1
  60. data/spec/bson/byte_buffer_read_spec.rb +197 -0
  61. data/spec/bson/byte_buffer_spec.rb +121 -381
  62. data/spec/bson/byte_buffer_write_spec.rb +854 -0
  63. data/spec/bson/code_spec.rb +1 -1
  64. data/spec/bson/code_with_scope_spec.rb +1 -1
  65. data/spec/bson/date_spec.rb +1 -1
  66. data/spec/bson/date_time_spec.rb +54 -1
  67. data/spec/bson/decimal128_spec.rb +35 -35
  68. data/spec/bson/document_as_spec.rb +46 -0
  69. data/spec/bson/document_spec.rb +197 -30
  70. data/spec/bson/ext_json_parse_spec.rb +308 -0
  71. data/spec/bson/false_class_spec.rb +1 -1
  72. data/spec/bson/float_spec.rb +37 -1
  73. data/spec/bson/hash_as_spec.rb +57 -0
  74. data/spec/bson/hash_spec.rb +209 -1
  75. data/spec/bson/int32_spec.rb +180 -6
  76. data/spec/bson/int64_spec.rb +199 -6
  77. data/spec/bson/integer_spec.rb +29 -3
  78. data/spec/bson/json_spec.rb +1 -1
  79. data/spec/bson/max_key_spec.rb +1 -1
  80. data/spec/bson/min_key_spec.rb +1 -1
  81. data/spec/bson/nil_class_spec.rb +1 -1
  82. data/spec/bson/object_id_spec.rb +1 -1
  83. data/spec/bson/object_spec.rb +1 -1
  84. data/spec/bson/open_struct_spec.rb +1 -1
  85. data/spec/bson/raw_spec.rb +34 -2
  86. data/spec/bson/regexp_spec.rb +1 -1
  87. data/spec/bson/registry_spec.rb +1 -1
  88. data/spec/bson/string_spec.rb +19 -1
  89. data/spec/bson/symbol_raw_spec.rb +45 -0
  90. data/spec/bson/symbol_spec.rb +63 -3
  91. data/spec/bson/time_spec.rb +205 -2
  92. data/spec/bson/time_with_zone_spec.rb +68 -0
  93. data/spec/bson/timestamp_spec.rb +56 -1
  94. data/spec/bson/true_class_spec.rb +1 -1
  95. data/spec/bson/undefined_spec.rb +1 -1
  96. data/spec/bson_spec.rb +1 -1
  97. data/spec/{support → runners}/common_driver.rb +1 -1
  98. data/spec/runners/corpus.rb +185 -0
  99. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  100. data/spec/spec_helper.rb +40 -3
  101. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  102. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +10 -7
  103. data/spec/spec_tests/corpus_spec.rb +124 -0
  104. data/spec/spec_tests/data/corpus/README.md +15 -0
  105. data/spec/spec_tests/data/corpus/array.json +49 -0
  106. data/spec/spec_tests/data/corpus/binary.json +113 -0
  107. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  108. data/spec/spec_tests/data/corpus/code.json +67 -0
  109. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  110. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  111. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  112. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  113. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  114. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  115. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  116. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  117. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  118. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  119. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  120. data/spec/spec_tests/data/corpus/document.json +36 -0
  121. data/spec/spec_tests/data/corpus/double.json +87 -0
  122. data/spec/spec_tests/data/corpus/int32.json +43 -0
  123. data/spec/spec_tests/data/corpus/int64.json +43 -0
  124. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  125. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  126. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  127. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  128. data/spec/spec_tests/data/corpus/null.json +12 -0
  129. data/spec/spec_tests/data/corpus/oid.json +28 -0
  130. data/spec/spec_tests/data/corpus/regex.json +65 -0
  131. data/spec/spec_tests/data/corpus/string.json +72 -0
  132. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  133. data/spec/spec_tests/data/corpus/timestamp.json +34 -0
  134. data/spec/spec_tests/data/corpus/top.json +236 -0
  135. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +8 -2
  137. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  147. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  148. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  149. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  150. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  151. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  152. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  153. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  154. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  155. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  156. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  157. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  158. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  159. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  160. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  161. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  162. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  163. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  164. data/spec/support/shared_examples.rb +3 -5
  165. data/spec/support/spec_config.rb +16 -0
  166. data/spec/support/utils.rb +10 -0
  167. metadata +227 -124
  168. metadata.gz.sig +0 -0
  169. data/ext/bson/bson_native.c +0 -762
data/lib/bson/hash.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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
- # An hash (embedded document) is type 0x03 in the BSON spec.
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
- position = buffer.length
42
- buffer.put_int32(0)
43
- each do |field, value|
44
- buffer.put_byte(value.bson_type)
45
- buffer.put_cstring(field.to_bson_key(validating_keys))
46
- value.to_bson(buffer, validating_keys)
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
- hash = Document.allocate
77
- buffer.get_int32 # Throw away the size.
78
- while (type = buffer.get_byte) != NULL_BYTE
79
- field = buffer.get_cstring
80
- hash.store(field, BSON::Registry.get(type, field).from_bson(buffer))
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-2014 MongoDB Inc.
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 a $maxKey type, which compares less than any other value in the
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 ] integer The 32-bit 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(integer)
61
- out_of_range! unless integer.bson_int32?
62
- @integer = integer.freeze
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(@integer)
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
- @integer.to_bson_key(validating_keys)
103
+ value
91
104
  end
92
105
 
93
- private
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
- def out_of_range!
96
- raise RangeError.new("#{self} is not a valid 4 byte integer value.")
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-2014 MongoDB Inc.
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 a $maxKey type, which compares less than any other value in the
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
- # @return [ Integer ] The decoded Integer.
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 ] integer The 64-bit 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(integer)
56
- out_of_range! unless integer.bson_int64?
57
- @integer = integer.freeze
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(@integer)
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
- @integer.to_bson_key(validating_keys)
103
+ value
86
104
  end
87
105
 
88
- private
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
- def out_of_range!
91
- raise RangeError.new("#{self} is not a valid 8 byte integer value.")
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-2014 MongoDB Inc.
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
- to_s.to_bson_key(validating_keys)
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
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
data/lib/bson/max_key.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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-2014 MongoDB Inc.
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