Skip to content

Commit

Permalink
Added quick fix for non-darwin platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed Jun 12, 2023
1 parent acf0422 commit 3b2f390
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 21 deletions.
39 changes: 30 additions & 9 deletions Sources/CodableDatastore/Datastore/DatastoreDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ extension DatastoreDescriptor {
)

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

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

let proposedKey = childValue.projectedValue.key

let actualKey: String
if label.prefix(1) == "_" {
actualKey = "$\(label.dropFirst())"
if proposedKey.isEmpty {
if label.prefix(1) == "_" {
actualKey = "$\(label.dropFirst())"
} else {
actualKey = label
}
} else {
actualKey = label
actualKey = proposedKey
}

let indexDescriptor = IndexDescriptor(
Expand All @@ -149,7 +155,7 @@ extension DatastoreDescriptor {
)

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

Expand Down Expand Up @@ -180,11 +186,18 @@ extension DatastoreDescriptor.IndexDescriptor {
sampleInstance: CodedType,
keypath: KeyPath<CodedType, _AnyIndexed>
) {
let indexType = sampleInstance[keyPath: keypath].indexedType
let sampleIndexValue = sampleInstance[keyPath: keypath]
let indexType = sampleIndexValue.indexedType
let indexKey = sampleIndexValue.key

let fullKeyPath = String(describing: keypath)
let rootComponent = "\\\(String(describing: CodedType.self))."
let path = String(fullKeyPath.dropFirst(rootComponent.count))
let path: String
if indexKey.isEmpty {
path = keypath.keyName
} else {
var components = keypath.keyName.components(separatedBy: ".")
components[components.count-1] = indexKey
path = components.joined(separator: ".")
}

self.init(
version: version,
Expand All @@ -193,3 +206,11 @@ extension DatastoreDescriptor.IndexDescriptor {
)
}
}

extension PartialKeyPath {
fileprivate var keyName: String {
let fullKeyPath = String(describing: self)
let rootComponent = "\\\(String(describing: Root.self))."
return String(fullKeyPath.dropFirst(rootComponent.count))
}
}
18 changes: 17 additions & 1 deletion Sources/CodableDatastore/Indexes/Indexed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,25 @@ public struct Indexed<T> where T: Indexable {
/// This is ordinarily handled transparently when used as a property wrapper.
public var wrappedValue: T

/// The key this property is indexed under.
///
/// If an empty string, we'll try to determine it from the keypath name, but this can fail on platforms such as Linux
public var key: String

/// Initialize an ``Indexed`` value with an initial value.
///
/// This is ordinarily handled transparently when used as a property wrapper.
public init(wrappedValue: T) {
self.wrappedValue = wrappedValue
self.key = ""
}

/// Initialize an ``Indexed`` value with an initial value and key name.
///
/// This is ordinarily handled transparently when used as a property wrapper.
public init(wrappedValue: T, key: String) {
self.wrappedValue = wrappedValue
self.key = key
}

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

init<T>(indexed: Indexed<T>) {
self.indexed = indexed
indexedType = String(describing: T.self)
key = indexed.key
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public actor DiskPersistence<AccessMode: _AccessMode>: Persistence {

let persistenceName = "DefaultStore.persistencestore"

#if os(Linux)
#if !canImport(Darwin)
guard let applicationSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first else {
throw DiskPersistenceError.missingAppSupportDirectory
}
Expand Down
Loading

0 comments on commit 3b2f390

Please sign in to comment.