Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 181 lines (157 sloc) 6.842 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
//
// SKRequestBuilder.m
// StackKit
/**
Copyright (c) 2011 Dave DeLong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/

#import "SKRequestBuilder.h"
#import "_SKConcreteRequestBuilder.h"
#import <objc/runtime.h>

@implementation SKRequestBuilder

+ (NSArray *) _requestBuilders {
static NSMutableArray * _requestBuilderClasses = nil;
if (_requestBuilderClasses == nil) {
//autodiscover all subclasses of "SKRequestBuilder"
_requestBuilderClasses = [[NSMutableArray alloc] init];

int numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0) {
Class * allClasses = malloc(numClasses * sizeof(Class));
numClasses = objc_getClassList(allClasses, numClasses);

for (int i = 0; i < numClasses; ++i) {
Class thisClass = allClasses[i];
/**
Why not use +isSubclassOfClass:?
Because one of the classes returned by objc_getClassList() is the NSZombie class,
and invoking *any* method on it will result in an NSZombie exception.
Instead, we have to walk the isa chain ourselves. Lame.
**/
if (sk_classIsSubclassOfClass(thisClass, [_SKConcreteRequestBuilder class])) {
[_requestBuilderClasses addObject:thisClass];
}
}
free(allClasses);
}
}
return _requestBuilderClasses;
}

+ (NSURL *) URLForFetchRequest:(SKFetchRequest *)fetchRequest error:(NSError **)error {
NSURL * url = nil;
NSError * buildError = nil;

NSMutableArray * builders = [[SKRequestBuilder _requestBuilders] mutableCopy];

[builders filterUsingPredicate:[NSPredicate predicateWithFormat:@"recognizedFetchEntity = %@", [fetchRequest entity]]];
if ([fetchRequest entity] == nil || [builders count] == 0) {
buildError = SK_ENTERROR(@"Unrecognized fetch entity: %@", NSStringFromClass([fetchRequest entity]));
goto errorExit;
}

if ([fetchRequest sortDescriptor] != nil) {
[builders filterUsingPredicate:[NSPredicate predicateWithFormat:@"recognizesASortDescriptor = YES"]];
[builders filterUsingPredicate:[NSPredicate predicateWithFormat:@"allRecognizedSortDescriptorKeys CONTAINS %@", [[fetchRequest sortDescriptor] key]]];
}
if ([builders count] == 0) {
buildError = SK_SORTERROR(@"Unrecognized sort key: %@", [[fetchRequest sortDescriptor] key]);
goto errorExit;
}

if ([fetchRequest predicate] != nil) {
if ([[fetchRequest predicate] isKindOfClass:[NSCompoundPredicate class]] && [[fetchRequest predicate] isSimpleAndPredicate] == NO) {
buildError = SK_PREDERROR(@"Only comparison predicates and AND predicates with no compound subpredicates are recognized");
goto errorExit;
}
//some endpoints do not recognize a predicate
NSPredicate * p = [NSPredicate predicateWithFormat:@"recognizesAPredicate = YES"];
[builders filterUsingPredicate:p];
if ([builders count] == 0) {
buildError = SK_PREDERROR(@"no possible builders are recognizers");
goto errorExit;
}

NSSet * leftKeyPaths = [[fetchRequest predicate] leftKeyPaths];

//the predicate must only use recognized keypaths
p = [NSPredicate predicateWithFormat:@"recognizedPredicateKeyPaths.@count == 0 OR ALL %@ IN recognizedPredicateKeyPaths.@allKeys", leftKeyPaths];
[builders filterUsingPredicate:p];
if ([builders count] == 0) {
buildError = SK_PREDERROR(@"predicate uses unrecognized keypaths");
goto errorExit;
}

//the predicate must have any required keypaths
p = [NSPredicate predicateWithFormat:@"ALL requiredPredicateKeyPaths IN %@", leftKeyPaths];
[builders filterUsingPredicate:p];
if ([builders count] == 0) {
buildError = SK_PREDERROR(@"predicate does not have all required keypaths");
goto errorExit;
}

//the predicate must only use certain operators for certain keypaths
p = [NSPredicate predicateWithFormat:@"FUNCTION(%@, 'sk_matchesRecognizedKeyPathsAndOperators:', SELF.recognizedPredicateKeyPaths) == YES", [fetchRequest predicate]];
[builders filterUsingPredicate:p];
if ([builders count] == 0) {
buildError = SK_PREDERROR(@"predicate uses invalid operators for keypaths");
goto errorExit;
}

} else {
//this request doesn't have a predicate
//therefore, keep only the builders that don't require any left keypaths
[builders filterUsingPredicate:[NSPredicate predicateWithFormat:@"requiredPredicateKeyPaths.@count == 0"]];
if ([builders count] == 0) {
buildError = SK_PREDERROR(@"no possible builders recognize an empty predicate");
goto errorExit;
}
}
if ([builders count] == 0) {
if (buildError == nil) {
buildError = SK_PREDERROR(@"Invalid predicate structure");
}
goto errorExit;
}

NSMutableArray * successfulBuilders = [NSMutableArray array];
NSMutableArray * failedBuilders = [NSMutableArray array];
for (Class builder in builders) {
_SKConcreteRequestBuilder * concreteBuilder = [[builder alloc] initWithFetchRequest:fetchRequest];
if ([concreteBuilder error] != nil) {
[failedBuilders addObject:concreteBuilder];
} else {
[successfulBuilders addObject:concreteBuilder];
}
[concreteBuilder release];
}

if ([successfulBuilders count] > 0) {
//something succeeded!
_SKConcreteRequestBuilder * successfulBuilder = [successfulBuilders objectAtIndex:0];
buildError = nil;
url = [successfulBuilder URL];

if ([successfulBuilders count] > 1) {
NSLog(@"multiple URLs built: %@", [successfulBuilders valueForKey:@"URL"]);
}
} else if ([failedBuilders count] > 0) {
//nothing succeeded and something failed
_SKConcreteRequestBuilder * failedBuilder = [failedBuilders objectAtIndex:0];
buildError = [failedBuilder error];
url = nil;
} else {
//nothing succeeded and nothing failed
//this should be impossible
NSLog(@"Impossible branch reached!");
}

errorExit:
[builders release];
if (buildError != nil) {
NSLog(@"build error: %@", buildError);
if (error != nil) {
*error = buildError;
}
}
return url;
}

@end
Something went wrong with that request. Please try again.