Skip to content

Commit

Permalink
Use TypedIdentifier (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Jan 9, 2025
1 parent 9980b55 commit 30cb308
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 127 deletions.
11 changes: 10 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "82f71c92629a240db65e931912dfb2240dd0b70609e84bc12a7b6ccab081b14b",
"originHash" : "ee3f350fd3d229f68927207ca24a9198872a51157f5ab77fc9f62d85458f30ed",
"pins" : [
{
"identity" : "rxswift",
Expand Down Expand Up @@ -64,6 +64,15 @@
"version" : "600.0.1"
}
},
{
"identity" : "typedidentifier",
"kind" : "remoteSourceControl",
"location" : "https://github.com/VergeGroup/TypedIdentifier",
"state" : {
"revision" : "297de87772cccec24f8f1a63376778d3227305d7",
"version" : "2.0.2"
}
},
{
"identity" : "viewinspector",
"kind" : "remoteSourceControl",
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
.package(url: "https://github.com/apple/swift-collections", from: "1.1.0"),
.package(url: "https://github.com/VergeGroup/swift-concurrency-task-manager", from: "1.1.0"),
.package(url: "https://github.com/VergeGroup/TypedIdentifier", from: "2.0.2"),

/// for testing
.package(url: "https://github.com/nalexn/ViewInspector.git", from: "0.10.0"),
Expand Down Expand Up @@ -62,13 +63,12 @@ let package = Package(
"VergeRx"
]
),
.target(name: "VergeTypedIdentifier"),
.target(
name: "VergeNormalization",
dependencies: [
"VergeMacros",
"VergeComparator",
"VergeTypedIdentifier",
.product(name: "TypedIdentifier", package: "TypedIdentifier"),
.product(name: "HashTreeCollections", package: "swift-collections"),
]
),
Expand Down
10 changes: 10 additions & 0 deletions Sources/VergeNormalization/EntityType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@_exported import TypedIdentifier

public protocol EntityType: TypedIdentifiable, Equatable, Sendable {
}

extension EntityType {
public var entityID: TypedID {
return typedID
}
}
4 changes: 2 additions & 2 deletions Sources/VergeNormalization/Indexes/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public enum Indexes {
]
```
*/
public typealias Hash<Key: Hashable, Entity: EntityType> = HashTreeCollections.TreeDictionary<Key, Entity.EntityID>
public typealias Hash<Key: Hashable, Entity: EntityType> = HashTreeCollections.TreeDictionary<Key, Entity.TypedID>

public typealias Ordered<Entity: EntityType> = Array<Entity.EntityID>
public typealias Ordered<Entity: EntityType> = Array<Entity.TypedID>

public typealias Set = Never

Expand Down
4 changes: 2 additions & 2 deletions Sources/VergeNormalization/NormalizedStorageComparators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public enum NormalizedStorageComparators<Storage: NormalizedStorageType> {
//
// public typealias Input = Storage
//
// public let entityID: Entity.EntityID
// public let entityID: Entity.TypedID
//
// public init(entityID: Entity.EntityID) {
// public init(entityID: Entity.TypedID) {
// self.entityID = entityID
// }
//
Expand Down
18 changes: 10 additions & 8 deletions Sources/VergeNormalization/Tables/TableType.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import VergeTypedIdentifier
import TypedIdentifier

/// a storage of the entity
public protocol TableType<Entity>: Equatable, Sendable {
Expand All @@ -12,17 +12,17 @@ public protocol TableType<Entity>: Equatable, Sendable {
var count: Int { get }
var isEmpty: Bool { get }

borrowing func find(by id: consuming Entity.EntityID) -> Entity?
borrowing func find(by id: consuming Entity.TypedID) -> Entity?

borrowing func find(in ids: consuming some Sequence<Entity.EntityID>) -> [Entity]
borrowing func find(in ids: consuming some Sequence<Entity.TypedID>) -> [Entity]

mutating func updateExists(
id: consuming Entity.EntityID,
id: consuming Entity.TypedID,
update: (inout Entity) throws -> Void
) throws -> Entity

mutating func updateIfExists(
id: consuming Entity.EntityID,
id: consuming Entity.TypedID,
update: (inout Entity) throws -> Void
) rethrows -> Entity?

Expand All @@ -32,17 +32,19 @@ public protocol TableType<Entity>: Equatable, Sendable {
@discardableResult
mutating func insert(_ addingEntities: consuming some Sequence<Entity>) -> [InsertionResult]

mutating func remove(_ id: Entity.EntityID)
mutating func remove(_ id: Entity.TypedID)

mutating func removeAll()
}

/// An object indicates result of insertion
/// It can be used to create a getter object.
public struct InsertionResult<Entity: EntityType> {
public var entityID: Entity.EntityID {
entity.entityID

public var entityID: Entity.TypedID {
entity.typedID
}

public let entity: Entity

init(entity: consuming Entity) {
Expand Down
28 changes: 14 additions & 14 deletions Sources/VergeNormalization/Tables/Tables.Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Tables {

public typealias Entity = Entity

private var storage: TreeDictionary<Entity.EntityID, Entity>
private var storage: TreeDictionary<Entity.TypedID, Entity>
public private(set) var updatedMarker = NonAtomicCounter()

/// The number of entities in table
Expand All @@ -22,12 +22,12 @@ extension Tables {
_read { yield storage.isEmpty }
}

public init(entities: TreeDictionary<Entity.EntityID, Entity> = .init()) {
public init(entities: TreeDictionary<Entity.TypedID, Entity> = .init()) {
self.storage = entities
}

/// Returns all entity ids that stored.
public borrowing func allIDs() -> TreeDictionary<Entity.EntityID, Entity>.Keys {
public borrowing func allIDs() -> TreeDictionary<Entity.TypedID, Entity>.Keys {
return storage.keys
}

Expand All @@ -40,7 +40,7 @@ extension Tables {
Finds an entity by the identifier of the entity.
- Returns: An entity that found by identifier. Nil if the table does not have that entity.
*/
public borrowing func find(by id: consuming Entity.EntityID) -> Entity? {
public borrowing func find(by id: consuming Entity.TypedID) -> Entity? {
return storage[id]
}

Expand All @@ -49,7 +49,7 @@ extension Tables {
///
/// if ids contains same id, result also contains same element.
/// - Parameter ids: sequence of Entity.ID
public borrowing func find(in ids: consuming some Sequence<Entity.EntityID>) -> [Entity] {
public borrowing func find(in ids: consuming some Sequence<Entity.TypedID>) -> [Entity] {

return ids.reduce(into: [Entity]()) { (buf, id) in
guard let entity = storage[id] else { return }
Expand All @@ -60,12 +60,12 @@ extension Tables {
/**
Updates the entity that already exsisting in the table.

- Attention: Please don't change `EntityType.entityID` value. if we changed, the crash happens (precondition)
- Attention: Please don't change `EntityType.TypedID` value. if we changed, the crash happens (precondition)
*/
@discardableResult
@inline(__always)
public mutating func updateExists(
id: consuming Entity.EntityID,
id: consuming Entity.TypedID,
update: (inout Entity) throws -> Void
) throws -> Entity {

Expand All @@ -74,7 +74,7 @@ extension Tables {
}

try update(&current)
precondition(current.entityID == id)
precondition(current.typedID == id)
storage[id] = current

updatedMarker.increment()
Expand All @@ -85,11 +85,11 @@ extension Tables {
/**
Updates the entity that already exsisting in the table.

- Attention: Please don't change `EntityType.entityID` value. if we changed, the crash happens (precondition)
- Attention: Please don't change `EntityType.TypedID` value. if we changed, the crash happens (precondition)
*/
@discardableResult
public mutating func updateIfExists(
id: consuming Entity.EntityID,
id: consuming Entity.TypedID,
update: (inout Entity) throws -> Void
) rethrows -> Entity? {
try? updateExists(id: id, update: update)
Expand All @@ -101,7 +101,7 @@ extension Tables {
@discardableResult
public mutating func insert(_ entity: Entity) -> Self.InsertionResult {

storage[entity.entityID] = entity
storage[entity.typedID] = entity

updatedMarker.increment()

Expand All @@ -115,7 +115,7 @@ extension Tables {
public mutating func insert(_ addingEntities: consuming some Sequence<Entity>) -> [Self.InsertionResult] {

let results = addingEntities.map { entity -> Self.InsertionResult in
storage[entity.entityID] = entity
storage[entity.typedID] = entity
return .init(entity: entity)
}

Expand All @@ -127,7 +127,7 @@ extension Tables {
/**
Removes the entity by the identifier.
*/
public mutating func remove(_ id: Entity.EntityID) {
public mutating func remove(_ id: Entity.TypedID) {
storage.removeValue(forKey: id)
updatedMarker.increment()
}
Expand All @@ -140,7 +140,7 @@ extension Tables {
updatedMarker.increment()
}

public var values: TreeDictionary<Entity.EntityID, Entity>.Values {
public var values: TreeDictionary<Entity.TypedID, Entity>.Values {
storage.values
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/VergeNormalization/VergeNormalization+Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public macro Index() = #externalMacro(module: "VergeMacrosPlugin", type: "IndexM
#if DEBUG

struct A: EntityType {
typealias EntityIDRawType = String
var entityID: EntityID {
typealias TypedIdentifierRawValue = String
var typedID: TypedID {
.init("")
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/VergeNormalization/VergeNormalization.swift
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@_exported import VergeTypedIdentifier
@_exported import TypedIdentifier
4 changes: 2 additions & 2 deletions Sources/VergeNormalizationDerived/DerivedResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct DerivedResult<Entity: EntityType, Derived: DerivedType> {

/// A dictionary of Derived that stored by id
/// It's faster than filtering values array to use this dictionary to find missing id or created id.
public private(set) var storage: [Entity.EntityID : Derived] = [:]
public private(set) var storage: [Entity.TypedID : Derived] = [:]

/// An array of Derived that orderd by specified the order of id.
public private(set) var values: [Derived]
Expand All @@ -25,7 +25,7 @@ public struct DerivedResult<Entity: EntityType, Derived: DerivedType> {
self.values = []
}

public mutating func append(derived: Derived, id: Entity.EntityID) {
public mutating func append(derived: Derived, id: Entity.TypedID) {
storage[id] = derived
values.append(derived)
}
Expand Down
22 changes: 11 additions & 11 deletions Sources/VergeNormalizationDerived/DispatcherType+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public struct NormalizedStorageTablePath<
let tableSelector: _TableSelector

public func derived(
from entityID: consuming Entity.EntityID
from entityID: consuming Entity.TypedID
) -> Derived<EntityWrapper<Entity>> {

return store.derivedEntity(
Expand All @@ -95,7 +95,7 @@ public struct NormalizedStorageTablePath<
}

public func derived(
entityIDs: some Sequence<Entity.EntityID>
entityIDs: some Sequence<Entity.TypedID>
) -> DerivedResult<Entity, Derived<EntityWrapper<Entity>>> {

var result = DerivedResult<Entity, Derived<EntityWrapper<Entity>>>()
Expand Down Expand Up @@ -151,7 +151,7 @@ public struct NormalizedStorageTablePath<
/// A pointer of the result derived object will be different from each other, but the backing source will be shared.
///
public func derivedNonNull(
entityID: consuming Entity.EntityID
entityID: consuming Entity.TypedID
) throws -> Derived<NonNullEntityWrapper<Entity>> {

let _initialValue = storageSelector
Expand Down Expand Up @@ -179,7 +179,7 @@ public struct NormalizedStorageTablePath<
for entity in entities {
result.append(
derived: self.derivedNonNull(entity: entity),
id: entity.entityID
id: entity.typedID
)
}

Expand All @@ -196,7 +196,7 @@ public struct NormalizedStorageTablePath<
/// A pointer of the result derived object will be different from each other, but the backing source will be shared.
///
public func derivedNonNull(
entityIDs: consuming some Sequence<Entity.EntityID>
entityIDs: consuming some Sequence<Entity.TypedID>
) -> NonNullDerivedResult<Entity> {

var result = NonNullDerivedResult<Entity>()
Expand Down Expand Up @@ -224,7 +224,7 @@ public struct NormalizedStorageTablePath<
/// A pointer of the result derived object will be different from each other, but the backing source will be shared.
///
public func derivedNonNull(
entityIDs: consuming Set<Entity.EntityID>
entityIDs: consuming Set<Entity.TypedID>
) -> NonNullDerivedResult<Entity> {

var result = NonNullDerivedResult<Entity>()
Expand Down Expand Up @@ -280,7 +280,7 @@ extension DerivedMaking {
_TableSelector: TableSelector
>(
selector: AbsoluteTableSelector<_StorageSelector, _TableSelector>,
entityID: consuming _TableSelector.Table.Entity.EntityID
entityID: consuming _TableSelector.Table.Entity.TypedID
) -> Derived<EntityWrapper<_TableSelector.Table.Entity>>
where
_StorageSelector.Storage == _TableSelector.Storage,
Expand Down Expand Up @@ -332,10 +332,10 @@ where _StorageSelector.Storage == _TableSelector.Storage {
typealias Output = EntityWrapper<Entity>

private let selector: AbsoluteTableSelector<_StorageSelector, _TableSelector>
private let entityID: Entity.EntityID
private let entityID: Entity.TypedID

init(
targetIdentifier: Entity.EntityID,
targetIdentifier: Entity.TypedID,
selector: consuming AbsoluteTableSelector<_StorageSelector, _TableSelector>
) {
self.entityID = targetIdentifier
Expand Down Expand Up @@ -383,7 +383,7 @@ where _StorageSelector.Storage == _TableSelector.Storage {
typealias Output = NonNullEntityWrapper<Entity>

private let selector: AbsoluteTableSelector<_StorageSelector, _TableSelector>
private let entityID: Entity.EntityID
private let entityID: Entity.TypedID

private let latestValue: Entity
private let lock: NSLock = .init()
Expand All @@ -393,7 +393,7 @@ where _StorageSelector.Storage == _TableSelector.Storage {
selector: consuming AbsoluteTableSelector<_StorageSelector, _TableSelector>
) {

self.entityID = initialEntity.entityID
self.entityID = initialEntity.typedID
self.latestValue = initialEntity
self.selector = selector

Expand Down
4 changes: 2 additions & 2 deletions Sources/VergeNormalizationDerived/EntityWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import VergeNormalization
public struct EntityWrapper<Entity: EntityType>: Sendable {

public private(set) var wrapped: Entity?
public let id: Entity.EntityID
public let id: Entity.TypedID

public init(id: Entity.EntityID, entity: Entity?) {
public init(id: Entity.TypedID, entity: Entity?) {
self.id = id
self.wrapped = entity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct NonNullEntityWrapper<Entity: EntityType> {
public private(set) var wrapped: Entity

/// An identifier
public let id: Entity.EntityID
public let id: Entity.TypedID

@available(*, deprecated, renamed: "isFallBack")
public var isUsingFallback: Bool {
Expand Down
Loading

0 comments on commit 30cb308

Please sign in to comment.