bson 4.15.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +94 -10
- data/Rakefile +68 -39
- data/ext/bson/bson-native.h +12 -4
- data/ext/bson/extconf.rb +8 -3
- data/ext/bson/init.c +11 -11
- data/ext/bson/read.c +39 -9
- data/ext/bson/util.c +171 -16
- data/ext/bson/write.c +34 -39
- data/lib/bson/active_support.rb +1 -0
- data/lib/bson/array.rb +58 -32
- data/lib/bson/big_decimal.rb +16 -6
- data/lib/bson/binary.rb +271 -129
- data/lib/bson/boolean.rb +1 -0
- data/lib/bson/code.rb +10 -12
- data/lib/bson/code_with_scope.rb +9 -11
- data/lib/bson/config.rb +1 -27
- data/lib/bson/date.rb +2 -1
- data/lib/bson/date_time.rb +2 -1
- data/lib/bson/db_pointer.rb +12 -13
- data/lib/bson/dbref.rb +11 -9
- data/lib/bson/decimal128/builder.rb +10 -9
- data/lib/bson/decimal128.rb +25 -111
- data/lib/bson/document.rb +1 -0
- data/lib/bson/environment.rb +1 -0
- data/lib/bson/error/bson_decode_error.rb +11 -0
- data/lib/bson/error/ext_json_parse_error.rb +11 -0
- data/lib/bson/error/illegal_key.rb +23 -0
- data/lib/bson/error/invalid_binary_type.rb +37 -0
- data/lib/bson/error/invalid_dbref_argument.rb +12 -0
- data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
- data/lib/bson/error/invalid_decimal128_range.rb +27 -0
- data/lib/bson/error/invalid_decimal128_string.rb +26 -0
- data/lib/bson/error/invalid_key.rb +24 -0
- data/lib/bson/error/invalid_object_id.rb +11 -0
- data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
- data/lib/bson/error/unrepresentable_precision.rb +19 -0
- data/lib/bson/error/unserializable_class.rb +13 -0
- data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
- data/lib/bson/error/unsupported_type.rb +11 -0
- data/lib/bson/error.rb +16 -28
- data/lib/bson/ext_json.rb +2 -1
- data/lib/bson/false_class.rb +2 -1
- data/lib/bson/float.rb +3 -2
- data/lib/bson/hash.rb +128 -73
- data/lib/bson/int32.rb +17 -5
- data/lib/bson/int64.rb +17 -5
- data/lib/bson/integer.rb +4 -5
- data/lib/bson/json.rb +1 -0
- data/lib/bson/max_key.rb +8 -10
- data/lib/bson/min_key.rb +8 -10
- data/lib/bson/nil_class.rb +1 -0
- data/lib/bson/object.rb +7 -27
- data/lib/bson/object_id.rb +84 -120
- data/lib/bson/open_struct.rb +3 -2
- data/lib/bson/regexp.rb +36 -65
- data/lib/bson/registry.rb +2 -6
- data/lib/bson/specialized.rb +2 -1
- data/lib/bson/string.rb +4 -27
- data/lib/bson/symbol.rb +23 -20
- data/lib/bson/time.rb +3 -2
- data/lib/bson/time_with_zone.rb +13 -1
- data/lib/bson/timestamp.rb +3 -2
- data/lib/bson/true_class.rb +2 -1
- data/lib/bson/undefined.rb +15 -1
- data/lib/bson/version.rb +3 -1
- data/lib/bson.rb +3 -2
- data/spec/bson/array_spec.rb +19 -60
- data/spec/bson/big_decimal_spec.rb +16 -4
- data/spec/bson/binary_spec.rb +129 -81
- data/spec/bson/binary_uuid_spec.rb +1 -0
- data/spec/bson/boolean_spec.rb +1 -0
- data/spec/bson/byte_buffer_read_spec.rb +1 -0
- data/spec/bson/byte_buffer_spec.rb +1 -0
- data/spec/bson/byte_buffer_write_spec.rb +1 -0
- data/spec/bson/code_spec.rb +5 -3
- data/spec/bson/code_with_scope_spec.rb +5 -3
- data/spec/bson/config_spec.rb +1 -35
- data/spec/bson/date_spec.rb +1 -0
- data/spec/bson/date_time_spec.rb +1 -0
- data/spec/bson/dbref_legacy_spec.rb +20 -3
- data/spec/bson/dbref_spec.rb +9 -9
- data/spec/bson/decimal128_spec.rb +40 -20
- data/spec/bson/document_as_spec.rb +1 -0
- data/spec/bson/document_spec.rb +1 -1
- data/spec/bson/ext_json_parse_spec.rb +1 -0
- data/spec/bson/false_class_spec.rb +8 -0
- data/spec/bson/float_spec.rb +8 -3
- data/spec/bson/hash_as_spec.rb +1 -0
- data/spec/bson/hash_spec.rb +87 -75
- data/spec/bson/int32_spec.rb +21 -6
- data/spec/bson/int64_spec.rb +21 -6
- data/spec/bson/integer_spec.rb +45 -13
- data/spec/bson/json_spec.rb +1 -0
- data/spec/bson/max_key_spec.rb +5 -3
- data/spec/bson/min_key_spec.rb +5 -3
- data/spec/bson/nil_class_spec.rb +1 -0
- data/spec/bson/object_id_spec.rb +57 -4
- data/spec/bson/object_spec.rb +2 -1
- data/spec/bson/open_struct_spec.rb +14 -71
- data/spec/bson/raw_spec.rb +9 -15
- data/spec/bson/regexp_spec.rb +4 -3
- data/spec/bson/registry_spec.rb +2 -1
- data/spec/bson/string_spec.rb +13 -38
- data/spec/bson/symbol_raw_spec.rb +25 -0
- data/spec/bson/symbol_spec.rb +15 -18
- data/spec/bson/time_spec.rb +1 -0
- data/spec/bson/time_with_zone_spec.rb +1 -0
- data/spec/bson/timestamp_spec.rb +1 -0
- data/spec/bson/true_class_spec.rb +8 -0
- data/spec/bson/undefined_spec.rb +27 -0
- data/spec/bson_spec.rb +1 -0
- data/spec/runners/common_driver.rb +6 -5
- data/spec/runners/corpus.rb +6 -0
- data/spec/runners/corpus_legacy.rb +1 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/spec_tests/common_driver_spec.rb +9 -4
- data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
- data/spec/spec_tests/corpus_spec.rb +13 -3
- data/spec/spec_tests/data/corpus/binary.json +5 -0
- data/spec/spec_tests/data/corpus/code.json +13 -13
- data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
- data/spec/spec_tests/data/corpus/document.json +20 -0
- data/spec/spec_tests/data/corpus/symbol.json +7 -7
- data/spec/spec_tests/data/corpus/top.json +18 -3
- data/spec/support/shared_examples.rb +28 -5
- data/spec/support/spec_config.rb +1 -0
- data/spec/support/utils.rb +49 -1
- metadata +114 -164
- checksums.yaml.gz.sig +0 -0
- data/spec/shared/LICENSE +0 -20
- data/spec/shared/bin/get-mongodb-download-url +0 -17
- data/spec/shared/bin/s3-copy +0 -45
- data/spec/shared/bin/s3-upload +0 -69
- data/spec/shared/lib/mrss/child_process_helper.rb +0 -80
- data/spec/shared/lib/mrss/cluster_config.rb +0 -231
- data/spec/shared/lib/mrss/constraints.rb +0 -386
- data/spec/shared/lib/mrss/docker_runner.rb +0 -271
- data/spec/shared/lib/mrss/event_subscriber.rb +0 -200
- data/spec/shared/lib/mrss/lite_constraints.rb +0 -191
- data/spec/shared/lib/mrss/server_version_registry.rb +0 -120
- data/spec/shared/lib/mrss/spec_organizer.rb +0 -179
- data/spec/shared/lib/mrss/utils.rb +0 -15
- data/spec/shared/share/Dockerfile.erb +0 -338
- data/spec/shared/share/haproxy-1.conf +0 -16
- data/spec/shared/share/haproxy-2.conf +0 -17
- data/spec/shared/shlib/distro.sh +0 -74
- data/spec/shared/shlib/server.sh +0 -367
- data/spec/shared/shlib/set_env.sh +0 -131
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -1
data/lib/bson/code_with_scope.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -52,21 +53,18 @@ module BSON
|
|
52
53
|
javascript == other.javascript && scope == other.scope
|
53
54
|
end
|
54
55
|
|
55
|
-
#
|
56
|
+
# Return a representation of the object for use in
|
57
|
+
# application-level JSON serialization. Since BSON::CodeWithScope
|
58
|
+
# is used exclusively in BSON-related contexts, this
|
59
|
+
# method returns the canonical Extended JSON representation.
|
56
60
|
#
|
57
|
-
# @
|
58
|
-
|
59
|
-
#
|
60
|
-
# @return [ Hash ] The code with scope as a JSON hash.
|
61
|
-
#
|
62
|
-
# @since 2.0.0
|
63
|
-
# @deprecated Use as_extended_json instead.
|
64
|
-
def as_json(*args)
|
61
|
+
# @return [ Hash ] The extended json representation.
|
62
|
+
def as_json(*_args)
|
65
63
|
as_extended_json
|
66
64
|
end
|
67
65
|
|
68
66
|
# Converts this object to a representation directly serializable to
|
69
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
67
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
70
68
|
#
|
71
69
|
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
72
70
|
# (default is canonical extended JSON)
|
@@ -100,7 +98,7 @@ module BSON
|
|
100
98
|
# @see http://bsonspec.org/#/specification
|
101
99
|
#
|
102
100
|
# @since 2.0.0
|
103
|
-
def to_bson(buffer = ByteBuffer.new
|
101
|
+
def to_bson(buffer = ByteBuffer.new)
|
104
102
|
position = buffer.length
|
105
103
|
buffer.put_int32(0)
|
106
104
|
buffer.put_string(javascript)
|
data/lib/bson/config.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2016-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -21,32 +22,5 @@ module BSON
|
|
21
22
|
module Config
|
22
23
|
extend self
|
23
24
|
|
24
|
-
# Set the configuration option for BSON to validate keys or not.
|
25
|
-
#
|
26
|
-
# @example Set the config option.
|
27
|
-
# BSON::Config.validating_keys = true
|
28
|
-
#
|
29
|
-
# @param [ true, false ] value The value to set.
|
30
|
-
#
|
31
|
-
# @return [ true, false ] The value.
|
32
|
-
#
|
33
|
-
# @since 4.1.0
|
34
|
-
def validating_keys=(value)
|
35
|
-
@validating_keys = value
|
36
|
-
end
|
37
|
-
|
38
|
-
# Returns true if BSON will validate the document keys on serialization to
|
39
|
-
# determine if they contain invalid MongoDB values. Invalid keys start with
|
40
|
-
# '$' or contain a '.' in them.
|
41
|
-
#
|
42
|
-
# @example Is BSON validating keys?
|
43
|
-
# BSON::Config.validating_keys?
|
44
|
-
#
|
45
|
-
# @return [ true, false ] If BSON is validating keys?
|
46
|
-
#
|
47
|
-
# @since 4.1.0
|
48
|
-
def validating_keys?
|
49
|
-
!!(@validating_keys||=nil)
|
50
|
-
end
|
51
25
|
end
|
52
26
|
end
|
data/lib/bson/date.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -45,7 +46,7 @@ module BSON
|
|
45
46
|
# @see http://bsonspec.org/#/specification
|
46
47
|
#
|
47
48
|
# @since 2.1.0
|
48
|
-
def to_bson(buffer = ByteBuffer.new
|
49
|
+
def to_bson(buffer = ByteBuffer.new)
|
49
50
|
buffer.put_int64((jd - DATE_REFERENCE) * MILLISECONDS_IN_DAY)
|
50
51
|
end
|
51
52
|
|
data/lib/bson/date_time.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -35,7 +36,7 @@ module BSON
|
|
35
36
|
# @see http://bsonspec.org/#/specification
|
36
37
|
#
|
37
38
|
# @since 2.1.0
|
38
|
-
def to_bson(buffer = ByteBuffer.new
|
39
|
+
def to_bson(buffer = ByteBuffer.new)
|
39
40
|
gregorian.to_time.to_bson(buffer)
|
40
41
|
end
|
41
42
|
end
|
data/lib/bson/db_pointer.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -54,24 +55,22 @@ module BSON
|
|
54
55
|
ref == other.ref && id == other.id
|
55
56
|
end
|
56
57
|
|
57
|
-
#
|
58
|
+
# Return a representation of the object for use in
|
59
|
+
# application-level JSON serialization. Since BSON::DbPointer
|
60
|
+
# is used exclusively in BSON-related contexts, this
|
61
|
+
# method returns the canonical Extended JSON representation.
|
58
62
|
#
|
59
|
-
# @return [ Hash ] The
|
60
|
-
|
61
|
-
# @deprecated Use as_extended_json instead.
|
62
|
-
def as_json(*args)
|
63
|
+
# @return [ Hash ] The extended json representation.
|
64
|
+
def as_json(*_args)
|
63
65
|
as_extended_json
|
64
66
|
end
|
65
67
|
|
66
68
|
# Converts this object to a representation directly serializable to
|
67
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
68
|
-
#
|
69
|
-
# @option options [ true | false ] :relaxed Whether to produce relaxed
|
70
|
-
# extended JSON representation.
|
69
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
71
70
|
#
|
72
71
|
# @return [ Hash ] The extended json representation.
|
73
|
-
def as_extended_json(**
|
74
|
-
{'$dbPointer' => { "$ref" => ref, '$id' => id.as_extended_json }}
|
72
|
+
def as_extended_json(**_options)
|
73
|
+
{ '$dbPointer' => { "$ref" => ref, '$id' => id.as_extended_json } }
|
75
74
|
end
|
76
75
|
|
77
76
|
# Encode the DBPointer.
|
@@ -79,9 +78,9 @@ module BSON
|
|
79
78
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
80
79
|
#
|
81
80
|
# @see http://bsonspec.org/#/specification
|
82
|
-
def to_bson(buffer = ByteBuffer.new
|
81
|
+
def to_bson(buffer = ByteBuffer.new)
|
83
82
|
buffer.put_string(ref)
|
84
|
-
id.to_bson(buffer
|
83
|
+
id.to_bson(buffer)
|
85
84
|
buffer
|
86
85
|
end
|
87
86
|
|
data/lib/bson/dbref.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
#
|
2
|
+
# rubocop:todo all
|
3
3
|
|
4
4
|
# Copyright (C) 2015-2021 MongoDB Inc.
|
5
5
|
#
|
@@ -74,18 +74,21 @@ module BSON
|
|
74
74
|
# this parameter must be a String containing the collection name.
|
75
75
|
# @param [ Object ] id The object id, when using the legacy API.
|
76
76
|
# @param [ String ] database The database name, when using the legacy API.
|
77
|
+
#
|
78
|
+
# @raise [ BSON::Error::InvalidDBRefArgument ] if giving invalid arguments
|
79
|
+
# to the constructor.
|
77
80
|
def initialize(hash_or_collection, id = nil, database = nil)
|
78
81
|
if hash_or_collection.is_a?(Hash)
|
79
82
|
hash = hash_or_collection
|
80
83
|
|
81
84
|
unless id.nil? && database.nil?
|
82
|
-
raise
|
85
|
+
raise Error::InvalidDBRefArgument, 'When using the hash API, DBRef constructor accepts only one argument'
|
83
86
|
end
|
84
87
|
else
|
85
88
|
warn("BSON::DBRef constructor called with the legacy API - please use the hash API instead")
|
86
89
|
|
87
90
|
if id.nil?
|
88
|
-
raise
|
91
|
+
raise Error::InvalidDBRefArgument, 'When using the legacy constructor API, id must be provided'
|
89
92
|
end
|
90
93
|
|
91
94
|
hash = {
|
@@ -98,17 +101,17 @@ module BSON
|
|
98
101
|
hash = reorder_fields(hash)
|
99
102
|
%w($ref $id).each do |key|
|
100
103
|
unless hash[key]
|
101
|
-
raise
|
104
|
+
raise Error::InvalidDBRefArgument, "DBRef must have #{key}: #{hash}"
|
102
105
|
end
|
103
106
|
end
|
104
107
|
|
105
108
|
unless hash['$ref'].is_a?(String)
|
106
|
-
raise
|
109
|
+
raise Error::InvalidDBRefArgument, "The value for key $ref must be a string, got: #{hash['$ref']}"
|
107
110
|
end
|
108
111
|
|
109
112
|
if db = hash['$db']
|
110
113
|
unless db.is_a?(String)
|
111
|
-
raise
|
114
|
+
raise Error::InvalidDBRefArgument, "The value for key $db must be a string, got: #{hash['$db']}"
|
112
115
|
end
|
113
116
|
end
|
114
117
|
|
@@ -121,11 +124,10 @@ module BSON
|
|
121
124
|
# dbref.to_bson
|
122
125
|
#
|
123
126
|
# @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
|
124
|
-
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
|
125
127
|
#
|
126
128
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
127
|
-
def to_bson(buffer = ByteBuffer.new
|
128
|
-
as_json.to_bson(buffer
|
129
|
+
def to_bson(buffer = ByteBuffer.new)
|
130
|
+
as_json.to_bson(buffer)
|
129
131
|
end
|
130
132
|
|
131
133
|
private
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2016-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -86,11 +87,11 @@ module BSON
|
|
86
87
|
|
87
88
|
def validate_range!(exponent, significand)
|
88
89
|
unless valid_exponent?(exponent)
|
89
|
-
raise
|
90
|
+
raise Error::InvalidDecimal128Range.new
|
90
91
|
end
|
91
92
|
|
92
93
|
unless valid_significand?(significand)
|
93
|
-
raise
|
94
|
+
raise Error::UnrepresentablePrecision.new
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
@@ -155,7 +156,7 @@ module BSON
|
|
155
156
|
# @return [ Regex ] The regex for a valid decimal128 string.
|
156
157
|
#
|
157
158
|
# @since 4.2.0
|
158
|
-
VALID_DECIMAL128_STRING_REGEX =
|
159
|
+
VALID_DECIMAL128_STRING_REGEX = /\A[\-\+]?(\d+(\.\d*)?|\.\d+)(E[\-\+]?\d+)?\Z/i
|
159
160
|
|
160
161
|
# Initialize the FromString Builder object.
|
161
162
|
#
|
@@ -189,9 +190,9 @@ module BSON
|
|
189
190
|
private
|
190
191
|
|
191
192
|
def to_bits
|
192
|
-
|
193
|
-
digits,
|
194
|
-
before_decimal,
|
193
|
+
_, sign, digits_str = SIGN_AND_DIGITS_REGEX.match(@string).to_a
|
194
|
+
digits, _, scientific_exp = digits_str.partition(SCIENTIFIC_EXPONENT_REGEX)
|
195
|
+
before_decimal, _, after_decimal = digits.partition('.')
|
195
196
|
|
196
197
|
significand_str = before_decimal << after_decimal
|
197
198
|
significand_str = SIGNIFICAND_WITH_LEADING_ZEROS_REGEX.match(significand_str).to_a[2]
|
@@ -262,7 +263,7 @@ module BSON
|
|
262
263
|
end
|
263
264
|
|
264
265
|
def validate_format!
|
265
|
-
raise
|
266
|
+
raise Error::InvalidDecimal128String.new unless @string =~ VALID_DECIMAL128_STRING_REGEX
|
266
267
|
end
|
267
268
|
end
|
268
269
|
|
@@ -317,7 +318,7 @@ module BSON
|
|
317
318
|
end
|
318
319
|
|
319
320
|
def to_bits
|
320
|
-
sign, significand_str,
|
321
|
+
sign, significand_str, _, exp = @big_decimal.split
|
321
322
|
exponent = @big_decimal.zero? ? 0 : exp - significand_str.length
|
322
323
|
is_negative = (sign == ::BigDecimal::SIGN_NEGATIVE_FINITE || sign == ::BigDecimal::SIGN_NEGATIVE_ZERO)
|
323
324
|
Builder.parts_to_bits(significand_str.to_i,
|
@@ -452,4 +453,4 @@ module BSON
|
|
452
453
|
end
|
453
454
|
end
|
454
455
|
end
|
455
|
-
end
|
456
|
+
end
|
data/lib/bson/decimal128.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2016-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -57,27 +58,28 @@ module BSON
|
|
57
58
|
# @since 4.2.0
|
58
59
|
NATIVE_TYPE = BigDecimal
|
59
60
|
|
60
|
-
#
|
61
|
+
# Return a string representation of the Decimal128 use in standard
|
62
|
+
# application-level JSON serialization. Returns nil for non-real
|
63
|
+
# numbers such as NaN and Infinity to be compatible with ActiveSupport.
|
64
|
+
# This method is intentionally different from #as_extended_json.
|
61
65
|
#
|
62
|
-
# @example Get the Decimal128 as a JSON
|
66
|
+
# @example Get the Decimal128 as a JSON-serializable object.
|
63
67
|
# decimal.as_json
|
64
68
|
#
|
65
|
-
# @return [
|
66
|
-
#
|
67
|
-
# @since 4.2.0
|
68
|
-
# @deprecated Use as_extended_json instead.
|
69
|
+
# @return [ String | nil ] The decimal128 as a String or nil for non-representable numbers.
|
69
70
|
def as_json(*args)
|
70
|
-
|
71
|
+
value = to_s
|
72
|
+
value unless %w[NaN Infinity -Infinity].include?(value)
|
71
73
|
end
|
72
74
|
|
73
75
|
# Converts this object to a representation directly serializable to
|
74
|
-
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.
|
76
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
|
75
77
|
#
|
76
78
|
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
77
79
|
# (default is canonical extended JSON)
|
78
80
|
#
|
79
81
|
# @return [ Hash ] The extended json representation.
|
80
|
-
def as_extended_json(**
|
82
|
+
def as_extended_json(**_options)
|
81
83
|
{ EXTENDED_JSON_KEY => to_s }
|
82
84
|
end
|
83
85
|
|
@@ -99,9 +101,9 @@ module BSON
|
|
99
101
|
alias :eql? :==
|
100
102
|
|
101
103
|
def <=>(other)
|
102
|
-
|
104
|
+
to_d <=> case other
|
103
105
|
when Decimal128
|
104
|
-
other.
|
106
|
+
other.to_d
|
105
107
|
else
|
106
108
|
other
|
107
109
|
end
|
@@ -115,7 +117,7 @@ module BSON
|
|
115
117
|
# @param [ String, BigDecimal ] object The BigDecimal or String to use for
|
116
118
|
# instantiating a Decimal128.
|
117
119
|
#
|
118
|
-
# @raise [
|
120
|
+
# @raise [ BSON::Error::InvalidDecimal128Argument ] When argument is not a String or BigDecimal.
|
119
121
|
#
|
120
122
|
# @since 4.2.0
|
121
123
|
def initialize(object)
|
@@ -124,10 +126,15 @@ module BSON
|
|
124
126
|
elsif object.is_a?(BigDecimal)
|
125
127
|
set_bits(*Builder::FromBigDecimal.new(object).bits)
|
126
128
|
else
|
127
|
-
raise
|
129
|
+
raise Error::InvalidDecimal128Argument.new
|
128
130
|
end
|
129
131
|
end
|
130
132
|
|
133
|
+
# Get the BSON type for Decimal128.
|
134
|
+
def bson_type
|
135
|
+
BSON_TYPE
|
136
|
+
end
|
137
|
+
|
131
138
|
# Get the decimal128 as its raw BSON data.
|
132
139
|
#
|
133
140
|
# @example Get the raw bson bytes in a buffer.
|
@@ -138,7 +145,7 @@ module BSON
|
|
138
145
|
# @see http://bsonspec.org/#/specification
|
139
146
|
#
|
140
147
|
# @since 4.2.0
|
141
|
-
def to_bson(buffer = ByteBuffer.new
|
148
|
+
def to_bson(buffer = ByteBuffer.new)
|
142
149
|
buffer.put_decimal128(@low, @high)
|
143
150
|
end
|
144
151
|
|
@@ -189,7 +196,7 @@ module BSON
|
|
189
196
|
# @example
|
190
197
|
# decimal128 = BSON::Decimal128.new("0.200")
|
191
198
|
# => BSON::Decimal128('0.200')
|
192
|
-
# big_decimal = decimal128.
|
199
|
+
# big_decimal = decimal128.to_d
|
193
200
|
# => #<BigDecimal:7fc619c95388,'0.2E0',9(18)>
|
194
201
|
# big_decimal.to_s
|
195
202
|
# => "0.2E0"
|
@@ -198,11 +205,10 @@ module BSON
|
|
198
205
|
# and -sNaN while Ruby's BigDecimal cannot.
|
199
206
|
#
|
200
207
|
# @return [ BigDecimal ] The decimal as a BigDecimal.
|
201
|
-
|
202
|
-
# @since 4.2.0
|
203
|
-
def to_big_decimal
|
208
|
+
def to_d
|
204
209
|
@big_decimal ||= BigDecimal(to_s)
|
205
210
|
end
|
211
|
+
alias :to_big_decimal :to_d
|
206
212
|
|
207
213
|
private
|
208
214
|
|
@@ -236,7 +242,7 @@ module BSON
|
|
236
242
|
#
|
237
243
|
# @param [ String ] string The string to parse.
|
238
244
|
#
|
239
|
-
# @raise [ BSON::
|
245
|
+
# @raise [ BSON::Error:InvalidDecimal128String ] If the provided string is invalid.
|
240
246
|
#
|
241
247
|
# @return [ BSON::Decimal128 ] The new decimal128.
|
242
248
|
#
|
@@ -262,97 +268,5 @@ module BSON
|
|
262
268
|
decimal
|
263
269
|
end
|
264
270
|
end
|
265
|
-
|
266
|
-
# Raised when trying to create a Decimal128 from an object that is neither a String nor a BigDecimal.
|
267
|
-
#
|
268
|
-
# @api private
|
269
|
-
#
|
270
|
-
# @since 4.2.0
|
271
|
-
class InvalidArgument < ArgumentError
|
272
|
-
|
273
|
-
# The custom error message for this error.
|
274
|
-
#
|
275
|
-
# @since 4.2.0
|
276
|
-
MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'
|
277
|
-
|
278
|
-
# Get the custom error message for the exception.
|
279
|
-
#
|
280
|
-
# @example Get the message.
|
281
|
-
# error.message
|
282
|
-
#
|
283
|
-
# @return [ String ] The error message.
|
284
|
-
#
|
285
|
-
# @since 4.2.0
|
286
|
-
def message
|
287
|
-
MESSAGE
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
# Raised when trying to create a Decimal128 from a string with
|
292
|
-
# an invalid format.
|
293
|
-
#
|
294
|
-
# @api private
|
295
|
-
#
|
296
|
-
# @since 4.2.0
|
297
|
-
class InvalidString < RuntimeError
|
298
|
-
|
299
|
-
# The custom error message for this error.
|
300
|
-
#
|
301
|
-
# @since 4.2.0
|
302
|
-
MESSAGE = 'Invalid string format for creating a Decimal128 object.'
|
303
|
-
|
304
|
-
# Get the custom error message for the exception.
|
305
|
-
#
|
306
|
-
# @example Get the message.
|
307
|
-
# error.message
|
308
|
-
#
|
309
|
-
# @return [ String ] The error message.
|
310
|
-
#
|
311
|
-
# @since 4.2.0
|
312
|
-
def message
|
313
|
-
MESSAGE
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
# Raised when the exponent is outside the valid range.
|
318
|
-
#
|
319
|
-
# @since 4.2.0
|
320
|
-
class InvalidRange < RuntimeError
|
321
|
-
|
322
|
-
# The custom error message for this error.
|
323
|
-
#
|
324
|
-
# @since 4.2.0
|
325
|
-
# @deprecated
|
326
|
-
MESSAGE = 'Value out of range for Decimal128 representation.'
|
327
|
-
|
328
|
-
# Get the custom error message for the exception.
|
329
|
-
#
|
330
|
-
# @example Get the message.
|
331
|
-
# error.message
|
332
|
-
#
|
333
|
-
# @return [ String ] The error message.
|
334
|
-
#
|
335
|
-
# @since 4.2.0
|
336
|
-
def message
|
337
|
-
MESSAGE
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
# Raised when the significand provided is outside the valid range.
|
342
|
-
#
|
343
|
-
# @note This class derives from InvalidRange for backwards compatibility,
|
344
|
-
# however when RUBY-1806 is implemented it should be changed to derive
|
345
|
-
# from the base BSON exception class.
|
346
|
-
class UnrepresentablePrecision < InvalidRange
|
347
|
-
|
348
|
-
# Get the custom error message for the exception.
|
349
|
-
#
|
350
|
-
# @return [ String ] The error message.
|
351
|
-
def message
|
352
|
-
'The value contains too much precision for Decimal128 representation'
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
|
-
Registry.register(BSON_TYPE, self)
|
357
271
|
end
|
358
272
|
end
|
data/lib/bson/document.rb
CHANGED
data/lib/bson/environment.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
3
|
+
|
4
|
+
module BSON
|
5
|
+
class Error
|
6
|
+
|
7
|
+
# Raised when validating keys and a key is illegal in MongoDB
|
8
|
+
class IllegalKey < Error
|
9
|
+
|
10
|
+
# Instantiate the exception.
|
11
|
+
#
|
12
|
+
# @example Instantiate the exception.
|
13
|
+
# BSON::Error::IllegalKey.new(string)
|
14
|
+
#
|
15
|
+
# @param [ String ] string The illegal string.
|
16
|
+
#
|
17
|
+
# @api private
|
18
|
+
def initialize(string)
|
19
|
+
super("'#{string}' is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
3
|
+
|
4
|
+
module BSON
|
5
|
+
class Error
|
6
|
+
|
7
|
+
# Raised when providing an invalid type to the Binary.
|
8
|
+
class InvalidBinaryType < Error
|
9
|
+
|
10
|
+
# @return [ Object ] The invalid type.
|
11
|
+
attr_reader :type
|
12
|
+
|
13
|
+
# Instantiate the new error.
|
14
|
+
#
|
15
|
+
# @example Instantiate the error.
|
16
|
+
# InvalidBinaryType.new(:error)
|
17
|
+
#
|
18
|
+
# @param [ Object ] type The invalid type.
|
19
|
+
#
|
20
|
+
# @api private
|
21
|
+
def initialize(type)
|
22
|
+
@type = type
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get the custom error message for the exception.
|
26
|
+
#
|
27
|
+
# @example Get the message.
|
28
|
+
# error.message
|
29
|
+
#
|
30
|
+
# @return [ String ] The error message.
|
31
|
+
def message
|
32
|
+
"#{type.inspect} is not a valid binary type. " +
|
33
|
+
"Please use one of #{BSON::Binary::SUBTYPES.keys.map(&:inspect).join(", ")}."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
3
|
+
|
4
|
+
module BSON
|
5
|
+
class Error
|
6
|
+
|
7
|
+
# Raised when trying to create a Decimal128 from an object that is neither a String nor a BigDecimal.
|
8
|
+
class InvalidDecimal128Argument < Error
|
9
|
+
|
10
|
+
# The custom error message for this error.
|
11
|
+
MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'
|
12
|
+
|
13
|
+
# Get the custom error message for the exception.
|
14
|
+
#
|
15
|
+
# @example Get the message.
|
16
|
+
# error.message
|
17
|
+
#
|
18
|
+
# @return [ String ] The error message.
|
19
|
+
def message
|
20
|
+
MESSAGE
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
3
|
+
|
4
|
+
module BSON
|
5
|
+
class Error
|
6
|
+
|
7
|
+
# Raised when the exponent is outside the valid range.
|
8
|
+
class InvalidDecimal128Range < Error
|
9
|
+
|
10
|
+
# The custom error message for this error.
|
11
|
+
#
|
12
|
+
# @deprecated
|
13
|
+
MESSAGE = 'Value out of range for Decimal128 representation.'
|
14
|
+
|
15
|
+
# Get the custom error message for the exception.
|
16
|
+
#
|
17
|
+
# @example Get the message.
|
18
|
+
# error.message
|
19
|
+
#
|
20
|
+
# @return [ String ] The error message.
|
21
|
+
def message
|
22
|
+
MESSAGE
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|