12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- #include <objc/runtime.h>
- #include <TargetConditionals.h>
- #import "GTLRRuntimeCommon.h"
- #import "GTLRDateTime.h"
- #import "GTLRDuration.h"
- #import "GTLRUtilities.h"
- #import "GTLRDefines.h"
- #ifndef GTLR_CLASSNAME_CSTR
- #define _GTLR_CLASSNAME_HELPER(x) #x
- #define GTLR_CLASSNAME_CSTR(x) _GTLR_CLASSNAME_HELPER(x)
- #endif
- @implementation GTLRRuntimeCommon
- + (id)objectFromJSON:(id)json
- defaultClass:(Class)defaultClass
- objectClassResolver:(id<GTLRObjectClassResolver>)objectClassResolver
- isCacheable:(BOOL*)isCacheable {
- id result = nil;
- BOOL canBeCached = YES;
-
-
- if ([json isKindOfClass:[NSDictionary class]]) {
-
-
- if ((defaultClass == Nil) || [defaultClass isEqual:[NSObject class]]) {
- defaultClass = [GTLRObject class];
- }
- result = [GTLRObject objectForJSON:json
- defaultClass:defaultClass
- objectClassResolver:objectClassResolver];
- } else if ([json isKindOfClass:[NSArray class]]) {
- NSArray *jsonArray = json;
-
- NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:jsonArray.count];
- for (id jsonItem in jsonArray) {
- id item = [self objectFromJSON:jsonItem
- defaultClass:defaultClass
- objectClassResolver:objectClassResolver
- isCacheable:NULL];
- [resultArray addObject:item];
- }
- result = resultArray;
- } else if ([json isKindOfClass:[NSString class]]) {
-
- if ([defaultClass isEqual:[GTLRDateTime class]]) {
- result = [GTLRDateTime dateTimeWithRFC3339String:json];
- } else if ([defaultClass isEqual:[GTLRDuration class]]) {
- result = [GTLRDuration durationWithJSONString:json];
- } else if ([defaultClass isEqual:[NSNumber class]]) {
- result = GTLR_EnsureNSNumber(json);
- canBeCached = NO;
- } else {
- result = json;
- canBeCached = NO;
- }
- } else if ([json isKindOfClass:[NSNumber class]] ||
- [json isKindOfClass:[NSNull class]]) {
- result = json;
- canBeCached = NO;
- } else {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: unsupported class '%s' in objectFromJSON",
- class_getName([json class]));
- }
- if (isCacheable) {
- *isCacheable = canBeCached;
- }
- return result;
- }
- + (id)jsonFromAPIObject:(id)obj
- expectedClass:(Class)expectedClass
- isCacheable:(BOOL *)isCacheable {
- id result = nil;
- BOOL canBeCached = YES;
- BOOL checkExpected = (expectedClass != Nil);
- if ([obj isKindOfClass:[NSString class]]) {
- result = [obj copy];
- canBeCached = NO;
- } else if ([obj isKindOfClass:[NSNumber class]] ||
- [obj isKindOfClass:[NSNull class]]) {
- result = obj;
- canBeCached = NO;
- } else if ([obj isKindOfClass:[GTLRObject class]]) {
- result = [(GTLRObject *)obj JSON];
- if (result == nil) {
-
-
- [(GTLRObject *)obj setJSON:[NSMutableDictionary dictionary]];
- result = [(GTLRObject *)obj JSON];
- }
- } else if ([obj isKindOfClass:[NSArray class]]) {
- checkExpected = NO;
- NSArray *array = obj;
-
- NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:array.count];
- for (id item in array) {
- id itemJSON = [self jsonFromAPIObject:item
- expectedClass:expectedClass
- isCacheable:NULL];
- [resultArray addObject:itemJSON];
- }
- result = resultArray;
- } else if ([obj isKindOfClass:[GTLRDateTime class]]) {
-
- GTLRDateTime *dateTime = obj;
- result = dateTime.RFC3339String;
- } else if ([obj isKindOfClass:[GTLRDuration class]]) {
-
- GTLRDuration *duration = obj;
- result = duration.jsonString;
- } else {
- checkExpected = NO;
- if (obj) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: unsupported class '%s' in jsonFromAPIObject",
- class_getName([obj class]));
- }
- }
- if (checkExpected) {
-
- if ([expectedClass isEqual:[NSObject class]] ||
- [obj isKindOfClass:[NSNull class]]) {
- expectedClass = nil;
- }
- if (expectedClass && ![obj isKindOfClass:expectedClass]) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: jsonFromAPIObject expected class '%s' instead got '%s'",
- class_getName(expectedClass), class_getName([obj class]));
- }
- }
- if (isCacheable) {
- *isCacheable = canBeCached;
- }
- return result;
- }
- + (NSDictionary *)mergedClassDictionaryForSelector:(SEL)selector
- startClass:(Class)startClass
- ancestorClass:(Class)ancestorClass
- cache:(NSMutableDictionary *)cache {
- NSDictionary *result;
- @synchronized(cache) {
- result = [cache objectForKey:startClass];
- if (result == nil) {
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- NSDictionary *classDict = [startClass performSelector:selector];
- #pragma clang diagnostic pop
-
- NSDictionary *parentClassMergedDict;
- if ([startClass isEqual:ancestorClass]) {
- parentClassMergedDict = nil;
- } else {
- Class parentClass = class_getSuperclass(startClass);
- parentClassMergedDict =
- [self mergedClassDictionaryForSelector:selector
- startClass:parentClass
- ancestorClass:ancestorClass
- cache:cache];
- }
-
- NSMutableDictionary *mergeDict;
- if (parentClassMergedDict != nil) {
- mergeDict =
- [NSMutableDictionary dictionaryWithDictionary:parentClassMergedDict];
- } else {
- mergeDict = [NSMutableDictionary dictionary];
- }
- if (classDict != nil) {
- [mergeDict addEntriesFromDictionary:classDict];
- }
-
- result = [NSDictionary dictionaryWithDictionary:mergeDict];
-
- [cache setObject:result forKey:(id<NSCopying>)startClass];
- }
- }
- return result;
- }
- #pragma mark Runtime lookup support
- static objc_property_t PropertyForSel(Class<GTLRRuntimeCommon> startClass,
- SEL sel, BOOL isSetter,
- Class<GTLRRuntimeCommon> *outFoundClass) {
- const char *selName = sel_getName(sel);
- const char *baseName = selName;
- size_t baseNameLen = strlen(baseName);
- if (isSetter) {
- baseName += 3;
- baseNameLen -= 4;
- }
-
- Class<GTLRRuntimeCommon> topClass = class_getSuperclass([startClass ancestorClass]);
- for (Class currClass = startClass;
- currClass != topClass;
- currClass = class_getSuperclass(currClass)) {
-
- objc_property_t foundProp = NULL;
- objc_property_t *properties = class_copyPropertyList(currClass, NULL);
- if (properties) {
- for (objc_property_t *prop = properties; *prop != NULL; ++prop) {
- const char *propAttrs = property_getAttributes(*prop);
- const char *dynamicMarker = strstr(propAttrs, ",D");
- if (!dynamicMarker ||
- (dynamicMarker[2] != 0 && dynamicMarker[2] != ',' )) {
-
- continue;
- }
- if (!isSetter) {
-
-
- const char *getterMarker = strstr(propAttrs, ",G");
- if (getterMarker) {
- const char *getterStart = getterMarker + 2;
- const char *getterEnd = getterStart;
- while ((*getterEnd != 0) && (*getterEnd != ',')) {
- ++getterEnd;
- }
- size_t getterLen = (size_t)(getterEnd - getterStart);
- if ((strncmp(selName, getterStart, getterLen) == 0)
- && (selName[getterLen] == 0)) {
-
- foundProp = *prop;
-
- if (outFoundClass) *outFoundClass = currClass;
- break;
- }
- }
- }
-
-
- const char *propName = property_getName(*prop);
- size_t propNameLen = strlen(propName);
- if (baseNameLen == propNameLen
- && strncasecmp(baseName, propName, 1) == 0
- && (baseNameLen <= 1
- || strncmp(baseName + 1, propName + 1, baseNameLen - 1) == 0)) {
-
- foundProp = *prop;
-
- if (outFoundClass) *outFoundClass = currClass;
- break;
- }
- }
- free(properties);
- }
- if (foundProp) return foundProp;
- }
-
-
- return NULL;
- }
- typedef NS_ENUM(NSUInteger, GTLRPropertyType) {
- #if !defined(__LP64__) || !__LP64__
-
- GTLRPropertyTypeInt32 = 1,
- GTLRPropertyTypeUInt32,
- #endif
- GTLRPropertyTypeLongLong = 3,
- GTLRPropertyTypeULongLong,
- GTLRPropertyTypeFloat,
- GTLRPropertyTypeDouble,
- GTLRPropertyTypeBool,
- GTLRPropertyTypeNSString,
- GTLRPropertyTypeNSNumber,
- GTLRPropertyTypeGTLRDateTime,
- GTLRPropertyTypeGTLRDuration,
- GTLRPropertyTypeNSArray,
- GTLRPropertyTypeNSObject,
- GTLRPropertyTypeGTLRObject,
- };
- typedef struct {
- const char *attributePrefix;
- GTLRPropertyType propertyType;
- const char *setterEncoding;
- const char *getterEncoding;
-
-
-
- const char *returnClassName;
- Class returnClass;
- BOOL extractReturnClass;
- } GTLRDynamicImpInfo;
- static const GTLRDynamicImpInfo *DynamicImpInfoForProperty(objc_property_t prop,
- Class *outReturnClass) {
- if (outReturnClass) *outReturnClass = nil;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static GTLRDynamicImpInfo kImplInfo[] = {
- #if !defined(__LP64__) || !__LP64__
- {
- "Ti",
- GTLRPropertyTypeInt32,
- "v@:i",
- "i@:",
- nil, nil,
- NO
- },
- {
- "TI",
- GTLRPropertyTypeUInt32,
- "v@:I",
- "I@:",
- nil, nil,
- NO
- },
- #endif
- {
- "Tq",
- GTLRPropertyTypeLongLong,
- "v@:q",
- "q@:",
- nil, nil,
- NO
- },
- {
- "TQ",
- GTLRPropertyTypeULongLong,
- "v@:Q",
- "Q@:",
- nil, nil,
- NO
- },
- {
- "Tf",
- GTLRPropertyTypeFloat,
- "v@:f",
- "f@:",
- nil, nil,
- NO
- },
- {
- "Td",
- GTLRPropertyTypeDouble,
- "v@:d",
- "d@:",
- nil, nil,
- NO
- },
- #if defined(OBJC_BOOL_IS_BOOL) && OBJC_BOOL_IS_BOOL
- {
- "TB",
- GTLRPropertyTypeBool,
- "v@:B",
- "B@:",
- nil, nil,
- NO
- },
- #elif defined(OBJC_BOOL_IS_CHAR) && OBJC_BOOL_IS_CHAR
- {
- "Tc",
- GTLRPropertyTypeBool,
- "v@:c",
- "c@:",
- nil, nil,
- NO
- },
- #else
- #error unknown definition for ObjC BOOL type
- #endif
- {
- "T@\"NSString\"",
- GTLRPropertyTypeNSString,
- "v@:@",
- "@@:",
- "NSString", nil,
- NO
- },
- {
- "T@\"NSNumber\"",
- GTLRPropertyTypeNSNumber,
- "v@:@",
- "@@:",
- "NSNumber", nil,
- NO
- },
- {
- "T@\"" GTLR_CLASSNAME_CSTR(GTLRDateTime) "\"",
- GTLRPropertyTypeGTLRDateTime,
- "v@:@",
- "@@:",
- GTLR_CLASSNAME_CSTR(GTLRDateTime), nil,
- NO
- },
- {
- "T@\"" GTLR_CLASSNAME_CSTR(GTLRDuration) "\"",
- GTLRPropertyTypeGTLRDuration,
- "v@:@",
- "@@:",
- GTLR_CLASSNAME_CSTR(GTLRDuration), nil,
- NO
- },
- {
- "T@\"NSArray\"",
- GTLRPropertyTypeNSArray,
- "v@:@",
- "@@:",
- "NSArray", nil,
- NO
- },
- {
- "T@,",
- GTLRPropertyTypeNSObject,
- "v@:@",
- "@@:",
- "NSObject", nil,
- NO
- },
- {
- "T@\"",
- GTLRPropertyTypeGTLRObject,
- "v@:@",
- "@@:",
- nil, nil,
- YES
- },
- };
- static BOOL hasLookedUpClasses = NO;
- if (!hasLookedUpClasses) {
-
-
- hasLookedUpClasses = YES;
- for (uint32_t idx = 0; idx < sizeof(kImplInfo)/sizeof(kImplInfo[0]); ++idx) {
- if (kImplInfo[idx].returnClassName) {
- kImplInfo[idx].returnClass = objc_getClass(kImplInfo[idx].returnClassName);
- NSCAssert1(kImplInfo[idx].returnClass != nil,
- @"GTLRRuntimeCommon: class lookup failed: %s", kImplInfo[idx].returnClassName);
- }
- }
- }
- const char *attr = property_getAttributes(prop);
- const char *dynamicMarker = strstr(attr, ",D");
- if (!dynamicMarker ||
- (dynamicMarker[2] != 0 && dynamicMarker[2] != ',' )) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: property %s isn't dynamic, attributes %s",
- property_getName(prop), attr ? attr : "(nil)");
- return NULL;
- }
- const GTLRDynamicImpInfo *result = NULL;
-
- for (uint32_t idx = 0; idx < sizeof(kImplInfo)/sizeof(kImplInfo[0]); ++idx) {
- const char *attributePrefix = kImplInfo[idx].attributePrefix;
- if (strncmp(attr, attributePrefix, strlen(attributePrefix)) == 0) {
- result = &kImplInfo[idx];
- if (outReturnClass) *outReturnClass = result->returnClass;
- break;
- }
- }
- if (result == NULL) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: unexpected attributes %s for property %s",
- attr ? attr : "(nil)", property_getName(prop));
- return NULL;
- }
- if (result->extractReturnClass && outReturnClass) {
-
- char *attrCopy = strdup(attr);
- char *classNameStart = attrCopy + 3;
- char *classNameEnd = strstr(classNameStart, "\"");
- if (classNameEnd) {
- *classNameEnd = '\0';
-
- *outReturnClass = objc_getClass(classNameStart);
- if (*outReturnClass == nil) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: did not find class with name \"%s\" "
- @"for property \"%s\" with attributes \"%s\"",
- classNameStart, property_getName(prop), attr);
- }
- } else {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: Failed to find end of class name for "
- @"property \"%s\" with attributes \"%s\"",
- property_getName(prop), attr);
- }
- free(attrCopy);
- }
- return result;
- }
- static IMP GTLRRuntimeGetterIMP(SEL sel,
- GTLRPropertyType propertyType,
- NSString *jsonKey,
- Class containedClass,
- Class returnClass) {
-
- #pragma unused(sel)
- IMP resultIMP;
- switch (propertyType) {
- #if !defined(__LP64__) || !__LP64__
- case GTLRPropertyTypeInt32: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- NSInteger result = num.integerValue;
- return result;
- });
- break;
- }
- case GTLRPropertyTypeUInt32: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- NSUInteger result = num.unsignedIntegerValue;
- return result;
- });
- break;
- }
- #endif // __LP64__
- case GTLRPropertyTypeLongLong: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- long long result = num.longLongValue;
- return result;
- });
- break;
- }
- case GTLRPropertyTypeULongLong: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- unsigned long long result = num.unsignedLongLongValue;
- return result;
- });
- break;
- }
- case GTLRPropertyTypeFloat: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- float result = num.floatValue;
- return result;
- });
- break;
- }
- case GTLRPropertyTypeDouble: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- double result = num.doubleValue;
- return result;
- });
- break;
- }
- case GTLRPropertyTypeBool: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- BOOL flag = num.boolValue;
- return flag;
- });
- break;
- }
- case GTLRPropertyTypeNSString: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSString *str = [obj JSONValueForKey:jsonKey];
- return str;
- });
- break;
- }
- case GTLRPropertyTypeGTLRDateTime: {
- resultIMP = imp_implementationWithBlock(^GTLRDateTime *(GTLRObject<GTLRRuntimeCommon> *obj) {
-
- GTLRDateTime *cachedDateTime = [obj cacheChildForKey:jsonKey];
- if (cachedDateTime != nil) {
- return cachedDateTime;
- }
- NSString *str = [obj JSONValueForKey:jsonKey];
- id cacheValue, resultValue;
- if (![str isKindOfClass:[NSNull class]]) {
- GTLRDateTime *dateTime = [GTLRDateTime dateTimeWithRFC3339String:str];
- cacheValue = dateTime;
- resultValue = dateTime;
- } else {
- cacheValue = nil;
- resultValue = [NSNull null];
- }
- [obj setCacheChild:cacheValue forKey:jsonKey];
- return resultValue;
- });
- break;
- }
- case GTLRPropertyTypeGTLRDuration: {
- resultIMP = imp_implementationWithBlock(^GTLRDuration *(GTLRObject<GTLRRuntimeCommon> *obj) {
-
- GTLRDuration *cachedDuration = [obj cacheChildForKey:jsonKey];
- if (cachedDuration != nil) {
- return cachedDuration;
- }
- NSString *str = [obj JSONValueForKey:jsonKey];
- id cacheValue, resultValue;
- if (![str isKindOfClass:[NSNull class]]) {
- GTLRDuration *duration = [GTLRDuration durationWithJSONString:str];
- cacheValue = duration;
- resultValue = duration;
- } else {
- cacheValue = nil;
- resultValue = [NSNull null];
- }
- [obj setCacheChild:cacheValue forKey:jsonKey];
- return resultValue;
- });
- break;
- }
- case GTLRPropertyTypeNSNumber: {
- resultIMP = imp_implementationWithBlock(^(id obj) {
- NSNumber *num = [obj JSONValueForKey:jsonKey];
- num = GTLR_EnsureNSNumber(num);
- return num;
- });
- break;
- }
- case GTLRPropertyTypeGTLRObject: {
-
- if (returnClass == Nil) {
- returnClass = [GTLRObject class];
- }
- resultIMP = imp_implementationWithBlock(^GTLRObject *(GTLRObject<GTLRRuntimeCommon> *obj) {
-
- GTLRObject *cachedObj = [obj cacheChildForKey:jsonKey];
- if (cachedObj != nil) {
- return cachedObj;
- }
- NSMutableDictionary *dict = [obj JSONValueForKey:jsonKey];
- if ([dict isKindOfClass:[NSMutableDictionary class]]) {
- id<GTLRObjectClassResolver>objectClassResolver = [obj objectClassResolver];
- GTLRObject *subObj = [GTLRObject objectForJSON:dict
- defaultClass:returnClass
- objectClassResolver:objectClassResolver];
- [obj setCacheChild:subObj forKey:jsonKey];
- return subObj;
- } else if ([dict isKindOfClass:[NSNull class]]) {
- [obj setCacheChild:nil forKey:jsonKey];
- return (GTLRObject*)[NSNull null];
- } else if (dict != nil) {
-
- GTLR_DEBUG_LOG(@"GTLRObject: unexpected JSON: %@.%@ should be a dictionary, actually is a %@:\n%@",
- NSStringFromClass([obj class]),
- NSStringFromSelector(sel),
- NSStringFromClass([dict class]), dict);
- return (GTLRObject *)dict;
- }
- return nil;
- });
- break;
- }
- case GTLRPropertyTypeNSArray: {
- resultIMP = imp_implementationWithBlock(^(GTLRObject<GTLRRuntimeCommon> *obj) {
-
- NSMutableArray *cachedArray = [obj cacheChildForKey:jsonKey];
- if (cachedArray != nil) {
- return cachedArray;
- }
- NSMutableArray *result = nil;
- NSArray *array = [obj JSONValueForKey:jsonKey];
- if (array != nil) {
- if ([array isKindOfClass:[NSArray class]]) {
- id<GTLRObjectClassResolver>objectClassResolver = [obj objectClassResolver];
- result = [GTLRRuntimeCommon objectFromJSON:array
- defaultClass:containedClass
- objectClassResolver:objectClassResolver
- isCacheable:NULL];
- } else {
- #if DEBUG
- if (![array isKindOfClass:[NSNull class]]) {
- GTLR_DEBUG_LOG(@"GTLRObject: unexpected JSON: %@.%@ should be an array, actually is a %@:\n%@",
- NSStringFromClass([obj class]),
- NSStringFromSelector(sel),
- NSStringFromClass([array class]), array);
- }
- #endif
- result = (NSMutableArray *)array;
- }
- }
- [obj setCacheChild:result forKey:jsonKey];
- return result;
- });
- break;
- }
- case GTLRPropertyTypeNSObject: {
- resultIMP = imp_implementationWithBlock(^id(GTLRObject<GTLRRuntimeCommon> *obj) {
-
- id cachedObj = [obj cacheChildForKey:jsonKey];
- if (cachedObj != nil) {
- return cachedObj;
- }
- id jsonObj = [obj JSONValueForKey:jsonKey];
- if (jsonObj != nil) {
- BOOL shouldCache = NO;
- id<GTLRObjectClassResolver>objectClassResolver = [obj objectClassResolver];
- id result = [GTLRRuntimeCommon objectFromJSON:jsonObj
- defaultClass:nil
- objectClassResolver:objectClassResolver
- isCacheable:&shouldCache];
- [obj setCacheChild:(shouldCache ? result : nil)
- forKey:jsonKey];
- return result;
- }
- return nil;
- });
- break;
- }
- }
- return resultIMP;
- }
- static IMP GTLRRuntimeSetterIMP(SEL sel,
- GTLRPropertyType propertyType,
- NSString *jsonKey,
- Class containedClass,
- Class returnClass) {
- #pragma unused(sel, returnClass)
- IMP resultIMP;
- switch (propertyType) {
- #if !defined(__LP64__) || !__LP64__
- case GTLRPropertyTypeInt32: {
- resultIMP = imp_implementationWithBlock(^(id obj, NSInteger val) {
- [obj setJSONValue:@(val) forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeUInt32: {
- resultIMP = imp_implementationWithBlock(^(id obj, NSUInteger val) {
- [obj setJSONValue:@(val) forKey:jsonKey];
- });
- break;
- }
- #endif // __LP64__
- case GTLRPropertyTypeLongLong: {
- resultIMP = imp_implementationWithBlock(^(id obj, long long val) {
- [obj setJSONValue:@(val) forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeULongLong: {
- resultIMP = imp_implementationWithBlock(^(id obj,
- unsigned long long val) {
- [obj setJSONValue:@(val) forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeFloat: {
- resultIMP = imp_implementationWithBlock(^(id obj, float val) {
- [obj setJSONValue:@(val) forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeDouble: {
- resultIMP = imp_implementationWithBlock(^(id obj, double val) {
- [obj setJSONValue:@(val) forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeBool: {
- resultIMP = imp_implementationWithBlock(^(id obj, BOOL val) {
- NSNumber *numValue = (NSNumber *)(val ? kCFBooleanTrue : kCFBooleanFalse);
- [obj setJSONValue:numValue forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeNSString: {
- resultIMP = imp_implementationWithBlock(^(id obj, NSString *val) {
- NSString *copiedStr = [val copy];
- [obj setJSONValue:copiedStr forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeGTLRDateTime: {
- resultIMP = imp_implementationWithBlock(^(GTLRObject<GTLRRuntimeCommon> *obj,
- GTLRDateTime *val) {
- id cacheValue, jsonValue;
- if (![val isKindOfClass:[NSNull class]]) {
- jsonValue = val.RFC3339String;
- cacheValue = val;
- } else {
- jsonValue = [NSNull null];
- cacheValue = nil;
- }
- [obj setJSONValue:jsonValue forKey:jsonKey];
- [obj setCacheChild:cacheValue forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeGTLRDuration: {
- resultIMP = imp_implementationWithBlock(^(GTLRObject<GTLRRuntimeCommon> *obj,
- GTLRDuration *val) {
- id cacheValue, jsonValue;
- if (![val isKindOfClass:[NSNull class]]) {
- jsonValue = val.jsonString;
- cacheValue = val;
- } else {
- jsonValue = [NSNull null];
- cacheValue = nil;
- }
- [obj setJSONValue:jsonValue forKey:jsonKey];
- [obj setCacheChild:cacheValue forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeNSNumber: {
- resultIMP = imp_implementationWithBlock(^(id obj, NSNumber *val) {
- [obj setJSONValue:val forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeGTLRObject: {
- resultIMP = imp_implementationWithBlock(^(GTLRObject<GTLRRuntimeCommon> *obj,
- GTLRObject *val) {
- id cacheValue, jsonValue;
- if (![val isKindOfClass:[NSNull class]]) {
- NSMutableDictionary *dict = [val JSON];
- if (dict == nil && val != nil) {
-
-
- val.JSON = [NSMutableDictionary dictionary];
- jsonValue = val.JSON;
- } else {
- jsonValue = dict;
- }
- cacheValue = val;
- } else {
- jsonValue = [NSNull null];
- cacheValue = nil;
- }
- [obj setJSONValue:jsonValue forKey:jsonKey];
- [obj setCacheChild:cacheValue forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeNSArray: {
- resultIMP = imp_implementationWithBlock(^(GTLRObject<GTLRRuntimeCommon> *obj,
- NSMutableArray *val) {
- id json = [GTLRRuntimeCommon jsonFromAPIObject:val
- expectedClass:containedClass
- isCacheable:NULL];
- [obj setJSONValue:json forKey:jsonKey];
- [obj setCacheChild:val forKey:jsonKey];
- });
- break;
- }
- case GTLRPropertyTypeNSObject: {
- resultIMP = imp_implementationWithBlock(^(GTLRObject<GTLRRuntimeCommon> *obj,
- id val) {
- BOOL shouldCache = NO;
- id json = [GTLRRuntimeCommon jsonFromAPIObject:val
- expectedClass:Nil
- isCacheable:&shouldCache];
- [obj setJSONValue:json forKey:jsonKey];
- [obj setCacheChild:(shouldCache ? val : nil)
- forKey:jsonKey];
- });
- break;
- }
- }
- return resultIMP;
- }
- #pragma mark Runtime - wiring point
- + (BOOL)resolveInstanceMethod:(SEL)sel onClass:(Class<GTLRRuntimeCommon>)onClass {
-
-
-
-
-
- const char *selName = sel_getName(sel);
- size_t selNameLen = strlen(selName);
- char lastChar = selName[selNameLen - 1];
- BOOL isSetter = (lastChar == ':');
-
- Class<GTLRRuntimeCommon> foundClass = nil;
- objc_property_t prop = PropertyForSel(onClass, sel, isSetter, &foundClass);
- if (prop == NULL || foundClass == nil) {
- return NO;
- }
- Class returnClass = nil;
- const GTLRDynamicImpInfo *implInfo = DynamicImpInfoForProperty(prop,
- &returnClass);
- if (implInfo == NULL) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: unexpected return type class %s for "
- @"property \"%s\" of class \"%s\"",
- returnClass ? class_getName(returnClass) : "<nil>",
- property_getName(prop),
- class_getName(onClass));
- return NO;
- }
- const char *propName = property_getName(prop);
- NSString *propStr = @(propName);
-
-
-
-
-
-
- NSDictionary *keyMap =
- [[foundClass ancestorClass] propertyToJSONKeyMapForClass:foundClass];
- NSString *jsonKey = [keyMap objectForKey:propStr];
- if (jsonKey == nil) {
- jsonKey = propStr;
- }
-
- Class containedClass = nil;
- if (implInfo->propertyType == GTLRPropertyTypeNSArray) {
- NSDictionary *classMap =
- [[foundClass ancestorClass] arrayPropertyToClassMapForClass:foundClass];
- containedClass = [classMap objectForKey:jsonKey];
- if (containedClass == Nil) {
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: expected array item class for "
- @"property \"%s\" of class \"%s\"",
- property_getName(prop), class_getName(foundClass));
- }
- }
-
- IMP imp;
- const char *encoding;
- if (isSetter) {
- imp = GTLRRuntimeSetterIMP(sel, implInfo->propertyType,
- jsonKey, containedClass, returnClass);
- encoding = implInfo->setterEncoding;
- } else {
- imp = GTLRRuntimeGetterIMP(sel, implInfo->propertyType,
- jsonKey, containedClass, returnClass);
- encoding = implInfo->getterEncoding;
- }
- if (class_addMethod(foundClass, sel, imp, encoding)) {
- return YES;
- }
-
- GTLR_DEBUG_LOG(@"GTLRRuntimeCommon: Failed to wire %@ on %@ (encoding: %s).",
- NSStringFromSelector(sel),
- NSStringFromClass(foundClass),
- encoding);
- return NO;
- }
- @end
|