handle symbol keys as well as strings

This commit is contained in:
Mike Dirolf 2009-03-05 10:27:19 -05:00
parent 0ac6f2b4ed
commit 709e819523
2 changed files with 10 additions and 2 deletions

View File

@ -102,6 +102,15 @@ static void write_name_and_type(bson_buffer* buffer, VALUE name, char type) {
static int write_element(VALUE key, VALUE value, VALUE extra) {
bson_buffer* buffer = (bson_buffer*)extra;
if (TYPE(key) == T_SYMBOL) {
// TODO better way to do this... ?
key = rb_str_new2(rb_id2name(SYM2ID(key)));
}
if (TYPE(key) != T_STRING) {
rb_raise(rb_eTypeError, "keys must be strings or symbols");
}
switch(TYPE(value)) {
case T_FIXNUM:
write_name_and_type(buffer, key, 0x10);

View File

@ -132,8 +132,7 @@ class BSONTest < Test::Unit::TestCase
doc2 = @b.deserialize
bin2 = doc2['bin']
assert_kind_of Binary, bin2
b = bin2.to_a
assert_equal [1, 2, 3, 4, 5], b
assert_equal [1, 2, 3, 4, 5], bin2.to_a
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
end