bson 4.2.0.rc1 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +1 -4
  3. data.tar.gz.sig +0 -0
  4. data/lib/bson/array.rb +1 -1
  5. data/lib/bson/binary.rb +4 -2
  6. data/lib/bson/code.rb +1 -1
  7. data/lib/bson/code_with_scope.rb +1 -1
  8. data/lib/bson/date.rb +1 -1
  9. data/lib/bson/date_time.rb +1 -1
  10. data/lib/bson/decimal128.rb +1 -1
  11. data/lib/bson/false_class.rb +1 -1
  12. data/lib/bson/float.rb +1 -1
  13. data/lib/bson/hash.rb +1 -1
  14. data/lib/bson/integer.rb +1 -1
  15. data/lib/bson/object_id.rb +1 -1
  16. data/lib/bson/open_struct.rb +1 -1
  17. data/lib/bson/regexp.rb +86 -18
  18. data/lib/bson/specialized.rb +1 -1
  19. data/lib/bson/string.rb +1 -1
  20. data/lib/bson/symbol.rb +1 -1
  21. data/lib/bson/time.rb +1 -1
  22. data/lib/bson/timestamp.rb +1 -1
  23. data/lib/bson/true_class.rb +1 -1
  24. data/lib/bson/version.rb +1 -1
  25. data/spec/bson/array_spec.rb +1 -1
  26. data/spec/bson/binary_spec.rb +2 -1
  27. data/spec/bson/corpus_spec.rb +68 -0
  28. data/spec/bson/raw_spec.rb +540 -0
  29. data/spec/bson/regexp_spec.rb +7 -7
  30. data/spec/spec_helper.rb +1 -0
  31. data/spec/support/corpus-tests/array.json +43 -0
  32. data/spec/support/corpus-tests/boolean.json +27 -0
  33. data/spec/support/corpus-tests/code.json +67 -0
  34. data/spec/support/corpus-tests/code_w_scope.json +78 -0
  35. data/spec/support/corpus-tests/document.json +36 -0
  36. data/spec/support/corpus-tests/double.json +69 -0
  37. data/spec/support/corpus-tests/failures/binary.json +69 -0
  38. data/spec/support/corpus-tests/failures/datetime.json +31 -0
  39. data/spec/support/corpus-tests/failures/dbpointer.json +42 -0
  40. data/spec/support/corpus-tests/failures/int64.json +38 -0
  41. data/spec/support/corpus-tests/failures/symbol.json +62 -0
  42. data/spec/support/corpus-tests/failures/undefined.json +13 -0
  43. data/spec/support/corpus-tests/int32.json +38 -0
  44. data/spec/support/corpus-tests/maxkey.json +12 -0
  45. data/spec/support/corpus-tests/minkey.json +12 -0
  46. data/spec/support/corpus-tests/null.json +12 -0
  47. data/spec/support/corpus-tests/oid.json +28 -0
  48. data/spec/support/corpus-tests/regex.json +37 -0
  49. data/spec/support/corpus-tests/string.json +67 -0
  50. data/spec/support/corpus-tests/timestamp.json +18 -0
  51. data/spec/support/corpus-tests/top.json +62 -0
  52. data/spec/support/corpus.rb +265 -0
  53. data/spec/support/shared_examples.rb +1 -1
  54. metadata +51 -3
  55. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7d7c6cc6a8abff094af51c1a8432494d09c1f72
4
- data.tar.gz: f9994f21778f1c9e15e42c668df5321ceab814e5
3
+ metadata.gz: cb696b32143262aff3587deb025217f4e35278f0
4
+ data.tar.gz: 98b45e1fad06f918e2f12ca974714e96ad07ddc5
5
5
  SHA512:
6
- metadata.gz: 41d44df3cbad351bbc8eb9108d71c2052ce9dc7a54af97c2663f60e92f860408e6d393bbe8f16ed0d62b3c8d183e7edf011de518b479840b4df749a080837ea5
7
- data.tar.gz: baa1976590ed70238de71cb312db9252cbe47bbc95ba6bd407a85989d84df03f01788a5f3cee4de204af2f4e7f35cdeaea037aec601f2ebb475f7e87d8fc5d35
6
+ metadata.gz: 583135decf330bf180f8600fb8ff976f4bc548778fc176ff4cfe62f07c434f18cc80fd2887308495ecef9eee626922cb26ae34e3b4603e846f131f3a03cbffb3
7
+ data.tar.gz: b0446ddea4855a3c0e7b0ec5afc7b5a5b9e0b2aa897539bb4e29bc75be733bf16e12283c6c4bfb840377c1773ab3dbe2ae115a4ce6e706a8f8dfc7019e1e6b42
@@ -1,4 +1 @@
1
- ������ة�����D&;�2�!][�������
2
- ���"s�!x�^������z�y�<84C#��"`����;yl�cN�(��5.ۘ�����v+���]`?3d�>�|�Hc=�h?�@U��t���*�?�W����/��|ϵyN��yn���M.� ��R����ZHq����sv�ô�r�0�,Y�Q
3
- )�!
4
- �m����2��֨�%{�`��&��Ntw�w�-�$���:�ڈK.���>�/���
1
+ R�K` �k{�{�g����Dg���v���\�W���"J7y\*�<o�ҩ�"��^پ��&K$�؛��CP\}�3��G�����Te�<F��ɴs��%���գ�0������;�{�-��=e�@����fY*�ň��gǽ���|�p�OhIw��!�a�Q)�ퟫsJ�^HM��*�#{�G��$���U���jݚ��P+vBgܛG��Uُ�7��3*5�gM^��9'���0:�� �VoL��\�{���p�
data.tar.gz.sig CHANGED
Binary file
@@ -35,7 +35,7 @@ module BSON
35
35
  # @note Arrays are encoded as documents, where the index of the value in
36
36
  # the array is the actual key.
37
37
  #
38
- # @return [ String ] The encoded string.
38
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
39
39
  #
40
40
  # @see http://bsonspec.org/#/specification
41
41
  #
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require 'base64'
16
+
15
17
  module BSON
16
18
 
17
19
  # Represents binary data.
@@ -89,7 +91,7 @@ module BSON
89
91
  #
90
92
  # @since 2.0.0
91
93
  def as_json(*args)
92
- { "$binary" => data, "$type" => type }
94
+ { "$binary" => Base64.encode64(data), "$type" => type }
93
95
  end
94
96
 
95
97
  # Instantiate the new binary object.
@@ -124,7 +126,7 @@ module BSON
124
126
  # @example Encode the binary.
125
127
  # binary.to_bson
126
128
  #
127
- # @return [ String ] The encoded binary.
129
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
128
130
  #
129
131
  # @see http://bsonspec.org/#/specification
130
132
  #
@@ -76,7 +76,7 @@ module BSON
76
76
  # @example Encode the code.
77
77
  # code.to_bson
78
78
  #
79
- # @return [ String ] The encoded string.
79
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
80
80
  #
81
81
  # @see http://bsonspec.org/#/specification
82
82
  #
@@ -82,7 +82,7 @@ module BSON
82
82
  # @example Encode the code with scope.
83
83
  # code_with_scope.to_bson
84
84
  #
85
- # @return [ String ] The encoded string.
85
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
86
86
  #
87
87
  # @see http://bsonspec.org/#/specification
88
88
  #
@@ -29,7 +29,7 @@ module BSON
29
29
  # @example Get the date as encoded BSON.
30
30
  # Date.new(2012, 1, 1).to_bson
31
31
  #
32
- # @return [ String ] The encoded string.
32
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
33
33
  #
34
34
  # @see http://bsonspec.org/#/specification
35
35
  #
@@ -29,7 +29,7 @@ module BSON
29
29
  # @example Get the date time as encoded BSON.
30
30
  # DateTime.new(2012, 1, 1, 0, 0, 0).to_bson
31
31
  #
32
- # @return [ String ] The encoded string.
32
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
33
33
  #
34
34
  # @see http://bsonspec.org/#/specification
35
35
  #
@@ -109,7 +109,7 @@ module BSON
109
109
  # @example Get the raw bson bytes in a buffer.
110
110
  # decimal.to_bson
111
111
  #
112
- # @return [ BSON::ByteBuffer ] The raw bytes in a buffer.
112
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
113
113
  #
114
114
  # @see http://bsonspec.org/#/specification
115
115
  #
@@ -44,7 +44,7 @@ module BSON
44
44
  # @example Get the false boolean as encoded BSON.
45
45
  # false.to_bson
46
46
  #
47
- # @return [ String ] The encoded string.
47
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
48
48
  #
49
49
  # @see http://bsonspec.org/#/specification
50
50
  #
@@ -37,7 +37,7 @@ module BSON
37
37
  # @example Get the floating point as encoded BSON.
38
38
  # 1.221311.to_bson
39
39
  #
40
- # @return [ String ] The encoded string.
40
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
41
41
  #
42
42
  # @see http://bsonspec.org/#/specification
43
43
  #
@@ -32,7 +32,7 @@ module BSON
32
32
  # @example Get the hash as encoded BSON.
33
33
  # { "field" => "value" }.to_bson
34
34
  #
35
- # @return [ String ] The encoded string.
35
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
36
36
  #
37
37
  # @see http://bsonspec.org/#/specification
38
38
  #
@@ -98,7 +98,7 @@ module BSON
98
98
  # @example Get the integer as encoded BSON.
99
99
  # 1024.to_bson
100
100
  #
101
- # @return [ String ] The encoded string.
101
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
102
102
  #
103
103
  # @see http://bsonspec.org/#/specification
104
104
  #
@@ -164,7 +164,7 @@ module BSON
164
164
  # where the object was instantiated in a non-standard way. (Like a
165
165
  # Marshal.load)
166
166
  #
167
- # @return [ String ] The raw bytes.
167
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
168
168
  #
169
169
  # @see http://bsonspec.org/#/specification
170
170
  #
@@ -27,7 +27,7 @@ module BSON
27
27
  # @example Get the OpenStruct object as encoded BSON.
28
28
  # OpenStruct.new({ "field" => "value" }).to_bson
29
29
  #
30
- # @return [ String ] The encoded string.
30
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
31
31
  #
32
32
  # @see http://bsonspec.org/#/specification
33
33
  #
@@ -51,6 +51,8 @@ module BSON
51
51
  # Ruby multiline constant.
52
52
  #
53
53
  # @since 3.2.6
54
+ #
55
+ # @deprecated Will be removed in 5.0
54
56
  RUBY_MULTILINE_VALUE = 'ms'.freeze
55
57
 
56
58
  # Get the regexp as JSON hash data.
@@ -79,7 +81,10 @@ module BSON
79
81
  # 's' for dotall mode ('.' matches everything),
80
82
  # and 'u' to make \w, \W, etc. match unicode.
81
83
  #
82
- # @return [ String ] The encoded string.
84
+ # @param [ BSON::ByteBuffer ] buffer The byte buffer to append to.
85
+ # @param [ true, false ] validating_keys
86
+ #
87
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
83
88
  #
84
89
  # @see http://bsonspec.org/#/specification
85
90
  #
@@ -92,7 +97,8 @@ module BSON
92
97
  private
93
98
 
94
99
  def bson_options
95
- bson_ignorecase + bson_multiline + bson_extended
100
+ # Ruby's Regexp always has BSON's equivalent of 'm' on, so always add it
101
+ bson_ignorecase + MULTILINE_VALUE + bson_dotall + bson_extended
96
102
  end
97
103
 
98
104
  def bson_extended
@@ -103,8 +109,9 @@ module BSON
103
109
  (options & ::Regexp::IGNORECASE != 0) ? IGNORECASE_VALUE : NO_VALUE
104
110
  end
105
111
 
106
- def bson_multiline
107
- (options & ::Regexp::MULTILINE != 0) ? RUBY_MULTILINE_VALUE : NO_VALUE
112
+ def bson_dotall
113
+ # Ruby Regexp's MULTILINE is equivalent to BSON's dotall value
114
+ (options & ::Regexp::MULTILINE != 0) ? NEWLINE_VALUE : NO_VALUE
108
115
  end
109
116
 
110
117
  # Represents the raw values for the regular expression.
@@ -113,6 +120,7 @@ module BSON
113
120
  #
114
121
  # @since 3.0.0
115
122
  class Raw
123
+ include JSON
116
124
 
117
125
  # @return [ String ] pattern The regex pattern.
118
126
  attr_reader :pattern
@@ -129,7 +137,7 @@ module BSON
129
137
  #
130
138
  # @since 3.0.0
131
139
  def compile
132
- @compiled ||= ::Regexp.new(pattern, options)
140
+ @compiled ||= ::Regexp.new(pattern, options_to_int)
133
141
  end
134
142
 
135
143
  # Initialize the new raw regular expression.
@@ -138,7 +146,11 @@ module BSON
138
146
  # Raw.new(pattern, options)
139
147
  #
140
148
  # @param [ String ] pattern The regular expression pattern.
141
- # @param [ Integer ] options The options.
149
+ # @param [ String, Integer ] options The options.
150
+ #
151
+ # @note The ability to specify options as an Integer is deprecated.
152
+ # Please specify options as a String. The ability to pass options as
153
+ # as Integer will be removed in version 5.0.0.
142
154
  #
143
155
  # @since 3.0.0
144
156
  def initialize(pattern, options)
@@ -153,15 +165,81 @@ module BSON
153
165
  #
154
166
  # @since 3.1.0
155
167
  def respond_to?(method, include_private = false)
156
- compile.respond_to?(method, include_private = false) || super
168
+ compile.respond_to?(method, include_private) || super
157
169
  end
158
170
 
171
+ # Encode the Raw Regexp object to BSON.
172
+ #
173
+ # @example Get the raw regular expression as encoded BSON.
174
+ # raw_regexp.to_bson
175
+ #
176
+ # @note From the BSON spec: The first cstring is the regex pattern,
177
+ # the second is the regex options string. Options are identified
178
+ # by characters, which must be stored in alphabetical order.
179
+ # Valid options are 'i' for case insensitive matching,
180
+ # 'm' for multiline matching, 'x' for verbose mode,
181
+ # 'l' to make \w, \W, etc. locale dependent,
182
+ # 's' for dotall mode ('.' matches everything),
183
+ # and 'u' to make \w, \W, etc. match unicode.
184
+ #
185
+ # @param [ BSON::ByteBuffer ] buffer The byte buffer to append to.
186
+ # @param [ true, false ] validating_keys
187
+ #
188
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
189
+ #
190
+ # @see http://bsonspec.org/#/specification
191
+ #
192
+ # @since 4.2.0
193
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
194
+ return compile.to_bson(buffer, validating_keys) if options.is_a?(Integer)
195
+ buffer.put_cstring(source)
196
+ buffer.put_cstring(options.chars.sort.join)
197
+ end
198
+
199
+ # Get the raw BSON regexp as JSON hash data.
200
+ #
201
+ # @example Get the raw regexp as a JSON hash.
202
+ # raw_regexp.as_json
203
+ #
204
+ # @return [ Hash ] The raw regexp as a JSON hash.
205
+ #
206
+ # @since 4.2.0
207
+ def as_json(*args)
208
+ { "$regex" => source, "$options" => options }
209
+ end
210
+
211
+ # Check equality of the raw bson regexp against another.
212
+ #
213
+ # @example Check if the raw bson regexp is equal to the other.
214
+ # raw_regexp == other
215
+ #
216
+ # @param [ Object ] other The object to check against.
217
+ #
218
+ # @return [ true, false ] If the objects are equal.
219
+ #
220
+ # @since 4.2.0
221
+ def ==(other)
222
+ return false unless other.is_a?(::Regexp::Raw)
223
+ pattern == other.pattern &&
224
+ options == other.options
225
+ end
226
+ alias :eql? :==
227
+
159
228
  private
160
229
 
161
230
  def method_missing(method, *arguments)
162
231
  return super unless respond_to?(method)
163
232
  compile.send(method, *arguments)
164
233
  end
234
+
235
+ def options_to_int
236
+ return options if options.is_a?(Integer)
237
+ opts = 0
238
+ opts |= ::Regexp::IGNORECASE if options.include?(IGNORECASE_VALUE)
239
+ opts |= ::Regexp::MULTILINE if options.include?(NEWLINE_VALUE)
240
+ opts |= ::Regexp::EXTENDED if options.include?(EXTENDED_VALUE)
241
+ opts
242
+ end
165
243
  end
166
244
 
167
245
  module ClassMethods
@@ -177,17 +255,7 @@ module BSON
177
255
  # @since 2.0.0
178
256
  def from_bson(buffer)
179
257
  pattern = buffer.get_cstring
180
- options = 0
181
- while (option = buffer.get_byte) != NULL_BYTE
182
- case option
183
- when IGNORECASE_VALUE
184
- options |= ::Regexp::IGNORECASE
185
- when MULTILINE_VALUE, NEWLINE_VALUE
186
- options |= ::Regexp::MULTILINE
187
- when EXTENDED_VALUE
188
- options |= ::Regexp::EXTENDED
189
- end
190
- end
258
+ options = buffer.get_cstring
191
259
  Raw.new(pattern, options)
192
260
  end
193
261
  end
@@ -42,7 +42,7 @@ module BSON
42
42
  # @example Encode the min key value.
43
43
  # min_key.to_bson
44
44
  #
45
- # @return [ String ] An empty string.
45
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
46
46
  #
47
47
  # @since 2.0.0
48
48
  def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
@@ -40,7 +40,7 @@ module BSON
40
40
  #
41
41
  # @raise [ EncodingError ] If the string is not UTF-8.
42
42
  #
43
- # @return [ String ] The encoded string.
43
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
44
44
  #
45
45
  # @see http://bsonspec.org/#/specification
46
46
  #
@@ -51,7 +51,7 @@ module BSON
51
51
  # @example Get the symbol as encoded BSON.
52
52
  # :test.to_bson
53
53
  #
54
- # @return [ Symbol ] The encoded symbol.
54
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
55
55
  #
56
56
  # @see http://bsonspec.org/#/specification
57
57
  #
@@ -32,7 +32,7 @@ module BSON
32
32
  # @example Get the time as encoded BSON.
33
33
  # Time.new(2012, 1, 1, 0, 0, 0).to_bson
34
34
  #
35
- # @return [ String ] The encoded string.
35
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
36
36
  #
37
37
  # @see http://bsonspec.org/#/specification
38
38
  #
@@ -82,7 +82,7 @@ module BSON
82
82
  # @example Get the timestamp as BSON.
83
83
  # timestamp.to_bson
84
84
  #
85
- # @return [ String ] The raw BSON bytes.
85
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
86
86
  #
87
87
  # @see http://bsonspec.org/#/specification
88
88
  #
@@ -44,7 +44,7 @@ module BSON
44
44
  # @example Get the true boolean as encoded BSON.
45
45
  # true.to_bson
46
46
  #
47
- # @return [ String ] The encoded string.
47
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
48
48
  #
49
49
  # @see http://bsonspec.org/#/specification
50
50
  #
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "4.2.0.rc1".freeze
16
+ VERSION = "4.2.0".freeze
17
17
  end
@@ -127,7 +127,7 @@ describe Array do
127
127
  end
128
128
 
129
129
  it "serializes the hash" do
130
- expect(obj.to_bson.length).to eq(251)
130
+ expect(obj.to_bson.length).to eq(252)
131
131
  end
132
132
  end
133
133
  end
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
  require "spec_helper"
16
+ require "base64"
16
17
 
17
18
  describe BSON::Binary do
18
19
  let(:testing1) { described_class.new("testing") }
@@ -62,7 +63,7 @@ describe BSON::Binary do
62
63
 
63
64
  it "returns the binary data plus type" do
64
65
  expect(object.as_json).to eq(
65
- { "$binary" => "testing", "$type" => :user }
66
+ { "$binary" => Base64.encode64("testing"), "$type" => :user }
66
67
  )
67
68
  end
68
69
 
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Driver BSON Corpus spec tests' do
4
+
5
+ specs = BSON_CORPUS_TESTS.map { |file| BSON::Corpus::Spec.new(file) }
6
+
7
+ specs.each do |spec|
8
+
9
+ context(spec.description) do
10
+
11
+ spec.valid_tests.each do |test|
12
+
13
+ context("VALID CASE: #{test.description}") do
14
+
15
+ it 'roundtrips the given bson correctly' do
16
+ expect(test.reencoded_bson).to eq(test.correct_bson)
17
+ end
18
+
19
+ context 'when the canonical bson is roundtripped', if: test.test_canonical_bson? do
20
+
21
+ it 'encodes the canonical bson correctly' do
22
+ expect(test.reencoded_canonical_bson).to eq(test.correct_bson)
23
+ end
24
+ end
25
+
26
+ context 'when the document can be represented as extended json', if: test.test_extjson? do
27
+
28
+ it 'decodes from the given bson, then encodes the document as extended json correctly' do
29
+ skip 'The extended json in this test case does not match' unless (test.extjson_from_bson == test.correct_extjson)
30
+ expect(test.extjson_from_bson).to eq(test.correct_extjson)
31
+ expect(test.extjson_from_bson[test.test_key]).to eq(test.correct_extjson[test.test_key])
32
+ end
33
+
34
+ it 'decodes from extended json, then encodes the document as extended json correctly' do
35
+ expect(test.extjson_from_encoded_extjson).to eq(test.correct_extjson)
36
+ expect(test.extjson_from_encoded_extjson[test.test_key]).to eq(test.correct_extjson[test.test_key])
37
+ end
38
+
39
+ context 'when the canonical bson can be represented as extended json', if: (test.test_canonical_bson? && test.test_extjson?) do
40
+
41
+ it 'encodes the canonical bson correctly as extended json' do
42
+ expect(test.extjson_from_canonical_bson).to eq(test.correct_extjson)
43
+ expect(test.extjson_from_canonical_bson[test.test_key]).to eq(test.correct_extjson[test.test_key])
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ spec.invalid_tests.each do |test|
51
+
52
+ context("INVALID CASE: #{test.description}") do
53
+
54
+ let(:error) do
55
+ begin; test.reencoded_bson; false; rescue => e; e; end
56
+ end
57
+
58
+ it 'raises an error' do
59
+ skip 'This test case does not raise and error but should' unless error
60
+ expect {
61
+ test.reencoded_bson
62
+ }.to raise_error
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end