Skip to content

Commit

Permalink
add nullability annotations for XCode 6.3 and Swift 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DenTelezhkin committed Mar 14, 2015
1 parent c790cae commit 85ae9be
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 49 deletions.
14 changes: 13 additions & 1 deletion EasyMapping/EKCoreDataImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
#import <CoreData/CoreData.h>
#import "EKManagedObjectMapping.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
`EKCoreDataImporter` is internal EasyMapping class and is used by `EKManagedObjectMapper` to manage CoreData imports and make them fast and efficient. It basically does 3 things:
Expand Down Expand Up @@ -75,8 +82,13 @@
@result managed object
*/
- (id)existingObjectForRepresentation:(id)representation mapping:(EKManagedObjectMapping *)mapping context:(NSManagedObjectContext *)context;
- (nullable id)existingObjectForRepresentation:(id)representation mapping:(EKManagedObjectMapping *)mapping context:(NSManagedObjectContext *)context;

- (void)cacheObject:(NSManagedObject *)object withMapping:(EKManagedObjectMapping *)mapping;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

12 changes: 12 additions & 0 deletions EasyMapping/EKManagedObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
#import <CoreData/CoreData.h>
#import "EKManagedObjectMapping.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
`EKManagedObjectMapper` is used to create and fill CoreData objects. Internally, it uses `EKCoreDataImporter` class to speed up data imports. You can find more info on this in project's readme.
*/
Expand Down Expand Up @@ -99,3 +106,8 @@
inManagedObjectContext:(NSManagedObjectContext *)context;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

11 changes: 11 additions & 0 deletions EasyMapping/EKManagedObjectMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
#import "EKObjectMapping.h"
#import "EKPropertyMapping.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

#define EKDesignatedInitializer(__SEL__) __attribute__((unavailable("Invoke the designated initializer `" # __SEL__ "` instead.")))

/**
Expand Down Expand Up @@ -125,3 +132,7 @@
+ (instancetype)mappingForClass:(Class)objectClass withRootPath:(NSString *)rootPath
withBlock:(void (^)(EKObjectMapping *mapping))mappingBlock EKDesignatedInitializer(mappingForEntityName:withRootPath:withBlock);
@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
14 changes: 13 additions & 1 deletion EasyMapping/EKManagedObjectModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#import <CoreData/CoreData.h>
#import "EKMappingProtocol.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
EKManagedObjectModel is a convenience base class for NSManagedObject subclasses. It allows creating CoreData objects from JSON representation and vice versa.
*/
Expand All @@ -35,7 +42,7 @@
@return NSDictionary representation of current object.
*/
- (NSDictionary *)serializedObjectInContext:(NSManagedObjectContext *)context;
- (NSDictionary *)serializedObjectInContext:(nullable NSManagedObjectContext *)context;

@end

Expand All @@ -44,3 +51,8 @@
- (NSDictionary *)serializedObject __deprecated_msg("Use serializedObjectInContext instead");

@end


#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
20 changes: 16 additions & 4 deletions EasyMapping/EKMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@

#import "EKObjectMapping.h"


#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
`EKMapper` provides an interface to create objective-c object from JSON representation, using `EKObjectMapping` class. For creating CoreData objects, use `EKManagedObjectMapper` class.
*/
Expand All @@ -38,8 +46,8 @@
@result mapped object
*/
+ (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;
+ (nullable id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;

/**
Fills previously existed object with values, provided in JSON representation. All values, that are included in mapping and were filled prior to calling this method, will be overwritten.
Expand All @@ -65,7 +73,11 @@
@result array of mapped objects
*/
+ (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;
+ (nullable NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
22 changes: 17 additions & 5 deletions EasyMapping/EKMappingBlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,27 @@
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

typedef id(^EKMappingValueBlock)(NSString *key, id value);
typedef id(^EKMappingReverseBlock)(id value);
#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

typedef id(^EKManagedMappingValueBlock)(NSString * key, id value, NSManagedObjectContext * context);
typedef id(^EKManagedMappingReverseValueBlock)(id value, NSManagedObjectContext * context);
typedef __nullable id(^EKMappingValueBlock)(NSString *key, __nullable id value);
typedef __nullable id(^EKMappingReverseBlock)(__nullable id value);

typedef __nullable id(^EKManagedMappingValueBlock)(NSString * key, __nullable id value, NSManagedObjectContext * context);
typedef __nullable id(^EKManagedMappingReverseValueBlock)(__nullable id value, NSManagedObjectContext * context);

@interface EKMappingBlocks: NSObject

+ (EKMappingValueBlock)urlMappingBlock;
+ (EKMappingReverseBlock)urlReverseMappingBlock;

@end
@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

12 changes: 12 additions & 0 deletions EasyMapping/EKMappingProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#import "EKObjectMapping.h"
#import "EKManagedObjectMapping.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
EKMappingProtocol must be implemented by NSObject subclasses, that will be mapped from JSON representation.
Expand Down Expand Up @@ -41,3 +48,8 @@
+(EKManagedObjectMapping *)objectMapping;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

19 changes: 16 additions & 3 deletions EasyMapping/EKObjectMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

@protocol EKMappingProtocol;


#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
`EKObjectMapping` class is used to define mappings between JSON representation and objective-c object.
*/
Expand Down Expand Up @@ -217,7 +225,7 @@
- (void) hasOne:(Class)objectClass
forDictionaryFromKeyPaths:(NSArray *)keyPaths
forProperty:(NSString *)property
withObjectMapping:(EKObjectMapping *)objectMapping;
withObjectMapping:(nullable EKObjectMapping *)objectMapping;

/**
Map to-one relationship for keyPath.
Expand All @@ -230,7 +238,7 @@ forDictionaryFromKeyPaths:(NSArray *)keyPaths
@warning If you have recursive mappings, do not use this method, cause it can cause infinite recursion to happen. Or you need to handle recursive mappings situation by yourself, subclassing EKObjectMapping and providing different mappings for different mapping levels.
*/
- (void)hasOne:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(EKObjectMapping*)objectMapping;
- (void)hasOne:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(nullable EKObjectMapping*)objectMapping;


/**
Expand Down Expand Up @@ -264,7 +272,7 @@ forDictionaryFromKeyPaths:(NSArray *)keyPaths
@warning If you have recursive mappings, do not use this method, cause it can cause infinite recursion to happen. Or you need to handle recursive mappings situation by yourself, subclassing EKObjectMapping and providing different mappings for different mapping levels.
*/
-(void)hasMany:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(EKObjectMapping*)objectMapping;
-(void)hasMany:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(nullable EKObjectMapping*)objectMapping;

@end

Expand All @@ -284,3 +292,8 @@ forDictionaryFromKeyPaths:(NSArray *)keyPaths
- (void)mapKeyPath:(NSString *)keyPath toProperty:(NSString *)property withDateFormat:(NSString *)dateFormat __deprecated;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

13 changes: 13 additions & 0 deletions EasyMapping/EKObjectModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#import <Foundation/Foundation.h>
#import "EKMappingProtocol.h"


#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
EKModel is convenience base class, that allows transforming JSON objects to NSObjects and vice versa.
*/
Expand Down Expand Up @@ -41,3 +49,8 @@
- (NSDictionary *)serializedObject;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

23 changes: 17 additions & 6 deletions EasyMapping/EKPropertyHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,47 @@
#import "EKPropertyMapping.h"
#import "EKObjectMapping.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
`EKPropertyHelper` is internal EasyMapping class, that works with objective-c runtime to get and set values of properties.
*/
@interface EKPropertyHelper : NSObject

+ (BOOL)propertyNameIsScalar:(NSString *)propertyName fromObject:(id)object;

+ (id)propertyRepresentation:(NSArray *)array forObject:(id)object withPropertyName:(NSString *)propertyName;
+ (nullable id)propertyRepresentation:(NSArray *)array forObject:(id)object withPropertyName:(NSString *)propertyName;

+ (void) setProperty:(EKPropertyMapping *)propertyMapping
onObject:(id)object
fromRepresentation:(NSDictionary *)representation;
onObject:(id)object
fromRepresentation:(NSDictionary *)representation;

+ (void) setProperty:(EKPropertyMapping *)propertyMapping
onObject:(id)object
fromRepresentation:(NSDictionary *)representation
inContext:(NSManagedObjectContext *)context;

+ (id)getValueOfProperty:(EKPropertyMapping *)propertyMapping
fromRepresentation:(NSDictionary *)representation;
+ (nullable id)getValueOfProperty:(EKPropertyMapping *)propertyMapping
fromRepresentation:(NSDictionary *)representation;

+ (id)getValueOfManagedProperty:(EKPropertyMapping *)mapping
fromRepresentation:(NSDictionary *)representation
inContext:(NSManagedObjectContext *)context;

+ (void)setValue:(id)value onObject:(id)object forKeyPath:(NSString *)keyPath;
+ (void)setValue:(nullable id)value onObject:(id)object forKeyPath:(NSString *)keyPath;

+ (void)addValue:(id)value onObject:(id)object forKeyPath:(NSString *)keyPath;

+ (NSDictionary *)extractRootPathFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
19 changes: 15 additions & 4 deletions EasyMapping/EKPropertyMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

#import "EKMappingBlocks.h"

#if __has_feature(nullability) // Xcode 6.3+
#pragma clang assume_nonnull begin
#else
#define nullable
#define __nullable
#endif

/**
`EKPropertyMapping` is a class, that represents relation between representation of a single field in JSON and objective-c model property.
*/
Expand All @@ -42,21 +49,25 @@
/**
Optional block to transform JSON value into objective-C object.
*/
@property (nonatomic, strong) EKMappingValueBlock valueBlock;
@property (nonatomic, strong, nullable) EKMappingValueBlock valueBlock;

/**
Optional block to serialize objective-c object into JSON representation.
*/
@property (nonatomic, strong) EKMappingReverseBlock reverseBlock;
@property (nonatomic, strong, nullable) EKMappingReverseBlock reverseBlock;

/**
Optional block to transform JSON value into CoreData object.
*/
@property (nonatomic, strong) EKManagedMappingValueBlock managedValueBlock;
@property (nonatomic, strong, nullable) EKManagedMappingValueBlock managedValueBlock;

/**
Optional block to serialize CoreData object into JSON representation.
*/
@property (nonatomic, strong) EKManagedMappingReverseValueBlock managedReverseBlock;
@property (nonatomic, strong, nullable) EKManagedMappingReverseValueBlock managedReverseBlock;

@end

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
Loading

0 comments on commit 85ae9be

Please sign in to comment.