Skip to content

Commit

Permalink
Add shorthand property for entity identifier in Derived (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Jan 17, 2025
1 parent 12ece9f commit 84a8331
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
16 changes: 15 additions & 1 deletion Sources/VergeNormalizationDerived/EntityType+Typealias.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@

extension EntityType {

public typealias Derived = Verge.Derived<EntityWrapper<Self>>
public typealias NonNullDerived = Verge.Derived<NonNullEntityWrapper<Self>>

}

extension Derived where Value: NonNullEntityWrapperType {

public var entityID: Value.Entity.TypedID {
self.state.id
}
}

extension Derived where Value: EntityWrapperType {

public var entityID: Value.Entity.TypedID? {
self.state.id
}

}
11 changes: 9 additions & 2 deletions Sources/VergeNormalizationDerived/EntityWrapper.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import Normalization

public protocol EntityWrapperType {
associatedtype Entity: EntityType

var id: Entity.TypedID { get }
}

/// A value that wraps an entity and results of fetching.
public struct EntityWrapper<Entity: EntityType>: Sendable {
public struct EntityWrapper<Entity: EntityType>: Sendable, EntityWrapperType {

public private(set) var wrapped: Entity?
public let wrapped: Entity?

public let id: Entity.TypedID

public init(id: Entity.TypedID, entity: Entity?) {
Expand Down
10 changes: 8 additions & 2 deletions Sources/VergeNormalizationDerived/NonNullEntityWrapper.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import Normalization

public protocol NonNullEntityWrapperType {
associatedtype Entity: EntityType

var id: Entity.TypedID { get }
}

/// A value that wraps an entity and results of fetching.
@dynamicMemberLookup
public struct NonNullEntityWrapper<Entity: EntityType> {
public struct NonNullEntityWrapper<Entity: EntityType>: Sendable, NonNullEntityWrapperType {

/// An entity value
public private(set) var wrapped: Entity
public let wrapped: Entity

/// An identifier
public let id: Entity.TypedID
Expand Down

0 comments on commit 84a8331

Please sign in to comment.