3131
3232@interface InCodeMappingProvider ()
3333@property (nonatomic , strong ) NSMutableDictionary *mappingDictionary;
34+ @property (nonatomic , strong ) NSMutableDictionary *inverseMappingDictionary;
3435@property (nonatomic , strong ) NSMutableDictionary *dateFormatterDictionary;
36+ @property (nonatomic , strong ) NSMutableDictionary *inverseDateFormatterDictionary;
3537@end
3638
3739@implementation InCodeMappingProvider
@@ -44,8 +46,11 @@ - (id)init
4446{
4547 if (self = [super init ])
4648 {
49+ self.automaticallyGenerateInverseMapping = YES ;
4750 self.mappingDictionary = [NSMutableDictionary dictionary ];
51+ self.inverseMappingDictionary = [NSMutableDictionary dictionary ];
4852 self.dateFormatterDictionary = [NSMutableDictionary dictionary ];
53+ self.inverseDateFormatterDictionary = [NSMutableDictionary dictionary ];
4954 }
5055
5156 return self;
@@ -58,6 +63,11 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
5863 ObjectMappingInfo *info = [[ObjectMappingInfo alloc ] initWithDictionaryKey: dictionaryKey propertyKey: propertyKey andObjectType: objectType];
5964 NSString *key = [self uniqueKeyForClass: class andKey: dictionaryKey];
6065 [self .mappingDictionary setObject: info forKey: key];
66+
67+ if (self.automaticallyGenerateInverseMapping )
68+ {
69+ [self mapFromPropertyKey: propertyKey toDictionaryKey: dictionaryKey forClass: class];
70+ }
6171}
6272
6373- (void )mapFromDictionaryKey : (NSString *)dictionaryKey toPropertyKey : (NSString *)propertyKey forClass : (Class )class
@@ -72,10 +82,33 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
7282 [self .mappingDictionary setObject: info forKey: key];
7383}
7484
75- - (void )setDateFormatter : (NSDateFormatter *)dateFormatter forProperty : (NSString *)property andClass : (Class )class
85+ - (void )mapFromPropertyKey : (NSString *)propertyKey toDictionaryKey : (NSString *)dictionaryKey forClass : (Class )class {
86+ ObjectMappingInfo *info = [[ObjectMappingInfo alloc ] initWithDictionaryKey: dictionaryKey propertyKey: propertyKey andObjectType: nil ];
87+ NSString *key = [self uniqueKeyForClass: class andKey: propertyKey];
88+ [self .inverseMappingDictionary setObject: info forKey: key];
89+ }
90+
91+ - (void )mapFromPropertyKey : (NSString *)propertyKey toDictionaryKey : (NSString *)dictionaryKey forClass : (Class )class withTransformer : (MappingTransformer)transformer {
92+ ObjectMappingInfo *info = [[ObjectMappingInfo alloc ] initWithDictionaryKey: dictionaryKey propertyKey: propertyKey andTransformer: transformer];
93+ NSString *key = [self uniqueKeyForClass: class andKey: propertyKey];
94+ [self .inverseMappingDictionary setObject: info forKey: key];
95+ }
96+
97+ - (void )setDateFormatter : (NSDateFormatter *)dateFormatter forPropertyKey : (NSString *)property andClass : (Class )class
7698{
7799 NSString *key = [self uniqueKeyForClass: class andKey: property];
78100 [self .dateFormatterDictionary setObject: dateFormatter forKey: key];
101+
102+ if (self.automaticallyGenerateInverseMapping )
103+ {
104+ [self setDateFormatter: dateFormatter forDictionaryKey: property andClass: class];
105+ }
106+ }
107+
108+ - (void )setDateFormatter : (NSDateFormatter *)dateFormatter forDictionaryKey : (NSString *)dictionaryKey andClass : (Class )class
109+ {
110+ NSString *key = [self uniqueKeyForClass: class andKey: dictionaryKey];
111+ [self .inverseDateFormatterDictionary setObject: dateFormatter forKey: key];
79112}
80113
81114#pragma mark - public Methods -
@@ -93,9 +126,20 @@ - (ObjectMappingInfo *)mappingInfoForClass:(Class)class andDictionaryKey:(NSStri
93126 return [self .mappingDictionary objectForKey: key];
94127}
95128
96- - (NSDateFormatter *)dateFormatterForClass : (Class )class andProperty : (NSString *)property
129+ - (ObjectMappingInfo *)mappingInfoForClass : (Class )class andPropertyKey : (NSString *)source {
130+ NSString *key = [self uniqueKeyForClass: class andKey: source];
131+ return [self .inverseMappingDictionary objectForKey: key];
132+ }
133+
134+ - (NSDateFormatter *)dateFormatterForClass : (Class )class andPropertyKey : (NSString *)propertyKey
97135{
98- NSString *key = [self uniqueKeyForClass: class andKey: property];
136+ NSString *key = [self uniqueKeyForClass: class andKey: propertyKey];
137+ return [self .dateFormatterDictionary objectForKey: key];
138+ }
139+
140+ - (NSDateFormatter *)dateFormatterForClass : (Class )class andDictionaryKey : (NSString *)dictionaryKey
141+ {
142+ NSString *key = [self uniqueKeyForClass: class andKey: dictionaryKey];
99143 return [self .dateFormatterDictionary objectForKey: key];
100144}
101145
0 commit comments