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

fixed bugs with serializing and deserializing js code with scope #5

Merged
merged 4 commits into from
Jul 1, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions lib/bson/code_with_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ def initialize(javascript = "", scope = {})
#
# @since 2.0.0
def to_bson(encoded = ''.force_encoding(BINARY))
encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
# -1 because we are removing an extra byte
out = encode_with_placeholder_and_null(BSON_ADJUST - 1, encoded) do |encoded|
javascript.to_bson(encoded)
scope.to_bson(encoded)
end
# an extra null byte has been added; we must remove it
out.chop!
end

# Deserialize a code with scope from BSON.
Expand All @@ -105,9 +108,18 @@ def to_bson(encoded = ''.force_encoding(BINARY))
#
# @since 2.0.0
def self.from_bson(bson)
code_with_scope = StringIO.new(bson.read(Int32.from_bson(bson)))
length = code_with_scope.read(4).unpack(Int32::PACK).first
new(code_with_scope.read(length).from_bson_string.chop!)
cws_total_length = Int32.from_bson(bson) - 4 #deduct the size of the int we just read
code_with_scope = StringIO.new(bson.read(cws_total_length))
code_length = code_with_scope.read(4).unpack(Int32::PACK).first
bson_code = code_with_scope.read(code_length).from_bson_string.chop!

scope_length_str = code_with_scope.read(4)
scope_length = scope_length_str.unpack(Int32::PACK).first
bson_scope_str = code_with_scope.read(scope_length)

# reconstruct the whole thing, since Hash throws away first 4 bytes
bson_scope = ::Hash.from_bson(StringIO.new(scope_length_str + bson_scope_str))
new(bson_code, bson_scope)
end

# Register this type when the module is loaded.
Expand Down
4 changes: 2 additions & 2 deletions lib/bson/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Hash
# Get the hash as encoded BSON.
#
# @example Get the hash as encoded BSON.
# { field: "value" }.to_bson
# { "field" => "value" }.to_bson
#
# @return [ String ] The encoded string.
#
Expand All @@ -52,7 +52,7 @@ module ClassMethods

# Deserialize the hash from BSON.
#
# @param [ String ] bson The bson representing a hash.
# @param [ IO ] bson The bson representing a hash.
#
# @return [ Array ] The decoded hash.
#
Expand Down
1 change: 1 addition & 0 deletions lib/bson/string.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2013 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
10 changes: 5 additions & 5 deletions spec/bson/code_with_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
end
let(:obj) { described_class.new(code, scope) }
let(:bson) do
"#{48.to_bson}#{(code.length + 1).to_bson}#{code}#{BSON::NULL_BYTE}" +
"#{scope.to_bson}#{BSON::NULL_BYTE}"
"#{47.to_bson}#{(code.length + 1).to_bson}#{code}#{BSON::NULL_BYTE}" +
"#{scope.to_bson}"
end

it_behaves_like "a bson element"
Expand All @@ -53,7 +53,7 @@
let(:type) { 15.chr }
let(:code) { "this.value == name" }
let(:scope) do
{ :name => "test" }
{ "name" => "test" }
end
let(:obj) { described_class.new(code, scope) }
let(:bson) { StringIO.new(obj.to_bson) }
Expand All @@ -63,8 +63,8 @@
expect(deserialized.javascript).to eq(code)
end

it "does not deserialize a scope" do
expect(deserialized.scope).to be_empty
it "deserializes the scope" do
expect(deserialized.scope).to eq(scope)
end
end
end