-
Notifications
You must be signed in to change notification settings - Fork 38
Add support for Buffer and GeoPoint data types #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,9 @@ | |
| * @copyright (c) 2013 StrongLoop. All rights reserved. | ||
| */ | ||
|
|
||
| #import "LBModel.h" | ||
|
|
||
| #import <objc/runtime.h> | ||
|
|
||
| #define NSSelectorForSetter(key) NSSelectorFromString([NSString stringWithFormat:@"set%@:", [key stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[key substringToIndex:1] capitalizedString]]]) | ||
| #import "LBModel.h" | ||
|
|
||
|
|
||
| @interface LBModel() { | ||
|
|
@@ -20,7 +18,6 @@ - (NSMutableDictionary *)_overflow; | |
|
|
||
| @end | ||
|
|
||
| static NSDateFormatter *jsonDateFormatter = nil; | ||
|
|
||
| @implementation LBModel | ||
|
|
||
|
|
@@ -31,12 +28,6 @@ - (instancetype)initWithRepository:(SLRepository *)repository parameters:(NSDict | |
| __overflow = [NSMutableDictionary dictionary]; | ||
| } | ||
|
|
||
| if (jsonDateFormatter == nil) { | ||
| jsonDateFormatter = [[NSDateFormatter alloc] init]; | ||
| [jsonDateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"]; | ||
| [jsonDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; | ||
| } | ||
|
|
||
| return self; | ||
| } | ||
|
|
||
|
|
@@ -63,9 +54,6 @@ - (NSDictionary *)toDictionary { | |
| NSString *propertyName = [NSString stringWithCString:property_getName(properties[i]) | ||
| encoding:NSUTF8StringEncoding]; | ||
| id obj = [self valueForKey:propertyName]; | ||
| if ([obj isKindOfClass:[NSDate class]]) { | ||
| obj = [jsonDateFormatter stringFromDate:obj]; | ||
| } | ||
| [dict setValue:obj forKey:propertyName]; | ||
| } | ||
| free(properties); | ||
|
|
@@ -105,7 +93,7 @@ - (SLRESTContract *)contract { | |
| } | ||
|
|
||
| - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary { | ||
| LBModel __block *model = (LBModel *)[[self.modelClass alloc] initWithRepository:self parameters:dictionary]; | ||
| LBModel *model = (LBModel *)[[self.modelClass alloc] initWithRepository:self parameters:dictionary]; | ||
|
|
||
| [[model _overflow] addEntriesFromDictionary:dictionary]; | ||
|
|
||
|
|
@@ -122,9 +110,21 @@ - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary { | |
| } | ||
|
|
||
| const char *type = property_getAttributes(property); | ||
| // if the property type is NSDate, convert the string to a date object | ||
| if ([obj isKindOfClass:[NSString class]] && strncmp(type, "T@\"NSDate\",", 11) == 0) { | ||
| obj = [jsonDateFormatter dateFromString:obj]; | ||
| if ([obj isKindOfClass:[NSString class]]) { | ||
| // if the property type is NSDate, convert the string to a date object | ||
| if (strncmp(type, "T@\"NSDate\",", 11) == 0) { | ||
| obj = [SLObject dateFromEncodedProperty:obj]; | ||
| } | ||
| } else if ([obj isKindOfClass:[NSDictionary class]]) { | ||
| // if the property type is NSMutableData, convert the json object to a data object | ||
| if (strncmp(type, "T@\"NSMutableData\",", 18) == 0 || | ||
| strncmp(type, "T@\"NSData\",", 11) == 0) { | ||
| obj = [SLObject dataFromEncodedProperty:obj]; | ||
| } | ||
| // if the property type is CLLocation, convert the json object to a location object | ||
| else if (strncmp(type, "T@\"CLLocation\",", 15) == 0) { | ||
| obj = [SLObject locationFromEncodedProperty:obj]; | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parameter conversions back to iOS data types are needed to be performed here in |
||
| } | ||
|
|
||
| @try { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,23 @@ | |
|
|
||
| #import "LBModel.h" | ||
|
|
||
|
|
||
| /** | ||
| * Blocks of this type are executed for a successful method invocation that returns | ||
| * an LBPersistedModel instance as a callback argument. | ||
| */ | ||
| @class LBPersistedModel; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since those success block types rather belong to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, isn't this a breaking change? I mean if there is any existing user code relying on Can we keep these typedefs around, perhaps with a deprecation notice (if there is a mechanism for that). typedef LBModelVoidSuccessBlock LBPersistedModelVoidSuccessBlock;
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't care about backward compatibility for those since those typedefs are added by me on Oct. 29 as a part of CodeGen support. It seems highly unlikely that someone already depends on the typedefs. How about make the above change and consider backward compatibility when we get any Issue on it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following @bajtos 's suggestion, I've left the typedefs with deprecation warning since I'm informed that Ver. v1.3.2 has been recently which includes all the typedefs (which I didn't realise until yesterday). |
||
| typedef void (^LBPersistedModelObjectSuccessBlock)(LBPersistedModel *model); | ||
| typedef void (^LBPersistedModelArraySuccessBlock)(NSArray *array); | ||
| typedef void (^LBPersistedModelVoidSuccessBlock)(); | ||
| typedef void (^LBPersistedModelBoolSuccessBlock)(BOOL boolean); | ||
| typedef void (^LBPersistedModelNumberSuccessBlock)(NSInteger number); | ||
|
|
||
| // The following typedefs are left for backward compatibility. | ||
| typedef LBModelVoidSuccessBlock LBPersistedModelVoidSuccessBlock | ||
| __attribute((deprecated("use LBModelVoidSuccessBlock"))); | ||
| typedef LBModelBoolSuccessBlock LBPersistedModelBoolSuccessBlock | ||
| __attribute((deprecated("use LBModelBoolSuccessBlock"))); | ||
| typedef LBModelNumberSuccessBlock LBPersistedModelNumberSuccessBlock | ||
| __attribute((deprecated("use LBModelNumberSuccessBlock"))); | ||
| typedef LBModelArraySuccessBlock LBPersistedModelArraySuccessBlock | ||
| __attribute((deprecated("use LBModelArraySuccessBlock"))); | ||
|
|
||
| /** | ||
| * A local representative of a single persisted model instance on the server. | ||
|
|
@@ -27,7 +38,7 @@ typedef void (^LBPersistedModelNumberSuccessBlock)(NSInteger number); | |
| * Blocks of this type are executed when LBPersistedModel::saveWithSuccess:failure: is | ||
| * successful. | ||
| */ | ||
| typedef LBPersistedModelVoidSuccessBlock LBPersistedModelSaveSuccessBlock; | ||
| typedef LBModelVoidSuccessBlock LBPersistedModelSaveSuccessBlock; | ||
| /** | ||
| * Saves the LBPersistedModel to the server. | ||
| * | ||
|
|
@@ -43,7 +54,7 @@ typedef LBPersistedModelVoidSuccessBlock LBPersistedModelSaveSuccessBlock; | |
| * Blocks of this type are executed when LBPersistedModel::destroyWithSuccess:failure: is | ||
| * successful. | ||
| */ | ||
| typedef LBPersistedModelVoidSuccessBlock LBPersistedModelDestroySuccessBlock; | ||
| typedef LBModelVoidSuccessBlock LBPersistedModelDestroySuccessBlock; | ||
| /** | ||
| * Destroys the LBPersistedModel from the server. | ||
| * | ||
|
|
@@ -88,7 +99,7 @@ typedef LBPersistedModelObjectSuccessBlock LBPersistedModelFindSuccessBlock; | |
| * Blocks of this type are executed when | ||
| * LBPersistedModelRepository::allWithSuccess:failure: is successful. | ||
| */ | ||
| typedef LBPersistedModelArraySuccessBlock LBPersistedModelAllSuccessBlock; | ||
| typedef LBModelArraySuccessBlock LBPersistedModelAllSuccessBlock; | ||
| /** | ||
| * Finds and downloads all models of this type on and from the server. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For convenience (especially for CodeGen), the success block types with supported data types are typedef'ed.