-
Notifications
You must be signed in to change notification settings - Fork 224
Add ability to transform values on migrate #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
043f4c9
1676cf5
5ebeebe
63d1611
7b89056
997b144
d5312fe
99873ee
d2a8ecc
f1bedd9
abb6d42
f62a360
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,134 @@ | ||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||
| // MigratableKeyValuePair.swift | ||||||||||||||||||||||||||||||||||||||
| // Valet | ||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||
| // Created by Dan Federman on 5/20/20. | ||||||||||||||||||||||||||||||||||||||
| // Copyright © 2020 Square, Inc. | ||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||
| // limitations under the License. | ||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import Foundation | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// A struct that represented a key:value pair that can be migrated. | ||||||||||||||||||||||||||||||||||||||
| public struct MigratableKeyValuePair<Key: Hashable>: Hashable { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Initialization | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// Creates a migratable key:value pair with the provided inputs. | ||||||||||||||||||||||||||||||||||||||
| /// - Parameters: | ||||||||||||||||||||||||||||||||||||||
| /// - key: The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| /// - value: The value in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| public init(key: Key, value: Data) { | ||||||||||||||||||||||||||||||||||||||
| self.key = key | ||||||||||||||||||||||||||||||||||||||
| self.value = value | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// Creates a migratable key:value pair with the provided inputs. | ||||||||||||||||||||||||||||||||||||||
| /// - Parameters: | ||||||||||||||||||||||||||||||||||||||
| /// - key: The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| /// - value: The desired value in the key:value pair, represented as a String. | ||||||||||||||||||||||||||||||||||||||
| public init(key: Key, value: String) { | ||||||||||||||||||||||||||||||||||||||
| self.key = key | ||||||||||||||||||||||||||||||||||||||
| self.value = Data(value.utf8) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Public | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| public let key: Key | ||||||||||||||||||||||||||||||||||||||
| /// The value in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| public let value: Data | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: - Objective-C Compatibility | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @objc(VALMigratableKeyValuePairInput) | ||||||||||||||||||||||||||||||||||||||
| public final class ObjectiveCCompatibilityMigratableKeyValuePairInput: NSObject { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Initialization | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| internal init(key: Any, value: Data) { | ||||||||||||||||||||||||||||||||||||||
| self.key = key | ||||||||||||||||||||||||||||||||||||||
| self.value = value | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Public | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public let key: Any | ||||||||||||||||||||||||||||||||||||||
| /// The value in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public let value: Data | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @objc(VALMigratableKeyValuePairOutput) | ||||||||||||||||||||||||||||||||||||||
| public class ObjectiveCCompatibilityMigratableKeyValuePairOutput: NSObject { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Initialization | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// Creates a migratable key:value pair with the provided inputs. | ||||||||||||||||||||||||||||||||||||||
| /// - Parameters: | ||||||||||||||||||||||||||||||||||||||
| /// - key: The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| /// - value: The value in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public init(key: String, value: Data) { | ||||||||||||||||||||||||||||||||||||||
| self.key = key | ||||||||||||||||||||||||||||||||||||||
| self.value = value | ||||||||||||||||||||||||||||||||||||||
| preventMigration = false | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// Creates a migratable key:value pair with the provided inputs. | ||||||||||||||||||||||||||||||||||||||
| /// - Parameters: | ||||||||||||||||||||||||||||||||||||||
| /// - key: The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| /// - stringValue: The desired value in the key:value pair, represented as a String. | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public init(key: String, stringValue: String) { | ||||||||||||||||||||||||||||||||||||||
| self.key = key | ||||||||||||||||||||||||||||||||||||||
| self.value = Data(stringValue.utf8) | ||||||||||||||||||||||||||||||||||||||
| preventMigration = false | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Public Static Methods | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// A sentinal `ObjectiveCCompatibilityMigratableKeyValuePairOutput` that conveys that the migration should be prevented. | ||||||||||||||||||||||||||||||||||||||
| @available(swift, obsoleted: 1.0) | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public static func preventMigration() -> ObjectiveCCompatibilityMigratableKeyValuePairOutput { | ||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method sounds to me more than its actually doing the action to prevent migration on an instance rather than creating an instance that prevents the migration. Maybe
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah the tough thing here is it isn't super clear that we're doing this work before the migration has started. This is why I wanted to use a future tense. A past-tense phrase here might make it read as if returning it prevented/disabled the migration of just this pair (or future pairs). In the context where it's used, I hope this method reads well: Valet/Tests/ValetObjectiveCBridgeTests/VALValetTests.m Lines 475 to 484 in c7cec75
The documentation on the Valet/Sources/Valet/Valet.swift Lines 661 to 668 in c7cec75
While I agree that reading this method by itself is a bit confusing, I think within the context it's supposed to be used it makes sense? Let me know if not @fdiaz.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, I should make this method available only to Objective-C |
||||||||||||||||||||||||||||||||||||||
| ObjectiveCCompatibilityPreventMigrationOutput() | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Public | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// The key in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public let key: String | ||||||||||||||||||||||||||||||||||||||
| /// The value in the key:value pair. | ||||||||||||||||||||||||||||||||||||||
| @objc | ||||||||||||||||||||||||||||||||||||||
| public let value: Data | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // MARK: Internal | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| internal fileprivate(set) var preventMigration: Bool | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private final class ObjectiveCCompatibilityPreventMigrationOutput: ObjectiveCCompatibilityMigratableKeyValuePairOutput { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| init() { | ||||||||||||||||||||||||||||||||||||||
| super.init(key: "", stringValue: "") | ||||||||||||||||||||||||||||||||||||||
| preventMigration = true | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll force the
keyto be aStringlater on as part of thecompactMap. For now, we just want to make sure we have a key.