oc: NGImap4Connection:fetchUids don't delete RawResponse

pull/69/head
Jesús García Sáez 2014-10-22 19:21:17 +02:00 committed by Julio García
parent 24e35103ff
commit c410a9fc3f
2 changed files with 80 additions and 27 deletions

View File

@ -24,12 +24,20 @@
#import <Foundation/NSArray.h> #import <Foundation/NSArray.h>
#import <NGImap4/NGImap4Client.h> #import <NGImap4/NGImap4Client.h>
#import <NGImap4/NGImap4Connection.h> #import <NGImap4/NGImap4Connection.h>
#import <NGExtensions/NGHashMap.h>
@interface NGImap4Connection (Monkeypatching) @interface NGImap4Connection (Monkeypatching)
- (NSArray *)fetchUIDs:(NSArray *)_uids inURL:(NSURL *)_url - (NSArray *) fetchUIDs: (NSArray *) _uids
parts:(NSArray *)_parts; inURL: (NSURL *) _url
parts: (NSArray *) _parts;
- (void) _mergeDict: (NSDictionary *) source
into: (NSMutableDictionary *) target;
- (void) _mergeNGHashMap: (NGMutableHashMap *) source
into: (NGMutableHashMap *) target;
@end @end

View File

@ -20,7 +20,7 @@
#import "NGImap4Connection+Monkeypatching.h" #import "NGImap4Connection+Monkeypatching.h"
#import <Foundation/NSObject.h> #import <Foundation/NSValue.h>
#import <Foundation/NSDictionary.h> #import <Foundation/NSDictionary.h>
#import <NGExtensions/NSObject+Logs.h> #import <NGExtensions/NSObject+Logs.h>
@ -28,8 +28,9 @@
@implementation NGImap4Connection (Monkeypatching) @implementation NGImap4Connection (Monkeypatching)
- (NSArray *)fetchUIDs:(NSArray *)_uids inURL:(NSURL *)_url - (NSArray *) fetchUIDs: (NSArray *) _uids
parts:(NSArray *)_parts inURL: (NSURL *) _url
parts: (NSArray *) _parts
{ {
// currently returns a dict?! // currently returns a dict?!
/* /*
@ -72,37 +73,81 @@
partial_result = [[self client] fetchUids:partial_uids parts:_parts]; partial_result = [[self client] fetchUids:partial_uids parts:_parts];
if (![[partial_result valueForKey:@"result"] boolValue]) { if (![[partial_result valueForKey:@"result"] boolValue]) {
[self errorWithFormat: @"could not fetch %d uids for url: %@", [_uids count], _url]; [self errorWithFormat: @"could not fetch %d uids for url: %@",
[_uids count], _url];
return nil; return nil;
} }
if (!result) { if (result == nil) {
/* First iteration, first result */ /* First iteration, first result */
result = [[partial_result mutableCopy] autorelease]; result = [[partial_result mutableCopy] autorelease];
/* RawResponse has already been processed, ignore it */ } else {
[result removeObjectForKey: @"RawResponse"]; /* Merge partial_result into previous result */
continue; [self _mergeDict: partial_result into: result];
}
/* Merge partial_result into previous result */
for (id key in [partial_result keyEnumerator]) {
id obj, current_obj;
current_obj = [result objectForKey: key];
if (!current_obj) continue;
obj = [partial_result objectForKey: key];
if ([obj isKindOfClass: [NSArray class]]) {
NSArray *data, *current_data, *new_data;
data = obj;
current_data = current_obj;
new_data = [current_data arrayByAddingObjectsFromArray: data];
[result setObject: new_data forKey: key];
}
} }
} }
return (id)result; return (id)result;
} }
- (void) _mergeDict: (NSDictionary *) source
into: (NSMutableDictionary *) target
{
for (id key in [source keyEnumerator]) {
id obj, current_obj;
current_obj = [target objectForKey: key];
if (current_obj == nil) {
/* This should never happen but just in case... */
[self errorWithFormat: @"Error while merging results: nonexistent key "
@"%@ on current target", key];
continue;
}
obj = [source objectForKey: key];
if ([obj isKindOfClass: [NSArray class]]) {
NSArray *data, *current_data, *new_data;
data = obj;
current_data = current_obj;
new_data = [current_data arrayByAddingObjectsFromArray: data];
[target setObject: new_data forKey: key];
} else if ([obj isKindOfClass: [NGMutableHashMap class]]) {
[self _mergeNGHashMap: obj into: current_obj];
} else if ([obj isKindOfClass: [NSNumber class]]) {
if (obj != current_obj) {
[self errorWithFormat: @"While fetching uids problem happened "
@"merging results for key %@: %@ != %@",
key, obj, current_obj];
}
} else {
[self errorWithFormat: @"While fetching uids and mergin results ignored "
@"%@ (%@) key", key, [key class]];
}
}
}
- (void) _mergeNGHashMap: (NGMutableHashMap *) source
into: (NGMutableHashMap *) target
{
for (id key in [source keyEnumerator]) {
NSArray *obj, *current_obj;
current_obj = [target objectsForKey: key];
if (current_obj == nil) {
/* This should never happen but just in case... */
[self errorWithFormat: @"Error while merging results: nonexistent key "
@"%@ on current target", key];
continue;
}
if ([current_obj count] == 1) {
/* Merge only results, that means fields with more than 1 object */
continue;
}
obj = [source objectsForKey: key];
[target addObjects: obj forKey: key];
}
}
@end @end