Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit e3bbd25

Browse files
committed
Add method to dump import configuration
1 parent 0d37e9c commit e3bbd25

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

CoreDataKit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
D50881A51A0ABF95007CA193 /* CoreDataStack+Importing.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50881A41A0ABF95007CA193 /* CoreDataStack+Importing.swift */; };
11+
D50881A71A0AC2CD007CA193 /* CoreDataStack+ImportingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50881A61A0AC2CD007CA193 /* CoreDataStack+ImportingTests.swift */; };
1012
D5093C6A19EF9B5700139262 /* NSPersistentStoreCoordinatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5093C6919EF9B5700139262 /* NSPersistentStoreCoordinatorTests.swift */; };
1113
D5098CDD1A052D9F000CC246 /* ManagedObjectObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5098CDC1A052D9F000CC246 /* ManagedObjectObserverTests.swift */; };
1214
D51AFE161965266000287A2D /* NSPersistentStoreCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D51AFE151965266000287A2D /* NSPersistentStoreCoordinator.swift */; };
@@ -60,6 +62,8 @@
6062
/* End PBXContainerItemProxy section */
6163

6264
/* Begin PBXFileReference section */
65+
D50881A41A0ABF95007CA193 /* CoreDataStack+Importing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CoreDataStack+Importing.swift"; sourceTree = "<group>"; };
66+
D50881A61A0AC2CD007CA193 /* CoreDataStack+ImportingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CoreDataStack+ImportingTests.swift"; sourceTree = "<group>"; };
6367
D5093C6919EF9B5700139262 /* NSPersistentStoreCoordinatorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSPersistentStoreCoordinatorTests.swift; sourceTree = "<group>"; };
6468
D5098CDC1A052D9F000CC246 /* ManagedObjectObserverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ManagedObjectObserverTests.swift; sourceTree = "<group>"; };
6569
D51AFE151965266000287A2D /* NSPersistentStoreCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSPersistentStoreCoordinator.swift; sourceTree = "<group>"; };
@@ -127,6 +131,7 @@
127131
D5090B3C19EDB8AA000D66C5 /* Importing */ = {
128132
isa = PBXGroup;
129133
children = (
134+
D50881A41A0ABF95007CA193 /* CoreDataStack+Importing.swift */,
130135
D540D82B19FE2A5E00769B84 /* Types+Importing.swift */,
131136
D566F6B119F0F277003C503F /* JsonDecode+Importing.swift */,
132137
D5B474B919F0181F004132F8 /* NSAttributeDescription+Importing.swift */,
@@ -232,6 +237,7 @@
232237
D5FD050419EFF68200AC0F9F /* Importing */ = {
233238
isa = PBXGroup;
234239
children = (
240+
D50881A61A0AC2CD007CA193 /* CoreDataStack+ImportingTests.swift */,
235241
D5FE0FBF19F286DF008DC1ED /* NSManagedObject+ImportingTests.swift */,
236242
);
237243
path = Importing;
@@ -353,6 +359,7 @@
353359
D5B474BA19F0181F004132F8 /* NSAttributeDescription+Importing.swift in Sources */,
354360
D566F6B219F0F277003C503F /* JsonDecode+Importing.swift in Sources */,
355361
D5C4B96719FEBB390058F0AC /* NSRelationshipDescription+Importing.swift in Sources */,
362+
D50881A51A0ABF95007CA193 /* CoreDataStack+Importing.swift in Sources */,
356363
D5253DA319F1398600E8B4AF /* NSManagedObjectContext+Importing.swift in Sources */,
357364
D595F68519EF0EA100D73790 /* CoreDataStack.swift in Sources */,
358365
D5253DA119F12F1C00E8B4AF /* NSManagedObject+Importing.swift in Sources */,
@@ -390,6 +397,7 @@
390397
D56E1EAC19FF749B006A280C /* EmployeeWithRelationWithoutId.swift in Sources */,
391398
D56E1EAE19FF749B006A280C /* Salary.swift in Sources */,
392399
D5FE0FC019F286DF008DC1ED /* NSManagedObject+ImportingTests.swift in Sources */,
400+
D50881A71A0AC2CD007CA193 /* CoreDataStack+ImportingTests.swift in Sources */,
393401
);
394402
runOnlyForDeploymentPostprocessing = 0;
395403
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// CoreDataStack+Importing.swift
3+
// CoreDataKit
4+
//
5+
// Created by Mathijs Kadijk on 05-11-14.
6+
// Copyright (c) 2014 Mathijs Kadijk. All rights reserved.
7+
//
8+
9+
import CoreData
10+
11+
extension CoreDataStack {
12+
/**
13+
Dumps the current import configuration to the logger
14+
*/
15+
public func dumpImportConfiguration() {
16+
let entityUserInfoKeys = [IdentifierUserInfoKey]
17+
18+
for (entityName, _entity) in persistentStoreCoordinator.managedObjectModel.entitiesByName {
19+
CoreDataKit.sharedLogger(.DEBUG, " ")
20+
21+
let entity = _entity as NSEntityDescription
22+
CoreDataKit.sharedLogger(.DEBUG, "\(entityName):")
23+
24+
for (_key, value) in entity.userInfo! {
25+
let key = _key as String
26+
27+
if !contains(entityUserInfoKeys, key) {
28+
CoreDataKit.sharedLogger(.DEBUG, "\(key)\(value)")
29+
}
30+
}
31+
32+
let optionalIdentifyingAttribute = entity.identifyingAttribute().value()
33+
if let identifyingAttibute = optionalIdentifyingAttribute {
34+
dumpPropertyDescription(identifyingAttibute, asIdentifyingAttribute: true)
35+
}
36+
37+
for (_, attribute) in entity.attributesByName {
38+
dumpPropertyDescription(attribute as NSPropertyDescription)
39+
}
40+
41+
for (_, relationship) in entity.relationshipsByName {
42+
dumpPropertyDescription(relationship as NSPropertyDescription)
43+
}
44+
}
45+
}
46+
47+
private func dumpPropertyDescription(property: NSPropertyDescription, asIdentifyingAttribute: Bool = false) {
48+
var propertyUserInfoKeys = [MappingUserInfoKey]
49+
for i in 0...MaxNumberedMappings+1 {
50+
propertyUserInfoKeys.append(MappingUserInfoKey + ".\(i)")
51+
}
52+
53+
let attributeUserInfoKeys: [String] = []
54+
let relationshipUserInfoKeys = [RelationTypeUserInfoKey]
55+
56+
let identifying = asIdentifyingAttribute ? "" : " "
57+
let indexed = property.indexed ? "" : ""
58+
let optional = property.optional ? "?" : ""
59+
let relationshipType = (property as? NSRelationshipDescription)?.relationType.rawValue
60+
let relationshipTypeDescription = relationshipType == nil ? "" : "\(relationshipType!)"
61+
62+
CoreDataKit.sharedLogger(.DEBUG, "\(identifying)\(indexed)\(property.name)\(optional)\(property.mappings)\(relationshipTypeDescription)")
63+
64+
for (_key, value) in property.userInfo! {
65+
let key = _key as String
66+
67+
if !contains(propertyUserInfoKeys, key) {
68+
if (property is NSAttributeDescription && !contains(attributeUserInfoKeys, key)) ||
69+
(property is NSRelationshipDescription && !contains(relationshipUserInfoKeys, key)) {
70+
CoreDataKit.sharedLogger(.DEBUG, "\(key)\(value)")
71+
}
72+
}
73+
}
74+
}
75+
}

CoreDataKitTests/CoreDataStackTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ class CoreDataStackTests: TestCase {
2323
XCTAssertNotNil(coreDataStack.mainThreadContext.parentContext, "Missing parent context")
2424
XCTAssertEqual(coreDataStack.mainThreadContext.parentContext!, coreDataStack.rootContext, "Incorrect parent context")
2525
}
26+
27+
func testDumpStack() {
28+
coreDataStack.dumpStack()
29+
}
2630
}

CoreDataKitTests/Fixtures/Model.xcdatamodeld/Model.xcdatamodel/contents

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="6252" systemVersion="14A388a" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
33
<entity name="Car" representedClassName="CoreDataKitTests.Car" syncable="YES">
44
<attribute name="color" attributeType="String" syncable="YES"/>
5+
<attribute name="name" optional="YES" attributeType="String" syncable="YES">
6+
<userInfo>
7+
<entry key="CDKMap" value="CDKNoMapping"/>
8+
<entry key="CDKTypo" value="SomeValue"/>
9+
</userInfo>
10+
</attribute>
511
<attribute name="plate" attributeType="String" syncable="YES"/>
612
<relationship name="owners" toMany="YES" deletionRule="Nullify" destinationEntity="EmployeeWithRelations" inverseName="cars" inverseEntity="EmployeeWithRelations" syncable="YES"/>
713
<userInfo>
814
<entry key="CDKId" value="plate"/>
15+
<entry key="CSKId" value="plate"/>
916
</userInfo>
1017
</entity>
1118
<entity name="Employee" representedClassName="CoreDataKitTests.Employee" syncable="YES">
@@ -55,7 +62,7 @@
5562
<relationship name="employee" maxCount="1" deletionRule="Nullify" destinationEntity="EmployeeWithRelationWithoutId" inverseName="salary" inverseEntity="EmployeeWithRelationWithoutId" syncable="YES"/>
5663
</entity>
5764
<elements>
58-
<element name="Car" positionX="-36" positionY="18" width="128" height="88"/>
65+
<element name="Car" positionX="-36" positionY="18" width="128" height="105"/>
5966
<element name="Employee" positionX="-63" positionY="-18" width="128" height="60"/>
6067
<element name="EmployeeImportable" positionX="-63" positionY="-9" width="128" height="90"/>
6168
<element name="EmployeeWithRelations" positionX="-54" positionY="9" width="128" height="88"/>

0 commit comments

Comments
 (0)