Skip to content
Browse files

BSON serialization of Nu code.

  • Loading branch information...
1 parent 7b9b3a8 commit 3a7307bd62d9f576b204a9ce8202cd52f2f4b912 @timburks committed Apr 24, 2011
Showing with 45 additions and 31 deletions.
  1. +35 −30 objc/NuBSON.m
  2. +10 −1 test/test_bson.nu
View
65 objc/NuBSON.m
@@ -10,7 +10,7 @@ @protocol NuSymbolProtocol
- (NSString *) labelName;
@end
-@interface NuBSONObjectID (Private)
+@interface NuBSONObjectID (Private)
- (const bson_oid_t *) objectIDPointer;
@end
@@ -86,30 +86,9 @@ void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
bson_append_oid(bb, name, [((NuBSONObjectID *) object) objectIDPointer]);
}
else if (NuCell && [object isKindOfClass:[NuCell class]]) {
- if ([[object car] isKindOfClass:[NuSymbol class]] && (([object length] % 2) == 0)) {
- // assume we have an object
- bson_buffer *sub = bson_append_start_object(bb, name);
- id cursor = object;
- while (cursor && (cursor != [NSNull null])) {
- id key = [[cursor car] labelName];
- id value = [[cursor cdr] car];
- add_object_to_bson_buffer(sub, key, value);
- cursor = [[cursor cdr] cdr];
- }
- bson_append_finish_object(sub);
- }
- else {
- // assume we have an array
- bson_buffer *arr = bson_append_start_array(bb, name);
- id cursor = object;
- int i = 0;
- while (cursor && (cursor != [NSNull null])) {
- add_object_to_bson_buffer(arr, [[NSNumber numberWithInt:i] stringValue], [cursor car]);
- i++;
- cursor = [cursor cdr];
- }
- bson_append_finish_object(arr);
- }
+ // serialize Nu code as binary data of type 1
+ NSData *serialization = [NSKeyedArchiver archivedDataWithRootObject:object];
+ bson_append_binary(bb, name, 1, [serialization bytes], [serialization length]);
}
else if (NuSymbol && [object isKindOfClass:[NuSymbol class]]) {
if ([[object stringValue] isEqualToString:@"t"]) {
@@ -138,7 +117,7 @@ + (NuBSONObjectID *) objectIDWithData:(NSData *) data
return [[[NuBSONObjectID alloc] initWithData:data] autorelease];
}
-+ (NuBSONObjectID *) objectIDWithObjectIDPointer:(const bson_oid_t *) objectIDPointer
++ (NuBSONObjectID *) objectIDWithObjectIDPointer:(const bson_oid_t *) objectIDPointer
{
return [[[NuBSONObjectID alloc] initWithObjectIDPointer:objectIDPointer] autorelease];
}
@@ -173,12 +152,12 @@ - (id) initWithData:(NSData *) data
return self;
}
-- (id) copyWithZone:(NSZone *) zone
+- (id) copyWithZone:(NSZone *) zone
{
return [[[self class] allocWithZone:zone] initWithObjectIDPointer:&oid];
}
-- (NSInteger) hash {
+- (NSUInteger) hash {
return oid.ints[0] + oid.ints[1] + oid.ints[2];
}
@@ -240,7 +219,7 @@ + (NSMutableArray *) bsonArrayWithData:(NSData *) data
NuBSON *bsonObject = [[[NuBSON alloc] initWithBSON:bsonValue] autorelease];
[results addObject:bsonObject];
}
- bson_destroy(&bsonBuffer);
+ bson_destroy(&bsonBuffer);
return results;
}
@@ -310,7 +289,8 @@ - (NuBSON *) initWithList:(id) cell
return [self initWithBSON:b];
}
-- (void) dealloc {
+- (void) dealloc
+{
bson_destroy(&bsonValue);
[super dealloc];
}
@@ -398,6 +378,7 @@ id object_for_bson_iterator(bson_iterator it, BOOL expandChildren)
bson_iterator it2;
bson subobject;
+ char bintype;
switch(bson_iterator_type(&it)) {
case bson_eoo:
break;
@@ -428,9 +409,13 @@ id object_for_bson_iterator(bson_iterator it, BOOL expandChildren)
add_bson_to_object(it2, value, expandChildren);
break;
case bson_bindata:
+ bintype = bson_iterator_bin_type(&it);
value = [NSData
dataWithBytes:bson_iterator_bin_data(&it)
length:bson_iterator_bin_len(&it)];
+ if (bintype == 1) {
+ value = [NSKeyedUnarchiver unarchiveObjectWithData:value];
+ }
break;
case bson_undefined:
break;
@@ -517,6 +502,18 @@ - (NSArray *) allKeys
return result;
}
+- (int) count
+{
+ int count = 0;
+ bson_iterator it;
+ bson_iterator_init(&it, bsonValue.data);
+
+ while(bson_iterator_next(&it)) {
+ count++;
+ }
+ return count;
+}
+
- (id) objectForKey:(NSString *) key
{
bson_iterator it;
@@ -536,6 +533,14 @@ - (id) objectForKeyPath:(NSString *) keypath
return cursor;
}
+- (id) valueForKey:(NSString *) key {
+ return [self objectForKey:key];
+}
+
+- (id) valueForKeyPath:(NSString *)keypath {
+ return [self objectForKeyPath:keypath];
+}
+
@end
bson *bson_for_object(id object)
View
11 test/test_bson.nu
@@ -59,4 +59,13 @@
(assert_equal 123 (d id1))
(assert_equal 123 (d id2))
(assert_equal 456 (d id3))
- (assert_equal 456 (d id4))))))
+ (assert_equal 456 (d id4)))))
+
+ (- testCode is
+ (set code '(do (x) (+ x 1)))
+ (set bson (NuBSON bsonWithDictionary:(dict code:code)))
+ (set code2 ((bson dictionaryValue) code:))
+ (assert_equal code code2)
+ (assert_equal 2 (eval (list code2 1)))))
+
+

0 comments on commit 3a7307b

Please sign in to comment.
Something went wrong with that request. Please try again.