Skip to content

Commit

Permalink
feat: Removes route controller requirement from router, but needs mor…
Browse files Browse the repository at this point in the history
…e work
  • Loading branch information
m-housh committed May 8, 2024
1 parent 9c6c4ce commit 791420e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ extension DatabaseRouter {

/// Used to match a route for an override.
public struct Override: Sendable {

let match: @Sendable (Route) async throws -> Bool
let result: @Sendable (Route) async throws -> DatabaseResult

public init(
matching match: @escaping @Sendable (Route) async throws -> Bool,
with result: @escaping @Sendable (Route) async throws -> DatabaseResult
Expand Down Expand Up @@ -173,7 +173,7 @@ extension DatabaseRouter {
_ id: String,
in table: DatabaseRoute.Table? = nil,
with result: @escaping @Sendable (Route) async throws -> DatabaseResult
) -> Self {
) -> Self where Route: RouteCollection {
self.init(
matching: { route in
let route = try await route.route()
Expand All @@ -193,7 +193,7 @@ extension DatabaseRouter {
_ id: String,
in table: DatabaseRoute.Table? = nil,
with result: DatabaseResult = .success()
) -> Self {
) -> Self where Route: RouteCollection {
self.id(id, in: table, with: { _ in result })
}

Expand All @@ -207,7 +207,7 @@ extension DatabaseRouter {
_ id: String,
in table: DatabaseRoute.Table? = nil,
with result: A
) -> Self {
) -> Self where Route: RouteCollection {
self.id(id, in: table, with: { _ in .success(result) })
}

Expand All @@ -221,7 +221,7 @@ extension DatabaseRouter {
_ method: DatabaseRoute.Method,
in table: DatabaseRoute.Table? = nil,
with result: @escaping @Sendable (Route) async throws -> DatabaseResult
) -> Self {
) -> Self where Route: RouteCollection {
self.init(
matching: { route in
let route = try await route.route()
Expand All @@ -240,7 +240,7 @@ extension DatabaseRouter {
_ method: DatabaseRoute.Method,
in table: DatabaseRoute.Table? = nil,
with result: DatabaseResult = .success()
) -> Self {
) -> Self where Route: RouteCollection {
self.method(method, in: table, with: { _ in result })
}

Expand All @@ -253,7 +253,7 @@ extension DatabaseRouter {
_ method: DatabaseRoute.Method,
in table: DatabaseRoute.Table? = nil,
with result: A
) -> Self {
) -> Self where Route: RouteCollection {
self.method(method, in: table, with: { _ in .success(result) })
}

Expand All @@ -268,7 +268,7 @@ extension DatabaseRouter {
public static func route(
_ route: DatabaseRoute,
with result: @escaping @Sendable (Route) async throws -> DatabaseResult
) -> Self {
) -> Self where Route: RouteCollection {
self.init(
matching: { try await $0.route() == route },
with: result
Expand All @@ -286,7 +286,7 @@ extension DatabaseRouter {
public static func route(
_ route: DatabaseRoute,
with result: DatabaseResult = .success()
) -> Self {
) -> Self where Route: RouteCollection {
self.route(route, with: { _ in result })
}

Expand All @@ -301,7 +301,7 @@ extension DatabaseRouter {
public static func route<A: Codable>(
_ route: DatabaseRoute,
with result: A
) -> Self {
) -> Self where Route: RouteCollection {
self.route(route, with: { _ in .success(result) })
}

Expand Down
51 changes: 27 additions & 24 deletions Sources/SupabaseDependencies/DatabaseRouter/DatabaseRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public typealias DatabaseResult = Result<(any Codable), (any Error)>
/// ```
///
@dynamicMemberLookup
public struct DatabaseRouter<Route: RouteCollection>: Sendable {
public struct DatabaseRouter<Route>: Sendable {

// TODO: Explore removing RouteCollection conformance for struct based routes??

Expand Down Expand Up @@ -147,14 +147,36 @@ public struct DatabaseRouter<Route: RouteCollection>: Sendable {
self.logger = logger
}


/// Removes all overrides currently set on the router.
public mutating func resetOverrides() {
overrides = []
}

@discardableResult
private func logIfError<T>(
_ prefix: String? = nil,
_ call: @escaping () async throws -> T
) async throws -> T {
do {
return try await call()
} catch {
let message = prefix != nil ? "\(prefix!) \(error)" : "\(error)"
logger?.error("\(message)")
throw error
}
}
}

extension DatabaseRouter where Route: RouteCollection {
/// Call the database route, respecting any overrides and return the decoded result.
///
/// - Parameters:
/// - route: The route to call on the database.
@discardableResult
public func callAsFunction<A: Decodable>(
_ route: Route
) async throws -> A {
) async throws -> A where Route: RouteCollection {
try await logIfError("Run Route:") {
try await decoder.decode(A.self, from: data(for: route))
}
Expand All @@ -166,22 +188,17 @@ public struct DatabaseRouter<Route: RouteCollection>: Sendable {
/// - route: The route to call on the database.
public func callAsFunction(
_ route: Route
) async throws {
) async throws where Route: RouteCollection {
try await logIfError("Run Route:") {
try await data(for: route)
}
}

/// Removes all overrides currently set on the router.
public mutating func resetOverrides() {
overrides = []
}

// Checks if there's an override for the given route, returning the
// override data otherwise executes the route returning the data from
// the database.
@discardableResult
private func data(for route: Route) async throws -> Data {
private func data(for route: Route) async throws -> Data where Route: RouteCollection {
guard let match = try await overrides.firstMatch(of: route) else {
logger?.debug("No match found for route.")
return try await logIfError("Execute Route:") {
Expand All @@ -193,23 +210,9 @@ public struct DatabaseRouter<Route: RouteCollection>: Sendable {
return try match.data(encoder)
}
}

@discardableResult
private func logIfError<T>(
_ prefix: String? = nil,
_ call: @escaping () async throws -> T
) async throws -> T {
do {
return try await call()
} catch {
let message = prefix != nil ? "\(prefix!) \(error)" : "\(error)"
logger?.error("\(message)")
throw error
}
}
}

extension DatabaseRouter: CasePathable where Route: CasePathable {
extension DatabaseRouter: CasePathable where Route: RouteCollection, Route: CasePathable {
public typealias AllCasePaths = Route.AllCasePaths
public static var allCasePaths: Route.AllCasePaths { Route.allCasePaths }

Expand Down

0 comments on commit 791420e

Please sign in to comment.