Skip to content

Commit

Permalink
- Split private APIs
Browse files Browse the repository at this point in the history
- Add CoreData `inverse` test cases
  • Loading branch information
wolfcon committed Jan 7, 2022
1 parent 3cc1f70 commit f05ed88
Show file tree
Hide file tree
Showing 24 changed files with 644 additions and 543 deletions.
16 changes: 12 additions & 4 deletions MJExtension.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
0107509B26E8C5A300AAEA10 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0107509926E8C5A300AAEA10 /* LaunchScreen.storyboard */; };
010DF01E2761D90A0007EEF0 /* SpecialPropertyTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 010DF01D2761D90A0007EEF0 /* SpecialPropertyTypeTests.swift */; };
0130EE80233C56D8008D2386 /* MJFrenchUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 0130EE7F233C56D8008D2386 /* MJFrenchUser.m */; };
0130F2032782DFD200800FF3 /* NSString+MJExtension_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 0130F2022782DFA300800FF3 /* NSString+MJExtension_Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
0130F2032782DFD200800FF3 /* MJExtension_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 0130F2022782DFA300800FF3 /* MJExtension_Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
0130F2122784442200800FF3 /* MJCoreDataTester+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0130F20F2784441E00800FF3 /* MJCoreDataTester+CoreDataProperties.swift */; };
0179886C24EFA460007F7FBC /* MJTester.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0179886B24EFA460007F7FBC /* MJTester.swift */; };
0179887024EFA58B007F7FBC /* SwiftModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0179886F24EFA58B007F7FBC /* SwiftModelTests.swift */; };
018717B52787E6CC0024B7F9 /* MJCoreDataPerson+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 018717B32787E6CB0024B7F9 /* MJCoreDataPerson+CoreDataClass.swift */; };
018717B62787E6CC0024B7F9 /* MJCoreDataPerson +CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 018717B42787E6CC0024B7F9 /* MJCoreDataPerson +CoreDataProperties.swift */; };
01BB00FD277EC1FF002EF5B3 /* DeprecatedAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01BB00FC277EC1FF002EF5B3 /* DeprecatedAPITests.swift */; };
01BB0100277EE8DC002EF5B3 /* NSDate+MJExtension.h in Headers */ = {isa = PBXBuildFile; fileRef = 01BB00FE277EE8DB002EF5B3 /* NSDate+MJExtension.h */; settings = {ATTRIBUTES = (Public, ); }; };
01BB0101277EE8DC002EF5B3 /* NSDate+MJExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 01BB00FF277EE8DB002EF5B3 /* NSDate+MJExtension.m */; };
Expand Down Expand Up @@ -92,11 +94,13 @@
010DF01D2761D90A0007EEF0 /* SpecialPropertyTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialPropertyTypeTests.swift; sourceTree = "<group>"; };
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJFrenchUser.h; sourceTree = "<group>"; };
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJFrenchUser.m; sourceTree = "<group>"; };
0130F2022782DFA300800FF3 /* NSString+MJExtension_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+MJExtension_Private.h"; sourceTree = "<group>"; };
0130F2022782DFA300800FF3 /* MJExtension_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJExtension_Private.h; sourceTree = "<group>"; };
0130F20F2784441E00800FF3 /* MJCoreDataTester+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MJCoreDataTester+CoreDataProperties.swift"; sourceTree = "<group>"; };
0179886A24EFA460007F7FBC /* MJExtensionTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MJExtensionTests-Bridging-Header.h"; sourceTree = "<group>"; };
0179886B24EFA460007F7FBC /* MJTester.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MJTester.swift; sourceTree = "<group>"; };
0179886F24EFA58B007F7FBC /* SwiftModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftModelTests.swift; sourceTree = "<group>"; };
018717B32787E6CB0024B7F9 /* MJCoreDataPerson+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MJCoreDataPerson+CoreDataClass.swift"; sourceTree = "<group>"; };
018717B42787E6CC0024B7F9 /* MJCoreDataPerson +CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MJCoreDataPerson +CoreDataProperties.swift"; sourceTree = "<group>"; };
01BB00FC277EC1FF002EF5B3 /* DeprecatedAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeprecatedAPITests.swift; sourceTree = "<group>"; };
01BB00FE277EE8DB002EF5B3 /* NSDate+MJExtension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSDate+MJExtension.h"; sourceTree = "<group>"; };
01BB00FF277EE8DB002EF5B3 /* NSDate+MJExtension.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSDate+MJExtension.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -187,6 +191,8 @@
0107507626E88DD400AAEA10 /* MJCoreDataTestModel.xcdatamodeld */,
0107507926E88EAC00AAEA10 /* MJCoreDataTester+CoreDataClass.swift */,
0130F20F2784441E00800FF3 /* MJCoreDataTester+CoreDataProperties.swift */,
018717B32787E6CB0024B7F9 /* MJCoreDataPerson+CoreDataClass.swift */,
018717B42787E6CC0024B7F9 /* MJCoreDataPerson +CoreDataProperties.swift */,
);
path = CoreDataModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -267,7 +273,7 @@
2D2DBA062317DB32005A689E /* NSObject+MJKeyValue.m */,
2D2DBA142317DB33005A689E /* NSString+MJExtension.h */,
2D2DBA0B2317DB32005A689E /* NSString+MJExtension.mm */,
0130F2022782DFA300800FF3 /* NSString+MJExtension_Private.h */,
0130F2022782DFA300800FF3 /* MJExtension_Private.h */,
01BB00FE277EE8DB002EF5B3 /* NSDate+MJExtension.h */,
01BB00FF277EE8DB002EF5B3 /* NSDate+MJExtension.m */,
2D2DB9F62317DA64005A689E /* MJExtension.h */,
Expand Down Expand Up @@ -350,7 +356,7 @@
01F5515E2757144500518218 /* MJEClass.h in Headers */,
01BB0100277EE8DC002EF5B3 /* NSDate+MJExtension.h in Headers */,
2D2DB9F82317DA64005A689E /* MJExtension.h in Headers */,
0130F2032782DFD200800FF3 /* NSString+MJExtension_Private.h in Headers */,
0130F2032782DFD200800FF3 /* MJExtension_Private.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -573,7 +579,9 @@
01BB00FD277EC1FF002EF5B3 /* DeprecatedAPITests.swift in Sources */,
2D2DBA7D2317DBE0005A689E /* MJBook.m in Sources */,
0107507E26E890C100AAEA10 /* CoreDataTests.swift in Sources */,
018717B52787E6CC0024B7F9 /* MJCoreDataPerson+CoreDataClass.swift in Sources */,
0107507826E88DD400AAEA10 /* MJCoreDataTestModel.xcdatamodeld in Sources */,
018717B62787E6CC0024B7F9 /* MJCoreDataPerson +CoreDataProperties.swift in Sources */,
2D2DBA7A2317DBE0005A689E /* MJDog.m in Sources */,
01052EAD25F872D00049EC6F /* MultiThreadTests.swift in Sources */,
01BC91E427741956004E5265 /* MJCredential.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/MJEClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "MJExtensionPredefine.h"
#import "MJExtensionProtocols.h"
#import "MJProperty.h"
#import "MJExtension_Private.h"

typedef void (^MJClassesEnumeration)(Class c, BOOL *stop);

Expand All @@ -20,7 +21,6 @@ + (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration;

@implementation NSObject (MJEClass)

BOOL MJE_isFromFoundation(Class _Nonnull cls);
+ (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration {
if (enumeration == nil) return;
BOOL stop = NO;
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/MJExtensionProtocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@end

@protocol MJECoding <NSObject>
@protocol MJECoding <NSObject, NSCoding>
@optional
/// Array for those properties that should only be allowed to coding.
+ (NSArray *)mj_allowedCodingPropertyNames;
Expand Down
36 changes: 36 additions & 0 deletions MJExtension/MJExtension_Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// MJExtension_Private.h
// MJExtension
//
// Created by Frank on 2022/1/3.
// Copyright © 2022 MJ Lee. All rights reserved.
//

#ifndef MJExtension_Private_h
#define MJExtension_Private_h

NS_ASSUME_NONNULL_BEGIN
@interface NSString (MJExtension_Private)

@property (nonatomic, readonly) SEL mj_defaultSetter;

@end

@class MJPropertyKey;
@interface NSString (MJPropertyKey)

/// If JSON key is "xxx.xxx", so add one more key for it.
- (MJPropertyKey *)propertyKey;

/// Create keys with dot form, which is splitted by dot.
- (NSArray<MJPropertyKey *> *)mj_multiKeys;

@end

@interface NSObject(MJExtension_Private)

BOOL MJE_isFromFoundation(Class cls);

@end
NS_ASSUME_NONNULL_END
#endif /* MJExtension_Private_h */
61 changes: 1 addition & 60 deletions MJExtension/MJProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,10 @@
#import <objc/message.h>
#include "TargetConditionals.h"
#import "NSString+MJExtension.h"
#import "NSString+MJExtension_Private.h"
#import "MJExtension_Private.h"

#define mj_objGet(obj, type) mj_msgSendGet(obj, _getter, type)

@interface NSString (MJPropertyKey)

/// If JSON key is "xxx.xxx", so add one more key for it.
- (MJPropertyKey *)propertyKey;

/// Create keys with dot form, which is splitted by dot.
- (NSArray<MJPropertyKey *> *)mj_multiKeys;

@end

@implementation NSString (MJPropertyKey)

- (MJPropertyKey *)propertyKey {
MJPropertyKey *specialKey = [[MJPropertyKey alloc] init];
specialKey.name = self;
return specialKey;
}

- (NSArray<MJPropertyKey *> *)mj_multiKeys {
if (self.length == 0) return nil;

NSMutableArray *multiKeys = [NSMutableArray array];
// 如果有多级映射
NSArray *oldKeys = [self componentsSeparatedByString:@"."];

for (NSString *oldKey in oldKeys) {
NSUInteger start = [oldKey rangeOfString:@"["].location;
if (start != NSNotFound) { // 有索引的key
NSString *prefixKey = [oldKey substringToIndex:start];
NSString *indexKey = prefixKey;
if (prefixKey.length) {
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
propertyKey.name = prefixKey;
[multiKeys addObject:propertyKey];

indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
}

/** 解析索引 **/
// 元素
NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
for (NSInteger i = 0; i<cmps.count - 1; i++) {
MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
subPropertyKey.type = MJPropertyKeyTypeArray;
subPropertyKey.name = cmps[i];
[multiKeys addObject:subPropertyKey];
}
} else { // 没有索引的key
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
propertyKey.name = oldKey;
[multiKeys addObject:propertyKey];
}
}

return multiKeys;
}

@end

@import CoreData;

@interface MJProperty()
Expand Down
50 changes: 50 additions & 0 deletions MJExtension/MJPropertyKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "MJPropertyKey.h"
#import "MJExtension_Private.h"

@implementation MJPropertyKey

Expand All @@ -23,3 +24,52 @@ - (id)valueInObject:(id)object
return nil;
}
@end

@implementation NSString (MJPropertyKey)

- (MJPropertyKey *)propertyKey {
MJPropertyKey *specialKey = [[MJPropertyKey alloc] init];
specialKey.name = self;
return specialKey;
}

- (NSArray<MJPropertyKey *> *)mj_multiKeys {
if (self.length == 0) return nil;

NSMutableArray *multiKeys = [NSMutableArray array];
// 如果有多级映射
NSArray *oldKeys = [self componentsSeparatedByString:@"."];

for (NSString *oldKey in oldKeys) {
NSUInteger start = [oldKey rangeOfString:@"["].location;
if (start != NSNotFound) { // 有索引的key
NSString *prefixKey = [oldKey substringToIndex:start];
NSString *indexKey = prefixKey;
if (prefixKey.length) {
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
propertyKey.name = prefixKey;
[multiKeys addObject:propertyKey];

indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
}

/** 解析索引 **/
// 元素
NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
for (NSInteger i = 0; i<cmps.count - 1; i++) {
MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
subPropertyKey.type = MJPropertyKeyTypeArray;
subPropertyKey.name = cmps[i];
[multiKeys addObject:subPropertyKey];
}
} else { // 没有索引的key
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
propertyKey.name = oldKey;
[multiKeys addObject:propertyKey];
}
}

return multiKeys;
}

@end
9 changes: 0 additions & 9 deletions MJExtension/NSObject+MJKeyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@
*/
- (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context;

/**
* 通过模型数组来创建一个字典数组
* @param objectArray 模型数组
* @return 字典数组
*/
+ (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray;
+ (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray keys:(NSArray *)keys;
+ (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray ignoredKeys:(NSArray *)ignoredKeys;

#pragma mark - 字典转模型
/**
* 通过字典来创建一个模型
Expand Down
Loading

0 comments on commit f05ed88

Please sign in to comment.