Skip to content

Commit

Permalink
Merge pull request #19 from MC-Studio/mapper
Browse files Browse the repository at this point in the history
增加字段映射宏定义,提升安全保护
  • Loading branch information
mylcode authored Aug 1, 2018
2 parents c339b64 + 02c56b2 commit e628157
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 81 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### 更新日志

### 2018-06-04

- 增加属性映射宏定义MCProperty,支持正确性校验

### 2018-5-12
- 增加单测,提升代码覆盖率
- 增加代码覆盖率badge
Expand Down
35 changes: 32 additions & 3 deletions Example/MCJSONKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = MC;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0930;
ORGANIZATIONNAME = "MC-Studio";
TargetAttributes = {
6003F5AD195388D20070C39A = {
Expand Down Expand Up @@ -504,12 +504,13 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-MCJSONKit_Example/Pods-MCJSONKit_Example-resources.sh",
$PODS_CONFIGURATION_BUILD_DIR/MWPhotoBrowser/MWPhotoBrowser.bundle,
"${PODS_CONFIGURATION_BUILD_DIR}/MWPhotoBrowser/MWPhotoBrowser.bundle",
"${PODS_ROOT}/Weibo_SDK/libWeiboSDK/WeiboSDK.bundle",
);
name = "[CP] Copy Pods Resources";
outputPaths = (
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MWPhotoBrowser.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/WeiboSDK.bundle",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -595,19 +596,32 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -635,18 +649,31 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand Down Expand Up @@ -678,6 +705,7 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = org.mcstuido.JSONKitExample;
PRODUCT_NAME = "$(TARGET_NAME)";
RUN_CLANG_STATIC_ANALYZER = YES;
WRAPPER_EXTENSION = app;
};
name = Debug;
Expand All @@ -699,6 +727,7 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = org.mcstuido.JSONKitExample;
PRODUCT_NAME = "$(TARGET_NAME)";
RUN_CLANG_STATIC_ANALYZER = YES;
WRAPPER_EXTENSION = app;
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -34,7 +34,7 @@
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "45DFEAAC3D7F63C7A79820E622594E53"
BlueprintIdentifier = "C9BF0B25B60BCEB920B78CBEB903E4A2"
BuildableName = "libMCJSONKit.a"
BlueprintName = "MCJSONKit"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
Expand Down
12 changes: 4 additions & 8 deletions Example/MCJSONKit/Classes/Models/StatuseModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

@implementation StatuseModel

+ (NSDictionary *)keyMappingDictionary {
return @{@"wid":@"id",
@"visible":@"visible.type",
@"listid":@"visible.list_id"};
- (NSDictionary *)keyMappingDictionary {
return @{MYPropMapper(wid):@"id",
MYPropMapper(visible):@"visible.type",
MYPropMapper(listid):@"visible.list_id"};
}

/*+ (NSArray *)allowedPropertyNames {
return @[@"visible",@"listid"];
}*/

@end
8 changes: 4 additions & 4 deletions Example/MCJSONKit/Classes/Models/UserModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

@implementation UserModel

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

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

@end
11 changes: 7 additions & 4 deletions Example/MCJSONKit/Classes/Models/WeiboModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//

#import <Foundation/Foundation.h>
#import <MCJSONKit/MCJSONKitDefine.h>

#define MYPropMapper(name) MCProperty(name)

@interface WeiboModel : NSObject

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

/**
忽略的属性集合
*/
+ (NSSet *)ignoreSet;
- (NSSet *)ignoreSet;

/**
key关联字段
@return key:对象属性 value:keyPath
*/
+ (NSDictionary *)keyMappingDictionary;
- (NSDictionary *)keyMappingDictionary;

/**
类型关联字典
@return key:对象属性 value:类型class
*/
+ (NSDictionary *)typeMappingDictionary;
- (NSDictionary *)typeMappingDictionary;
- (NSDictionary *)toDictionary;
- (NSString *)toJSONString;

Expand Down
32 changes: 16 additions & 16 deletions Example/MCJSONKit/Classes/Models/WeiboModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ + (NSArray *)arrayOfModelsFromKeyValues:(id)keyValues {
return [self mc_arrayOfModelsFromKeyValues:keyValues];
}

+ (NSSet *)allowedPropertyNames {
- (NSSet *)allowedPropertyNames {
return nil;
}

+ (NSDictionary *)keyMappingDictionary {
- (NSDictionary *)keyMappingDictionary {
return nil;
}

+ (NSDictionary *)typeMappingDictionary {
- (NSDictionary *)typeMappingDictionary {
return nil;
}

+ (NSSet *)ignoreSet {
- (NSSet *)ignoreSet {
return nil;
}

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

#pragma mark JSONCoreConfig

+ (NSSet *)mc_allowedPropertiesSet {
- (NSSet *)mc_allowedPropertiesSet {
return [self allowedPropertyNames];
}

+ (NSDictionary *)mc_keyMappingDictionary {
- (NSDictionary *)mc_keyMappingDictionary {
return [self keyMappingDictionary];
}

+ (NSDictionary *)mc_typeMappingDictionary {
- (NSDictionary *)mc_typeMappingDictionary {
return [self typeMappingDictionary];
}

+ (NSSet *)mc_ignorePropertiesSet {
- (NSSet *)mc_ignorePropertiesSet {
return [self ignoreSet];
}

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

+ (NSSet *)allowedPropertyNames {
- (NSSet *)allowedPropertyNames {
return nil;
}

+ (NSDictionary *)keyMappingDictionary {
- (NSDictionary *)keyMappingDictionary {
return nil;
}

+ (NSDictionary *)typeMappingDictionary {
- (NSDictionary *)typeMappingDictionary {
return nil;
}

+ (NSSet *)ignoreSet {
- (NSSet *)ignoreSet {
return nil;
}

Expand All @@ -119,19 +119,19 @@ - (NSString *)toJSONString {
return [self mj_JSONString];
}

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

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
- (NSDictionary *)mj_replacedKeyFromPropertyName {
return [self keyMappingDictionary];
}

+ (NSDictionary *)mj_objectClassInArray {
- (NSDictionary *)mj_objectClassInArray {
return [self typeMappingDictionary];
}

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

Expand Down
16 changes: 8 additions & 8 deletions Example/MCJSONKit/Classes/Models/WeiboResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@

@implementation WeiboResult

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

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

@end

@implementation WeiboResult1

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

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

@end
2 changes: 1 addition & 1 deletion Example/MCJSONKit/Classes/ViewModel/WeiboViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
/**
获取当前登录用户及其所关注用户的最新微博
*/
- (void)requestFriendsTimeline:(void (^)())completion;
- (void)requestFriendsTimeline:(void (^)(void))completion;

@end
2 changes: 1 addition & 1 deletion Example/MCJSONKit/Classes/ViewModel/WeiboViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (BOOL)ssoAuthorizeRequest {
return NO;
}

- (void)requestFriendsTimeline:(void (^)())completion {
- (void)requestFriendsTimeline:(void (^)(void))completion {
[WeiboRequest startRequestWithURL:@"https://api.weibo.com/2/statuses/friends_timeline.json" completion:^(id obj) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingMutableContainers error:nil];
self.statuses = [StatuseModel arrayOfModelsFromKeyValues:dict[@"statuses"]];
Expand Down
2 changes: 1 addition & 1 deletion Example/MCJSONKit/Classes/Views/LoadingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (instancetype)initWithFrame:(CGRect)frame
if (CGSizeEqualToSize(frame.size, CGSizeZero)) {
frame.size = CGSizeMake(48, 48);
}
if ([super initWithFrame:frame]) {
if (self = [super initWithFrame:frame]) {
[self initView];
}
return self;
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ SPEC CHECKSUMS:
Kiwi: fbeafef0f00e4d8f7dcb3420a4930afe70af77f7
MBProgressHUD: 1569cf7ace17a8bac47aabfbb8580a49690386d1
MCFoundation: f03b2f02918ed3d8e6c30cef7ce030cf0b93f9aa
MCJSONKit: b1cc47768aebeef5e3335d1dcc3b0e5d04bc0e79
MCJSONKit: af26a8cc55a39d9ad42fab9f5ef339495e292bc3
MCLogger: f258a43a313bfeabcd85781847626a6f51e6a715
MCUIKit: 4d9fb8d30b0cf44bf99605ecd248a3092d6f4e13
MJExtension: 5932755f451458eefa24239358817f8d291240c7
Expand All @@ -59,4 +59,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 2f2c60f27931b02540794580c6f3c54cfcf585ce

COCOAPODS: 1.3.1
COCOAPODS: 1.4.0
Binary file added Example/Podfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e628157

Please sign in to comment.