Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #56 from nodes-vapor/feature/mutable-leaf-tag-config
Browse files Browse the repository at this point in the history
Mutable leaf tag config + Provider
  • Loading branch information
siemensikkema authored Jun 21, 2018
2 parents 5193215 + bee0730 commit f7cf364
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Sources/Sugar/Authentication/AuthenticationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ public enum AuthenticationError: String, Error {

// MARK: - AbortError
extension AuthenticationError: AbortError {

/// See `AbortError.status`
public var status: HTTPResponseStatus {
switch self {
case .signingError : return .internalServerError
case .userNotFound : return .unauthorized
case .weakPassword : return .unprocessableEntity
case .usernameAlreadyExists : return .unprocessableEntity
case .incorrectPassword : return .unauthorized
case .incorrectOldPassword : return .unprocessableEntity
case .passwordWithoutUsernameOrOldPassword : return .unprocessableEntity
case .malformedPayload : return .badRequest
case .signingError: return .internalServerError
case .userNotFound: return .unauthorized
case .weakPassword: return .unprocessableEntity
case .usernameAlreadyExists: return .unprocessableEntity
case .incorrectPassword: return .unauthorized
case .incorrectOldPassword: return .unprocessableEntity
case .passwordWithoutUsernameOrOldPassword: return .unprocessableEntity
case .malformedPayload: return .badRequest
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Leaf

public extension LeafTagConfig {
/// Register multiple tags using the keys as their names.
///
/// - Parameter list: Map of names to tags.
public mutating func use(_ list: [String: TagRenderer]) {
for item in list {
self.use(item.value, as: item.key)
Expand Down
30 changes: 30 additions & 0 deletions Sources/Sugar/Helpers/Leaf/MutableLeafTagConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Leaf
import Vapor

/// Service that can be used to add tags from multiple sources which can then be registered at once
/// on `Leaf`s `LeafTagConfig`.
/// This is a workaround for not being able to mutate `Leaf`'s `LeafTagConfig`.
/// - See: https://github.com/vapor/leaf/pull/113.
public final class MutableLeafTagConfig: Service {
var storage: [String: TagRenderer] = [:]
}

extension MutableLeafTagConfig {
/// Register multiple tags using the keys as their names.
///
/// - Parameter list: Map of names to tags.
public func use(_ list: [String: TagRenderer]) {
for (name, tag) in list {
storage[name] = tag
}
}

/// Register a single tag.
///
/// - Parameters:
/// - tag: the tag to register.
/// - name: the name for the tag to use in template files.
public func use(_ tag: TagRenderer, as name: String) {
storage[name] = tag
}
}
28 changes: 28 additions & 0 deletions Sources/Sugar/Helpers/Leaf/MutableLeafTagConfigProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Leaf

/// Provider that transfers the leaf tags from the `MutableLeafTagConfig` to `Leaf`'s
/// `LeafTagConfig`.
/// This is a workaround for not being able to mutate `Leaf`'s `LeafTagConfig`.
/// - See: https://github.com/vapor/leaf/pull/113.
public final class MutableLeafTagConfigProvider: Provider {
/// Creates an `MutableLeafTagConfigProvider`.
public init() {}

/// See `Provider.register`.
public func register(_ services: inout Services) throws {
// register LeafProvider here to ensure it does not override our `LeafTagConfig`.
try services.register(LeafProvider())
services.register(MutableLeafTagConfig())
services.register { container -> LeafTagConfig in
let mutableConfig: MutableLeafTagConfig = try container.make()
var config = LeafTagConfig.default()
config.use(mutableConfig.storage)
return config
}
}

/// See `Provider.didBoot`.
public func didBoot(_ container: Container) throws -> Future<Void> {
return .done(on: container)
}
}

0 comments on commit f7cf364

Please sign in to comment.