// // AccountInfo.swift // Database // // Created by Brent Simmons on 7/3/17. // Copyright © 2017 Ranchero Software. All rights reserved. // import Foundation import RSDatabase import Data // This allows for serializing structures such as Author, Attachment, and AccountInfo // without having to create separate tables and lookup tables. // While there are good strong arguments for using separate tables, // we decided that the relative simplicity this allows is worth it. struct PropertyListTransformer { static func accountInfoWithRow(_ row: FMResultSet) -> AccountInfo? { guard let rawAccountInfo = row.data(forColumn: DatabaseKey.accountInfo) else { return nil } return propertyList(withData: rawAccountInfo) as? AccountInfo } static func propertyListWithRow(_ row: FMResultSet, column: String) -> Any? { guard let rawData = row.data(forColumn: column) else { return nil } return propertyList(withData: rawData) } static func propertyList(withData data: Data) -> Any? { do { return try PropertyListSerialization.propertyList(fromData: rawAccountInfo, options: [], format: nil) } catch { return nil } } static func data(withPropertyList plist: Any) -> Data? { do { return try PropertyListSerialization.data(from: plist, format: .binary, options: []) } catch { return nil } } }