Skip to content

Commit

Permalink
Support iteration in builtin SharedRepository implementations (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg authored Aug 21, 2023
1 parent 9ad506d commit 7462510
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/Spezi/SharedRepository/Builtin/HeapRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@ public final class HeapRepository<Anchor>: SharedRepository, BuiltinRepository {
collect0(allOf: type)
}
}


extension HeapRepository: Collection {
public typealias Index = Dictionary<ObjectIdentifier, AnyRepositoryValue>.Index

public var startIndex: Index {
storage.values.startIndex
}

public var endIndex: Index {
storage.values.endIndex
}

public func index(after index: Index) -> Index {
storage.values.index(after: index)
}


public subscript(position: Index) -> AnyRepositoryValue {
storage.values[position]
}
}
21 changes: 21 additions & 0 deletions Sources/Spezi/SharedRepository/Builtin/ValueRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,26 @@ public struct ValueRepository<Anchor>: SharedRepository, BuiltinRepository {
}
}

extension ValueRepository: Collection {
public typealias Index = Dictionary<ObjectIdentifier, AnyRepositoryValue>.Index

public var startIndex: Index {
storage.values.startIndex
}

public var endIndex: Index {
storage.values.endIndex
}

public func index(after index: Index) -> Index {
storage.values.index(after: index)
}


public subscript(position: Index) -> AnyRepositoryValue {
storage.values[position]
}
}


extension ValueRepository: @unchecked Sendable where Anchor: Sendable {}
7 changes: 7 additions & 0 deletions Sources/Spezi/SharedRepository/RepositoryValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/// Represents type erased ``RepositoryValue``.
public protocol AnyRepositoryValue {
/// This property gives access to a type-erased version of ``RepositoryValue/Source``
var anySource: any KnowledgeSource.Type { get }
/// This property gives access to a type-erased version of ``RepositoryValue/value``.
var anyValue: Any { get }
}
Expand All @@ -31,6 +33,11 @@ public protocol RepositoryValue<Source>: AnyRepositoryValue {


extension RepositoryValue {
/// The type erased ``RepositoryValue/Source``.
public var anySource: any KnowledgeSource.Type {
Source.self
}

/// The type erased ``RepositoryValue/value``.
public var anyValue: Any {
value
Expand Down
21 changes: 21 additions & 0 deletions Tests/SpeziTests/HelperTests/SharedRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,27 @@ final class SharedRepositoryTests: XCTestCase {
Self.optionalComputedValue = nil
}

func testValueRepositoryIteration() {
var repository = ValueRepository<TestAnchor>()
repository[TestStruct.self] = TestStruct(value: 3)
iterationTest(repository)
}

func testHeapRepositoryIteration() {
var repository = HeapRepository<TestAnchor>()
repository[TestStruct.self] = TestStruct(value: 3)
iterationTest(repository)
}

func iterationTest<Repository: SharedRepository<TestAnchor>>(_ repository: Repository)
where Repository: Collection, Repository.Element == AnyRepositoryValue {
for value in repository {
XCTAssertTrue(value.anySource is TestStruct.Type)
XCTAssertTrue(value.anyValue is TestStruct)
XCTAssertEqual(value.anyValue as? TestStruct, TestStruct(value: 3))
}
}

func testSetAndGet() {
repos.forEach { $0.testSetAndGet() }
}
Expand Down

0 comments on commit 7462510

Please sign in to comment.