use nil for BSON undefined instead of useless Undefined class. deprecate Undefined class

This commit is contained in:
Mike Dirolf 2009-08-19 15:18:02 -04:00
parent 66e8d1a13b
commit bb13fbe687
6 changed files with 19 additions and 24 deletions

View File

@ -42,7 +42,6 @@
#define INITIAL_BUFFER_SIZE 256 #define INITIAL_BUFFER_SIZE 256
static VALUE Binary; static VALUE Binary;
static VALUE Undefined;
static VALUE Time; static VALUE Time;
static VALUE ObjectID; static VALUE ObjectID;
static VALUE DBRef; static VALUE DBRef;
@ -366,7 +365,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
break; break;
} }
if (strcmp(cls, "XGen::Mongo::Driver::Undefined") == 0) { if (strcmp(cls, "XGen::Mongo::Driver::Undefined") == 0) {
write_name_and_type(buffer, key, 0x06); write_name_and_type(buffer, key, 0x0A); // just use nil type
break; break;
} }
} }
@ -563,7 +562,7 @@ static VALUE get_value(const char* buffer, int* position, int type) {
} }
case 6: case 6:
{ {
value = rb_class_new_instance(0, NULL, Undefined); value = Qnil;
break; break;
} }
case 7: case 7:
@ -746,8 +745,6 @@ void Init_cbson() {
rb_intern("Driver")); rb_intern("Driver"));
rb_require("mongo/types/binary"); rb_require("mongo/types/binary");
Binary = rb_const_get(driver, rb_intern("Binary")); Binary = rb_const_get(driver, rb_intern("Binary"));
rb_require("mongo/types/undefined");
Undefined = rb_const_get(driver, rb_intern("Undefined"));
rb_require("mongo/types/objectid"); rb_require("mongo/types/objectid");
ObjectID = rb_const_get(driver, rb_intern("ObjectID")); ObjectID = rb_const_get(driver, rb_intern("ObjectID"));
rb_require("mongo/types/dbref"); rb_require("mongo/types/dbref");

View File

@ -18,14 +18,15 @@ module XGen
module Mongo module Mongo
module Driver module Driver
# A special "undefined" type to match Mongo's storage of UNKNOWN values. # DEPRECATED - the ruby driver converts the BSON undefined type to nil,
# "UNKNOWN" comes from JavaScript. # and saves this type as nil
# class Undefined < Object
# 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
def initialize
super
warn "the Undefined type is deprecated and will be removed - BSON undefineds get implicitely converted to nil now"
end
end
end end
end end
end end

View File

@ -135,7 +135,7 @@ class BSON
when BINARY when BINARY
serialize_binary_element(@buf, k, v) serialize_binary_element(@buf, k, v)
when UNDEFINED when UNDEFINED
serialize_undefined_element(@buf, k) serialize_null_element(@buf, k)
when CODE_W_SCOPE when CODE_W_SCOPE
serialize_code_w_scope(@buf, k, v) serialize_code_w_scope(@buf, k, v)
else else
@ -207,7 +207,7 @@ class BSON
doc[key] = nil doc[key] = nil
when UNDEFINED when UNDEFINED
key = deserialize_cstr(@buf) key = deserialize_cstr(@buf)
doc[key] = Undefined.new doc[key] = nil
when REF when REF
key = deserialize_cstr(@buf) key = deserialize_cstr(@buf)
doc[key] = deserialize_dbref_data(@buf) doc[key] = deserialize_dbref_data(@buf)
@ -385,11 +385,6 @@ class BSON
end end
end end
def serialize_undefined_element(buf, key)
buf.put(UNDEFINED)
self.class.serialize_cstr(buf, key)
end
def serialize_boolean_element(buf, key, val) def serialize_boolean_element(buf, key, val)
buf.put(BOOLEAN) buf.put(BOOLEAN)
self.class.serialize_cstr(buf, key) self.class.serialize_cstr(buf, key)
@ -543,7 +538,7 @@ class BSON
when Symbol when Symbol
SYMBOL SYMBOL
when Undefined when Undefined
UNDEFINED NULL
else else
raise "Unknown type of object: #{o.class.name}" raise "Unknown type of object: #{o.class.name}"
end end

View File

@ -63,8 +63,6 @@ class XMLToRuby
regex_to_ruby(e.elements) regex_to_ruby(e.elements)
when 'null' when 'null'
nil nil
when 'undefined'
Undefined.new
when 'doc' when 'doc'
doc_to_ruby(e) doc_to_ruby(e)
else else

View File

@ -183,7 +183,7 @@ class BSONTest < Test::Unit::TestCase
doc = {'undef' => Undefined.new} doc = {'undef' => Undefined.new}
@b.serialize(doc) @b.serialize(doc)
doc2 = @b.deserialize doc2 = @b.deserialize
assert_kind_of Undefined, doc2['undef'] assert_equal nil, doc2['undef']
end end
def test_put_id_first def test_put_id_first

View File

@ -64,7 +64,11 @@ EOS
# generated) # generated)
def one_round_trip(dir, name) def one_round_trip(dir, name)
obj = File.open(File.join(dir, "#{name}.xson")) { |f| obj = File.open(File.join(dir, "#{name}.xson")) { |f|
XMLToRuby.new.xml_to_ruby(f) begin
XMLToRuby.new.xml_to_ruby(f)
rescue => ex # unsupported type
return
end
} }
File.open(File.join(dir, "#{name}.bson"), 'rb') { |f| File.open(File.join(dir, "#{name}.bson"), 'rb') { |f|