Skip to content

Commit

Permalink
fix(store): Fixed storeAlias not storing relationships (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjechris authored Oct 11, 2023
1 parent 534112b commit 24903df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
20 changes: 8 additions & 12 deletions Sources/CohesionKit/Identity/IdentityStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public class IdentityMap {
}
}

func nodeStore<T: Identifiable>(entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
func nodeStore<T: Identifiable>(in node: EntityNode<T>? = nil, entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = node ?? storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
registry.enqueueChange(for: $0)
}]

Expand All @@ -167,8 +167,8 @@ public class IdentityMap {
return node
}

func nodeStore<T: Aggregate>(entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
func nodeStore<T: Aggregate>(in node: EntityNode<T>? = nil, entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = node ?? storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
registry.enqueueChange(for: $0)
}]

Expand Down Expand Up @@ -196,16 +196,12 @@ public class IdentityMap {

private func storeAlias<T>(content: T, key: AliasKey<T>, modifiedAt: Stamp?) {
let aliasNode = refAliases[safe: key]
let aliasContainer = AliasContainer(key: key, content: content)

do {
try aliasNode.updateEntity(AliasContainer(key: key, content: content), modifiedAt: modifiedAt)
_ = nodeStore(in: aliasNode, entity: aliasContainer, modifiedAt: modifiedAt)

registry.enqueueChange(for: aliasNode)
logger?.didRegisterAlias(key)
}
catch {

}
registry.enqueueChange(for: aliasNode)
logger?.didRegisterAlias(key)
}

private func transaction<T>(_ body: () -> T) -> T {
Expand Down
38 changes: 21 additions & 17 deletions Sources/CohesionKit/Storage/AliasContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,36 @@ struct AliasContainer<T>: Identifiable, Aggregate {
var content: T?
}

extension AliasContainer where T: Aggregate {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
extension AliasContainer {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<Self>] {
if let self = self as? IdentifiableKeyPathsEraser {
return self.erasedEntitiesKeyPaths as! [PartialIdentifiableKeyPath<Self>]
}

if let self = self as? CollectionIdentifiableKeyPathsEraser {
return self.erasedEntitiesKeyPaths as! [PartialIdentifiableKeyPath<Self>]
}

return []
}
}

extension AliasContainer where T: Identifiable {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
}
private protocol IdentifiableKeyPathsEraser {
var erasedEntitiesKeyPaths: [Any] { get }
}

extension AliasContainer where T: MutableCollection, T.Element: Aggregate, T.Index: Hashable {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
extension AliasContainer: IdentifiableKeyPathsEraser where T: Identifiable {
var erasedEntitiesKeyPaths: [Any] {
[PartialIdentifiableKeyPath<Self>(\.content)]
}
}

extension AliasContainer where T: MutableCollection, T.Element: Identifiable, T.Index: Hashable {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
}
private protocol CollectionIdentifiableKeyPathsEraser {
var erasedEntitiesKeyPaths: [Any] { get }
}

extension AliasContainer {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[]
extension AliasContainer: CollectionIdentifiableKeyPathsEraser where T: MutableCollection, T.Element: Identifiable, T.Index: Hashable {
var erasedEntitiesKeyPaths: [Any] {
[PartialIdentifiableKeyPath<Self>(\.content)]
}
}
19 changes: 18 additions & 1 deletion Tests/CohesionKitTests/IdentityMapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class IdentityMapTests: XCTestCase {
wait(for: [expectation], timeout: 0)
}

func test_storeAlias_itEnqueuesAliasInRegistry() {
func test_storeAggregate_named_itEnqueuesAliasInRegistry() {
let root = SingleNodeFixture(id: 1)
let registry = ObserverRegistryStub()
let identityMap = IdentityMap(registry: registry)
Expand Down Expand Up @@ -218,6 +218,23 @@ extension IdentityMapTests {
}
}

func test_find_storedByAliasCollection_itReturnsEntity() {
let identityMap = IdentityMap()

_ = identityMap.store(entities: [SingleNodeFixture(id: 1)], named: .listOfNodes)

XCTAssertNotNil(identityMap.find(SingleNodeFixture.self, id: 1))
}

func test_find_storedByAliasAggregate_itReturnsEntity() {
let identityMap = IdentityMap()
let aggregate = RootFixture(id: 1, primitive: "", singleNode: SingleNodeFixture(id: 1), listNodes: [])

_ = identityMap.store(entity: aggregate, named: .root)

XCTAssertNotNil(identityMap.find(SingleNodeFixture.self, id: 1))
}

func test_findNamed_entityStored_noObserver_returnValue() {
let identityMap = IdentityMap()
let entity = SingleNodeFixture(id: 1)
Expand Down

0 comments on commit 24903df

Please sign in to comment.