Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
Provide a functional default implementation of register in Endpoints (#8
Browse files Browse the repository at this point in the history
)

* Provide a functional default implementation for `register(to routes: RoutesBuilder)` for an `Endpoint` to enable Corvus Users to write composable `Endpoints` without the need to write a `register(to routes: RoutesBuilder)` function

* Use an implicit return in `mapEachThrowing`
  • Loading branch information
PSchmiedmayer authored Apr 4, 2020
1 parent 2d2a9cd commit 17fadda
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
9 changes: 0 additions & 9 deletions Sources/Corvus/Endpoints/CRUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,4 @@ public final class CRUD<T: CorvusModel>: Endpoint {
}
}
}

/// A method that invokes the `register` function for the component's
/// `content`.
///
/// - Parameter routes: A `RoutesBuilder` containing all the information
/// about the HTTP route leading to the current component.
public func register(to routes: RoutesBuilder) {
content.register(to: routes)
}
}
7 changes: 6 additions & 1 deletion Sources/Corvus/Endpoints/Utilities/EmptyEndpoint.swift
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import Vapor

/// An empty default value when no `Endpoint` value is needed.
public struct EmptyEndpoint: Endpoint {}
public struct EmptyEndpoint: Endpoint {
/// An empty default implementation of `.register()` to avoid endless loops when registering `EmptyEndpoint`s
public func register(to routes: RoutesBuilder) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension EventLoopFuture where Value: Sequence {
func mapEachThrowing<Result>(
_ transform: @escaping (_ element: Value.Element) throws -> Result
) -> EventLoopFuture<[Result]> {
return self.flatMapThrowing { sequence -> [Result] in
self.flatMapThrowing { sequence in
try sequence.map(transform)
}
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/Corvus/Protocols/Endpoints/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ extension Endpoint {
/// do not need to be registered.
extension Endpoint {

/// An empty default implementation of `.register()` for components that do
/// not need it.
public func register(to routes: RoutesBuilder) {}
/// A default implementation of `.register()` for components that do not need special behaviour.
public func register(to routes: RoutesBuilder) {
content.register(to: routes)
}
}

/// An extension to make an `Array` of `Endpoint` conform to `Endpoint` and thus
Expand Down

0 comments on commit 17fadda

Please sign in to comment.