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

Swift updates #373

Closed
wants to merge 13 commits into from
Closed
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
2 changes: 1 addition & 1 deletion mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 6 additions & 3 deletions templates/human.swift.motemplate
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Foundation
import CoreData

@objc(<$sanitizedManagedObjectClassName$>)
open class <$sanitizedManagedObjectClassName$>: <$customSuperentity$> {

// Custom code for <$sanitizedManagedObjectClassName$> goes here.

@objc(<$managedObjectClassName$>)
open class <$managedObjectClassName$>: _<$managedObjectClassName$> {
// Custom logic goes here.
}
235 changes: 61 additions & 174 deletions templates/machine.swift.motemplate
Original file line number Diff line number Diff line change
@@ -1,211 +1,98 @@
// 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 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$>
public extension <$managedObjectClassName$> {

<$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 {
@objc <$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? {
return NSEntityDescription.entity(forEntityName: self.entityName(), in: managedObjectContext)
}

@nonobjc
open class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> {
return NSFetchRequest(entityName: self.entityName())
@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$>
public struct Attributes {<$foreach Attribute noninheritedAttributes do$>
static let <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$>
}
<$endif$>

// MARK: - Life cycle methods
<$if noninheritedRelationships.@count > 0$>
public struct Relationships {<$foreach Relationship noninheritedRelationships do$>
static let <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$>
}
<$endif$>

public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
super.init(entity: entity, insertInto: context)
}
<$if noninheritedFetchedProperties.@count > 0$>
public struct FetchedProperties {<$foreach FetchedProperty noninheritedFetchedProperties do$>
static let <$FetchedProperty.name$> = "<$FetchedProperty.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 hasUserInfoKeys && userInfoKeyValues.@count > 0$>
public struct UserInfo {<$foreach UserInfo userInfoKeyValues do$>
static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$>
}
<$endif$>
public class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> {
return NSFetchRequest(entityName: self.entityName)
}

// 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$>

// 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<Any>()
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 public 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<NSObject><$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<NSObject><$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$>
2 changes: 1 addition & 1 deletion test/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
20 changes: 10 additions & 10 deletions test/swift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down