From bb13fbe687ba2d16083115a7d4e362c3deb5418c Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Wed, 19 Aug 2009 15:18:02 -0400 Subject: [PATCH] use nil for BSON undefined instead of useless Undefined class. deprecate Undefined class --- ext/cbson/cbson.c | 7 ++----- lib/mongo/types/undefined.rb | 15 ++++++++------- lib/mongo/util/bson.rb | 11 +++-------- lib/mongo/util/xml_to_ruby.rb | 2 -- test/test_bson.rb | 2 +- test/test_round_trip.rb | 6 +++++- 6 files changed, 19 insertions(+), 24 deletions(-) diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index f8fe2e5..406e5b1 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -42,7 +42,6 @@ #define INITIAL_BUFFER_SIZE 256 static VALUE Binary; -static VALUE Undefined; static VALUE Time; static VALUE ObjectID; static VALUE DBRef; @@ -366,7 +365,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow break; } 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; } } @@ -563,7 +562,7 @@ static VALUE get_value(const char* buffer, int* position, int type) { } case 6: { - value = rb_class_new_instance(0, NULL, Undefined); + value = Qnil; break; } case 7: @@ -746,8 +745,6 @@ void Init_cbson() { rb_intern("Driver")); rb_require("mongo/types/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"); ObjectID = rb_const_get(driver, rb_intern("ObjectID")); rb_require("mongo/types/dbref"); diff --git a/lib/mongo/types/undefined.rb b/lib/mongo/types/undefined.rb index 61a2a07..72face1 100644 --- a/lib/mongo/types/undefined.rb +++ b/lib/mongo/types/undefined.rb @@ -18,14 +18,15 @@ 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 + # DEPRECATED - the ruby driver converts the BSON undefined type to nil, + # and saves this type as nil + class Undefined < Object + 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 diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index 487e7c3..bfa0b25 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -135,7 +135,7 @@ class BSON when BINARY serialize_binary_element(@buf, k, v) when UNDEFINED - serialize_undefined_element(@buf, k) + serialize_null_element(@buf, k) when CODE_W_SCOPE serialize_code_w_scope(@buf, k, v) else @@ -207,7 +207,7 @@ class BSON doc[key] = nil when UNDEFINED key = deserialize_cstr(@buf) - doc[key] = Undefined.new + doc[key] = nil when REF key = deserialize_cstr(@buf) doc[key] = deserialize_dbref_data(@buf) @@ -385,11 +385,6 @@ class BSON 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) buf.put(BOOLEAN) self.class.serialize_cstr(buf, key) @@ -543,7 +538,7 @@ class BSON when Symbol SYMBOL when Undefined - UNDEFINED + NULL else raise "Unknown type of object: #{o.class.name}" end diff --git a/lib/mongo/util/xml_to_ruby.rb b/lib/mongo/util/xml_to_ruby.rb index fc7b003..dfc3ec5 100644 --- a/lib/mongo/util/xml_to_ruby.rb +++ b/lib/mongo/util/xml_to_ruby.rb @@ -63,8 +63,6 @@ class XMLToRuby regex_to_ruby(e.elements) when 'null' nil - when 'undefined' - Undefined.new when 'doc' doc_to_ruby(e) else diff --git a/test/test_bson.rb b/test/test_bson.rb index 8651ca3..78b2673 100644 --- a/test/test_bson.rb +++ b/test/test_bson.rb @@ -183,7 +183,7 @@ class BSONTest < Test::Unit::TestCase doc = {'undef' => Undefined.new} @b.serialize(doc) doc2 = @b.deserialize - assert_kind_of Undefined, doc2['undef'] + assert_equal nil, doc2['undef'] end def test_put_id_first diff --git a/test/test_round_trip.rb b/test/test_round_trip.rb index a25345a..a6faad9 100644 --- a/test/test_round_trip.rb +++ b/test/test_round_trip.rb @@ -64,7 +64,11 @@ EOS # generated) def one_round_trip(dir, name) 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|