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

Commit 6a75bff

Browse files
committed
Use CDKMapStrategy instead of CDKNoMapping value
1 parent 3ef3739 commit 6a75bff

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

CoreDataKit/Importing/NSPropertyDescription+Importing.swift

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,57 @@ import CoreData
1010

1111
extension NSPropertyDescription
1212
{
13+
var mapStrategy: MapStrategy {
14+
let fallbackStrategy = MapStrategy.Mapping
15+
16+
if let mappingStrategyString = userInfo?[MappingUserInfoKey] as? String {
17+
if let strategy = MapStrategy(rawValue: mappingStrategyString) {
18+
return strategy
19+
} else {
20+
CoreDataKit.sharedLogger(.ERROR, "Unsupported \(MappingUserInfoKey) given for \(entity.name).\(name), falling back to \(fallbackStrategy.rawValue) strategy")
21+
return fallbackStrategy
22+
}
23+
}
24+
25+
return fallbackStrategy
26+
}
27+
1328
/**
1429
Keys that could contain data for this property as defined by the model
1530

1631
:returns: Array of keys to look for when mapping data into this property
1732
*/
1833
var mappings: [String] {
19-
var _mappings = [String]()
34+
switch mapStrategy {
35+
case .NoMapping:
36+
return [String]()
2037

21-
// Fetch the unnumbered mapping
22-
if let unnumberedMapping = userInfo?[MappingUserInfoKey] as? String {
23-
if SuppressMappingUserInfoValue == unnumberedMapping {
24-
return [String]()
25-
} else {
38+
case .Mapping:
39+
var _mappings = [String]()
40+
41+
// Fetch the unnumbered mapping
42+
if let unnumberedMapping = userInfo?[MappingUserInfoKey] as? String {
2643
_mappings.append(unnumberedMapping)
2744
}
28-
}
2945

30-
// Fetch the numbered mappings
31-
for i in 0...MaxNumberedMappings+1 {
32-
if let numberedMapping = userInfo?[MappingUserInfoKey + ".\(i)"] as? String {
33-
_mappings.append(numberedMapping)
46+
// Fetch the numbered mappings
47+
for i in 0...MaxNumberedMappings+1 {
48+
if let numberedMapping = userInfo?[MappingUserInfoKey + ".\(i)"] as? String {
49+
_mappings.append(numberedMapping)
3450

35-
if i == MaxNumberedMappings+1 {
36-
CoreDataKit.sharedLogger(.WARN, "Only mappings up to \(MappingUserInfoKey).\(MaxNumberedMappings) mappings are supported, you defined more for \(entity.name).\(name)")
51+
if i == MaxNumberedMappings+1 {
52+
CoreDataKit.sharedLogger(.WARN, "Only mappings up to \(MappingUserInfoKey).\(MaxNumberedMappings) mappings are supported all others are ignored, you defined more for \(entity.name).\(name)")
53+
}
3754
}
3855
}
39-
}
4056

41-
// Fallback to the name of the property as a mapping if no mappings are defined
42-
if 0 == _mappings.count {
43-
_mappings.append(name)
57+
// Fallback to the name of the property as a mapping if no mappings are defined
58+
if 0 == _mappings.count {
59+
_mappings.append(name)
60+
}
61+
62+
return _mappings
4463
}
45-
46-
return _mappings
4764
}
4865

4966
/**

CoreDataKit/Importing/NSRelationshipDescription+Importing.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ extension NSRelationshipDescription {
1212

1313
/// Type of the relation as defined in the model
1414
var relationType: RelationType {
15+
let fallbackRelationType = RelationType.RelatedById
16+
1517
if let relationTypeString = userInfo?[RelationTypeUserInfoKey] as? String {
16-
return RelationType.fromString(relationTypeString)
18+
if let relationType = RelationType(rawValue: relationTypeString) {
19+
return relationType
20+
} else {
21+
CoreDataKit.sharedLogger(.ERROR, "Unsupported \(RelationTypeUserInfoKey) given for \(entity.name).\(name), falling back to \(fallbackRelationType.rawValue) relation type")
22+
return fallbackRelationType
23+
}
1724
}
1825

19-
return RelationType.RelatedById
26+
return fallbackRelationType
2027
}
2128
}

CoreDataKit/Importing/Types+Importing.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ let IdentifierUserInfoKey = "CDKId"
1818
/// Key used on property to define mapping in CoreData user info
1919
let MappingUserInfoKey = "CDKMap"
2020

21-
/// Value used with MappingUserInfoKey to disable all mapping behaviour
22-
let SuppressMappingUserInfoValue = "CDKNoMapping"
23-
2421
/// Maximum of numbered MappingUserInfoKeys on an property
25-
let MaxNumberedMappings = 10
22+
let MaxNumberedMappings = 9
23+
24+
/// Key used on property to define mapping strategy in CoreData user info
25+
let MapStrategyUserInfoKey = "CDKMapStrategy"
26+
27+
/// Type of mapping to use
28+
enum MapStrategy: String {
29+
/// Stategy to use default mapping behaviour with the available MappingUserInfoKey and fallbacks
30+
case Mapping = "CDKStandardMapping"
31+
32+
/// Strategy to disable all mapping behaviour
33+
case NoMapping = "CDKNoMapping"
34+
}
2635

2736
// MARK: Relations
2837

@@ -36,10 +45,6 @@ enum RelationType: String {
3645

3746
/// Relation that doesn't use a ID of some sort
3847
case Embedding = "CDKEmbedding"
39-
40-
static func fromString(string: String) -> RelationType {
41-
return RelationType(rawValue: string) ?? .RelatedById
42-
}
4348
}
4449

4550
// MARK: - Importable value

0 commit comments

Comments
 (0)