Added the XGen::Mongo::Driver::Undefined data type (yuck)

This commit is contained in:
Jim Menard 2009-01-12 12:08:02 -05:00
parent 921030c0f8
commit c6bd4fc100
6 changed files with 57 additions and 5 deletions

View File

@ -2,6 +2,7 @@ require 'mongo/mongo'
require 'mongo/objectid'
require 'mongo/dbref'
require 'mongo/binary'
require 'mongo/undefined'
require 'mongo/message'
require 'mongo/db'
require 'mongo/cursor'

31
lib/mongo/undefined.rb Normal file
View File

@ -0,0 +1,31 @@
# --
# Copyright (C) 2008-2009 10gen Inc.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ++
module XGen
module Mongo
module Driver
# A special "undefined" type to match Mongo's storage of UNKNOWN values.
# "UNKNOWN" comes from JavaScript.
#
# NOTE: this class does not attempt to provide ANY of the semantics an
# "unknown" object might need. It isn't nil, it isn't special in any
# way, and there isn't any singleton value.
class Undefined < Object; end
end
end
end

View File

@ -91,8 +91,9 @@ class BSON
serialize_symbol_element(@buf, k, v)
when BINARY
serialize_binary_element(@buf, k, v)
when UNDEFINED, CODE_W_SCOPE
# UNDEFINED should never happen because Ruby has no UNDEFINED type
when UNDEFINED
serialize_undefined_element(@buf, k)
when CODE_W_SCOPE
# TODO
raise "unimplemented type #{type}"
else
@ -141,9 +142,12 @@ class BSON
when DATE
key = deserialize_cstr(@buf)
doc[key] = deserialize_date_data(@buf)
when NULL, UNDEFINED
when NULL
key = deserialize_cstr(@buf)
doc[key] = nil
when UNDEFINED
key = deserialize_cstr(@buf)
doc[key] = XGen::Mongo::Driver::Undefined.new
when REF
key = deserialize_cstr(@buf)
doc[key] = deserialize_dbref_data(@buf, key, parent)
@ -284,6 +288,11 @@ class BSON
buf.put_array(bytes)
end
def serialize_undefined_element(buf, key)
buf.put(UNDEFINED)
self.class.serialize_cstr(buf, key)
end
def serialize_boolean_element(buf, key, val)
buf.put(BOOLEAN)
self.class.serialize_cstr(buf, key)
@ -402,6 +411,8 @@ class BSON
OBJECT
when Symbol
SYMBOL
when XGen::Mongo::Driver::Undefined
UNDEFINED
else
raise "Unknown type of object: #{o.class.name}"
end

View File

@ -56,8 +56,10 @@ class XMLToRuby
Time.at(e.text.to_f / 1000.0)
when 'regex'
regex_to_ruby(e.elements)
when 'null', 'undefined'
when 'null'
nil
when 'undefined'
Undefined.new
when 'doc'
doc_to_ruby(e)
else

View File

@ -107,4 +107,11 @@ class BSONTest < Test::Unit::TestCase
assert_equal 'binstring', doc2['bin']
end
def test_undefined
doc = {'undef' => Undefined.new}
@b.serialize(doc)
doc2 = @b.deserialize
assert_kind_of Undefined, doc2['undef']
end
end

View File

@ -105,7 +105,7 @@ EOS
assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby
rescue => ex
# File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| DEBUG
# File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| # DEBUG
# bson_from_ruby.each { |b| f.putc(b) }
# }
raise ex