handle symbol keys as well as strings
This commit is contained in:
parent
0ac6f2b4ed
commit
709e819523
@ -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) {
|
static int write_element(VALUE key, VALUE value, VALUE extra) {
|
||||||
bson_buffer* buffer = (bson_buffer*)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)) {
|
switch(TYPE(value)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
write_name_and_type(buffer, key, 0x10);
|
write_name_and_type(buffer, key, 0x10);
|
||||||
|
@ -132,8 +132,7 @@ class BSONTest < Test::Unit::TestCase
|
|||||||
doc2 = @b.deserialize
|
doc2 = @b.deserialize
|
||||||
bin2 = doc2['bin']
|
bin2 = doc2['bin']
|
||||||
assert_kind_of Binary, bin2
|
assert_kind_of Binary, bin2
|
||||||
b = bin2.to_a
|
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
||||||
assert_equal [1, 2, 3, 4, 5], b
|
|
||||||
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
|
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user