Skip to content

Commit

Permalink
Merge pull request #186 from qutheory/04-12
Browse files Browse the repository at this point in the history
New Snapshot - 04-12
  • Loading branch information
loganwright committed Apr 22, 2016
2 parents d869f1f + c725760 commit 4ae4b0e
Show file tree
Hide file tree
Showing 54 changed files with 1,803 additions and 2,015 deletions.
2 changes: 1 addition & 1 deletion .swift-version
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-03-24-a
DEVELOPMENT-SNAPSHOT-2016-04-12-a
18 changes: 10 additions & 8 deletions Package.swift
Expand Up @@ -4,24 +4,23 @@ let package = Package(
name: "Vapor",
dependencies: [
//Standards package. Contains protocols for cross-project compatability.
.Package(url: "https://github.com/swiftx/s4.git", majorVersion: 0, minor: 2),
.Package(url: "https://github.com/open-swift/S4.git", majorVersion: 0, minor: 4),

//Provides critical String functions Foundation is missing on Linux
.Package(url: "https://github.com/Zewo/String.git", majorVersion: 0, minor: 4),
.Package(url: "https://github.com/Zewo/String.git", majorVersion: 0, minor: 5),

//Parses and serializes JSON
.Package(url: "https://github.com/Zewo/JSON.git", majorVersion: 0, minor: 4),
.Package(url: "https://github.com/Zewo/JSON.git", majorVersion: 0, minor: 5),

//Swift wrapper around Sockets, used for built-in HTTP server
.Package(url: "https://github.com/qutheory/Hummingbird.git", majorVersion: 2, minor: 0),
.Package(url: "https://github.com/ketzusaka/Hummingbird.git", majorVersion: 1, minor: 4),

//SHA2 + HMAC hashing. Used by the core to create session identifiers.
.Package(url: "https://github.com/CryptoKitten/HMAC.git", majorVersion: 0, minor: 3),
.Package(url: "https://github.com/CryptoKitten/SHA2.git", majorVersion: 0, minor: 1)
.Package(url: "https://github.com/CryptoKitten/HMAC.git", majorVersion: 0, minor: 4),
.Package(url: "https://github.com/CryptoKitten/SHA2.git", majorVersion: 0, minor: 3)
],
exclude: [
"XcodeProject",
"Sources/Generator"
"XcodeProject"
],
targets: [
Target(
Expand All @@ -41,6 +40,9 @@ let package = Package(
dependencies: [
.Target(name: "Vapor")
]
),
Target(
name: "Generator"
)
]
)
33 changes: 23 additions & 10 deletions Sources/Development/Controllers/UserController.swift
Expand Up @@ -5,37 +5,50 @@ class UserController: Controller {
Log.info("User controller created")
}

/// Display many instances
func index(request: Request) throws -> ResponseRepresentable {
/**
Display many instances
*/
func index(_ request: Request) throws -> ResponseRepresentable {
return Json([
"controller": "MyController.index"
])
}

/// Create a new instance.
func store(request: Request) throws -> ResponseRepresentable {

/**
Create a new instance.
*/
func store(_ request: Request) throws -> ResponseRepresentable {
return Json([
"controller": "MyController.store"
])
}

/// Show an instance.
func show(request: Request, item user: User) throws -> ResponseRepresentable {

/**
Show an instance.
*/
func show(_ request: Request, item user: User) throws -> ResponseRepresentable {
//User can be used like JSON with JsonRepresentable
return Json([
"controller": "MyController.show",
"user": user
])
}

/// Update an instance.
func update(request: Request, item user: User) throws -> ResponseRepresentable {
/**
Update an instance.
*/
func update(_ request: Request, item user: User) throws -> ResponseRepresentable {
//Testing JsonRepresentable
return user.makeJson()
}

/// Delete an instance.
func destroy(request: Request, item user: User) throws -> ResponseRepresentable {

/**
Delete an instance.
*/
func destroy(_ request: Request, item user: User) throws -> ResponseRepresentable {
//User is ResponseRepresentable by proxy of JsonRepresentable
return user
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Development/Middleware/AuthMiddleware.swift
Expand Up @@ -5,7 +5,7 @@ class AuthMiddleware: Middleware {
case Unauthorized
}

func respond(request: Request, chain: Responder) throws -> Response {
func respond(to request: Request, chainingTo chain: Responder) throws -> Response {
guard let session = request.session else {
throw Error.Unauthorized
}
Expand All @@ -14,7 +14,7 @@ class AuthMiddleware: Middleware {
throw Error.Unauthorized
}

return try chain.respond(request)
return try chain.respond(to: request)
}

}
12 changes: 4 additions & 8 deletions Sources/Generator/main.swift
Expand Up @@ -35,13 +35,9 @@ struct Func: CustomStringConvertible {
f += "<\(genericsString)>"
}

let paramsString = params.enumerated().map { (index, param) in
if index > 0 {
return "_ \(param)"
} else {
return param.description
}
}.joined(separator: ", ")
let paramsString = params
.map { param in "_ \(param)" }
.joined(separator: ", ")

f += "(\(paramsString), handler: (Request"

Expand Down Expand Up @@ -113,7 +109,7 @@ struct Func: CustomStringConvertible {
}
}

func paramTypeCount(type: Param.`Type`, params: [Param]) -> Int {
func paramTypeCount(_ type: Param.`Type`, params: [Param]) -> Int {
var i = 0

for param in params {
Expand Down
74 changes: 49 additions & 25 deletions Sources/Vapor/Config/Config.swift
Expand Up @@ -18,8 +18,10 @@
sensitive information does not get added to version control.
*/
public class Config {
//The internal store of configuration options
//backed by `Json`
/**
The internal store of configuration options
backed by `Json`
*/
private var repository: [String: Json]

public enum Error: ErrorProtocol {
Expand All @@ -40,16 +42,19 @@ public class Config {
}
}

///Returns whether this instance of `Config` contains the key
public func has(keyPath: String) -> Bool {
/**
Returns whether this instance of `Config` contains the key
*/
public func has(_ keyPath: String) -> Bool {
let result: Node? = try? get(keyPath)
return result != nil
}

///Returns the generic Json representation for an item at a given path or throws
public func get(keyPath: String) throws -> Node {
/**
Returns the generic Json representation for an item at a given path or throws
*/
public func get(_ keyPath: String) throws -> Node {
var keys = keyPath.keys

guard let json: Json = repository[keys.removeFirst()] else {
throw Error.NoFileFound
}
Expand All @@ -67,22 +72,28 @@ public class Config {
return result
}

//Returns the value for a given type from the Config or throws
public func get<T: NodeInitializable>(keyPath: String) throws -> T {
/**
Returns the value for a given type from the Config or throws
*/
public func get<T: NodeInitializable>(_ keyPath: String) throws -> T {
let result: Node = try get(keyPath)
return try T.makeWith(result)
return try T.make(with: result)
}


///Returns the result of `get(key: String)` but with a `String` fallback for `nil` cases
public func get<T: NodeInitializable>(keyPath: String, _ fallback: T) -> T {
/**
Returns the result of `get(key: String)` but with a `String` fallback for `nil` cases
*/
public func get<T: NodeInitializable>(_ keyPath: String, _ fallback: T) -> T {
let string: T? = try? get(keyPath)
return string ?? fallback
}


///Temporarily sets a value for a given key path
public func set(value: Json, forKeyPath keyPath: String) {
/**
Temporarily sets a value for a given key path
*/
public func set(_ value: Json, forKeyPath keyPath: String) {
var keys = keyPath.keys
let group = keys.removeFirst()

Expand All @@ -93,8 +104,10 @@ public class Config {
}
}

///Calls populate() in a convenient non-throwing manner
public func populate(application: Application) -> Bool {
/**
Calls populate() in a convenient non-throwing manner
*/
public func populate(_ application: Application) -> Bool {
let configDir = application.workDir + "Config"

if FileManager.fileAtPath(configDir).exists {
Expand All @@ -112,8 +125,10 @@ public class Config {



///Attempts to populate the internal configuration store
public func populate(path: String, application: Application) throws {
/**
Attempts to populate the internal configuration store
*/
public func populate(_ path: String, application: Application) throws {
var path = path.finish("/")
var files = [String: [String]]()

Expand All @@ -127,10 +142,14 @@ public class Config {
try populateConfigFiles(&files, in: path)
}
}

// Loop through files and merge config upwards so the
// environment always overrides the base config
for (group, files) in files {
//
//
// The isEmpty check is a workaround for the Linux system and is necessary until
// an alternative solution is fixed, or it's confirmed appropriate
// This is duplicated below in `populateConfigFiles`. just doubling down.
for (group, files) in files where !group.isEmpty {
if group == ".env" {
// .env is handled differently below
continue
Expand Down Expand Up @@ -170,19 +189,24 @@ public class Config {
}
}

private func populateConfigFiles(files: inout [String: [String]], in path: String) throws {
private func populateConfigFiles(_ files: inout [String: [String]], in path: String) throws {
let contents = try FileManager.contentsOfDirectory(path)

for file in contents {
guard let fileName = file.split("/").last else {
//
//
// The isEmpty check is a workaround for the Linux system and is necessary until
// an alternative solution is fixed, or it's confirmed appropriate
// This is duplicated above. just doubling down.
guard let fileName = file.split(byString: "/").last where !fileName.isEmpty else {
continue
}

let name: String

if fileName == ".env.json" {
name = ".env"
} else if fileName.hasSuffix(".json"), let value = fileName.split(".").first {
} else if fileName.hasSuffix(".json"), let value = fileName.split(byString: ".").first {
name = value
} else {
continue
Expand All @@ -200,7 +224,7 @@ public class Config {

extension Json {

private mutating func set(value: Json, keys: [Swift.String]) {
private mutating func set(_ value: Json, keys: [Swift.String]) {
var keys = keys

guard keys.count > 0 else {
Expand Down Expand Up @@ -229,7 +253,7 @@ extension Json {
extension String {

private var keys: [String] {
return split(".")
return split(byString: ".")
}

}

0 comments on commit 4ae4b0e

Please sign in to comment.