Skip to content

Instantly share code, notes, and snippets.

View ChristianKienle's full-sized avatar
🐶
Coconut Shock

Christian Kienle ChristianKienle

🐶
Coconut Shock
  • Apple
  • Cupertino
View GitHub Profile
func testSimpleObjectTransformation() {
// Lets create the first version of our data model:
// A single entity called Person with a single property called name.
let person1 = Entity("Person", [Property(.String, "name")])
let model1 = Model(1, [person1])
// Now, in version 2 of our app we want to rename the name-property of Person
// to firstName. So let's define the person property V2 as:
let person2 = Entity("Person", [Property(.String, "firstName", "name")])
let model2 = Model(2, [person2])
Pseudo Code:
// Create table:
CREATE TABLE LOCK (who Text, locked Bool);
// You have to use PGCKit to be able to query this table and create instances of:
struct Lock {
NSString *who;
class Person {}
class SpecialPerson : Person {}
func use(person: Person) { println("person") }
func use(person: SpecialPerson) { println("special person") }
func prepare(person: Person) { use(person) }
use(SpecialPerson()) // logs: special person
prepare(SpecialPerson()) // logs: person
import Foundation
/*
Lets suppose you have a class called "Action" which has to represent an user action in a drawing app. There are a fixed number of different actions: Delete, Add, Modify. Some of those action types have different properties. For example:
If the user adds something, the add action contains "what" the user has added and the "content" of the new object.
If the user modifies something, the modify action contains "what" has been edited, the "previousContent" and the "newContent".
So there are basically at least three different approaches how you can model that. Which one is the best?
import Foundation
class QueryResult {} // if I make this class public then the compiler says that generate has to be declared public.
// If I make generate public I get a different error...
// But QueryResult is a class which has to be public... it is part of my public API...
public class Row {}
class QueryResultGenerator : GeneratorType {
typealias Element = Row
func next() -> Element? {
import Foundation
public struct Row {
private let valuesByColumnNames = [String: Bindable]()
internal init(valuesByColumnNames:[String: Bindable]) {
self.valuesByColumnNames = valuesByColumnNames
}
}
// General
import Foundation
public struct Row {
private let valuesByColumnNames = [String: Bindable]()
internal init(valuesByColumnNames:[String: Bindable]) {
self.valuesByColumnNames = valuesByColumnNames
}
}
// General
import Foundation
public class Statement {}
public protocol Bindable {
func bindTo(statement:Statement, atIndex:Int32) -> Bool
}
public enum UpdateResult {
case Success
}
public class Database {
0 swift 0x0000000102db6968 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x0000000102db6e54 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff94535f1a _sigtramp + 26
3 swift 0x0000000102cc3e7b llvm::FoldingSet<llvm::AttributeSetNode>::NodeEquals(llvm::FoldingSetImpl::Node*, llvm::FoldingSetNodeID const&, unsigned int, llvm::FoldingSetNodeID&) const + 59
4 swift 0x000000010217e2ef swift::irgen::ProtocolInfo::getConformance(swift::irgen::IRGenModule&, swift::CanType, swift::irgen::TypeInfo const&, swift::ProtocolDecl*, swift::ProtocolConformance const&) const + 207
5 swift 0x0000000102181363 swift::irgen::emitPolymorphicArguments(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Substitution>, swift::irgen::Explosion&) + 819
6 swift 0x00000001021c3f31 swift::SILVisitor<(anonymous na
var queryResult = self.database.executeQuery("SELECT * FROM PERSON", arguments:[])
XCTAssertTrue(queryResult.isSuccess)
queryResult.consume { resultSet in
var name:String = resultSet.value(0)
XCTAssertEqual("Christian", "Name")
}