no message
This commit is contained in:
1 parent
de5bd21a43
commit
568ad5c409
262 files changed
+23226
-78
No files matched your search
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// PNObject.h
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 28/12/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface PNObject : NSObject
|
||||
|
||||
@property (nonatomic, strong) NSString *objID;
|
||||
@property (nonatomic, strong) NSDate *createdDate;
|
||||
@property (nonatomic, strong) NSDictionary *objectMapping;
|
||||
|
||||
- (instancetype) initWithJSON:(NSDictionary*) JSON;
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,334 @@
|
||||
//
|
||||
// PNObject.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 28/12/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNObject.h"
|
||||
#import <PNObjectProperty.h>
|
||||
#import <NSDate_Utils/NSDate+NSDate_Util.h>
|
||||
|
||||
@interface PNObject()
|
||||
|
||||
@property (nonatomic, strong) NSDictionary *JSON;
|
||||
|
||||
@property (nonatomic, strong) NSString *endPoint;
|
||||
|
||||
@end
|
||||
|
||||
@implementation PNObject
|
||||
|
||||
- (instancetype) init {
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
NSAssert(_objectMapping, @"You must create objectMapping");
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype) initWithJSON:(NSDictionary*) JSON {
|
||||
self = [self init];
|
||||
if (self) {
|
||||
_JSON = [[NSDictionary alloc] initWithDictionary:JSON];
|
||||
|
||||
[self populateObjectFromJSON:JSON];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)isStringNull:(NSString *)str
|
||||
{
|
||||
if(nil == str || NSNull.null == (id)str)
|
||||
return YES;
|
||||
else
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)isObjNull:(id)obj
|
||||
{
|
||||
if(nil == obj || NSNull.null == obj)
|
||||
return YES;
|
||||
else
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSDictionary *)reverseMapping
|
||||
{
|
||||
NSMutableDictionary *JSON = [NSMutableDictionary dictionary];
|
||||
|
||||
NSString *mappedJSONKey;
|
||||
NSString *mappedJSONType;
|
||||
|
||||
NSDictionary *properties = [PNObjectProperty propertiesForClass:self.class];
|
||||
|
||||
for (NSString* propertyName in _objectMapping) {
|
||||
id mappingValue = [_objectMapping objectForKey:propertyName];
|
||||
|
||||
if([mappingValue isKindOfClass:NSDictionary.class]) {
|
||||
mappedJSONKey = [mappingValue valueForKey:@"key"];
|
||||
mappedJSONType = [mappingValue valueForKey:@"type"];
|
||||
} else {
|
||||
mappedJSONKey = mappingValue;
|
||||
}
|
||||
|
||||
NSString *propertyType = [properties valueForKey:propertyName];
|
||||
|
||||
id value = [self valueForKey:propertyName];
|
||||
|
||||
// NSDate
|
||||
if([propertyType isEqualToString:@"NSDate"]) {
|
||||
|
||||
value = [NSDate stringFromDate:value];
|
||||
}
|
||||
// NSURL
|
||||
else if([propertyType isEqualToString:@"NSURL"]) {
|
||||
NSURL *url = value;
|
||||
value = [url absoluteString];
|
||||
}
|
||||
// NSArray, NSMutableArray
|
||||
else if([propertyType isEqualToString:@"NSArray"] ||
|
||||
[propertyType isEqualToString:@"NSMutableArray"]) {
|
||||
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id LLObject in value) {
|
||||
SEL selector = NSSelectorFromString(@"reverseMapping");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
|
||||
[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:LLObject];
|
||||
[invocation invoke];
|
||||
NSDictionary *returnValue;
|
||||
[invocation getReturnValue:&returnValue];
|
||||
|
||||
[arr addObject:returnValue];
|
||||
}
|
||||
|
||||
value = arr;
|
||||
|
||||
}
|
||||
// Other LLModel or an unidentified value
|
||||
else {
|
||||
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
|
||||
if(isPNObjectSubclass) {
|
||||
SEL selector = NSSelectorFromString(@"reverseMapping");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
|
||||
[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:value];
|
||||
[invocation invoke];
|
||||
NSDictionary *returnValue;
|
||||
[invocation getReturnValue:&returnValue];
|
||||
|
||||
value = returnValue;
|
||||
|
||||
}
|
||||
else {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[JSON setValue:value forKey:propertyName];
|
||||
}
|
||||
|
||||
_JSON = JSON;
|
||||
|
||||
return _JSON;
|
||||
}
|
||||
|
||||
- (void)populateObjectFromJSON:(id)JSON
|
||||
{
|
||||
|
||||
NSDictionary *properties = [PNObjectProperty propertiesForClass:self.class];
|
||||
|
||||
for(NSString *propertyName in properties) {
|
||||
|
||||
if([propertyName isEqualToString:@"mappingError"])
|
||||
continue;
|
||||
|
||||
NSString *mappedJSONKey;
|
||||
NSString *mappedJSONType;
|
||||
|
||||
NSString *propertyType = [properties valueForKey:propertyName];
|
||||
|
||||
id mappingValue = [_objectMapping valueForKey:propertyName];
|
||||
|
||||
if([mappingValue isKindOfClass:NSDictionary.class]) {
|
||||
mappedJSONKey = [mappingValue valueForKey:@"key"];
|
||||
mappedJSONType = [mappingValue valueForKey:@"type"];
|
||||
} else {
|
||||
mappedJSONKey = mappingValue;
|
||||
}
|
||||
|
||||
// Check if there is mapping for the property
|
||||
if([self isStringNull:mappedJSONKey]) {
|
||||
// No mapping so just continue
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Get JSON value for the mapped key
|
||||
id value = [JSON valueForKeyPath:mappedJSONKey];
|
||||
if([self isObjNull:value]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//NSLog(@"Looking for : %@ -- %@ -- %@", propertyType, mappedJSONKey, value);
|
||||
|
||||
|
||||
((void (^)())@{
|
||||
@"c" : ^{
|
||||
char val = [value charValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"d" : ^{
|
||||
double val = [value doubleValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"f" : ^{
|
||||
float val = [value floatValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"i" : ^{
|
||||
int val = [value intValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"l" : ^{
|
||||
long val = [value longValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"s" : ^{
|
||||
short val = [value shortValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"NSString" : ^{
|
||||
NSString *val = [NSString stringWithFormat:@"%@", value];
|
||||
[self setValue:val forKey:propertyName];
|
||||
},
|
||||
@"NSNumber" : ^{
|
||||
NSInteger val = [value integerValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"NSDate" : ^{
|
||||
NSString *str = [NSString stringWithFormat:@"%@", value];
|
||||
NSDate *val = [NSDate dateFromString:str];
|
||||
[self setValue:val forKey:propertyName];
|
||||
},
|
||||
@"NSArray" : ^{
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
|
||||
[arr addObject:val];
|
||||
}
|
||||
|
||||
[self setValue:arr forKey:propertyName];
|
||||
},
|
||||
@"NSMutableArray" : ^{
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
|
||||
[arr addObject:val];
|
||||
}
|
||||
|
||||
[self setValue:arr forKey:propertyName];
|
||||
}
|
||||
}[propertyType] ?: ^{
|
||||
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
|
||||
if(isPNObjectSubclass) {
|
||||
PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value];
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
else {
|
||||
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
|
||||
NSLog(@"%@",errorStr);
|
||||
}
|
||||
})();
|
||||
/*// char
|
||||
if([propertyType isEqualToString:@"c"]) {
|
||||
char val = [value charValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// double
|
||||
else if([propertyType isEqualToString:@"d"]) {
|
||||
double val = [value doubleValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// float
|
||||
else if([propertyType isEqualToString:@"f"]) {
|
||||
float val = [value floatValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// integer
|
||||
else if([propertyType isEqualToString:@"i"]) {
|
||||
int val = [value intValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// long
|
||||
else if([propertyType isEqualToString:@"l"]) {
|
||||
long val = [value longValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// short
|
||||
else if([propertyType isEqualToString:@"s"]) {
|
||||
short val = [value shortValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// NSString
|
||||
else if([propertyType isEqualToString:@"NSString"]) {
|
||||
NSString *val = [NSString stringWithFormat:@"%@", value];
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
// NSNumber
|
||||
else if([propertyType isEqualToString:@"NSNumber"]) {
|
||||
NSInteger val = [value integerValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
}
|
||||
// NSDate
|
||||
else if([propertyType isEqualToString:@"NSDate"]) {
|
||||
|
||||
|
||||
NSString *str = [NSString stringWithFormat:@"%@", value];
|
||||
NSDate *val = [NSDate dateFromString:str];
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
// NSURL
|
||||
else if([propertyType isEqualToString:@"NSURL"]) {
|
||||
NSString *str = [NSString stringWithFormat:@"%@", value];
|
||||
NSURL *val = [NSURL URLWithString:str];
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
// NSArray, NSMutableArray
|
||||
else if([propertyType isEqualToString:@"NSArray"] ||
|
||||
[propertyType isEqualToString:@"NSMutableArray"]) {
|
||||
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
|
||||
[arr addObject:val];
|
||||
}
|
||||
|
||||
[self setValue:arr forKey:propertyName];
|
||||
|
||||
}
|
||||
// Other LLModel or an unidentified value
|
||||
else {
|
||||
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
|
||||
if(isPNObjectSubclass) {
|
||||
PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value];
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
else {
|
||||
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
|
||||
NSLog(@"%@",errorStr);
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// PNObjectProperty.h
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 28/12/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface PNObjectProperty : NSObject
|
||||
|
||||
+ (NSDictionary *)propertiesForClass:(Class)PNObjClass;
|
||||
|
||||
@end
|
||||
Executable
+77
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// PNObjectProperty.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 28/12/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNObjectProperty.h"
|
||||
#import "objc/runtime.h"
|
||||
|
||||
@implementation PNObjectProperty
|
||||
|
||||
static BOOL property_getTypeString( objc_property_t property, char *buffer )
|
||||
{
|
||||
const char * attrs = property_getAttributes( property );
|
||||
if ( attrs == NULL )
|
||||
return NO;
|
||||
|
||||
const char * e = strchr( attrs, ',' );
|
||||
if ( e == NULL )
|
||||
return NO;
|
||||
|
||||
int len = (int)(e - attrs);
|
||||
memcpy( buffer, attrs, len );
|
||||
buffer[len] = '\0';
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+ (NSDictionary *)propertiesForClass:(Class)PNObjClass
|
||||
{
|
||||
if (PNObjClass == NULL) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableDictionary *results = [NSMutableDictionary dictionary];
|
||||
|
||||
unsigned int outCount, i;
|
||||
objc_property_t *properties = class_copyPropertyList(PNObjClass, &outCount);
|
||||
for (i = 0; i < outCount; i++) {
|
||||
objc_property_t property = properties[i];
|
||||
const char *propName = property_getName(property);
|
||||
if(propName) {
|
||||
//const char *propType = getPropertyType(property);
|
||||
char propType[256];
|
||||
if (!property_getTypeString(property, propType)) {
|
||||
continue;
|
||||
}
|
||||
NSString *propertyName = [NSString stringWithUTF8String:propName];
|
||||
NSString *propertyType = [NSString stringWithUTF8String:propType];
|
||||
|
||||
NSRange range = [propertyType rangeOfString:@"T@\""];
|
||||
NSRange range2 = [propertyType rangeOfString:@"T"];
|
||||
if (range.location != NSNotFound) {
|
||||
NSRange subStrRange = NSMakeRange(range.length, propertyType.length - (range.length + 1));
|
||||
propertyType = [propertyType substringWithRange:subStrRange];
|
||||
}
|
||||
else if (range2.location != NSNotFound) {
|
||||
NSRange subStrRange = NSMakeRange(range2.length, propertyType.length - (range2.length));
|
||||
propertyType = [propertyType substringWithRange:subStrRange];
|
||||
}
|
||||
|
||||
//NSLog(@"Prop type & name: %@ -- %@", propertyType, propertyName);
|
||||
|
||||
[results setObject:propertyType forKey:propertyName];
|
||||
}
|
||||
}
|
||||
free(properties);
|
||||
|
||||
// returning a copy here to make sure the dictionary is immutable
|
||||
return [NSDictionary dictionaryWithDictionary:results];
|
||||
}
|
||||
|
||||
@end
|
||||
Whitespace-only changes.
Reference in new issue
Block a user