Repository: vapor/vapor
File: Sources/Vapor/Session/MemorySessionDriver.swift (Swift)

Code before:
import Foundation

/**
 * The `MemorySessionDriver` stores session data
 * in a Swift `Dictionary`. This means all session
 * data will be purged if the server is restarted.
 */
public class MemorySessionDriver: SessionDriver {
    var sessions = [String: [String: String]]()

    public init() { }

    public func valueForKey(key: String, inSessionIdentifiedBy sessionIdentifier: String) -> String? {
        return sessions[sessionIdentifier]?[key]
    }

    public func setValue(value: String?, forKey key: String, inSessionIdentifiedBy sessionIdentifier: String) {
        if sessions[sessionIdentifier] == nil {
            sessions[sessionIdentifier] = [String: String]()
        }

        sessions[sessionIdentifier]![key] = value
    }

    public var randomSessionIdentifier: String {
        var identifier = String(NSDate().timeIntervalSinceNow)
        identifier += "v@p0r"
        identifier += String(Int.random(min: 0, max: 9999))
        identifier += "s3sS10n"
        identifier += String(Int.random(min: 0, max: 9999))
        identifier += "k3y"
        identifier += String(Int.random(min: 0, max: 9999))
        return Hash.make(identifier)
    }

    public func destroySessionIdentifiedBy(sessionIdentifier: String) {
        sessions[sessionIdentifier] = nil
    }
}


Review comment (suggestion):
I know we're setting it above and it should definitely exist, but I'd still feel more comfortable using `?` here.  It shouldn't change behavior at all


Code after:
import Foundation

/**
 * The `MemorySessionDriver` stores session data
 * in a Swift `Dictionary`. This means all session
 * data will be purged if the server is restarted.
 */
public class MemorySessionDriver: SessionDriver {
    var sessions = [String: [String: String]]()

    public init() { }

    public func valueFor(key key: String, inSession session: Session) -> String? {
        guard let sessionIdentifier = session.identifier else {
            Log.warning("Unable to read a value for '\(key)': The session has not been registered yet")
            return nil
        }

        return sessions[sessionIdentifier]?[key]
    }

    public func set(value: String?, forKey key: String, inSession session: Session) {
        guard let sessionIdentifier = session.identifier else {
            Log.warning("Unable to store a value for '\(key)': The session has not been registered yet")
            return
        }

        if sessions[sessionIdentifier] == nil {
            sessions[sessionIdentifier] = [String: String]()
        }

        sessions[sessionIdentifier]?[key] = value
    }

    public func newSessionIdentifier() -> String {
        var identifier = String(NSDate().timeIntervalSinceNow)
        identifier += "v@p0r"
        identifier += String(Int.random(min: 0, max: 9999))
        identifier += "s3sS10n"
        identifier += String(Int.random(min: 0, max: 9999))
        identifier += "k3y"
        identifier += String(Int.random(min: 0, max: 9999))
        return Hash.make(identifier)
    }

    public func destroy(session: Session) {
        guard let sessionIdentifier = session.identifier else {
            Log.warning("Unable to destroy the session: The session has not been registered yet")
            return
        }

        sessions[sessionIdentifier] = nil
    }
}