Updating the changes to my storm module

This commit is contained in:
Ryan 2018-12-13 21:23:19 -05:00
commit aaf2e00774
4 changed files with 127 additions and 5 deletions

View File

@ -1,11 +1,12 @@
// Generated automatically by Perfect Assistant Application // Generated automatically by Perfect Assistant Application
// Date: 2017-08-13 18:42:55 +0000 // Date: 2018-03-02 16:12:45 +0000
import PackageDescription import PackageDescription
let package = Package( let package = Package(
name: "StORM", name: "StORM",
targets: [], targets: [],
dependencies: [ dependencies: [
.Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 3), .Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 3),
.Package(url: "https://github.com/iamjono/SwiftMoment.git", majorVersion: 0) .Package(url: "https://github.com/iamjono/SwiftMoment.git", majorVersion: 1),
.Package(url: "https://github.com/iamjono/SwiftString.git", majorVersion: 2),
] ]
) )

112
Sources/StORM/Extract.swift Normal file
View File

@ -0,0 +1,112 @@
//
// Extract.swift
// StORM
//
// Created by Jonathan Guthrie on 2018-03-02.
//
import Foundation
import SwiftMoment
import SwiftString
extension StORM {
public class Extract {
// =======================================================================================
// Process strings
// =======================================================================================
public static func string(_ data: [String: Any], _ name: String, _ def: String? = String()) -> String? {
return data[name] as? String ?? def
}
// =======================================================================================
// Process integers
// The Int32 is here for some MySQL use cases
// =======================================================================================
public static func int(_ data: [String: Any], _ name: String, _ def: Int? = Int()) -> Int? {
if let x = data[name] as? Int32, x > 0 {
return Int(x)
}
return data[name] as? Int ?? def
}
// =======================================================================================
// Process date
// Note this leverages SwiftMoment
// =======================================================================================
public static func date(_ data: [String: Any], _ name: String, _ def: Date? = Date()) -> Date? {
return moment(Extract.string(data, name)!)?.date ?? def
}
// =======================================================================================
// Double
// =======================================================================================
public static func double(_ data: [String: Any], _ name: String, _ def: Double? = Double()) -> Double? {
if let d = data[name] as? Double {
return d
} else if let d = data[name] as? Float {
return Double(d)
}
return Double((data[name] as? String) ?? "\(def ?? 0.00)")
// return Double((data[name] as? Float) ?? Float(def ?? 0.00))
}
// =======================================================================================
// Bool
// =======================================================================================
public static func bool(_ data: [String: Any], _ name: String, _ def: Bool? = Bool()) -> Bool? {
return data[name] as? Bool ?? def
}
// =======================================================================================
// Bytes / UInt8 array
// =======================================================================================
public static func bytes(_ data: [String: Any], _ name: String, _ def: [UInt8]? = [UInt8]()) -> [UInt8]? {
return data[name] as? [UInt8] ?? def
}
// =======================================================================================
// Array Of Strings
// =======================================================================================
public static func arrayOfStrings(_ data: [String: Any], _ name: String, _ def: [String]? = [String]()) -> [String]? {
return (data[name] as? String ?? "").split(",").map{ $0.trimmed() } // note default ignored right now
}
// =======================================================================================
// Array Of Integers
// =======================================================================================
public static func arrayOfIntegers(_ data: [String: Any], _ name: String, _ def: [Int]? = [Int]()) -> [Int]? {
return (data[name] as? String ?? "").split(",").map{ Int($0.trimmed()) ?? 0 } // note default ignored right now
}
// =======================================================================================
// Array Of Any
// =======================================================================================
public static func arrayOfAny(_ data: [String: Any], _ name: String, _ def: [Any]? = [Any]()) -> [Any]? {
return data[name] as? [Any] ?? def // not sure about this right now...
}
// =======================================================================================
// Return a JSON-type map [String:Any]
// =======================================================================================
public static func map(_ data: [String: Any], _ name: String, _ def: [String:Any]? = [String:Any]()) -> [String:Any]? {
return data[name] as? [String:Any] ?? def
}
}
}

View File

@ -32,9 +32,10 @@ open class StORMConnect {
host: String, host: String,
username: String = "", username: String = "",
password: String = "", password: String = "",
port: Int = 0) { port: Int = 0,
method: StORMConnectionMethod = .network) {
self.datasource = ds self.datasource = ds
self.credentials = StORMDataSourceCredentials(host: host, port: port, user: username, pass: password) self.credentials = StORMDataSourceCredentials(host: host, port: port, user: username, pass: password, method: method)
} }
} }

View File

@ -6,6 +6,10 @@
// //
// //
public enum StORMConnectionMethod: String {
case socket, network
}
/// Storage for current datasource connection properties. /// Storage for current datasource connection properties.
public struct StORMDataSourceCredentials { public struct StORMDataSourceCredentials {
@ -21,13 +25,17 @@ public struct StORMDataSourceCredentials {
/// Password for accessing the datasource. /// Password for accessing the datasource.
public var password: String = "" public var password: String = ""
/// Set the connection method - network (tcp/ip), or socket/dsn.
public var method: StORMConnectionMethod = .network
public init() {} public init() {}
/// Initializer to set properties at instantiation. /// Initializer to set properties at instantiation.
public init(host: String, port: Int = 0, user: String = "", pass: String = "") { public init(host: String, port: Int = 0, user: String = "", pass: String = "", method: StORMConnectionMethod = .network) {
self.host = host self.host = host
self.port = port self.port = port
self.username = user self.username = user
self.password = pass self.password = pass
self.method = method
} }
} }