forked from loafle/openapi-generator-original
83 lines
2.8 KiB
Objective-C
83 lines
2.8 KiB
Objective-C
#import "SWGSanitizer.h"
|
|
#import "SWGObject.h"
|
|
#import "SWGQueryParamCollection.h"
|
|
#import <ISO8601/ISO8601.h>
|
|
|
|
@interface SWGSanitizer ()
|
|
|
|
@end
|
|
|
|
@implementation SWGSanitizer
|
|
|
|
- (id) sanitizeForSerialization:(id) object {
|
|
if (object == nil) {
|
|
return nil;
|
|
}
|
|
else if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[SWGQueryParamCollection class]]) {
|
|
return object;
|
|
}
|
|
else if ([object isKindOfClass:[NSDate class]]) {
|
|
return [object ISO8601String];
|
|
}
|
|
else if ([object isKindOfClass:[NSArray class]]) {
|
|
NSArray *objectArray = object;
|
|
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[objectArray count]];
|
|
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
|
id sanitizedObj = [self sanitizeForSerialization:obj];
|
|
if (sanitizedObj) {
|
|
[sanitizedObjs addObject:sanitizedObj];
|
|
}
|
|
}];
|
|
return sanitizedObjs;
|
|
}
|
|
else if ([object isKindOfClass:[NSDictionary class]]) {
|
|
NSDictionary *objectDict = object;
|
|
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[objectDict count]];
|
|
[object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
|
id sanitizedObj = [self sanitizeForSerialization:obj];
|
|
if (sanitizedObj) {
|
|
sanitizedObjs[key] = sanitizedObj;
|
|
}
|
|
}];
|
|
return sanitizedObjs;
|
|
}
|
|
else if ([object isKindOfClass:[SWGObject class]]) {
|
|
return [object toDictionary];
|
|
}
|
|
else {
|
|
NSException *e = [NSException
|
|
exceptionWithName:@"InvalidObjectArgumentException"
|
|
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object]
|
|
userInfo:nil];
|
|
@throw e;
|
|
}
|
|
}
|
|
|
|
- (NSString *) parameterToString:(id)param {
|
|
if ([param isKindOfClass:[NSString class]]) {
|
|
return param;
|
|
}
|
|
else if ([param isKindOfClass:[NSNumber class]]) {
|
|
return [param stringValue];
|
|
}
|
|
else if ([param isKindOfClass:[NSDate class]]) {
|
|
return [param ISO8601String];
|
|
}
|
|
else if ([param isKindOfClass:[NSArray class]]) {
|
|
NSMutableArray *mutableParam = [NSMutableArray array];
|
|
[param enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
|
[mutableParam addObject:[self parameterToString:obj]];
|
|
}];
|
|
return [mutableParam componentsJoinedByString:@","];
|
|
}
|
|
else {
|
|
NSException *e = [NSException
|
|
exceptionWithName:@"InvalidObjectArgumentException"
|
|
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", param]
|
|
userInfo:nil];
|
|
@throw e;
|
|
}
|
|
}
|
|
|
|
@end
|