bson 4.15.0-java → 5.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +94 -10
  3. data/Rakefile +68 -39
  4. data/lib/bson/active_support.rb +1 -0
  5. data/lib/bson/array.rb +57 -31
  6. data/lib/bson/big_decimal.rb +16 -6
  7. data/lib/bson/binary.rb +255 -128
  8. data/lib/bson/boolean.rb +1 -0
  9. data/lib/bson/code.rb +9 -11
  10. data/lib/bson/code_with_scope.rb +8 -10
  11. data/lib/bson/config.rb +1 -27
  12. data/lib/bson/date.rb +2 -1
  13. data/lib/bson/date_time.rb +2 -1
  14. data/lib/bson/db_pointer.rb +11 -12
  15. data/lib/bson/dbref.rb +11 -9
  16. data/lib/bson/decimal128/builder.rb +10 -9
  17. data/lib/bson/decimal128.rb +24 -110
  18. data/lib/bson/document.rb +1 -0
  19. data/lib/bson/environment.rb +1 -0
  20. data/lib/bson/error/bson_decode_error.rb +11 -0
  21. data/lib/bson/error/ext_json_parse_error.rb +11 -0
  22. data/lib/bson/error/illegal_key.rb +23 -0
  23. data/lib/bson/error/invalid_binary_type.rb +37 -0
  24. data/lib/bson/error/invalid_dbref_argument.rb +12 -0
  25. data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
  26. data/lib/bson/error/invalid_decimal128_range.rb +27 -0
  27. data/lib/bson/error/invalid_decimal128_string.rb +26 -0
  28. data/lib/bson/error/invalid_key.rb +24 -0
  29. data/lib/bson/error/invalid_object_id.rb +11 -0
  30. data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
  31. data/lib/bson/error/unrepresentable_precision.rb +19 -0
  32. data/lib/bson/error/unserializable_class.rb +13 -0
  33. data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
  34. data/lib/bson/error/unsupported_type.rb +11 -0
  35. data/lib/bson/error.rb +16 -28
  36. data/lib/bson/ext_json.rb +1 -0
  37. data/lib/bson/false_class.rb +2 -1
  38. data/lib/bson/float.rb +2 -1
  39. data/lib/bson/hash.rb +127 -72
  40. data/lib/bson/int32.rb +16 -4
  41. data/lib/bson/int64.rb +16 -4
  42. data/lib/bson/integer.rb +3 -4
  43. data/lib/bson/json.rb +1 -0
  44. data/lib/bson/max_key.rb +7 -9
  45. data/lib/bson/min_key.rb +7 -9
  46. data/lib/bson/nil_class.rb +1 -0
  47. data/lib/bson/object.rb +5 -25
  48. data/lib/bson/object_id.rb +83 -119
  49. data/lib/bson/open_struct.rb +3 -2
  50. data/lib/bson/regexp.rb +35 -64
  51. data/lib/bson/registry.rb +2 -6
  52. data/lib/bson/specialized.rb +2 -1
  53. data/lib/bson/string.rb +4 -27
  54. data/lib/bson/symbol.rb +22 -19
  55. data/lib/bson/time.rb +2 -1
  56. data/lib/bson/time_with_zone.rb +13 -1
  57. data/lib/bson/timestamp.rb +2 -1
  58. data/lib/bson/true_class.rb +2 -1
  59. data/lib/bson/undefined.rb +14 -0
  60. data/lib/bson/version.rb +3 -1
  61. data/lib/bson-ruby.jar +0 -0
  62. data/lib/bson.rb +3 -2
  63. data/spec/bson/array_spec.rb +19 -60
  64. data/spec/bson/big_decimal_spec.rb +16 -4
  65. data/spec/bson/binary_spec.rb +83 -74
  66. data/spec/bson/binary_uuid_spec.rb +1 -0
  67. data/spec/bson/boolean_spec.rb +1 -0
  68. data/spec/bson/byte_buffer_read_spec.rb +1 -0
  69. data/spec/bson/byte_buffer_spec.rb +1 -0
  70. data/spec/bson/byte_buffer_write_spec.rb +1 -0
  71. data/spec/bson/code_spec.rb +5 -3
  72. data/spec/bson/code_with_scope_spec.rb +5 -3
  73. data/spec/bson/config_spec.rb +1 -35
  74. data/spec/bson/date_spec.rb +1 -0
  75. data/spec/bson/date_time_spec.rb +1 -0
  76. data/spec/bson/dbref_legacy_spec.rb +20 -3
  77. data/spec/bson/dbref_spec.rb +9 -9
  78. data/spec/bson/decimal128_spec.rb +40 -20
  79. data/spec/bson/document_as_spec.rb +1 -0
  80. data/spec/bson/document_spec.rb +1 -1
  81. data/spec/bson/ext_json_parse_spec.rb +1 -0
  82. data/spec/bson/false_class_spec.rb +8 -0
  83. data/spec/bson/float_spec.rb +8 -3
  84. data/spec/bson/hash_as_spec.rb +1 -0
  85. data/spec/bson/hash_spec.rb +87 -75
  86. data/spec/bson/int32_spec.rb +21 -6
  87. data/spec/bson/int64_spec.rb +21 -6
  88. data/spec/bson/integer_spec.rb +45 -13
  89. data/spec/bson/json_spec.rb +1 -0
  90. data/spec/bson/max_key_spec.rb +5 -3
  91. data/spec/bson/min_key_spec.rb +5 -3
  92. data/spec/bson/nil_class_spec.rb +1 -0
  93. data/spec/bson/object_id_spec.rb +57 -4
  94. data/spec/bson/object_spec.rb +2 -1
  95. data/spec/bson/open_struct_spec.rb +14 -71
  96. data/spec/bson/raw_spec.rb +9 -15
  97. data/spec/bson/regexp_spec.rb +4 -3
  98. data/spec/bson/registry_spec.rb +2 -1
  99. data/spec/bson/string_spec.rb +13 -38
  100. data/spec/bson/symbol_raw_spec.rb +25 -0
  101. data/spec/bson/symbol_spec.rb +15 -18
  102. data/spec/bson/time_spec.rb +1 -0
  103. data/spec/bson/time_with_zone_spec.rb +1 -0
  104. data/spec/bson/timestamp_spec.rb +1 -0
  105. data/spec/bson/true_class_spec.rb +8 -0
  106. data/spec/bson/undefined_spec.rb +27 -0
  107. data/spec/bson_spec.rb +1 -0
  108. data/spec/runners/common_driver.rb +6 -5
  109. data/spec/runners/corpus.rb +6 -0
  110. data/spec/runners/corpus_legacy.rb +1 -0
  111. data/spec/spec_helper.rb +1 -0
  112. data/spec/spec_tests/common_driver_spec.rb +9 -4
  113. data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
  114. data/spec/spec_tests/corpus_spec.rb +13 -3
  115. data/spec/spec_tests/data/corpus/binary.json +5 -0
  116. data/spec/spec_tests/data/corpus/code.json +13 -13
  117. data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
  118. data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
  119. data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
  120. data/spec/spec_tests/data/corpus/document.json +20 -0
  121. data/spec/spec_tests/data/corpus/symbol.json +7 -7
  122. data/spec/spec_tests/data/corpus/top.json +18 -3
  123. data/spec/support/shared_examples.rb +28 -5
  124. data/spec/support/spec_config.rb +1 -0
  125. data/spec/support/utils.rb +49 -1
  126. metadata +112 -161
  127. checksums.yaml.gz.sig +0 -0
  128. data/spec/shared/LICENSE +0 -20
  129. data/spec/shared/bin/get-mongodb-download-url +0 -17
  130. data/spec/shared/bin/s3-copy +0 -45
  131. data/spec/shared/bin/s3-upload +0 -69
  132. data/spec/shared/lib/mrss/child_process_helper.rb +0 -80
  133. data/spec/shared/lib/mrss/cluster_config.rb +0 -231
  134. data/spec/shared/lib/mrss/constraints.rb +0 -386
  135. data/spec/shared/lib/mrss/docker_runner.rb +0 -271
  136. data/spec/shared/lib/mrss/event_subscriber.rb +0 -200
  137. data/spec/shared/lib/mrss/lite_constraints.rb +0 -191
  138. data/spec/shared/lib/mrss/server_version_registry.rb +0 -120
  139. data/spec/shared/lib/mrss/spec_organizer.rb +0 -179
  140. data/spec/shared/lib/mrss/utils.rb +0 -15
  141. data/spec/shared/share/Dockerfile.erb +0 -338
  142. data/spec/shared/share/haproxy-1.conf +0 -16
  143. data/spec/shared/share/haproxy-2.conf +0 -17
  144. data/spec/shared/shlib/distro.sh +0 -74
  145. data/spec/shared/shlib/server.sh +0 -367
  146. data/spec/shared/shlib/set_env.sh +0 -131
  147. data.tar.gz.sig +0 -1
  148. metadata.gz.sig +0 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc99b74544db3686a580a0f25c2ef2a36e5d4c95dc17924d36285cc1e23b4f70
4
- data.tar.gz: 387a4a3a3af6a56e980e93648d44d2b8196e76cc7f65ba494b8d6613d5580660
3
+ metadata.gz: 8abd0210ffaac5ff6d506ec5a872ad8230f9a4253be7bb26d7b5726ff5f4f0e1
4
+ data.tar.gz: 2abc7512e12d5ebd109038a22e1a71d1174df6e86d82a43bd52dc5f2ae368c4b
5
5
  SHA512:
6
- metadata.gz: e495d9ae63176d4d594d715f983cc1cf01d23761f383f079397ed2b24927c868c2c45a50d1943eb8bfccab4c8bfbd9a839830a8f6473b31a590a6408f531637c
7
- data.tar.gz: f1750558d5e912736870d1d4e82c0ae5ca48eec13521efc8d504cb7483bdc2c2ec20912f47fc7ae30efd3d7e7111f9992e8b4c28e18280dc21b00848ac314979
6
+ metadata.gz: bb55a15f35cb6700df2a3f45f13dae8525714adb796037db59cab549d21ae299c1999321e33c1e74b4f6eb1731e84972fd77e1814392f38e62ab7398ea35b7af
7
+ data.tar.gz: 52af2ff9e32b7f935b3abbfc482044eb5f62b2b4b1e70a85154ea32b7f36913dabbc183831864589d56b4293458d7b9290eeed15b2e535ac5f1301b8ecd2104c
data/README.md CHANGED
@@ -1,34 +1,122 @@
1
1
  BSON
2
2
  [![Gem Version][rubygems-img]][rubygems-url]
3
3
  [![Build Status][ghactions-img]][ghactions-url]
4
- [![Coverage Status][coveralls-img]][coveralls-url]
5
- [![Inline docs][inch-img]][inch-url]
6
4
  ====
7
5
 
8
6
  An implementation of the BSON specification in Ruby.
9
7
 
8
+ Installation
9
+ ------------
10
+
11
+ BSON can be installed via RubyGems:
12
+
13
+ ```
14
+ > gem install bson
15
+ ```
16
+
17
+ Or by adding it to your project's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'bson'
21
+ ```
22
+
23
+ ### Release Integrity
24
+
25
+ Each release of the BSON library for Ruby after version 5.0.0 has been automatically built and signed using the team's GPG key.
26
+
27
+ To verify the bson gem file:
28
+
29
+ 1. [Download the GPG key](https://pgp.mongodb.com/ruby-driver.asc).
30
+ 2. Import the key into your GPG keyring with `gpg --import ruby-driver.asc`.
31
+ 3. Download the gem file (if you don't already have it). You can download it from RubyGems with `gem fetch bson`, or you can download it from the [releases page](https://github.com/mongodb/bson-ruby/releases) on GitHub.
32
+ 4. Download the corresponding detached signature file from the [same release](https://github.com/mongodb/bson-ruby/releases). Look at the bottom of the release that corresponds to the gem file, under the 'Assets' list, for a `.sig` file with the same version number as the gem you wish to install.
33
+ 5. Verify the gem with `gpg --verify bson-X.Y.Z.gem.sig bson-X.Y.Z.gem` (replacing `X.Y.Z` with the actual version number).
34
+
35
+ You are looking for text like "Good signature from "MongoDB Ruby Driver Release Signing Key <packaging@mongodb.com>" in the output. If you see that, the signature was found to correspond to the given gem file.
36
+
37
+ (Note that other output, like "This key is not certified with a trusted signature!", is related to *web of trust* and depends on how strongly you, personally, trust the `ruby-driver.asc` key that you downloaded from us. To learn more, see https://www.gnupg.org/gph/en/manual/x334.html)
38
+
39
+ ### Why not use RubyGems' gem-signing functionality?
40
+
41
+ RubyGems' own gem signing is problematic, most significantly because there is no established chain of trust related to the keys used to sign gems. RubyGems' own documentation admits that "this method of signing gems is not widely used" (see https://guides.rubygems.org/security/). Discussions about this in the RubyGems community have been off-and-on for more than a decade, and while a solution will eventually arrive, we have settled on using GPG instead for the following reasons:
42
+
43
+ 1. Many of the other driver teams at MongoDB are using GPG to sign their product releases. Consistency with the other teams means that we can reuse existing tooling for our own product releases.
44
+ 2. GPG is widely available and has existing tools and procedures for dealing with web of trust (though they are admittedly quite arcane and intimidating to the uninitiated, unfortunately).
45
+
46
+ Ultimately, most users do not bother to verify gems, and will not be impacted by our choice of GPG over RubyGems' native method.
47
+
10
48
  Compatibility
11
49
  -------------
12
50
 
13
- BSON is tested against MRI (2.5) and JRuby (9.2+).
51
+ BSON is tested against MRI (2.7+) and JRuby (9.3+).
14
52
 
15
53
  Documentation
16
54
  -------------
17
55
 
18
56
  Current documentation can be found
19
- [here](http://docs.mongodb.org/ecosystem/tutorial/ruby-bson-tutorial/#ruby-bson-tutorial).
57
+ [here](https://www.mongodb.com/docs/ruby-driver/current/bson-tutorials/).
20
58
 
21
59
  API Documentation
22
60
  -----------------
23
61
 
24
- The [API Documentation](https://api.mongodb.com/bson-ruby/current/) is
25
- located at api.mongodb.com.
62
+ The [API Documentation](https://www.mongodb.com/docs/ruby-driver/master/api/) is
63
+ located at mongodb.com/docs.
26
64
 
27
65
  BSON Specification
28
66
  ------------------
29
67
 
30
68
  The [BSON specification](http://bsonspec.org) is at bsonspec.org.
31
69
 
70
+ ## Bugs & Feature Requests
71
+
72
+ To report a bug in the `bson` gem or request a feature:
73
+
74
+ 1. Visit [our issue tracker](https://jira.mongodb.org/) and login
75
+ (or create an account if you do not have one already).
76
+ 2. Navigate to the [RUBY project](https://jira.mongodb.org/browse/RUBY).
77
+ 3. Click 'Create Issue' and fill out all of the applicable form fields, making
78
+ sure to select `BSON` in the _Component/s_ field.
79
+
80
+ When creating an issue, please keep in mind that all information in JIRA
81
+ for the RUBY project, as well as the core server (the SERVER project),
82
+ is publicly visible.
83
+
84
+ **PLEASE DO:**
85
+
86
+ - Provide as much information as possible about the issue.
87
+ - Provide detailed steps for reproducing the issue.
88
+ - Provide any applicable code snippets, stack traces and log data.
89
+ - Specify version numbers of the `bson` gem and/or Ruby driver and MongoDB
90
+ server.
91
+
92
+ **PLEASE DO NOT:**
93
+
94
+ - Provide any sensitive data or server logs.
95
+ - Report potential security issues publicly (see 'Security Issues' below).
96
+
97
+ ## Security Issues
98
+
99
+ If you have identified a potential security-related issue in the `bson` gem
100
+ (or any other MongoDB product), please report it by following the
101
+ [instructions here](https://www.mongodb.com/docs/manual/tutorial/create-a-vulnerability-report).
102
+
103
+ ## Product Feature Requests
104
+
105
+ To request a feature which is not specific to the `bson` gem, or which
106
+ affects more than the `bson` gem and/or Ruby driver alone (for example, a
107
+ feature which requires MongoDB server support), please submit your idea through
108
+ the [MongoDB Feedback Forum](https://feedback.mongodb.com/forums/924286-drivers).
109
+
110
+ ## Maintenance and Bug Fix Policy
111
+
112
+ New library functionality is generally added in a backwards-compatible manner
113
+ and results in new minor releases. Bug fixes are generally made on
114
+ master first and are backported to the current minor library release. Exceptions
115
+ may be made on a case-by-case basis, for example security fixes may be
116
+ backported to older stable branches. Only the most recent minor release
117
+ is officially supported. Customers should use the most recent release in
118
+ their applications.
119
+
32
120
  Versioning
33
121
  ----------
34
122
 
@@ -56,7 +144,3 @@ limitations under the License.
56
144
  [rubygems-url]: http://badge.fury.io/rb/bson
57
145
  [ghactions-img]: https://github.com/mongodb/bson-ruby/actions/workflows/bson-ruby.yml/badge.svg?query=branch%3Amaster
58
146
  [ghactions-url]: https://github.com/mongodb/bson-ruby/actions/workflows/bson-ruby.yml?query=branch%3Amaster
59
- [coveralls-img]: https://coveralls.io/repos/mongodb/bson-ruby/badge.svg?branch=master
60
- [coveralls-url]: https://coveralls.io/r/mongodb/bson-ruby?branch=master
61
- [inch-img]: http://inch-ci.org/github/mongodb/bson-ruby.svg?branch=master
62
- [inch-url]: http://inch-ci.org/github/mongodb/bson-ruby
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
1
4
  # Copyright (C) 2009-2013 MongoDB Inc.
2
5
  #
3
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,6 +35,8 @@ if jruby?
32
35
  ext.name = "bson-ruby"
33
36
  ext.ext_dir = "src"
34
37
  ext.lib_dir = "lib"
38
+ ext.target_version = ENV['TARGET_VERSION'] if ENV['TARGET_VERSION']
39
+ ext.source_version = ENV['SOURCE_VERSION'] if ENV['SOURCE_VERSION']
35
40
  end
36
41
  else
37
42
  require "rake/extensiontask"
@@ -42,28 +47,37 @@ else
42
47
  end
43
48
  end
44
49
 
45
- require "bson/version"
50
+ RSpec::Core::RakeTask.new(:rspec)
46
51
 
47
- def extension
48
- RUBY_PLATFORM =~ /darwin/ ? "bundle" : "so"
52
+ desc 'Build the bson gem'
53
+ task :build => [ :clean_all, *(jruby? ? :compile : nil) ] do
54
+ output = "--output=#{ENV['GEM_FILE_NAME']}" if ENV['GEM_FILE_NAME']
55
+ system "gem build #{output} bson.gemspec"
49
56
  end
50
57
 
51
- require_relative "perf/bench"
52
-
53
- RSpec::Core::RakeTask.new(:rspec)
58
+ # `rake version` is used by the deployment system so get the release version
59
+ # of the product beng deployed. It must do nothing more than just print the
60
+ # product version number.
61
+ desc 'Print the current version of the Ruby-BSON library'
62
+ task :version do
63
+ require 'bson/version'
64
+ puts BSON::VERSION
65
+ end
54
66
 
55
- if jruby?
56
- task :build => [ :clean_all, :compile ] do
57
- system "gem build bson.gemspec"
58
- end
59
- else
60
- task :build => :clean_all do
61
- system "gem build bson.gemspec"
62
- end
67
+ # `rake gem_file_name` is used by the deployment system so get the name of
68
+ # the gem file to be generated. It must do nothing more than just print the
69
+ # name of the gem file to generate.
70
+ desc 'Print the name of the gem file to generate.'
71
+ task :gem_file_name do
72
+ require 'bson/version'
73
+ base = "bson-#{BSON::VERSION}"
74
+ base << '-java' if jruby?
75
+ puts "#{base}.gem"
63
76
  end
64
77
 
65
78
  task :clean_all => :clean do
66
- FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson_native.#{extension}"))
79
+ FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson_native.bundle"))
80
+ FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson_native.so"))
67
81
  FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson_native.o"))
68
82
  FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson-ruby.jar"))
69
83
  end
@@ -72,49 +86,63 @@ task :spec => :compile do
72
86
  Rake::Task["rspec"].invoke
73
87
  end
74
88
 
75
- # Run bundle exec rake release with mri and jruby. Ex:
76
- #
77
- # rvm use 2.1.0@bson
78
- # bundle exec rake release
79
- # rvm use jruby@bson
80
- # bundle exec rake release
81
- task :release => :build do
82
- system "git tag -a v#{BSON::VERSION} -m 'Tagging release: #{BSON::VERSION}'"
83
- system "git push --tags"
84
- if jruby?
85
- system "gem push bson-#{BSON::VERSION}-java.gem"
86
- system "rm bson-#{BSON::VERSION}-java.gem"
87
- else
88
- system "gem push bson-#{BSON::VERSION}.gem"
89
- system "rm bson-#{BSON::VERSION}.gem"
89
+ # overrides the default Bundler-provided `release` task, which also
90
+ # builds the gem. Our release process assumes the gem has already
91
+ # been built (and signed via GPG), so we just need `rake release` to
92
+ # push the gem to rubygems.
93
+ task :release do
94
+ require 'bson/version'
95
+
96
+ # confirm: there ought to be two gems, one for MRI, and one for Java. These
97
+ # will have been previously generated by the 'BSON Release' GitHub action.
98
+ gems = Dir['*.gem']
99
+ if gems.length != 2
100
+ abort "Expected two gem files to be ready to release; got #{gems.length}"
101
+ end
102
+
103
+ if ENV['GITHUB_ACTION'].nil?
104
+ abort <<~WARNING
105
+ `rake release` must be invoked from the `BSON Release` GitHub action,
106
+ and must not be invoked locally. This ensures the gem is properly signed
107
+ and distributed by the appropriate user.
108
+
109
+ Note that it is the `rubygems/release-gem@v1` step in the `BSON Release`
110
+ action that invokes this task. Do not rename or remove this task, or the
111
+ release-gem step will fail. Reimplement this task with caution.
112
+
113
+ NO GEMS were pushed to RubyGems.
114
+ WARNING
115
+ end
116
+
117
+ gems.each do |gem|
118
+ system 'gem', 'push', gem
90
119
  end
91
120
  end
92
121
 
93
122
  namespace :benchmark do
94
123
 
95
- task :ruby => :clean_all do
124
+ task :prep do
125
+ require_relative "perf/bench"
126
+ end
127
+
128
+ task ruby: [ :clean_all, 'benchmark:prep' ] do
96
129
  puts "Benchmarking pure Ruby..."
97
- require "bson"
98
130
  benchmark!
99
131
  end
100
132
 
101
- task :native => :compile do
133
+ task native: [ :compile, 'benchmark:prep' ] do
102
134
  puts "Benchmarking with native extensions..."
103
- require "bson"
104
135
  benchmark!
105
136
  end
106
137
 
107
138
  namespace :decimal128 do
108
-
109
- task :from_string do
139
+ task from_string: 'benchmark:prep' do
110
140
  puts "Benchmarking creating Decimal128 objects from a string"
111
- require 'bson'
112
141
  benchmark_decimal128_from_string!
113
142
  end
114
143
 
115
- task :to_string do
144
+ task to_string: 'benchmark:prep' do
116
145
  puts "Benchmarking getting a string representation of a Decimal128"
117
- require 'bson'
118
146
  benchmark_decimal128_to_string!
119
147
  end
120
148
  end
@@ -128,6 +156,7 @@ task :docs => 'docs:yard'
128
156
  namespace :docs do
129
157
  desc "Generate yard documention"
130
158
  task :yard do
159
+ require 'bson/version'
131
160
  out = File.join('yard-docs', BSON::VERSION)
132
161
  FileUtils.rm_rf(out)
133
162
  system "yardoc -o #{out} --title bson-#{BSON::VERSION}"
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2018-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/bson/array.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # Copyright (C) 2009-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +14,8 @@
13
14
  # See the License for the specific language governing permissions and
14
15
  # limitations under the License.
15
16
 
17
+ # The top-level BSON module.
16
18
  module BSON
17
-
18
19
  # Injects behaviour for encoding and decoding arrays to
19
20
  # and from raw bytes as specified by the BSON spec.
20
21
  #
@@ -22,7 +23,6 @@ module BSON
22
23
  #
23
24
  # @since 2.0.0
24
25
  module Array
25
-
26
26
  # An array is type 0x04 in the BSON spec.
27
27
  #
28
28
  # @since 2.0.0
@@ -41,19 +41,21 @@ module BSON
41
41
  # @see http://bsonspec.org/#/specification
42
42
  #
43
43
  # @since 2.0.0
44
- def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
44
+ def to_bson(buffer = ByteBuffer.new)
45
45
  if buffer.respond_to?(:put_array)
46
- buffer.put_array(self, validating_keys)
46
+ buffer.put_array(self)
47
47
  else
48
48
  position = buffer.length
49
49
  buffer.put_int32(0)
50
50
  each_with_index do |value, index|
51
51
  unless value.respond_to?(:bson_type)
52
- raise Error::UnserializableClass, "Array element at position #{index} does not define its BSON serialized type: #{value}"
52
+ raise Error::UnserializableClass,
53
+ "Array element at position #{index} does not define its BSON serialized type: #{value}"
53
54
  end
55
+
54
56
  buffer.put_byte(value.bson_type)
55
57
  buffer.put_cstring(index.to_s)
56
- value.to_bson(buffer, validating_keys)
58
+ value.to_bson(buffer)
57
59
  end
58
60
  buffer.put_byte(NULL_BYTE)
59
61
  buffer.replace_int32(position, buffer.length - position)
@@ -68,13 +70,13 @@ module BSON
68
70
  #
69
71
  # @note This is used for repairing legacy bson data.
70
72
  #
71
- # @raise [ BSON::ObjectId::Invalid ] If the array is not 12 elements.
73
+ # @raise [ BSON::Error::InvalidObjectId ] If the array is not 12 elements.
72
74
  #
73
75
  # @return [ String ] The raw object id bytes.
74
76
  #
75
77
  # @since 2.0.0
76
78
  def to_bson_object_id
77
- ObjectId.repair(self) { pack("C*") }
79
+ ObjectId.repair(self) { pack('C*') }
78
80
  end
79
81
 
80
82
  # Converts the array to a normalized value in a BSON document.
@@ -86,7 +88,7 @@ module BSON
86
88
  #
87
89
  # @since 3.0.0
88
90
  def to_bson_normalized_value
89
- map { |value| value.to_bson_normalized_value }
91
+ map(&:to_bson_normalized_value)
90
92
  end
91
93
 
92
94
  # Converts this object to a representation directly serializable to
@@ -105,10 +107,15 @@ module BSON
105
107
  end
106
108
  end
107
109
 
110
+ # Class-level methods to be added to the Array class.
108
111
  module ClassMethods
109
-
110
112
  # Deserialize the array from BSON.
111
113
  #
114
+ # @note If the argument cannot be parsed, an exception will be raised
115
+ # and the argument will be left in an undefined state. The caller
116
+ # must explicitly call `rewind` on the buffer before trying to parse
117
+ # it again.
118
+ #
112
119
  # @param [ ByteBuffer ] buffer The byte buffer.
113
120
  #
114
121
  # @option options [ nil | :bson ] :mode Decoding mode to use.
@@ -116,43 +123,62 @@ module BSON
116
123
  # @return [ Array ] The decoded array.
117
124
  #
118
125
  # @see http://bsonspec.org/#/specification
119
- #
120
- # @since 2.0.0
121
126
  def from_bson(buffer, **options)
122
127
  if buffer.respond_to?(:get_array)
123
128
  buffer.get_array(**options)
124
129
  else
125
- array = new
130
+ parse_array_from_buffer(buffer, **options)
131
+ end
132
+ end
133
+
134
+ private
135
+
136
+ # Parse an array from the buffer.
137
+ #
138
+ # @param [ ByteBuf ] buffer the buffer to read from
139
+ # @param [ Hash ] options the optional keyword arguments
140
+ #
141
+ # @return [ Array ] the array that was parsed
142
+ #
143
+ # @raise [ BSON::Error::BSONDecodeError ] if the expected number of
144
+ # bytes were not read from the buffer
145
+ def parse_array_from_buffer(buffer, **options)
146
+ new.tap do |array|
126
147
  start_position = buffer.read_position
127
148
  expected_byte_size = buffer.get_int32
128
- while (type = buffer.get_byte) != NULL_BYTE
129
- buffer.get_cstring
130
- cls = BSON::Registry.get(type)
131
- value = if options.empty?
132
- cls.from_bson(buffer)
133
- else
134
- cls.from_bson(buffer, **options)
135
- end
136
- array << value
137
- end
149
+ parse_array_elements_from_buffer(array, buffer, **options)
138
150
  actual_byte_size = buffer.read_position - start_position
139
151
  if actual_byte_size != expected_byte_size
140
- raise Error::BSONDecodeError, "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
152
+ raise Error::BSONDecodeError,
153
+ "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
141
154
  end
142
- array
155
+ end
156
+ end
157
+
158
+ # Parse a sequence of array elements from the buffer.
159
+ #
160
+ # @param [ Array ] array the array to populate
161
+ # @param [ ByteBuf ] buffer the buffer to read from
162
+ # @param [ Hash ] options the optional keyword arguments
163
+ def parse_array_elements_from_buffer(array, buffer, **options)
164
+ while (type = buffer.get_byte) != NULL_BYTE
165
+ buffer.get_cstring
166
+ cls = BSON::Registry.get(type)
167
+ value = if options.empty?
168
+ cls.from_bson(buffer)
169
+ else
170
+ cls.from_bson(buffer, **options)
171
+ end
172
+ array << value
143
173
  end
144
174
  end
145
175
  end
146
176
 
147
177
  # Register this type when the module is loaded.
148
- #
149
- # @since 2.0.0
150
178
  Registry.register(BSON_TYPE, ::Array)
151
179
  end
152
180
 
153
181
  # Enrich the core Array class with this module.
154
- #
155
- # @since 2.0.0
156
- ::Array.send(:include, Array)
157
- ::Array.send(:extend, Array::ClassMethods)
182
+ ::Array.include Array
183
+ ::Array.extend Array::ClassMethods
158
184
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop:todo all
2
3
  # Copyright (C) 2009-2021 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,7 +22,7 @@ module BSON
21
22
  # @see http://bsonspec.org/#/specification
22
23
  module BigDecimal
23
24
 
24
- # BigDecimals are serialized as Decimal128s under the hood. A Decimal128
25
+ # BigDecimals are serialized as Decimal128s under the hood. A Decimal128
25
26
  # is type 0x13 in the BSON spec.
26
27
  BSON_TYPE = ::String.new(19.chr, encoding: BINARY).freeze
27
28
 
@@ -33,8 +34,8 @@ module BSON
33
34
  # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
34
35
  #
35
36
  # @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)
37
+ def to_bson(buffer = ByteBuffer.new)
38
+ BSON::Decimal128.new(to_s).to_bson(buffer)
38
39
  end
39
40
 
40
41
  # Get the BSON type for BigDecimal. This is the same BSON type as
@@ -45,7 +46,8 @@ module BSON
45
46
 
46
47
  module ClassMethods
47
48
 
48
- # Deserialize the BigDecimal from raw BSON bytes.
49
+ # Deserialize the BigDecimal from raw BSON bytes. If the :mode option
50
+ # is set to BSON, this will return a BSON::Decimal128
49
51
  #
50
52
  # @example Get the BigDecimal from BSON.
51
53
  # BigDecimal.from_bson(bson)
@@ -54,11 +56,19 @@ module BSON
54
56
  #
55
57
  # @option options [ nil | :bson ] :mode Decoding mode to use.
56
58
  #
57
- # @return [ BigDecimal ] The decimal object.
59
+ # @return [ BigDecimal | BSON::Decimal128 ] The decimal object.
58
60
  def from_bson(buffer, **options)
59
- Decimal128.from_bson(buffer, **options).to_big_decimal
61
+ dec128 = Decimal128.from_bson(buffer, **options)
62
+ if options[:mode] == :bson
63
+ dec128
64
+ else
65
+ dec128.to_d
66
+ end
60
67
  end
61
68
  end
69
+
70
+ # Register this type when the module is loaded.
71
+ Registry.register(BSON_TYPE, ::BigDecimal)
62
72
  end
63
73
 
64
74
  # Enrich the core BigDecimal class with this module.