Skip to content

Commit 4192056

Browse files
author
mylcode
committed
增加属性映射宏定义MCProperty,支持正确性校验,防止属性名称被更改后不能正确映射
1 parent c339b64 commit 4192056

File tree

15 files changed

+107
-76
lines changed

15 files changed

+107
-76
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### 更新日志
22

3+
### 2018-06-04
4+
5+
- 增加属性映射宏定义MCProperty,支持正确性校验
6+
37
### 2018-5-12
48
- 增加单测,提升代码覆盖率
59
- 增加代码覆盖率badge

Example/MCJSONKit.xcodeproj/project.pbxproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,13 @@
504504
);
505505
inputPaths = (
506506
"${SRCROOT}/Pods/Target Support Files/Pods-MCJSONKit_Example/Pods-MCJSONKit_Example-resources.sh",
507-
$PODS_CONFIGURATION_BUILD_DIR/MWPhotoBrowser/MWPhotoBrowser.bundle,
507+
"${PODS_CONFIGURATION_BUILD_DIR}/MWPhotoBrowser/MWPhotoBrowser.bundle",
508508
"${PODS_ROOT}/Weibo_SDK/libWeiboSDK/WeiboSDK.bundle",
509509
);
510510
name = "[CP] Copy Pods Resources";
511511
outputPaths = (
512-
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}",
512+
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MWPhotoBrowser.bundle",
513+
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/WeiboSDK.bundle",
513514
);
514515
runOnlyForDeploymentPostprocessing = 0;
515516
shellPath = /bin/sh;

Example/MCJSONKit.xcodeproj/xcshareddata/xcschemes/MCJSONKit-Example.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<CodeCoverageTargets>
3535
<BuildableReference
3636
BuildableIdentifier = "primary"
37-
BlueprintIdentifier = "45DFEAAC3D7F63C7A79820E622594E53"
37+
BlueprintIdentifier = "C9BF0B25B60BCEB920B78CBEB903E4A2"
3838
BuildableName = "libMCJSONKit.a"
3939
BlueprintName = "MCJSONKit"
4040
ReferencedContainer = "container:Pods/Pods.xcodeproj">

Example/MCJSONKit/Classes/Models/StatuseModel.m

+4-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@
1010

1111
@implementation StatuseModel
1212

13-
+ (NSDictionary *)keyMappingDictionary {
14-
return @{@"wid":@"id",
15-
@"visible":@"visible.type",
16-
@"listid":@"visible.list_id"};
13+
- (NSDictionary *)keyMappingDictionary {
14+
return @{MYPropMapper(wid):@"id",
15+
MYPropMapper(visible):@"visible.type",
16+
MYPropMapper(listid):@"visible.list_id"};
1717
}
1818

19-
/*+ (NSArray *)allowedPropertyNames {
20-
return @[@"visible",@"listid"];
21-
}*/
22-
2319
@end

Example/MCJSONKit/Classes/Models/UserModel.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
@implementation UserModel
1212

13-
+ (NSDictionary *)keyMappingDictionary {
14-
return @{@"uid":@"id",@"desc":@"description"};
13+
- (NSDictionary *)keyMappingDictionary {
14+
return @{MYPropMapper(province):@"id",MYPropMapper(desc):@"description"};
1515
}
1616

17-
+ (NSSet *)ignoreDictionary {
18-
return [NSSet setWithObject:@"verified_type"];
17+
- (NSSet *)ignoreDictionary {
18+
return [NSSet setWithObject:MYPropMapper(verified_type)];
1919
}
2020

2121
@end

Example/MCJSONKit/Classes/Models/WeiboModel.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import <MCJSONKit/MCJSONKitDefine.h>
11+
12+
#define MYPropMapper(name) MCProperty(name)
1013

1114
@interface WeiboModel : NSObject
1215

@@ -28,26 +31,26 @@
2831
/**
2932
允许的属性集合,配置后则ignoreSet失效
3033
*/
31-
+ (NSSet *)allowedPropertyNames;
34+
- (NSSet *)allowedPropertyNames;
3235

3336
/**
3437
忽略的属性集合
3538
*/
36-
+ (NSSet *)ignoreSet;
39+
- (NSSet *)ignoreSet;
3740

3841
/**
3942
key关联字段
4043
4144
@return key:对象属性 value:keyPath
4245
*/
43-
+ (NSDictionary *)keyMappingDictionary;
46+
- (NSDictionary *)keyMappingDictionary;
4447

4548
/**
4649
类型关联字典
4750
4851
@return key:对象属性 value:类型class
4952
*/
50-
+ (NSDictionary *)typeMappingDictionary;
53+
- (NSDictionary *)typeMappingDictionary;
5154
- (NSDictionary *)toDictionary;
5255
- (NSString *)toJSONString;
5356

Example/MCJSONKit/Classes/Models/WeiboModel.m

+16-16
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ + (NSArray *)arrayOfModelsFromKeyValues:(id)keyValues {
3939
return [self mc_arrayOfModelsFromKeyValues:keyValues];
4040
}
4141

42-
+ (NSSet *)allowedPropertyNames {
42+
- (NSSet *)allowedPropertyNames {
4343
return nil;
4444
}
4545

46-
+ (NSDictionary *)keyMappingDictionary {
46+
- (NSDictionary *)keyMappingDictionary {
4747
return nil;
4848
}
4949

50-
+ (NSDictionary *)typeMappingDictionary {
50+
- (NSDictionary *)typeMappingDictionary {
5151
return nil;
5252
}
5353

54-
+ (NSSet *)ignoreSet {
54+
- (NSSet *)ignoreSet {
5555
return nil;
5656
}
5757

@@ -65,19 +65,19 @@ - (NSString *)toJSONString {
6565

6666
#pragma mark JSONCoreConfig
6767

68-
+ (NSSet *)mc_allowedPropertiesSet {
68+
- (NSSet *)mc_allowedPropertiesSet {
6969
return [self allowedPropertyNames];
7070
}
7171

72-
+ (NSDictionary *)mc_keyMappingDictionary {
72+
- (NSDictionary *)mc_keyMappingDictionary {
7373
return [self keyMappingDictionary];
7474
}
7575

76-
+ (NSDictionary *)mc_typeMappingDictionary {
76+
- (NSDictionary *)mc_typeMappingDictionary {
7777
return [self typeMappingDictionary];
7878
}
7979

80-
+ (NSSet *)mc_ignorePropertiesSet {
80+
- (NSSet *)mc_ignorePropertiesSet {
8181
return [self ignoreSet];
8282
}
8383

@@ -95,19 +95,19 @@ + (NSArray *)arrayOfModelsFromKeyValues:(id)keyValues {
9595
return [self mj_objectArrayWithKeyValuesArray:keyValues];
9696
}
9797

98-
+ (NSSet *)allowedPropertyNames {
98+
- (NSSet *)allowedPropertyNames {
9999
return nil;
100100
}
101101

102-
+ (NSDictionary *)keyMappingDictionary {
102+
- (NSDictionary *)keyMappingDictionary {
103103
return nil;
104104
}
105105

106-
+ (NSDictionary *)typeMappingDictionary {
106+
- (NSDictionary *)typeMappingDictionary {
107107
return nil;
108108
}
109109

110-
+ (NSSet *)ignoreSet {
110+
- (NSSet *)ignoreSet {
111111
return nil;
112112
}
113113

@@ -119,19 +119,19 @@ - (NSString *)toJSONString {
119119
return [self mj_JSONString];
120120
}
121121

122-
+ (NSArray *)mj_ignoredPropertyNames {
122+
- (NSArray *)mj_ignoredPropertyNames {
123123
return [self ignoreSet].allObjects;
124124
}
125125

126-
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
126+
- (NSDictionary *)mj_replacedKeyFromPropertyName {
127127
return [self keyMappingDictionary];
128128
}
129129

130-
+ (NSDictionary *)mj_objectClassInArray {
130+
- (NSDictionary *)mj_objectClassInArray {
131131
return [self typeMappingDictionary];
132132
}
133133

134-
+ (NSArray *)mj_allowedPropertyNames {
134+
- (NSArray *)mj_allowedPropertyNames {
135135
return [self allowedPropertyNames].allObjects;
136136
}
137137

Example/MCJSONKit/Classes/Models/WeiboResult.m

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010

1111
@implementation WeiboResult
1212

13-
+ (NSDictionary *)typeMappingDictionary {
14-
return @{@"statuses": [StatuseModel class]};
13+
- (NSDictionary *)typeMappingDictionary {
14+
return @{MYPropMapper(statuses): [StatuseModel class]};
1515
}
1616

17-
+ (NSSet *)ignoreSet {
18-
return [NSSet setWithObjects:@"next_cursor", nil];
17+
- (NSSet *)ignoreSet {
18+
return [NSSet setWithObjects:MYPropMapper(next_cursor), nil];
1919
}
2020

2121
@end
2222

2323
@implementation WeiboResult1
2424

25-
+ (NSSet *)allowedPropertyNames {
26-
return [NSSet setWithObjects:@"statuses", @"updateTime", @"timeStr", @"max_id", nil];
25+
- (NSSet *)allowedPropertyNames {
26+
return [NSSet setWithObjects:MYPropMapper(statuses), MYPropMapper(updateTime), MYPropMapper(timeStr), MYPropMapper(max_id), nil];
2727
}
2828

29-
+ (NSSet *)ignoreSet {
30-
return [NSSet setWithObjects:@"statuses", nil];
29+
- (NSSet *)ignoreSet {
30+
return [NSSet setWithObjects:MYPropMapper(statuses), MYPropMapper(since_id), nil];
3131
}
3232

3333
@end

Example/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ SPEC CHECKSUMS:
4949
Kiwi: fbeafef0f00e4d8f7dcb3420a4930afe70af77f7
5050
MBProgressHUD: 1569cf7ace17a8bac47aabfbb8580a49690386d1
5151
MCFoundation: f03b2f02918ed3d8e6c30cef7ce030cf0b93f9aa
52-
MCJSONKit: b1cc47768aebeef5e3335d1dcc3b0e5d04bc0e79
52+
MCJSONKit: af26a8cc55a39d9ad42fab9f5ef339495e292bc3
5353
MCLogger: f258a43a313bfeabcd85781847626a6f51e6a715
5454
MCUIKit: 4d9fb8d30b0cf44bf99605ecd248a3092d6f4e13
5555
MJExtension: 5932755f451458eefa24239358817f8d291240c7
@@ -59,4 +59,4 @@ SPEC CHECKSUMS:
5959

6060
PODFILE CHECKSUM: 2f2c60f27931b02540794580c6f3c54cfcf585ce
6161

62-
COCOAPODS: 1.3.1
62+
COCOAPODS: 1.4.0

Example/Tests/MCNSObjectJSONkitTest.m

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
it(@"data type assert", ^{
3535
[[result shouldNot] beNil];
36+
[[theValue(result.next_cursor) should] equal:@0];
3637
[[theValue(result.statuses.count) shouldNot] equal:@0];
3738
[[theValue(result.since_id) should] equal:@4238558845532583];
3839
});
@@ -85,6 +86,8 @@
8586
it(@"from NSString", ^{
8687
NSString *jsonStr = [[NSString alloc] initWithData:JSONFileData(kFriendsTimelineFile2) encoding:NSUTF8StringEncoding];
8788
array = [StatuseModel arrayOfModelsFromKeyValues:jsonStr];
89+
[[theValue(array.firstObject.wid) shouldNot] equal:@0];
90+
[[theValue(array.firstObject.user.verified_type) should] equal:@0];
8891
[[array shouldNot] beNil];
8992
[[theValue(array.count) should] equal:@20];
9093
});
@@ -108,6 +111,7 @@
108111
NSString *jsonStr = [[NSString alloc] initWithData:JSONFileData(kJSONConfigFile) encoding:NSUTF8StringEncoding];
109112
WeiboResult1 *result = [WeiboResult1 jsonObjectFromData:jsonStr];
110113
[[result shouldNot] beNil];
114+
[[theValue(result.since_id) should] equal:@0];
111115
[[theValue(result.statuses.count) should] equal:@20];
112116
[[theValue(result.updateTime.timeIntervalSince1970) should] equal:@1526183950];
113117
[[theValue(result.timeStr.timeIntervalSince1970) should] equal:@1526183950];

MCJSONKit/Classes/MCJSONKit.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ FOUNDATION_EXPORT const unsigned char MCJSONKitVersionString[];
1414

1515
// In this header, you should import all the public headers of your framework using statements like #import <MCJSONKit/PublicHeader.h>
1616

17+
#import <MCJSONKit/MCJSONKitDefine.h>
1718
#import <MCJSONKit/NSDictionary+MCJSONkit.h>
1819
#import <MCJSONKit/NSArray+MCJSONKit.h>
1920
#import <MCJSONKit/NSObject+MCJSONKit.h>

MCJSONKit/Classes/MCJSONKitDefine.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// MCJSONKitDefine.h
3+
// MCJSONKit
4+
//
5+
// Created by lingjing on 2018/6/4.
6+
// Copyright © 2018年 MC-Stuido. All rights reserved.
7+
//
8+
9+
#import <MCFoundation/MCFoundation.h>
10+
11+
/*
12+
属性映射,正确校验 MCProperty(name) 展开后等同于 = @"name"
13+
*/
14+
15+
/**
16+
属性映射,正确性校验
17+
用法:MCProperty()
18+
19+
@param name 属性名称
20+
@return 返回OC字符串@"name"
21+
*/
22+
#define MCProperty(name) @keypath(self, name)

MCJSONKit/Classes/NSObject+MCJSONKit.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,25 @@
4545
/**
4646
允许的属性集合,重写后则mc_ignorePropertiesSet失效
4747
*/
48-
+ (NSSet *)mc_allowedPropertiesSet;
48+
- (NSSet *)mc_allowedPropertiesSet;
4949

5050
/**
5151
忽略的属性集合
5252
*/
53-
+ (NSSet *)mc_ignorePropertiesSet;
53+
- (NSSet *)mc_ignorePropertiesSet;
5454

5555
/**
5656
key关联字段
5757
5858
@return key:对象属性 value:keyPath
5959
*/
60-
+ (NSDictionary *)mc_keyMappingDictionary;
61-
60+
- (NSDictionary *)mc_keyMappingDictionary;
6261

6362
/**
6463
类型关联字典
6564
6665
@return key:对象属性 value:类型class
6766
*/
68-
+ (NSDictionary *)mc_typeMappingDictionary;
67+
- (NSDictionary *)mc_typeMappingDictionary;
6968

7069
@end

MCJSONKit/Classes/NSObject+MCJSONKit.m

+9-8
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,11 @@ - (NSMutableDictionary *)allProperties {
244244
objc_property_t *properties = class_copyPropertyList(cls, &outCount);
245245

246246
NSScanner *scanner;
247-
NSSet *ignoreSet = [cls mc_ignorePropertiesSet];
248-
NSDictionary *keyMapping = [cls mc_keyMappingDictionary];
249-
NSDictionary *typeMapping = [cls mc_typeMappingDictionary];
250-
NSSet *allowSet = [cls mc_allowedPropertiesSet];
247+
NSObject *jsonObj = [[cls alloc] init];
248+
NSSet *ignoreSet = [jsonObj mc_ignorePropertiesSet];
249+
NSDictionary *keyMapping = [jsonObj mc_keyMappingDictionary];
250+
NSDictionary *typeMapping = [jsonObj mc_typeMappingDictionary];
251+
NSSet *allowSet = [jsonObj mc_allowedPropertiesSet];
251252
for (unsigned int i = 0; i<outCount; i++) {
252253
objc_property_t property = properties[i];
253254
//属性名称
@@ -395,19 +396,19 @@ - (id)_jsonObjectWithData:(id)data {
395396

396397
@implementation NSObject (JSONCoreConfig)
397398

398-
+ (NSSet *)mc_allowedPropertiesSet {
399+
- (NSSet *)mc_allowedPropertiesSet {
399400
return nil;
400401
}
401402

402-
+ (NSSet *)mc_ignorePropertiesSet {
403+
- (NSSet *)mc_ignorePropertiesSet {
403404
return nil;
404405
}
405406

406-
+ (NSDictionary *)mc_keyMappingDictionary {
407+
- (NSDictionary *)mc_keyMappingDictionary {
407408
return nil;
408409
}
409410

410-
+ (NSDictionary *)mc_typeMappingDictionary {
411+
- (NSDictionary *)mc_typeMappingDictionary {
411412
return nil;
412413
}
413414

0 commit comments

Comments
 (0)