Skip to content

Instantly share code, notes, and snippets.

@Sega-Zero
Last active May 1, 2016 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sega-Zero/655e76726c23e8acc87b04144e82b9bc to your computer and use it in GitHub Desktop.
Save Sega-Zero/655e76726c23e8acc87b04144e82b9bc to your computer and use it in GitHub Desktop.
Example structures for stress-testing a Cereal library
enum SerializationError: ErrorType {
case IncorrectData
case FileNotExists
}
extension Array where Element: CerealType {
func serialize(to filePath: String) throws {
var encoder = CerealEncoder(capacity: self.count)
try encoder.encode(self, forKey: "array")
let data = encoder.toData()
try data.writeToFile(filePath, options: [.AtomicWrite])
}
mutating func deserialize(from filePath: String) throws {
guard let data = NSData(contentsOfFile: filePath) else { throw SerializationError.FileNotExists }
let decoder = try CerealDecoder(data: data)
guard let result: [Element] = try decoder.decodeCereal("array") else { throw SerializationError.IncorrectData }
self = result
}
}
public struct NameData {
public let givenName: String
public let familyName: String
}
public struct TestEmployee {
public enum DataKind: String {
case Phone = "phone"
case Address = "address"
}
public struct ExtraInfo {
let data: String
let kind: DataKind
}
public enum EmailKind: String {
case Home = "home"
case Work = "work"
}
public struct EmailInfo {
let email: String
let kind: EmailKind
}
public let id: String
public let name: NameData
public let company: String?
public let data: [ExtraInfo]
public let emails: [EmailInfo]
}
extension TestEmployee.DataKind: CerealRepresentable {}
extension TestEmployee.EmailKind: CerealRepresentable {}
extension TestEmployee.ExtraInfo: CerealType {
private struct DecodeKeys {
static let data = "data"
static let kind = "kind"
}
public func numberOfEntries() -> Int {
return 2
}
public init(decoder: CerealDecoder) throws {
data = try decoder.decode(DecodeKeys.data) ?? ""
kind = try decoder.decode(DecodeKeys.kind) ?? .Phone
}
public func encodeWithCereal(inout encoder: CerealEncoder) throws {
try encoder.encode(data, forKey: DecodeKeys.data)
try encoder.encode(kind, forKey: DecodeKeys.kind)
}
}
extension TestEmployee.EmailInfo: CerealType {
private struct DecodeKeys {
static let email = "email"
static let kind = "kind"
}
public func numberOfEntries() -> Int {
return 2
}
public init(decoder: CerealDecoder) throws {
email = try decoder.decode(DecodeKeys.email) ?? ""
kind = try decoder.decode(DecodeKeys.kind) ?? .Home
}
public func encodeWithCereal(inout encoder: CerealEncoder) throws {
try encoder.encode(email, forKey: DecodeKeys.email)
try encoder.encode(kind, forKey: DecodeKeys.kind)
}
}
extension TestEmployee: CerealType {
private struct DecodeKeys {
static let id = "id"
static let name = "name"
static let company = "company"
static let data = "data"
static let emails = "emails"
}
public func numberOfEntries() -> Int {
return 5
}
public init(decoder: CerealDecoder) throws {
guard let id: String = try decoder.decode(DecodeKeys.id) else { throw SerializationError.IncorrectData }
self.id = id
name = try decoder.decodeCereal(DecodeKeys.name) ?? NameData(givenName: "", familyName: "")
company = try decoder.decode(DecodeKeys.company)
data = try decoder.decodeCereal(DecodeKeys.data) ?? []
emails = try decoder.decodeCereal(DecodeKeys.emails) ?? []
}
public func encodeWithCereal(inout encoder: CerealEncoder) throws {
try encoder.encode(id, forKey: DecodeKeys.id)
try encoder.encode(name, forKey: DecodeKeys.name)
try encoder.encode(company, forKey: DecodeKeys.company)
try encoder.encode(data, forKey: DecodeKeys.data)
try encoder.encode(emails, forKey: DecodeKeys.emails)
}
}
extension NameData: CerealType {
private struct Keys {
static let givenName = "givenName"
static let familyName = "familyName"
}
public func numberOfEntries() -> Int {
return 2
}
public init(decoder: CerealDecoder) throws {
guard let givenName: String = try decoder.decode(Keys.givenName) else { throw SerializationError.IncorrectData }
self.givenName = givenName
familyName = try decoder.decode(Keys.familyName) ?? ""
}
public func encodeWithCereal(inout encoder: CerealEncoder) throws {
try encoder.encode(givenName, forKey: Keys.givenName)
try encoder.encode(familyName, forKey: Keys.familyName)
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
let contact = TestEmployee(id: "12", name: NameData(givenName: "first", familyName: "last"), company: "company", data: [TestEmployee.ExtraInfo(data: "00000", kind: .Phone)], emails: [TestEmployee.EmailInfo(email: "mail@mail.com", kind: .Home), TestEmployee.EmailInfo(email: "mail@mail.com", kind: .Work)])
var employees = Array(count: 10_000, repeatedValue: contact)
let rootPath = NSSearchPathForDirectoriesInDomains(.ApplicationSupportDirectory, .UserDomainMask, true)[0]
do {
try employees.serialize(to: rootPath + "/test.tst")
try employees.deserialize(from: rootPath + "/test.tst")
} catch {
print("oops")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment