Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Rubocop on a few files and update the files accordingly #313

Merged
merged 6 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ AllCops:
NewCops: enable
Exclude:
- 'spec/shared/**/*'
- 'tmp/**/*'

Bundler:
Enabled: true

Gemspec:
Enabled: true
Expand Down Expand Up @@ -50,6 +54,68 @@ Bundler/OrderedGems:
Gemspec/OrderedDependencies:
Enabled: false

Layout/SpaceInsideArrayLiteralBrackets:
EnforcedStyle: space

Layout/SpaceInsidePercentLiteralDelimiters:
Enabled: false

Metrics/ClassLength:
Max: 200

Metrics/ModuleLength:
Enabled: false

Metrics/MethodLength:
Max: 20

RSpec/BeforeAfterAll:
Enabled: false

# Ideally, we'd use this one, too, but our tests have not historically followed
# this style and it's not worth changing right now, IMO
RSpec/DescribeClass:
Enabled: false

Style/FetchEnvVar:
Enabled: false

Style/FormatString:
Enabled: false

RSpec/ImplicitExpect:
EnforcedStyle: is_expected

RSpec/MultipleExpectations:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Enabled: false

RSpec/NestedGroups:
Enabled: false

Style/ClassVars:
Enabled: false

Style/Documentation:
Exclude:
- 'spec/**/*'

Style/ModuleFunction:
EnforcedStyle: extend_self

Style/OptionalBooleanParameter:
Enabled: false

Style/ParallelAssignment:
Enabled: false

Style/TernaryParentheses:
EnforcedStyle: require_parentheses_when_complex

Style/TrailingCommaInArrayLiteral:
Enabled: false

Style/TrailingCommaInHashLiteral:
Enabled: false
3 changes: 3 additions & 0 deletions ext/bson/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true
# rubocop:disable all

require 'mkmf'

$CFLAGS << ' -Wall -g -std=c99'
Expand Down
2 changes: 2 additions & 0 deletions ext/bson/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void Init_bson_native()
_db_str = rb_str_new_cstr("$db");
rb_gc_register_mark_object(_db_str);

rb_require("digest/md5");

VALUE rb_bson_module = rb_define_module("BSON");

/* Document-class: BSON::ByteBuffer
Expand Down
76 changes: 48 additions & 28 deletions lib/bson/array.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2009-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,16 +14,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# The top-level BSON module.
module BSON

# Injects behaviour for encoding and decoding arrays to
# and from raw bytes as specified by the BSON spec.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
module Array

# An array is type 0x04 in the BSON spec.
#
# @since 2.0.0
Expand All @@ -50,8 +49,10 @@ def to_bson(buffer = ByteBuffer.new)
buffer.put_int32(0)
each_with_index do |value, index|
unless value.respond_to?(:bson_type)
raise Error::UnserializableClass, "Array element at position #{index} does not define its BSON serialized type: #{value}"
raise Error::UnserializableClass,
"Array element at position #{index} does not define its BSON serialized type: #{value}"
end

buffer.put_byte(value.bson_type)
buffer.put_cstring(index.to_s)
value.to_bson(buffer)
Expand All @@ -75,7 +76,7 @@ def to_bson(buffer = ByteBuffer.new)
#
# @since 2.0.0
def to_bson_object_id
ObjectId.repair(self) { pack("C*") }
ObjectId.repair(self) { pack('C*') }
end

# Converts the array to a normalized value in a BSON document.
Expand All @@ -87,7 +88,7 @@ def to_bson_object_id
#
# @since 3.0.0
def to_bson_normalized_value
map { |value| value.to_bson_normalized_value }
map(&:to_bson_normalized_value)
end

# Converts this object to a representation directly serializable to
Expand All @@ -106,8 +107,8 @@ def as_extended_json(**options)
end
end

# Class-level methods to be added to the Array class.
module ClassMethods

# Deserialize the array from BSON.
#
# @note If the argument cannot be parsed, an exception will be raised
Expand All @@ -122,43 +123,62 @@ module ClassMethods
# @return [ Array ] The decoded array.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def from_bson(buffer, **options)
if buffer.respond_to?(:get_array)
buffer.get_array(**options)
else
array = new
parse_array_from_buffer(buffer, **options)
end
end

private

# Parse an array from the buffer.
#
# @param [ ByteBuf ] buffer the buffer to read from
# @param [ Hash ] options the optional keyword arguments
#
# @return [ Array ] the array that was parsed
#
# @raise [ BSON::Error::BSONDecodeError ] if the expected number of
# bytes were not read from the buffer
def parse_array_from_buffer(buffer, **options)
new.tap do |array|
start_position = buffer.read_position
expected_byte_size = buffer.get_int32
while (type = buffer.get_byte) != NULL_BYTE
buffer.get_cstring
cls = BSON::Registry.get(type)
value = if options.empty?
cls.from_bson(buffer)
else
cls.from_bson(buffer, **options)
end
array << value
end
parse_array_elements_from_buffer(array, buffer, **options)
actual_byte_size = buffer.read_position - start_position
if actual_byte_size != expected_byte_size
raise Error::BSONDecodeError, "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
raise Error::BSONDecodeError,
"Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
end
array
end
end

# Parse a sequence of array elements from the buffer.
#
# @param [ Array ] array the array to populate
# @param [ ByteBuf ] buffer the buffer to read from
# @param [ Hash ] options the optional keyword arguments
def parse_array_elements_from_buffer(array, buffer, **options)
while (type = buffer.get_byte) != NULL_BYTE
buffer.get_cstring
cls = BSON::Registry.get(type)
value = if options.empty?
cls.from_bson(buffer)
else
cls.from_bson(buffer, **options)
end
array << value
end
end
end

# Register this type when the module is loaded.
#
# @since 2.0.0
Registry.register(BSON_TYPE, ::Array)
end

# Enrich the core Array class with this module.
#
# @since 2.0.0
::Array.send(:include, Array)
::Array.send(:extend, Array::ClassMethods)
::Array.include Array
::Array.extend Array::ClassMethods
end
Loading
Loading