Update Models + docs
Summary: - Sources: Models - Docs: README Stats: - 3 files changed, 16 insertions(+), 10 deletions(-)
This commit is contained in:
parent
bd96135d3b
commit
91aa0df7f4
@ -161,8 +161,8 @@ LocalData can generate a catalog of all configured storage keys, even if no data
|
||||
struct AppStorageCatalog: StorageKeyCatalog {
|
||||
static var allKeys: [StorageKeyDescriptor] {
|
||||
[
|
||||
.from(StorageKeys.AppVersionKey(), serializer: "json"),
|
||||
.from(StorageKeys.UserPreferencesKey(), serializer: "json")
|
||||
.from(StorageKeys.AppVersionKey(), serializer: .json),
|
||||
.from(StorageKeys.UserPreferencesKey(), serializer: .json)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,39 +3,45 @@ import Foundation
|
||||
public struct Serializer<Value: Codable & Sendable>: Sendable {
|
||||
public let encode: @Sendable (Value) throws -> Data
|
||||
public let decode: @Sendable (Data) throws -> Value
|
||||
public let name: String
|
||||
|
||||
public init(
|
||||
encode: @escaping @Sendable (Value) throws -> Data,
|
||||
decode: @escaping @Sendable (Data) throws -> Value
|
||||
decode: @escaping @Sendable (Data) throws -> Value,
|
||||
name: String = "custom"
|
||||
) {
|
||||
self.encode = encode
|
||||
self.decode = decode
|
||||
self.name = name
|
||||
}
|
||||
|
||||
public static var json: Serializer<Value> {
|
||||
Serializer<Value>(
|
||||
encode: { try JSONEncoder().encode($0) },
|
||||
decode: { try JSONDecoder().decode(Value.self, from: $0) }
|
||||
decode: { try JSONDecoder().decode(Value.self, from: $0) },
|
||||
name: "json"
|
||||
)
|
||||
}
|
||||
|
||||
public static var plist: Serializer<Value> {
|
||||
Serializer<Value>(
|
||||
encode: { try PropertyListEncoder().encode($0) },
|
||||
decode: { try PropertyListDecoder().decode(Value.self, from: $0) }
|
||||
decode: { try PropertyListDecoder().decode(Value.self, from: $0) },
|
||||
name: "plist"
|
||||
)
|
||||
}
|
||||
|
||||
public static func custom(
|
||||
encode: @escaping @Sendable (Value) throws -> Data,
|
||||
decode: @escaping @Sendable (Data) throws -> Value
|
||||
decode: @escaping @Sendable (Data) throws -> Value,
|
||||
name: String = "custom"
|
||||
) -> Serializer<Value> {
|
||||
Serializer<Value>(encode: encode, decode: decode)
|
||||
Serializer<Value>(encode: encode, decode: decode, name: name)
|
||||
}
|
||||
}
|
||||
|
||||
public extension Serializer where Value == Data {
|
||||
static var data: Serializer<Value> {
|
||||
Serializer<Value>(encode: { $0 }, decode: { $0 })
|
||||
Serializer<Value>(encode: { $0 }, decode: { $0 }, name: "data")
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,14 +35,14 @@ public struct StorageKeyDescriptor: Sendable {
|
||||
|
||||
public static func from<Key: StorageKey>(
|
||||
_ key: Key,
|
||||
serializer: String,
|
||||
serializer: Serializer<Key.Value>,
|
||||
notes: String? = nil
|
||||
) -> StorageKeyDescriptor {
|
||||
StorageKeyDescriptor(
|
||||
name: key.name,
|
||||
domain: key.domain,
|
||||
security: key.security,
|
||||
serializer: serializer,
|
||||
serializer: serializer.name,
|
||||
valueType: String(describing: Key.Value.self),
|
||||
owner: key.owner,
|
||||
availability: key.availability,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user