From 41aeab46adf08305feb835153dd6c4163bb6f60c Mon Sep 17 00:00:00 2001 From: Aleksandar Vacic Date: Sun, 18 Jun 2017 19:17:33 +0200 Subject: [PATCH 01/18] Swiftified both templates --- templates/human.swift.motemplate | 15 +- templates/machine.swift.motemplate | 239 ++++++++--------------------- 2 files changed, 72 insertions(+), 182 deletions(-) diff --git a/templates/human.swift.motemplate b/templates/human.swift.motemplate index 3abb1aa5..6cfdabc7 100644 --- a/templates/human.swift.motemplate +++ b/templates/human.swift.motemplate @@ -1,6 +1,15 @@ import Foundation +import CoreData -@objc(<$managedObjectClassName$>) -open class <$managedObjectClassName$>: _<$managedObjectClassName$> { - // Custom logic goes here. +@objc(<$sanitizedManagedObjectClassName$>) +open class <$sanitizedManagedObjectClassName$>: <$customSuperentity$> { + + public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) { + super.init(entity: entity, insertInto: context) + } + + required public init?(managedObjectContext moc: NSManagedObjectContext) { + guard let entity = NSEntityDescription.entity(forEntityName: "<$managedObjectClassName$>", in: moc) else { return nil } + super.init(entity: entity, insertInto: moc) + } } diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 229ab325..5726d4d2 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -1,99 +1,81 @@ // DO NOT EDIT. This file is machine-generated and constantly overwritten. -// Make changes to <$sanitizedManagedObjectClassName$>.swift instead. +// Make changes to <$managedObjectClassName$>.swift instead. -import Foundation import CoreData +<$if hasAdditionalHeaderFile$>import <$additionalHeaderFileName$><$endif$> <$if hasCustomBaseCaseImport$>import <$baseClassImport$><$endif$> -<$if noninheritedAttributes.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>Attributes: String {<$foreach Attribute noninheritedAttributes do$> - case <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$> -} -<$endif$> - -<$if noninheritedRelationships.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>Relationships: String {<$foreach Relationship noninheritedRelationships do$> - case <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$> -} -<$endif$> - -<$if noninheritedFetchedProperties.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>FetchedProperties: String {<$foreach FetchedProperty noninheritedFetchedProperties do$> - case <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$> -} -<$endif$> - -<$if hasUserInfoKeys && userInfoKeyValues.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>UserInfo: String {<$foreach UserInfo userInfoKeyValues do$> - case <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> -} -<$endif$> - - -<$if hasCustomSuperentity $> -open class _<$sanitizedManagedObjectClassName$>: <$customSuperentity$> { -<$else$> -open class _<$sanitizedManagedObjectClassName$>: NSManagedObject { -<$endif$> - // MARK: - Class methods - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entityName () -> String { +public extension <$managedObjectClassName$> { + + <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class var entityName: String { return "<$name$>" } <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { - return NSEntityDescription.entity(forEntityName: self.entityName(), in: managedObjectContext) + return NSEntityDescription.entity(forEntityName: entityName, in: managedObjectContext) } + + <$if noninheritedAttributes.@count > 0$> + public struct Attributes {<$foreach Attribute noninheritedAttributes do$> + static let <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$> + } + <$endif$> - @nonobjc - open class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> { - if #available(iOS 10.0, tvOS 10.0, watchOS 3.0, macOS 10.12, *) { - return NSManagedObject.fetchRequest() as! NSFetchRequest<<$sanitizedManagedObjectClassName$>> - } else { - return NSFetchRequest(entityName: self.entityName()) - } - } - - // MARK: - Life cycle methods - - public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) { - super.init(entity: entity, insertInto: context) - } + <$if noninheritedRelationships.@count > 0$> + public struct Relationships {<$foreach Relationship noninheritedRelationships do$> + static let <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$> + } + <$endif$> - public convenience init?(managedObjectContext: NSManagedObjectContext) { - guard let entity = _<$sanitizedManagedObjectClassName$>.entity(managedObjectContext: managedObjectContext) else { return nil } - self.init(entity: entity, insertInto: managedObjectContext) - } + <$if noninheritedFetchedProperties.@count > 0$> + public struct FetchedProperties {<$foreach FetchedProperty noninheritedFetchedProperties do$> + static let <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$> + } + <$endif$> + <$if hasUserInfoKeys && userInfoKeyValues.@count > 0$> + public struct UserInfo: String {<$foreach UserInfo userInfoKeyValues do$> + static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> + } + <$endif$> + // MARK: - Properties <$foreach Attribute noninheritedAttributes do$> -<$if Attribute.hasDefinedAttributeType$> +<$if Attribute.hasDefinedAttributeType$> <$if Attribute.hasScalarAttributeType$> -<$if Attribute.isReadonly$> - open var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$if Attribute.isOptional$> // Optional scalars not supported<$endif$><$else$>NSNumber<$if Attribute.isOptional$>?<$else$>!<$endif$><$endif$> - { - self.willAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - let <$Attribute.name$> = self.primitiveValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> - self.didAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - return <$Attribute.name$> - } + +<$if Attribute.isOptional$> +<$if Attribute.usesScalarAttributeType$> + public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + return primitiveValue(forKey: key) as? <$Attribute.scalarAttributeType$> + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + guard let value = newValue else { + setPrimitiveValue(nil, forKey: key) + return + } + setPrimitiveValue(value, forKey: key) + } + } <$else$> - @NSManaged open - var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$if Attribute.isOptional$> // Optional scalars not supported<$endif$><$else$>NSNumber<$if Attribute.isOptional$>?<$else$>!<$endif$><$endif$> + @NSManaged public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: NSNumber? <$endif$> <$else$> -<$if Attribute.isReadonly$> - open var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$else$>!<$endif$> - { - self.willAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - let <$Attribute.name$> = self.primitiveValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$> - self.didAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - return <$Attribute.name$> - } -<$else$> - @NSManaged open - var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$else$>!<$endif$> + @NSManaged public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> <$endif$> + +<$else$> + @NSManaged public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> <$endif$> <$endif$> <$endforeach do$> @@ -101,115 +83,14 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject { // MARK: - Relationships <$foreach Relationship noninheritedRelationships do$> <$if Relationship.isToMany$> - @NSManaged open - var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$> - - open func <$Relationship.name$>Set() -> <$Relationship.mutableCollectionClassName$> { - return self.<$Relationship.name$>.mutableCopy() as! <$Relationship.mutableCollectionClassName$> - } - + @NSManaged public var <$Relationship.name$>: Set<<$Relationship.destinationEntity.managedObjectClassName$>><$if Relationship.isOptional$>?<$endif$> <$else$> - @NSManaged open - var <$Relationship.name$>: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$><$if Relationship.isOptional$>?<$endif$> -<$endif$> -<$endforeach do$> - -<$foreach FetchRequest prettyFetchRequests do$> -<$if FetchRequest.singleResult$> - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> Any? { - return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil) - } - - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> Any? { - guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil } - let model = psc.managedObjectModel - let substitutionVariables : [String : Any] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$> - "<$Binding.name$>": <$Binding.name$>, - <$endforeach do2$><$else$>:<$endif$>] - - guard let fetchRequest = model.fetchRequestFromTemplate(withName: "<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else { - assert(false, "Can't find fetch request named \"<$FetchRequest.name$>\".") - return nil - } - - var result: Any? = nil - do { - let results = try managedObjectContext.fetch(fetchRequest) - switch results.count { - case 0: - // Nothing found matching the fetch request. That's cool, though: we'll just return nil. - break - case 1: - result = results.first - default: - print("WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, \(results.count) found (substitutionVariables: \(substitutionVariables), results: \(results))") - } - - } catch { - print("Error executing fetch request: \(error)") - } - return result - } -<$else$> - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> [Any]? { - return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil) - } - - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> [Any]? { - guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil } - let model = psc.managedObjectModel - let substitutionVariables : [String : Any] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$> - "<$Binding.name$>": <$Binding.name$>, -<$endforeach do2$><$else$>:<$endif$>] - - guard let fetchRequest = model.fetchRequestFromTemplate(withName: "<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else { - assert(false, "Can't find fetch request named \"<$FetchRequest.name$>\".") - return nil - } - var results = Array() - do { - results = try managedObjectContext.fetch(fetchRequest) - } catch { - print("Error executing fetch request: \(error)") - } - - return results - } + @NSManaged public var <$Relationship.name$>: <$Relationship.destinationEntity.managedObjectClassName$><$if Relationship.isOptional$>?<$endif$> <$endif$> <$endforeach do$> <$foreach FetchedProperty noninheritedFetchedProperties do$> - @NSManaged open - let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] + @NSManaged open let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] <$endforeach do$> -} - -<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$> -extension _<$sanitizedManagedObjectClassName$> { - - open func add<$Relationship.name.initialCapitalString$>(_ objects: <$Relationship.immutableCollectionClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.union(objects<$if !Relationship.jr_isOrdered$> as Set<$endif$>) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } - - open func remove<$Relationship.name.initialCapitalString$>(_ objects: <$Relationship.immutableCollectionClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.minus(objects<$if !Relationship.jr_isOrdered$> as Set<$endif$>) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } - - open func add<$Relationship.name.initialCapitalString$>Object(_ value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.add(value) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } - - open func remove<$Relationship.name.initialCapitalString$>Object(_ value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.remove(value) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } } -<$endif$><$endforeach do$> From 04e8a219ffdc44bccc4220d2ba367b77ec2b5a02 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sun, 29 Jul 2018 16:48:25 -0600 Subject: [PATCH 02/18] Fix codegen errors from merge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Don’t import C style headers in Swift code * Add a space between “public” and “private(set)” to avoid “publicprivate” in code. * Don’t declare “open” items in public extension * Use “entityName” instead of “entityName()” --- templates/machine.swift.motemplate | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 2dce8c06..20910e5f 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -2,17 +2,16 @@ // Make changes to <$managedObjectClassName$>.swift instead. import CoreData -<$if hasAdditionalHeaderFile$>import <$additionalHeaderFileName$><$endif$> <$if hasCustomBaseCaseImport$>import <$baseClassImport$><$endif$> public extension <$managedObjectClassName$> { - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class var entityName: String { + <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { return "<$name$>" } - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { + <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { return NSEntityDescription.entity(forEntityName: entityName, in: managedObjectContext) } @@ -39,8 +38,8 @@ public extension <$managedObjectClassName$> { static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> } <$endif$> - open class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> { - return NSFetchRequest(entityName: self.entityName()) + public class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> { + return NSFetchRequest(entityName: self.entityName) } // MARK: - Properties @@ -50,7 +49,7 @@ public extension <$managedObjectClassName$> { <$if Attribute.isOptional$> <$if Attribute.usesScalarAttributeType$> - public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { get { let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> willAccessValue(forKey: key) @@ -71,14 +70,14 @@ public extension <$managedObjectClassName$> { } } <$else$> - @NSManaged public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: NSNumber? + @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: NSNumber? <$endif$> <$else$> - @NSManaged public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> + @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> <$endif$> <$else$> - @NSManaged public<$if Attribute.isReadonly$>private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> + @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> <$endif$> <$endif$> <$endforeach do$> @@ -93,7 +92,7 @@ public extension <$managedObjectClassName$> { <$endforeach do$> <$foreach FetchedProperty noninheritedFetchedProperties do$> - @NSManaged open let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] + @NSManaged public let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] <$endforeach do$> } From 3b24550cb66f084c6c6e95f59867d92b12a864e0 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 15:56:38 -0600 Subject: [PATCH 03/18] =?UTF-8?q?Keep=20generated=20code=20in=20the=20?= =?UTF-8?q?=E2=80=9Cmachine=E2=80=9D=20file.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/human.swift.motemplate | 8 +------- templates/machine.swift.motemplate | 7 ++++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/templates/human.swift.motemplate b/templates/human.swift.motemplate index 6cfdabc7..4da8776e 100644 --- a/templates/human.swift.motemplate +++ b/templates/human.swift.motemplate @@ -4,12 +4,6 @@ import CoreData @objc(<$sanitizedManagedObjectClassName$>) open class <$sanitizedManagedObjectClassName$>: <$customSuperentity$> { - public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) { - super.init(entity: entity, insertInto: context) - } + // Custom code for <$sanitizedManagedObjectClassName$> goes here. - required public init?(managedObjectContext moc: NSManagedObjectContext) { - guard let entity = NSEntityDescription.entity(forEntityName: "<$managedObjectClassName$>", in: moc) else { return nil } - super.init(entity: entity, insertInto: moc) - } } diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 20910e5f..04308db2 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -6,7 +6,12 @@ import CoreData public extension <$managedObjectClassName$> { - + + public convenience init?(managedObjectContext moc: NSManagedObjectContext) { + guard let entity = NSEntityDescription.entity(forEntityName: "<$managedObjectClassName$>", in: moc) else { return nil } + self.init(entity: entity, insertInto: moc) + } + <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { return "<$name$>" } From bd5fe029e22d4497b14354458029f51152508e13 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 16:01:57 -0600 Subject: [PATCH 04/18] Remove convenience init, unnecessary since iOS 10. --- templates/machine.swift.motemplate | 5 ----- 1 file changed, 5 deletions(-) diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 04308db2..893a9478 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -7,11 +7,6 @@ import CoreData public extension <$managedObjectClassName$> { - public convenience init?(managedObjectContext moc: NSManagedObjectContext) { - guard let entity = NSEntityDescription.entity(forEntityName: "<$managedObjectClassName$>", in: moc) else { return nil } - self.init(entity: entity, insertInto: moc) - } - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { return "<$name$>" } From c4289283f862aec3037340d9c87179d219ac2956 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 17:17:40 -0600 Subject: [PATCH 05/18] =?UTF-8?q?Don=E2=80=99t=20try=20to=20make=20a=20str?= =?UTF-8?q?uct=20inherit=20from=20String.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/machine.swift.motemplate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 893a9478..6ff054c7 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -34,7 +34,7 @@ public extension <$managedObjectClassName$> { <$endif$> <$if hasUserInfoKeys && userInfoKeyValues.@count > 0$> - public struct UserInfo: String {<$foreach UserInfo userInfoKeyValues do$> + public struct UserInfo {<$foreach UserInfo userInfoKeyValues do$> static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> } <$endif$> From 92d46f97fa3189bc39b7f50948028cbebd4137a9 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 17:18:51 -0600 Subject: [PATCH 06/18] =?UTF-8?q?Fix=20=E2=80=9Coverriding=20non-@objc=20d?= =?UTF-8?q?eclarations=20from=20extensions=20is=20not=20supported=E2=80=9D?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/machine.swift.motemplate | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 6ff054c7..5ec4a1d8 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -7,11 +7,11 @@ import CoreData public extension <$managedObjectClassName$> { - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { + @objc <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { return "<$name$>" } - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { + @objc <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { return NSEntityDescription.entity(forEntityName: entityName, in: managedObjectContext) } From d19acbb36668ec37a361698c8381d183b5499f40 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 17:28:02 -0600 Subject: [PATCH 07/18] Update Swift test code for new templates --- test/swift/main.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/swift/main.swift b/test/swift/main.swift index 75d6e568..9f281937 100644 --- a/test/swift/main.swift +++ b/test/swift/main.swift @@ -23,38 +23,38 @@ struct CoreDataStore { let dataStore = CoreDataStore() let moc = dataStore.moc // -let homer = ParentMO(managedObjectContext: moc)! +let homer = ParentMO(context: moc) homer.humanName = "homer" homer.parentName = homer.humanName homer.ivar = 1.0 homer.gender = NSNumber(value: Gender.Male.rawValue) -let marge = ParentMO(managedObjectContext: moc)! +let marge = ParentMO(context: moc) marge.humanName = "marge" marge.parentName = marge.humanName marge.ivar = 1.0 marge.gender = NSNumber(value: Gender.Female.rawValue) -assert(homer.children.count == 0) -assert(marge.children.count == 0) +assert(homer.children!.count == 0) +assert(marge.children!.count == 0) -let bart = ChildMO(managedObjectContext: moc)! +let bart = ChildMO(context: moc) bart.humanName = "bart" bart.childName = bart.humanName bart.ivar = 1.0 bart.type = 64 -let lisa = ChildMO(managedObjectContext: moc)! +let lisa = ChildMO(context: moc) lisa.humanName = "lisa" lisa.childName = lisa.humanName lisa.ivar = 1.0 do { try moc.save() - assert(Gender(rawValue: homer.gender!.intValue) == .Male) - assert(Gender(rawValue: marge.gender!.intValue) == .Female) - assert(Gender(rawValue: bart.gender!.intValue) == .Undefined) - assert(Gender(rawValue: homer.gender!.intValue)!.toString() == "Male") + assert(Gender(rawValue: homer.gender.intValue) == .Male) + assert(Gender(rawValue: marge.gender.intValue) == .Female) + assert(Gender(rawValue: bart.gender.intValue) == .Undefined) + assert(Gender(rawValue: homer.gender.intValue)!.toString() == "Male") } catch { assertionFailure("Failed to save") From 39a663408cfe6da625f1499e1e3e1237091c223d Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 17:32:44 -0600 Subject: [PATCH 08/18] Update OSX_SDK to target Xcode 10 beta --- test/Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Rakefile b/test/Rakefile index 6f04d33f..b3534f0b 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -12,7 +12,7 @@ end #============================== # Variables #============================== -OSX_SDK="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk" +OSX_SDK="/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" OSX_VERSION="10.10" LINKED_FRAMEWORKS="-framework Foundation -framework AppKit -framework CoreData" From 85d4f1bd289138ce523a2aa6ca534d0828c3665b Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 4 Aug 2018 18:09:19 -0600 Subject: [PATCH 09/18] =?UTF-8?q?Change=20version=20to=201.32=20=E2=80=9Cb?= =?UTF-8?q?eta=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mogenerator.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mogenerator.m b/mogenerator.m index 63d8ae1a..3a0b796a 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -1039,7 +1039,7 @@ - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { } if (_version) { - printf("mogenerator 1.31. By Jonathan 'Wolf' Rentzsch + friends.\n"); + printf("mogenerator 1.32b. By Jonathan 'Wolf' Rentzsch + friends.\n"); return EXIT_SUCCESS; } From c27a020ceabfa59b1d7a707121727aba1ddbdd86 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sun, 5 Aug 2018 15:34:15 -0600 Subject: [PATCH 10/18] Add README credits for upcoming release. --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index b24f6025..47169098 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,28 @@ Senseful wrote up a [nice summary of mogenerator's command-line options](http:// ## Version History +### v1.32: TBD + +* [NEW] Update Swift templates for better Swift style ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/368)) + +* [FIX] Fixed invalid call of `NSManagedObject.fetchRequest()` ([Vincent Esche](https://github.com/rentzsch/mogenerator/pull/372)) + +* [NEW] Add support for URI and UUID attribute types ([Trevor Squires](https://github.com/rentzsch/mogenerator/pull/370)) + +* [NEW] Expose information when custom type is set through userInfo ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/369)) + +* [CHANGE] Nullability annotations for generated primitive accessors ([Michael Babin](https://github.com/rentzsch/mogenerator/pull/363)) + +* [FIX] Support for “Uses Scalar Type” ([Rok Gregorič](https://github.com/rentzsch/mogenerator/pull/352)) + +* [FIX] Swift 3 `NSData` to `Data` ([Christopher Rogers](https://github.com/rentzsch/mogenerator/pull/350)) + +* [FIX] Add specialized `fetchRequest()` func to Swift's machine generated files ([0xpablo](https://github.com/rentzsch/mogenerator/pull/358)) + +* [FIX] Correct Swift machine template for singleton (fetchOne…) fetch request results ([Warren Burton](https://github.com/rentzsch/mogenerator/pull/359)) + +* [FIX] Expose allAttributes and allRelationships ([Trevor Squires](https://github.com/rentzsch/mogenerator/pull/360)) + ### v1.31: Mon Oct 3 2016 [download](https://github.com/rentzsch/mogenerator/releases/download/1.31/mogenerator-1.31.dmg) * [NEW] Swift 3 support. ([Goncharov Vladimir](https://github.com/rentzsch/mogenerator/pull/347), [Hardik](https://github.com/rentzsch/mogenerator/pull/349), [rentzsch](https://github.com/rentzsch/mogenerator/commit/a65f6421baf39dc1458f22836907cfc55fc8ceb1)) From 3f3eb0fc63e5c5ee333f83ecb270db0dd7d97385 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 18 Aug 2018 17:02:16 -0600 Subject: [PATCH 11/18] Add `attributeRawValueEnumType` to handle Swift enums with numeric raw types. Intended to fix issue #376 --- mogenerator.m | 10 +++++ templates/machine.swift.motemplate | 59 +++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/mogenerator.m b/mogenerator.m index 2e91a23c..be8ca47c 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -16,6 +16,7 @@ static const NSString *const kAttributeValueScalarTypeKey = @"attributeValueScalarType"; static const NSString *const kAdditionalHeaderFileNameKey = @"additionalHeaderFileName"; +static const NSString *const kUsesRawValueEnumType = @"attributeRawValueEnumType"; static const NSString *const kAdditionalImportsKey = @"additionalImports"; static const NSString *const kCustomBaseClass = @"mogenerator.customBaseClass"; static const NSString *const kReadOnly = @"mogenerator.readonly"; @@ -447,6 +448,15 @@ - (BOOL)hasScalarAttributeType { } } +- (BOOL)usesRawValueEnumType { + NSNumber *usesRawValueEnumType = [[self userInfo] objectForKey:kUsesRawValueEnumType]; + return (usesRawValueEnumType != NULL); +} + +- (NSString *)attributeRawValueEnumType { + return [[self userInfo] objectForKey:kUsesRawValueEnumType]; +} + - (BOOL)usesScalarAttributeType { NSNumber *usesScalarAttributeType = [[self userInfo] objectForKey:kUsesScalarAttributeType]; diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 14d3328c..e8da5291 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -53,6 +53,29 @@ public extension <$managedObjectClassName$> { <$if Attribute.hasDefinedAttributeType$> <$if Attribute.hasScalarAttributeType$> +<$if Attribute.usesRawValueEnumType$> + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + guard let primitiveValue = primitiveValue(forKey: key) as? <$Attribute.attributeRawValueEnumType$>.RawValue else { return nil } + return <$Attribute.attributeRawValueEnumType$>(rawValue: primitiveValue) + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + guard let value = newValue else { + setPrimitiveValue(nil, forKey: key) + return + } + setPrimitiveValue(value.rawValue, forKey: key) + } + } +<$else$> <$comment usesRawValueEnumType = false $> <$if Attribute.isOptional$> <$if Attribute.usesScalarAttributeType$> public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { @@ -75,17 +98,41 @@ public extension <$managedObjectClassName$> { setPrimitiveValue(value, forKey: key) } } -<$else$> +<$else$> <$comment usesScalarAttributeType = true but the above checks are not true $> @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: NSNumber? <$endif$> -<$else$> +<$else$> <$comment isOptional is not true $> @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> -<$endif$> +<$endif$> <$comment isOptional $> +<$endif$> <$comment usesRawValueEnumType $> +<$else$> <$comment hasScalarAttribuetType = false (object type) $> +<$if Attribute.usesRawValueEnumType$> <$comment This is here because enums with String raw value don't count as scalars $> + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } -<$else$> + guard let primitiveValue = primitiveValue(forKey: key) as? <$Attribute.attributeRawValueEnumType$>.RawValue else { return nil } + return <$Attribute.attributeRawValueEnumType$>(rawValue: primitiveValue) + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + guard let value = newValue else { + setPrimitiveValue(nil, forKey: key) + return + } + setPrimitiveValue(value.rawValue, forKey: key) + } + } +<$else$> <$comment usesRawValueEnumType = false $> @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> -<$endif$> -<$endif$> +<$endif$> <$comment usesRawValueEnumType $> +<$endif$> <$comment hasScalarAttribuetType$> +<$endif$> <$comment hasDefinedAttributeType$> <$endforeach do$> // MARK: - Relationships From d4927d1b68daa1416d49824ee57a6d622b055b56 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 1 Sep 2018 14:28:20 -0600 Subject: [PATCH 12/18] Fix typos --- templates/machine.swift.motemplate | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index e8da5291..7d7bc749 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -105,7 +105,7 @@ public extension <$managedObjectClassName$> { @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> <$endif$> <$comment isOptional $> <$endif$> <$comment usesRawValueEnumType $> -<$else$> <$comment hasScalarAttribuetType = false (object type) $> +<$else$> <$comment hasScalarAttributeType = false (object type) $> <$if Attribute.usesRawValueEnumType$> <$comment This is here because enums with String raw value don't count as scalars $> public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { get { @@ -131,7 +131,7 @@ public extension <$managedObjectClassName$> { <$else$> <$comment usesRawValueEnumType = false $> @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> <$endif$> <$comment usesRawValueEnumType $> -<$endif$> <$comment hasScalarAttribuetType$> +<$endif$> <$comment hasScalarAttributrType$> <$endif$> <$comment hasDefinedAttributeType$> <$endforeach do$> From 28c0a72da5f6fc3fd32d16c63c0b9eab650becbe Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 1 Sep 2018 16:48:47 -0600 Subject: [PATCH 13/18] Add Codable property support via a new attributeCodableTypeName user info key, as described in #375. --- mogenerator.m | 11 +++++++++++ templates/machine.swift.motemplate | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/mogenerator.m b/mogenerator.m index be8ca47c..77c908f3 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -586,6 +586,13 @@ - (NSString*)objectAttributeClassName { if (!result) { result = gSwift ? @"AnyObject" : @"NSObject"; } + } else if (gSwift && [self attributeType] == NSBinaryDataAttributeType) { + NSString *codableName = [self userInfo][@"attributeCodableTypeName"]; + if (!codableName) { + result = @"Data"; + } else { + result = codableName; + } } else { // Forcibly generate the correct class name in case we are // running on macOS < 10.13 @@ -638,6 +645,10 @@ - (BOOL)usesCustomObjectAttributeType { return (attributeValueClassName != nil); } +- (BOOL)usesCustomCodableAttributeType { + return [self userInfo][@"attributeCodableTypeName"] != NULL; +} + - (NSString*)objectAttributeType { NSString *result = [self objectAttributeClassName]; if ([result isEqualToString:@"Class"]) { diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 7d7bc749..2ea3dabf 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -128,6 +128,29 @@ public extension <$managedObjectClassName$> { setPrimitiveValue(value.rawValue, forKey: key) } } +<$elseif Attribute.usesCustomCodableAttributeType$> + // Attribute <$Attribute.name$> with Codable type <$Attribute.objectAttributeType$> + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + guard let primitiveValue = primitiveValue(forKey: key) as? Data else { + return nil + } + + return try? JSONDecoder().decode(<$Attribute.objectAttributeType$>.self, from: primitiveValue) + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + let primitiveValue = try? JSONEncoder().encode(newValue) + setPrimitiveValue(primitiveValue, forKey: key) + } + } <$else$> <$comment usesRawValueEnumType = false $> @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> <$endif$> <$comment usesRawValueEnumType $> From 6d5927eef00a951bd1cb701f5abf80734783cd71 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 8 Sep 2018 16:08:49 -0600 Subject: [PATCH 14/18] Update tests to use/require Xcode 10. --- .travis.yml | 2 +- test/Rakefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b105ef28..ef076d60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: objective-c -osx_image: xcode9 +osx_image: xcode10 #xcode_project: mogenerator.xcodeproj #xcode_scheme: mogenerator script: cd test && rake diff --git a/test/Rakefile b/test/Rakefile index b3534f0b..95c67d11 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -12,7 +12,7 @@ end #============================== # Variables #============================== -OSX_SDK="/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" +OSX_SDK="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" OSX_VERSION="10.10" LINKED_FRAMEWORKS="-framework Foundation -framework AppKit -framework CoreData" From 47e39c17e6f42307b4813a249cf410b451657bca Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 8 Sep 2018 16:49:25 -0600 Subject: [PATCH 15/18] Add explanatory comments in generated code --- templates/human.swift.motemplate | 3 ++- templates/machine.swift.motemplate | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/templates/human.swift.motemplate b/templates/human.swift.motemplate index 4da8776e..881757b3 100644 --- a/templates/human.swift.motemplate +++ b/templates/human.swift.motemplate @@ -4,6 +4,7 @@ import CoreData @objc(<$sanitizedManagedObjectClassName$>) open class <$sanitizedManagedObjectClassName$>: <$customSuperentity$> { - // Custom code for <$sanitizedManagedObjectClassName$> goes here. + // Put custom code for <$sanitizedManagedObjectClassName$> here. + // This file will be created if it doesn't exist, but mogenerator will not replace an existing copy. } diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 2ea3dabf..39eb8dfe 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -1,10 +1,11 @@ -// DO NOT EDIT. This file is machine-generated and constantly overwritten. +// DO NOT EDIT. This file is generated by mogenerator and will be overwritten. // Make changes to <$managedObjectClassName$>.swift instead. import CoreData <$if hasCustomBaseCaseImport$>import <$baseClassImport$><$endif$> <$if hasAdditionalImports$> +// Additional imports via the "additionalImports" userInfo key. <$foreach Import additionalImports do$> import <$Import$> <$endforeach do$> @@ -13,27 +14,32 @@ import <$Import$> public extension <$managedObjectClassName$> { + // Convenience lookup of the entity name for <$managedObjectClassName$> @objc <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { return "<$name$>" } + // Convenience to create a new <$managedObjectClassName$> in a managed object context. @objc <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { return NSEntityDescription.entity(forEntityName: entityName, in: managedObjectContext) } <$if noninheritedAttributes.@count > 0$> + // Attribute names for <$managedObjectClassName$> public struct Attributes {<$foreach Attribute noninheritedAttributes do$> static let <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$> } <$endif$> <$if noninheritedRelationships.@count > 0$> + // Core Data relationship names for <$managedObjectClassName$> public struct Relationships {<$foreach Relationship noninheritedRelationships do$> static let <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$> } <$endif$> <$if noninheritedFetchedProperties.@count > 0$> + // Fetched propertie names for <$managedObjectClassName$> public struct FetchedProperties {<$foreach FetchedProperty noninheritedFetchedProperties do$> static let <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$> } @@ -44,6 +50,7 @@ public extension <$managedObjectClassName$> { static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> } <$endif$> + /// Create a fetch request for this entity. public class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> { return NSFetchRequest(entityName: self.entityName) } @@ -54,6 +61,7 @@ public extension <$managedObjectClassName$> { <$if Attribute.hasScalarAttributeType$> <$if Attribute.usesRawValueEnumType$> + // Attribute <$Attribute.name$> is an enum with a numeric raw type. public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { get { let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> @@ -78,6 +86,7 @@ public extension <$managedObjectClassName$> { <$else$> <$comment usesRawValueEnumType = false $> <$if Attribute.isOptional$> <$if Attribute.usesScalarAttributeType$> + // Attribtue <$Attribute.name$> is a scalar optional public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { get { let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> @@ -102,11 +111,13 @@ public extension <$managedObjectClassName$> { @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: NSNumber? <$endif$> <$else$> <$comment isOptional is not true $> + // <$Attribute.name$> is required (not optional) @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> <$endif$> <$comment isOptional $> <$endif$> <$comment usesRawValueEnumType $> <$else$> <$comment hasScalarAttributeType = false (object type) $> <$if Attribute.usesRawValueEnumType$> <$comment This is here because enums with String raw value don't count as scalars $> + // Attribute <$Attribute.name$> is an enum with a non-numeric raw type. public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { get { let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> From 4ce753449dbf9a828f1af9b67138f10305732955 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Tue, 1 Jan 2019 14:38:31 -0700 Subject: [PATCH 16/18] Fix attributeRawValueEnumType for non-optionals. Additional fix for #376 --- templates/machine.swift.motemplate | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index 39eb8dfe..37cdb0e3 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -60,6 +60,7 @@ public extension <$managedObjectClassName$> { <$if Attribute.hasDefinedAttributeType$> <$if Attribute.hasScalarAttributeType$> +<$if Attribute.isOptional$> <$if Attribute.usesRawValueEnumType$> // Attribute <$Attribute.name$> is an enum with a numeric raw type. public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { @@ -83,9 +84,7 @@ public extension <$managedObjectClassName$> { setPrimitiveValue(value.rawValue, forKey: key) } } -<$else$> <$comment usesRawValueEnumType = false $> -<$if Attribute.isOptional$> -<$if Attribute.usesScalarAttributeType$> +<$elseif Attribute.usesScalarAttributeType$> // Attribtue <$Attribute.name$> is a scalar optional public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { get { @@ -107,14 +106,13 @@ public extension <$managedObjectClassName$> { setPrimitiveValue(value, forKey: key) } } -<$else$> <$comment usesScalarAttributeType = true but the above checks are not true $> +<$else$> <$comment isOptional = true but the above checks are not true $> @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: NSNumber? -<$endif$> -<$else$> <$comment isOptional is not true $> +<$endif$> <$comment end of if/elesif/else for optional properties $> +<$else$> <$comment isOptional is not true but hasScalarAttributeType is still true $> // <$Attribute.name$> is required (not optional) @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> <$endif$> <$comment isOptional $> -<$endif$> <$comment usesRawValueEnumType $> <$else$> <$comment hasScalarAttributeType = false (object type) $> <$if Attribute.usesRawValueEnumType$> <$comment This is here because enums with String raw value don't count as scalars $> // Attribute <$Attribute.name$> is an enum with a non-numeric raw type. @@ -167,7 +165,7 @@ public extension <$managedObjectClassName$> { <$endif$> <$comment usesRawValueEnumType $> <$endif$> <$comment hasScalarAttributrType$> <$endif$> <$comment hasDefinedAttributeType$> -<$endforeach do$> +<$endforeach do$> <$comment end of properties $> // MARK: - Relationships <$foreach Relationship noninheritedRelationships do$> From bbb23578bf53cb267e8005ad8b3a35ba03be015e Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 20 Apr 2019 12:56:58 -0600 Subject: [PATCH 17/18] Re-add new README description for this branch and update version. --- README.md | 22 ++++++++++++++++++++++ mogenerator.m | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 91584a43..1d68e432 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,28 @@ Senseful wrote up a [nice summary of mogenerator's command-line options](http:// ## Version History +### v1.33: Date TBD + +* [NEW] Update Swift templates for better Swift style ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/368)) + +* [FIX] Fixed invalid call of `NSManagedObject.fetchRequest()` ([Vincent Esche](https://github.com/rentzsch/mogenerator/pull/372)) + +* [NEW] Add support for URI and UUID attribute types ([Trevor Squires](https://github.com/rentzsch/mogenerator/pull/370)) + +* [NEW] Expose information when custom type is set through userInfo ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/369)) + +* [CHANGE] Nullability annotations for generated primitive accessors ([Michael Babin](https://github.com/rentzsch/mogenerator/pull/363)) + +* [FIX] Support for “Uses Scalar Type” ([Rok Gregorič](https://github.com/rentzsch/mogenerator/pull/352)) + +* [FIX] Swift 3 `NSData` to `Data` ([Christopher Rogers](https://github.com/rentzsch/mogenerator/pull/350)) + +* [FIX] Add specialized `fetchRequest()` func to Swift's machine generated files ([0xpablo](https://github.com/rentzsch/mogenerator/pull/358)) + +* [FIX] Correct Swift machine template for singleton (fetchOne…) fetch request results ([Warren Burton](https://github.com/rentzsch/mogenerator/pull/359)) + +* [FIX] Expose allAttributes and allRelationships ([Trevor Squires](https://github.com/rentzsch/mogenerator/pull/360)) + ### v1.32: Wed Jan 30 2019 [download](https://github.com/rentzsch/mogenerator/releases/tag/1.32) * [NEW] Support for URL and UUID property types ([Trevor Squires](https://github.com/tomekc/mogenerator/pull/1), [original PR](https://github.com/rentzsch/mogenerator/pull/370)) diff --git a/mogenerator.m b/mogenerator.m index 970b8cc2..f6c8eaf1 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -1084,7 +1084,7 @@ - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { } if (_version) { - printf("mogenerator 1.32b. By Jonathan 'Wolf' Rentzsch + friends.\n"); + printf("mogenerator 1.33. By Jonathan 'Wolf' Rentzsch + friends.\n"); return EXIT_SUCCESS; } From 9f8504ca90961a0ec44ab69b6aa602e7aef243d4 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 20 Apr 2019 15:24:56 -0600 Subject: [PATCH 18/18] One more README bullet --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1d68e432..52b5bb91 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ Senseful wrote up a [nice summary of mogenerator's command-line options](http:// * [NEW] Expose information when custom type is set through userInfo ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/369)) +* [NEW] Add Codable property support via a new attributeCodableTypeName user info key ([Tom Harrington](https://github.com/rentzsch/mogenerator/issues/375)) + * [CHANGE] Nullability annotations for generated primitive accessors ([Michael Babin](https://github.com/rentzsch/mogenerator/pull/363)) * [FIX] Support for “Uses Scalar Type” ([Rok Gregorič](https://github.com/rentzsch/mogenerator/pull/352))