Skip to content

Commit bddb522

Browse files
authored
Disfavor appStorage's codable strategies (#185)
In the case in which they overlap `RawRepresentable` we should favor `RawRepresentable`, which preserves existing behavior and is more performant and succinct in storage. Fixes #184.
1 parent 83ff583 commit bddb522

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Sources/Sharing/SharedKeys/AppStorageKey.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@
109109
AppStorageKey(key, store: store)
110110
}
111111

112-
/// Creates a shared key that can read and write a Codable value to user defaults.
112+
/// Creates a shared key that can read and write a codable value to user defaults.
113113
///
114114
/// - Parameters:
115115
/// - key: The key to read and write the value to in the user defaults store.
116116
/// - store: The user defaults store to read and write to. A value of `nil` will use the user
117117
/// default store from dependencies.
118118
/// - Returns: A user defaults shared key.
119+
@_disfavoredOverload
119120
public static func appStorage<Value: Codable>(
120121
_ key: String, store: UserDefaults? = nil
121122
) -> Self
@@ -249,13 +250,14 @@
249250
AppStorageKey(key, store: store)
250251
}
251252

252-
/// Creates a shared key that can read and write a Codable value to user defaults.
253+
/// Creates a shared key that can read and write a codable value to user defaults.
253254
///
254255
/// - Parameters:
255256
/// - key: The key to read and write the value to in the user defaults store.
256257
/// - store: The user defaults store to read and write to. A value of `nil` will use the user
257258
/// default store from dependencies.
258259
/// - Returns: A user defaults shared key.
260+
@_disfavoredOverload
259261
public static func appStorage<Value: Codable>(
260262
_ key: String, store: UserDefaults? = nil
261263
) -> Self
@@ -373,7 +375,7 @@
373375
fileprivate init(_ key: String, store: UserDefaults?) where Value == Date {
374376
self.init(lookup: CastableLookup(), key: key, store: store)
375377
}
376-
378+
377379
fileprivate init(_ key: String, store: UserDefaults?) where Value: Codable {
378380
self.init(lookup: CodableLookup(), key: key, store: store)
379381
}

Tests/SharingTests/AppStorageTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@
104104
#expect(id == ID(rawValue: "Blob, Jr."))
105105
}
106106

107+
@Test func rawRepresentableCodableString() {
108+
struct ID: Codable, RawRepresentable {
109+
var rawValue: String
110+
}
111+
@Shared(.appStorage("raw-representable-string")) var id = ID(rawValue: "Blob")
112+
#expect(store.string(forKey: "raw-representable-string") == "Blob")
113+
store.set("Blob, Jr.", forKey: "raw-representable-string")
114+
#expect(id == ID(rawValue: "Blob, Jr."))
115+
}
116+
107117
@Test func optional() {
108118
@Shared(.appStorage("bool")) var bool: Bool?
109119
#expect(store.value(forKey: "bool") == nil)

0 commit comments

Comments
 (0)