Skip to content

Commit

Permalink
Merge pull request #193 from scribd/vijays/make-entities-optionally-s…
Browse files Browse the repository at this point in the history
…endable

Allows for parameterizing entities to be marked as sendable
  • Loading branch information
vijaysharm authored Nov 15, 2024
2 parents 2d1f778 + f00e183 commit dd82489
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CodeGen/Sources/LucidCodeGen/Meta/MetaEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct MetaEntity {

return Type(identifier: entity.typeID())
.adding(inheritedType: .codable)
.adding(inheritedType: entity.senable ? .sendable : nil)
.with(kind: .class(final: true))
.with(accessLevel: .public)
.with(body: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct MetaEntityIdentifier {

return Type(identifier: entity.identifierTypeID())
.adding(inheritedType: .codable)
.adding(inheritedType: entity.senable ? .sendable : nil)
.with(kind: .class(final: true))
.with(accessLevel: .public)
.adding(inheritedType: .coreDataIdentifier)
Expand Down
1 change: 1 addition & 0 deletions CodeGen/Sources/LucidCodeGen/Meta/MetaSubtype.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct MetaSubtype {
let type = Type(identifier: subtype.typeID())
.with(accessLevel: .public)
.adding(inheritedType: .codable)
.adding(inheritedType: subtype.sendable ? .sendable : nil)
.adding(inheritedType: .hashable)

switch subtype.items {
Expand Down
5 changes: 5 additions & 0 deletions CodeGen/Sources/LucidCodeGenCore/Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum DescriptionDefaults {
public static let ignorePropertyMigrationChecksOn = [String]()
public static let httpMethod: EndpointPayloadTest.HTTPMethod = .get
public static let cacheSize: EntityCacheSize = .group(.medium)
public static let sendable = false
}

public extension Entity {
Expand Down Expand Up @@ -322,6 +323,7 @@ extension Entity: Codable {
case queryContext
case clientQueueName
case cacheSize
case sendable
}

public init(from decoder: Decoder) throws {
Expand All @@ -348,6 +350,7 @@ extension Entity: Codable {
queryContext = try container.decodeIfPresent(Bool.self, forKey: .queryContext) ?? DescriptionDefaults.queryContext
clientQueueName = try container.decodeIfPresent(String.self, forKey: .clientQueueName) ?? DescriptionDefaults.clientQueueName
cacheSize = try container.decodeIfPresent(EntityCacheSize.self, forKey: .cacheSize) ?? DescriptionDefaults.cacheSize
senable = try container.decodeIfPresent(Bool.self, forKey: .sendable) ?? DescriptionDefaults.sendable

let systemPropertiesSet = Set(SystemPropertyName.allCases.map { $0.rawValue })
for property in properties where systemPropertiesSet.contains(property.name) {
Expand Down Expand Up @@ -754,6 +757,7 @@ extension Subtype: Codable {
case objc
case objcNoneCase
case platforms
case sendable
}

public init(from decoder: Decoder) throws {
Expand All @@ -762,6 +766,7 @@ extension Subtype: Codable {
name = try container.decode(String.self, forKey: .name)
manualImplementations = Set(try container.decodeIfPresent([`Protocol`].self, forKey: .manualImplementations) ?? [])
platforms = try container.decodeIfPresent(Set<Platform>.self, forKey: .platforms) ?? DescriptionDefaults.platforms
sendable = try container.decodeIfPresent(Bool.self, forKey: .sendable) ?? DescriptionDefaults.sendable

if let usedCases = try container.decodeIfPresent([String].self, forKey: .cases) {
let unusedCases = try container.decodeIfPresent([String].self, forKey: .unusedCases) ?? []
Expand Down
8 changes: 7 additions & 1 deletion CodeGen/Sources/LucidCodeGenCore/Descriptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ public struct Entity: Equatable {
public let clientQueueName: String

public let cacheSize: EntityCacheSize

public let senable: Bool

public init(name: String,
persistedName: String? = nil,
Expand All @@ -382,7 +384,8 @@ public struct Entity: Equatable {
versionHistory: [VersionHistoryItem] = [],
queryContext: Bool = DescriptionDefaults.queryContext,
clientQueueName: String = DescriptionDefaults.clientQueueName,
cacheSize: EntityCacheSize = DescriptionDefaults.cacheSize) {
cacheSize: EntityCacheSize = DescriptionDefaults.cacheSize,
sendable: Bool = DescriptionDefaults.sendable) {

self.name = name
self.persistedName = persistedName
Expand All @@ -400,6 +403,7 @@ public struct Entity: Equatable {
self.queryContext = queryContext
self.clientQueueName = clientQueueName
self.cacheSize = cacheSize
self.senable = sendable
}
}

Expand Down Expand Up @@ -641,6 +645,8 @@ public struct Subtype: Equatable {
public let objc: Bool

public let platforms: Set<Platform>

public let sendable: Bool
}

// MARK: - Conversions
Expand Down
4 changes: 4 additions & 0 deletions CodeGen/Sources/LucidCodeGenCore/MetaUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public extension TypeIdentifier {
return TypeIdentifier(name: "CoreDataConversionError")
}

static var sendable: TypeIdentifier {
return TypeIdentifier(name: "Sendable")
}

static var equatable: TypeIdentifier {
return TypeIdentifier(name: "Equatable")
}
Expand Down
2 changes: 2 additions & 0 deletions Lucid/Core/Payload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public enum Lazy<T> {
case unrequested
}

extension Lazy: Sendable where T: Sendable {}

public extension Lazy where T: PayloadIdentifiable {

func identifier() -> Lazy<T.Identifier> {
Expand Down
2 changes: 2 additions & 0 deletions Lucid/Utils/AnySequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ extension AnySequence: Equatable where Element: Equatable {
}
}

extension AnySequence: @unchecked Sendable where Element: Sendable {}

public extension Result where Success: Sequence {

@inlinable var any: Result<AnySequence<Success.Element>, Failure> {
Expand Down
2 changes: 1 addition & 1 deletion Lucid/Utils/PropertyBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// A mutable property which can either ensure atomicity or not.
public final class PropertyBox<T> {
public final class PropertyBox<T>: @unchecked Sendable {

private let valueQueue: DispatchQueue?

Expand Down

0 comments on commit dd82489

Please sign in to comment.