Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shorthand property for entity identifier in Derived #501

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading