Skip to content

Commit e628157

Browse files
author
mylcode
authored
Merge pull request #19 from MC-Studio/mapper
增加字段映射宏定义,提升安全保护
2 parents c339b64 + 02c56b2 commit e628157

19 files changed

+140
-81
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

+32-3
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
isa = PBXProject;
347347
attributes = {
348348
CLASSPREFIX = MC;
349-
LastUpgradeCheck = 0720;
349+
LastUpgradeCheck = 0930;
350350
ORGANIZATIONNAME = "MC-Studio";
351351
TargetAttributes = {
352352
6003F5AD195388D20070C39A = {
@@ -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;
@@ -595,19 +596,32 @@
595596
CLANG_CXX_LIBRARY = "libc++";
596597
CLANG_ENABLE_MODULES = YES;
597598
CLANG_ENABLE_OBJC_ARC = YES;
599+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
598600
CLANG_WARN_BOOL_CONVERSION = YES;
601+
CLANG_WARN_COMMA = YES;
599602
CLANG_WARN_CONSTANT_CONVERSION = YES;
603+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
600604
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
601605
CLANG_WARN_EMPTY_BODY = YES;
602606
CLANG_WARN_ENUM_CONVERSION = YES;
607+
CLANG_WARN_INFINITE_RECURSION = YES;
603608
CLANG_WARN_INT_CONVERSION = YES;
609+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
610+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
611+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
604612
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
613+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
614+
CLANG_WARN_STRICT_PROTOTYPES = YES;
615+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
616+
CLANG_WARN_UNREACHABLE_CODE = YES;
605617
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
606618
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
607619
COPY_PHASE_STRIP = NO;
620+
ENABLE_STRICT_OBJC_MSGSEND = YES;
608621
ENABLE_TESTABILITY = YES;
609622
GCC_C_LANGUAGE_STANDARD = gnu99;
610623
GCC_DYNAMIC_NO_PIC = NO;
624+
GCC_NO_COMMON_BLOCKS = YES;
611625
GCC_OPTIMIZATION_LEVEL = 0;
612626
GCC_PREPROCESSOR_DEFINITIONS = (
613627
"DEBUG=1",
@@ -635,18 +649,31 @@
635649
CLANG_CXX_LIBRARY = "libc++";
636650
CLANG_ENABLE_MODULES = YES;
637651
CLANG_ENABLE_OBJC_ARC = YES;
652+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
638653
CLANG_WARN_BOOL_CONVERSION = YES;
654+
CLANG_WARN_COMMA = YES;
639655
CLANG_WARN_CONSTANT_CONVERSION = YES;
656+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
640657
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
641658
CLANG_WARN_EMPTY_BODY = YES;
642659
CLANG_WARN_ENUM_CONVERSION = YES;
660+
CLANG_WARN_INFINITE_RECURSION = YES;
643661
CLANG_WARN_INT_CONVERSION = YES;
662+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
663+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
664+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
644665
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
666+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
667+
CLANG_WARN_STRICT_PROTOTYPES = YES;
668+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
669+
CLANG_WARN_UNREACHABLE_CODE = YES;
645670
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
646671
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
647672
COPY_PHASE_STRIP = YES;
648673
ENABLE_NS_ASSERTIONS = NO;
674+
ENABLE_STRICT_OBJC_MSGSEND = YES;
649675
GCC_C_LANGUAGE_STANDARD = gnu99;
676+
GCC_NO_COMMON_BLOCKS = YES;
650677
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
651678
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
652679
GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -678,6 +705,7 @@
678705
MODULE_NAME = ExampleApp;
679706
PRODUCT_BUNDLE_IDENTIFIER = org.mcstuido.JSONKitExample;
680707
PRODUCT_NAME = "$(TARGET_NAME)";
708+
RUN_CLANG_STATIC_ANALYZER = YES;
681709
WRAPPER_EXTENSION = app;
682710
};
683711
name = Debug;
@@ -699,6 +727,7 @@
699727
MODULE_NAME = ExampleApp;
700728
PRODUCT_BUNDLE_IDENTIFIER = org.mcstuido.JSONKitExample;
701729
PRODUCT_NAME = "$(TARGET_NAME)";
730+
RUN_CLANG_STATIC_ANALYZER = YES;
702731
WRAPPER_EXTENSION = app;
703732
};
704733
name = Release;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0930"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -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/MCJSONKit/Classes/ViewModel/WeiboViewModel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
/**
2222
获取当前登录用户及其所关注用户的最新微博
2323
*/
24-
- (void)requestFriendsTimeline:(void (^)())completion;
24+
- (void)requestFriendsTimeline:(void (^)(void))completion;
2525

2626
@end

Example/MCJSONKit/Classes/ViewModel/WeiboViewModel.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ - (BOOL)ssoAuthorizeRequest {
4242
return NO;
4343
}
4444

45-
- (void)requestFriendsTimeline:(void (^)())completion {
45+
- (void)requestFriendsTimeline:(void (^)(void))completion {
4646
[WeiboRequest startRequestWithURL:@"https://api.weibo.com/2/statuses/friends_timeline.json" completion:^(id obj) {
4747
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingMutableContainers error:nil];
4848
self.statuses = [StatuseModel arrayOfModelsFromKeyValues:dict[@"statuses"]];

Example/MCJSONKit/Classes/Views/LoadingView.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ - (instancetype)initWithFrame:(CGRect)frame
7272
if (CGSizeEqualToSize(frame.size, CGSizeZero)) {
7373
frame.size = CGSizeMake(48, 48);
7474
}
75-
if ([super initWithFrame:frame]) {
75+
if (self = [super initWithFrame:frame]) {
7676
[self initView];
7777
}
7878
return self;

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/Podfile.png

110 KB
Loading

0 commit comments

Comments
 (0)