Skip to content

Commit

Permalink
Add migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
ApolloZhu committed Aug 16, 2019
1 parent 06c0c96 commit d17f8a0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,20 @@ var isNewUser: Bool
persistDefaultContent: true))
var hasPaidBefore: Bool
```

To migrate from another data type, you can use a migrator

```swift
@EFStorageComposition(
EFStorageUserDefaults<String>(forKey: "sameKey",
defaultsTo: "Nah"),
EFStorageMigrate(
from: EFStorageUserDefaults<Int>(
forKey: "sameKey",
defaultsTo: 1551,
persistDefaultContent: true),
by: { number in String(number) }
)
)
var mixedType: String
```
23 changes: 23 additions & 0 deletions Sources/EFStorageCore/EFStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,26 @@ public struct EFStorageComposition<A: EFStorage, B: EFStorage, Content>
self.b = b
}
}

/// Only for migration purposes. Setting the content/wrappedValue does nothing.
public struct EFStorageMigrate<A: EFStorage, OldContent, Content>
: EFStorage where A.Content == OldContent {
public typealias Migrator = (OldContent) -> Content

public var wrappedValue: Content {
get { return content ?? migrator(a.wrappedValue) }
set { }
}

public var content: Content? {
get { return a.content.map(migrator) }
set { }
}

private var a: A
private var migrator: Migrator
public init(from a: A, by migrator: @escaping Migrator) {
self.a = a
self.migrator = migrator
}
}
18 changes: 17 additions & 1 deletion Tests/EFStorageTests/EFStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ final class EFStorageTests: XCTestCase {
persistDefaultContent: true))
var hasPaidBefore: Bool


@EFStorageComposition(
EFStorageUserDefaults<String>(forKey: "sameKey", defaultsTo: "Nah"),
EFStorageMigrate(from: EFStorageUserDefaults<Int>(forKey: "sameKey",
defaultsTo: 1551,
persistDefaultContent: true),
by: { number in String(number) })
)
var mixedType: String

var storageText: EFStorageUserDefaultsRef<String> = UserDefaults.efStorage.text

func testExample() {
Expand All @@ -51,13 +61,19 @@ final class EFStorageTests: XCTestCase {
XCTAssertEqual(_text.wrappedValue, "meow")
_text.removeContentFromUnderlyingStorage()
XCTAssertEqual(text, EFStorageTests.defaultText)
// XCTAssertEqual(text, UserDefaults.efStorageContents.text)
XCTAssertEqual(text, UserDefaults.efStorage.text)
XCTAssertEqual(storageText.content, text)
let hasPaidBeforeRef: EFStorageUserDefaultsRef<Bool> = UserDefaults.efStorage.oldHasPaidBeforeKey
XCTAssertEqual(hasPaidBeforeRef.content, true)
XCTAssertEqual(UserDefaults.standard.bool(forKey: "oldHasPaidBeforeKey"), true)
debugPrint(efStorages)
XCTAssertEqual(hasPaidBefore, true)
XCTAssertEqual(mixedType, "1551")
mixedType = "Brand New"
XCTAssertTrue(UserDefaults.standard.object(forKey: "sameKey") is String, "IS NOT STRING")
XCTAssertFalse(UserDefaults.standard.object(forKey: "sameKey") is Int, "IS NUMBER")
XCTAssertEqual(mixedType, "Brand New")
XCTAssertEqual(UserDefaults.efStorage.sameKey, "Brand New")
}

static var allTests = [
Expand Down

0 comments on commit d17f8a0

Please sign in to comment.