From 7b917ae7a78756a157e2dd25cb79f6900d8cc596 Mon Sep 17 00:00:00 2001 From: Paul Schmiedmayer Date: Sat, 4 Apr 2020 14:15:06 +0200 Subject: [PATCH 1/3] 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 --- Sources/Corvus/Endpoints/CRUD.swift | 9 --------- Sources/Corvus/Endpoints/Utilities/EmptyEndpoint.swift | 7 ++++++- Sources/Corvus/Protocols/Endpoints/Endpoint.swift | 7 ++++--- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Sources/Corvus/Endpoints/CRUD.swift b/Sources/Corvus/Endpoints/CRUD.swift index d0024f8..094fc4f 100644 --- a/Sources/Corvus/Endpoints/CRUD.swift +++ b/Sources/Corvus/Endpoints/CRUD.swift @@ -68,13 +68,4 @@ public final class CRUD: 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) - } } diff --git a/Sources/Corvus/Endpoints/Utilities/EmptyEndpoint.swift b/Sources/Corvus/Endpoints/Utilities/EmptyEndpoint.swift index bd8c2e2..19410d0 100644 --- a/Sources/Corvus/Endpoints/Utilities/EmptyEndpoint.swift +++ b/Sources/Corvus/Endpoints/Utilities/EmptyEndpoint.swift @@ -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) { } +} diff --git a/Sources/Corvus/Protocols/Endpoints/Endpoint.swift b/Sources/Corvus/Protocols/Endpoints/Endpoint.swift index 1a7ca08..9519ff6 100644 --- a/Sources/Corvus/Protocols/Endpoints/Endpoint.swift +++ b/Sources/Corvus/Protocols/Endpoints/Endpoint.swift @@ -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 From 7d0d7ed952ab5d632ce39d20ee281063c46d8558 Mon Sep 17 00:00:00 2001 From: Paul Schmiedmayer Date: Sat, 4 Apr 2020 15:04:29 +0200 Subject: [PATCH 2/3] Use an implicit return in `mapEachThrowing` --- .../Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift b/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift index 59a576f..262c446 100644 --- a/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift +++ b/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift @@ -12,7 +12,7 @@ extension EventLoopFuture where Value: Sequence { func mapEachThrowing( _ transform: @escaping (_ element: Value.Element) throws -> Result ) -> EventLoopFuture<[Result]> { - return self.flatMapThrowing { sequence -> [Result] in + return self.flatMapThrowing { sequence in try sequence.map(transform) } } From 34cfc81c6521098cef0961bc453883c2d242306d Mon Sep 17 00:00:00 2001 From: Paul Schmiedmayer Date: Sat, 4 Apr 2020 15:06:29 +0200 Subject: [PATCH 3/3] Use an implicit return in `mapEachThrowing` --- .../Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift b/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift index 262c446..59db7b1 100644 --- a/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift +++ b/Sources/Corvus/Endpoints/Utilities/EventLoopFuture+MapEachThrowing.swift @@ -12,7 +12,7 @@ extension EventLoopFuture where Value: Sequence { func mapEachThrowing( _ transform: @escaping (_ element: Value.Element) throws -> Result ) -> EventLoopFuture<[Result]> { - return self.flatMapThrowing { sequence in + self.flatMapThrowing { sequence in try sequence.map(transform) } }