Skip to content

Commit 3b2f390

Browse files
Added quick fix for non-darwin platforms
1 parent acf0422 commit 3b2f390

File tree

6 files changed

+350
-21
lines changed

6 files changed

+350
-21
lines changed

Sources/CodableDatastore/Datastore/DatastoreDescriptor.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extension DatastoreDescriptor {
120120
)
121121

122122
/// If the type is identifiable, skip the `id` index as we always make one based on `id`
123-
if indexDescriptor.key == "$id" && sampleInstance is any Identifiable {
123+
if (indexDescriptor.key == "$id" || indexDescriptor.key == "id") && sampleInstance is any Identifiable {
124124
continue
125125
}
126126

@@ -135,11 +135,17 @@ extension DatastoreDescriptor {
135135
guard let label = child.label else { continue }
136136
guard let childValue = child.value as? any _IndexedProtocol else { continue }
137137

138+
let proposedKey = childValue.projectedValue.key
139+
138140
let actualKey: String
139-
if label.prefix(1) == "_" {
140-
actualKey = "$\(label.dropFirst())"
141+
if proposedKey.isEmpty {
142+
if label.prefix(1) == "_" {
143+
actualKey = "$\(label.dropFirst())"
144+
} else {
145+
actualKey = label
146+
}
141147
} else {
142-
actualKey = label
148+
actualKey = proposedKey
143149
}
144150

145151
let indexDescriptor = IndexDescriptor(
@@ -149,7 +155,7 @@ extension DatastoreDescriptor {
149155
)
150156

151157
/// If the type is identifiable, skip the `id` index as we always make one based on `id`
152-
if indexDescriptor.key == "$id" && sampleInstance is any Identifiable {
158+
if (indexDescriptor.key == "$id" || indexDescriptor.key == "id") && sampleInstance is any Identifiable {
153159
continue
154160
}
155161

@@ -180,11 +186,18 @@ extension DatastoreDescriptor.IndexDescriptor {
180186
sampleInstance: CodedType,
181187
keypath: KeyPath<CodedType, _AnyIndexed>
182188
) {
183-
let indexType = sampleInstance[keyPath: keypath].indexedType
189+
let sampleIndexValue = sampleInstance[keyPath: keypath]
190+
let indexType = sampleIndexValue.indexedType
191+
let indexKey = sampleIndexValue.key
184192

185-
let fullKeyPath = String(describing: keypath)
186-
let rootComponent = "\\\(String(describing: CodedType.self))."
187-
let path = String(fullKeyPath.dropFirst(rootComponent.count))
193+
let path: String
194+
if indexKey.isEmpty {
195+
path = keypath.keyName
196+
} else {
197+
var components = keypath.keyName.components(separatedBy: ".")
198+
components[components.count-1] = indexKey
199+
path = components.joined(separator: ".")
200+
}
188201

189202
self.init(
190203
version: version,
@@ -193,3 +206,11 @@ extension DatastoreDescriptor.IndexDescriptor {
193206
)
194207
}
195208
}
209+
210+
extension PartialKeyPath {
211+
fileprivate var keyName: String {
212+
let fullKeyPath = String(describing: self)
213+
let rootComponent = "\\\(String(describing: Root.self))."
214+
return String(fullKeyPath.dropFirst(rootComponent.count))
215+
}
216+
}

Sources/CodableDatastore/Indexes/Indexed.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,25 @@ public struct Indexed<T> where T: Indexable {
7070
/// This is ordinarily handled transparently when used as a property wrapper.
7171
public var wrappedValue: T
7272

73+
/// The key this property is indexed under.
74+
///
75+
/// If an empty string, we'll try to determine it from the keypath name, but this can fail on platforms such as Linux
76+
public var key: String
77+
7378
/// Initialize an ``Indexed`` value with an initial value.
7479
///
7580
/// This is ordinarily handled transparently when used as a property wrapper.
7681
public init(wrappedValue: T) {
7782
self.wrappedValue = wrappedValue
83+
self.key = ""
84+
}
85+
86+
/// Initialize an ``Indexed`` value with an initial value and key name.
87+
///
88+
/// This is ordinarily handled transparently when used as a property wrapper.
89+
public init(wrappedValue: T, key: String) {
90+
self.wrappedValue = wrappedValue
91+
self.key = key
7892
}
7993

8094
/// The projected value of the indexed property, which is a type-erased version of ourself.
@@ -87,12 +101,14 @@ public struct Indexed<T> where T: Indexable {
87101
///
88102
/// You should not reach for this directly, and instead use the @``Indexed`` property wrapper.
89103
public struct _AnyIndexed {
90-
var indexedType: String
91104
var indexed: any _IndexedProtocol
105+
var indexedType: String
106+
var key: String
92107

93108
init<T>(indexed: Indexed<T>) {
94109
self.indexed = indexed
95110
indexedType = String(describing: T.self)
111+
key = indexed.key
96112
}
97113
}
98114

Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public actor DiskPersistence<AccessMode: _AccessMode>: Persistence {
5151

5252
let persistenceName = "DefaultStore.persistencestore"
5353

54-
#if os(Linux)
54+
#if !canImport(Darwin)
5555
guard let applicationSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first else {
5656
throw DiskPersistenceError.missingAppSupportDirectory
5757
}

0 commit comments

Comments
 (0)