Skip to content

Commit

Permalink
Merge pull request #7 from vapor/spring-cleaning
Browse files Browse the repository at this point in the history
Spring cleaning
  • Loading branch information
loganwright authored Mar 7, 2017
2 parents 4f6d93d + f87de87 commit cf6b6a4
Show file tree
Hide file tree
Showing 36 changed files with 528 additions and 824 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ let package = Package(
name: "Routing",
targets: [
// Routing
Target(name: "Routing"),
Target(name: "HTTPRouting", dependencies: ["Routing"]),
Target(name: "Branches"),
Target(name: "Routing", dependencies: ["Branches"]),

// Type Safe
Target(name: "TypeSafeRouting", dependencies: ["Routing", "HTTPRouting"]),
Target(name: "TypeSafeRouting", dependencies: ["Branches", "Routing"]),
// Target(name: "TypeSafeGenerator"),
],
dependencies: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
//
// Router+Routes.swift
// Routing
//
// Created by Logan Wright on 11/1/16.
//
//

import Foundation

extension Router {
public var routes: [String] {
var routes = [String]()
tree.forEach { host, methodTree in
methodTree.forEach { method, branch in
let base = "\(host) \(method) "
routes += branch.routes.map { base + $0 }
}
}
return routes
}
}

extension Branch {
public var routes: [String] {
Expand Down
40 changes: 14 additions & 26 deletions Sources/Routing/Branch.swift → Sources/Branches/Branch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,6 @@ extension Branch {
}
}

/**
Branch result is used to encapsulate some metadata when fetching a branch.
Less useful now, but will likely play role in chaining in future using remaining iterator.
*/
public class BranchResult<Output> {
public let branch: Branch<Output>
public let remaining: IndexingIterator<[String]>

init(_ branch: Branch<Output>, _ remaining: IndexingIterator<[String]>) {
self.branch = branch
self.remaining = remaining
}
}

/**
When routing requests, different branches will be established,
in a linked list style stemming from their host and request method.
Expand Down Expand Up @@ -115,6 +100,10 @@ public class Branch<Output> { // TODO: Rename Context
return output != nil
}

/// A branch has a singular parent, but multiple children with
/// varying levels of priority
/// named branches match first, followed by slugs, followed by
/// wildcard
internal fileprivate(set) var subBranches = SubBranchMap<Output>()

/**
Expand Down Expand Up @@ -147,7 +136,7 @@ public class Branch<Output> { // TODO: Rename Context
- returns: a request handler or nil if not supported
*/
public func fetch(_ path: [String]) -> BranchResult<Output>? {
public func fetch(_ path: [String]) -> Branch? {
return fetch(path.makeIterator())
}

Expand All @@ -160,30 +149,29 @@ public class Branch<Output> { // TODO: Rename Context
- returns: a request handler or nil if not supported
*/
public func fetch(_ path: IndexingIterator<[String]>) -> BranchResult<Output>? {
public func fetch(_ path: IndexingIterator<[String]>) -> Branch? {
var comps = path
guard let key = comps.next() else { return BranchResult(self, comps) }
guard let key = comps.next() else { return self }

// first check if direct path exists
if let result = subBranches.paramBranches[key]?.fetch(comps), result.branch.hasValidOutput {
return result
if let branch = subBranches.paramBranches[key]?.fetch(comps), branch.hasValidOutput {
return branch
}

// next attempt to find slug matches if any exist
for slug in subBranches.slugBranches {
guard let result = slug.fetch(comps), result.branch.hasValidOutput else { continue }
return result
guard let branch = slug.fetch(comps), branch.hasValidOutput else { continue }
return branch
}

// see if wildcard with path exists
if let result = subBranches.wildcard?.fetch(comps), result.branch.hasValidOutput {
return result
if let branch = subBranches.wildcard?.fetch(comps), branch.hasValidOutput {
return branch
}

// use fallback
if let wildcard = subBranches.wildcard, wildcard.hasValidOutput {
let subRoute = [key] + comps
return BranchResult(wildcard, subRoute.makeIterator())
return wildcard
}

// unmatchable route
Expand Down
File renamed without changes.
33 changes: 0 additions & 33 deletions Sources/HTTPRouting/Request+Routing.swift

This file was deleted.

76 changes: 0 additions & 76 deletions Sources/HTTPRouting/RouteBuilder+HTTP.swift

This file was deleted.

46 changes: 0 additions & 46 deletions Sources/HTTPRouting/Router+HTTP.swift

This file was deleted.

8 changes: 0 additions & 8 deletions Sources/HTTPRouting/Routing+HTTP.swift

This file was deleted.

23 changes: 0 additions & 23 deletions Sources/HTTPRouting/Utilities.swift

This file was deleted.

7 changes: 7 additions & 0 deletions Sources/Routing/Method+Wildcard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import HTTP

extension Method {
public static var wildcard: Method {
return .other(method: "*")
}
}
12 changes: 0 additions & 12 deletions Sources/Routing/ParametersContainer.swift

This file was deleted.

29 changes: 29 additions & 0 deletions Sources/Routing/Request+Routing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Branches
import HTTP
import Node

private let parametersKey = "parameters"

extension HTTP.Request {
/// when routing a url with slug parameters, ie:
/// foo/:id
/// the request will populate these values before passing to handeler
/// for example:
/// given route: /foo/:id
/// and request with path `/foo/123`
/// parameters will be `["id": 123]`
public var parameters: Node {
get {
if let existing = storage[parametersKey] as? Node {
return existing
}

let node = Node([:])
storage[parametersKey] = node
return node
}
set {
storage[parametersKey] = newValue
}
}
}
Loading

0 comments on commit cf6b6a4

Please sign in to comment.