Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Feb 9, 2024
2 parents cc4ba2b + 5d7c1ac commit 2ebc35a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
align="right" width="64" height="64" />
</h2>

**2024-01-19**: Seems to be b0rked w/ Xcode 15.2: Issue https://github.com/Data-swift/ManagedModels/issues/25

> Instead of wrapping CoreData, use it directly :-)
The key thing **ManagedModels** provides is a `@Model` macro,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ extension CoreData.NSAttributeDescription: SchemaProperty {
ValueTransformer
.setValueTransformer(transformer, forName: .init(name))
}
assert(ValueTransformer.valueTransformerNames().contains(.init(name)))
valueTransformerName = name
assert(valueTransformerName != nil)
}
setValueClassName(for: codableType)
return
}

// TBD:
Expand Down Expand Up @@ -162,7 +165,11 @@ public extension NSAttributeDescription {
if let hashModifier { versionHashModifier = hashModifier }
if let defaultValue { self.defaultValue = defaultValue }
isOptional = valueType is any AnyOptional.Type

assert(valueTransformerName == nil)
valueTransformerName = nil
if valueType != Any.self { self.valueType = valueType }

setOptions(options)
}
}
Expand All @@ -174,7 +181,6 @@ private extension NSAttributeDescription {
allowsExternalBinaryDataStorage = false
isIndexedBySpotlight = false
isTransient = false
valueTransformerName = nil
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) {
allowsCloudEncryption = false
}
Expand All @@ -190,8 +196,10 @@ private extension NSAttributeDescription {
case .ephemeral: isTransient = true

case .transformableByName(let name):
assert(valueTransformerName == nil)
valueTransformerName = name
case .transformableByType(let type):
assert(valueTransformerName == nil)
valueTransformerName = NSStringFromClass(type)

case .allowsCloudEncryption: // FIXME: restrict availability
Expand Down
71 changes: 71 additions & 0 deletions Tests/ManagedModelTests/CodablePropertiesTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Created by Helge Heß.
// Copyright © 2024 ZeeZide GmbH.
//

import XCTest
import Foundation
import CoreData
@testable import ManagedModels

final class CodablePropertiesTests: XCTestCase {

private lazy var container = try? ModelContainer(
for: Fixtures.CodablePropertiesSchema.managedObjectModel,
configurations: ModelConfiguration(isStoredInMemoryOnly: true)
)

func testEntityName() throws {
let entityType = Fixtures.CodablePropertiesSchema.StoredAccess.self
XCTAssertEqual(entityType.entity().name, "StoredAccess")
}

func testPropertySetup() throws {
let valueType = Fixtures.CodablePropertiesSchema.AccessSIP.self
let attribute = CoreData.NSAttributeDescription(
name: "sip",
valueType: valueType,
defaultValue: nil
)
XCTAssertEqual(attribute.name, "sip")
XCTAssertEqual(attribute.attributeType, .transformableAttributeType)

let transformerName = try XCTUnwrap(
ValueTransformer.valueTransformerNames().first(where: {
$0.rawValue.range(of: "CodableBox11TransformerVOO17ManagedModelTests8")
!= nil
})
)
let transformer = try XCTUnwrap(ValueTransformer(forName: transformerName))
_ = transformer // to clear unused-wraning

XCTAssertTrue(attribute.valueType ==
CodableBox<Fixtures.CodablePropertiesSchema.AccessSIP>.self)
XCTAssertNotNil(attribute.valueTransformerName)
XCTAssertEqual(attribute.valueTransformerName, transformerName.rawValue)
}

func testCodablePropertyEntity() throws {
let entity = try XCTUnwrap(
container?.managedObjectModel.entitiesByName["StoredAccess"]
)

// Creating the entity should have registered the transformer for the
// CodableBox.
let transformerName = try XCTUnwrap(
ValueTransformer.valueTransformerNames().first(where: {
$0.rawValue.range(of: "CodableBox11TransformerVOO17ManagedModelTests8")
!= nil
})
)
let transformer = try XCTUnwrap(ValueTransformer(forName: transformerName))
_ = transformer // to clear unused-wraning

let attribute = try XCTUnwrap(entity.attributesByName["sip"])
XCTAssertEqual(attribute.name, "sip")
XCTAssertTrue(attribute.valueType ==
CodableBox<Fixtures.CodablePropertiesSchema.AccessSIP>.self)
XCTAssertNotNil(attribute.valueTransformerName)
XCTAssertEqual(attribute.valueTransformerName, transformerName.rawValue)
}
}
31 changes: 31 additions & 0 deletions Tests/ManagedModelTests/Schemas/CodablePropertySchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by Helge Heß.
// Copyright © 2024 ZeeZide GmbH.
//

import ManagedModels

extension Fixtures {
// https://github.com/Data-swift/ManagedModels/issues/27

enum CodablePropertiesSchema: VersionedSchema {
static var models : [ any PersistentModel.Type ] = [
StoredAccess.self
]

public static let versionIdentifier = Schema.Version(0, 1, 0)

@Model
final class StoredAccess: NSManagedObject {
var token : String
var expires : Date
var sip : AccessSIP
}

struct AccessSIP: Codable {
var username : String
var password : String
var realm : String
}
}
}

0 comments on commit 2ebc35a

Please sign in to comment.