26
26
// THE SOFTWARE.
27
27
28
28
#import " ObjectMapper.h"
29
+ #import < objc/runtime.h>
30
+ #import " ObjectMappingInfo.h"
31
+ #import " InstanceProvider.h"
32
+ #import " MappingProvider.h"
33
+ #import " LoggingProvider.h"
34
+ #import " ObjectInstanceProvider.h"
29
35
30
36
#ifdef DEBUG
31
37
#define ILog (format, ...) [self .loggingProvider log :[NSString stringWithFormat: (format), ##__VA_ARGS__] withLevel: LogLevelInfo]
@@ -42,6 +48,7 @@ @interface ObjectMapper()
42
48
@property (nonatomic , strong ) NSMutableArray *classNamesInMainBundle;
43
49
@property (nonatomic , strong ) NSMutableDictionary *mappedClassNames;
44
50
@property (nonatomic , strong ) NSMutableDictionary *mappedPropertyNames;
51
+ @property (nonatomic , strong ) NSMutableArray *instanceProviders;
45
52
@end
46
53
47
54
@implementation ObjectMapper
@@ -66,6 +73,10 @@ - (id)init
66
73
{
67
74
[self populateClassNamesFromMainBundle ];
68
75
76
+ self.instanceProviders = [NSMutableArray array ];
77
+ ObjectInstanceProvider *objectInstanceProvider = [[ObjectInstanceProvider alloc ] init ];
78
+ [self addInstanceProvider: objectInstanceProvider];
79
+
69
80
self.mappedClassNames = [NSMutableDictionary dictionary ];
70
81
self.mappedPropertyNames = [NSMutableDictionary dictionary ];
71
82
}
@@ -80,9 +91,6 @@ - (id)objectFromSource:(id)source toInstanceOfClass:(Class)class
80
91
if (!_mappingProvider)
81
92
@throw ([NSException exceptionWithName: @" MissingMappingProvider" reason: @" Mapping provider is not set" userInfo: nil ]);
82
93
83
- if (!_instanceProvider)
84
- @throw ([NSException exceptionWithName: @" MissingInstanceProvider" reason: @" Instance provider is not set" userInfo: nil ]);
85
-
86
94
if ([source isKindOfClass: [NSDictionary class ]])
87
95
{
88
96
ILog (@" ____________________ Mapping Dictionary to instance [%@ ] ____________________" , NSStringFromClass (class));
@@ -112,6 +120,11 @@ - (id)dictionaryFromObject:(NSObject *)object
112
120
}
113
121
}
114
122
123
+ - (void )addInstanceProvider : (id <InstanceProvider>)instanceProvider
124
+ {
125
+ [self .instanceProviders addObject: instanceProvider];
126
+ }
127
+
115
128
#pragma mark - Private Methods -
116
129
117
130
- (void )populateClassNamesFromMainBundle
@@ -259,9 +272,10 @@ - (NSDictionary *)normalizedDictionaryFromDictionary:(NSDictionary *)source forC
259
272
260
273
- (id )processDictionary : (NSDictionary *)source forClass : (Class )class
261
274
{
262
- NSDictionary *normalizedSource = [self normalizedDictionaryFromDictionary: source forClass: class];
275
+ NSDictionary *normalizedSource = (self. normalizeDictionary ) ? [self normalizedDictionaryFromDictionary: source forClass: class] : source ;
263
276
264
- id object = [self .instanceProvider emptyInstanceForClass: class];
277
+ id <InstanceProvider> instanceProvider = [self instanceProviderForClass: class];
278
+ id object = [instanceProvider emptyInstanceForClass: class];
265
279
266
280
for (NSString *key in normalizedSource)
267
281
{
@@ -276,13 +290,13 @@ - (id)processDictionary:(NSDictionary *)source forClass:(Class)class
276
290
277
291
if (mappingInfo)
278
292
{
279
- propertyName = [self . instanceProvider propertyNameForObject: object byCaseInsensitivePropertyName: mappingInfo.propertyKey];
293
+ propertyName = [instanceProvider propertyNameForObject: object byCaseInsensitivePropertyName: mappingInfo.propertyKey];
280
294
objectType = mappingInfo.objectType ;
281
295
mappingTransformer = mappingInfo.transformer ;
282
296
}
283
297
else
284
298
{
285
- propertyName = [self . instanceProvider propertyNameForObject: object byCaseInsensitivePropertyName: key];
299
+ propertyName = [instanceProvider propertyNameForObject: object byCaseInsensitivePropertyName: key];
286
300
287
301
if (propertyName && ([value isKindOfClass: [NSDictionary class ]] || [value isKindOfClass: [NSArray class ]]))
288
302
{
@@ -350,7 +364,7 @@ - (id)processDictionary:(NSDictionary *)source forClass:(Class)class
350
364
}
351
365
352
366
NSError *error;
353
- object = [self . instanceProvider upsertObject: object error: &error];
367
+ object = [instanceProvider upsertObject: object error: &error];
354
368
355
369
if (error)
356
370
ELog (@" Attempt to update existing instance failed with error '%@ ' for class (%@ ) and object %@ " ,
@@ -361,9 +375,21 @@ - (id)processDictionary:(NSDictionary *)source forClass:(Class)class
361
375
return object;
362
376
}
363
377
378
+ - (id <InstanceProvider>)instanceProviderForClass : (Class )class
379
+ {
380
+ for (id <InstanceProvider> instanceProvider in self.instanceProviders )
381
+ {
382
+ if ([instanceProvider canHandleClass: class])
383
+ return instanceProvider;
384
+ }
385
+
386
+ return nil ;
387
+ }
388
+
364
389
- (id )processArray : (NSArray *)value forClass : (Class )class
365
390
{
366
- id collection = [self .instanceProvider emptyCollectionInstance ];
391
+ id <InstanceProvider> instanceProvider = [self instanceProviderForClass: class];
392
+ id collection = [instanceProvider emptyCollectionInstance ];
367
393
368
394
for (id objectInArray in value)
369
395
{
0 commit comments