From f2022d43a88f048dd72b1e0a6541a910ba6892fe Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 12 Mar 2024 11:26:26 +0000 Subject: [PATCH] Remove "HB" prefix (#15) * Remove HB prefix * Add deprecations * Use hummingbird main --- README.md | 8 +++---- Sources/HummingbirdFluent/Deprecations.swift | 24 +++++++++++++++++++ Sources/HummingbirdFluent/Fluent.swift | 7 ++---- .../HummingbirdFluent/Persist+fluent.swift | 12 +++++----- .../HummingbirdFluentTests/FluentTests.swift | 10 ++++---- .../HummingbirdFluentTests/PersistTests.swift | 24 +++++++++---------- 6 files changed, 53 insertions(+), 32 deletions(-) create mode 100644 Sources/HummingbirdFluent/Deprecations.swift diff --git a/README.md b/README.md index 7467e9a..d2ee470 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ import FluentSQLiteDriver import HummingbirdFluent let logger = Logger(label: "MyApp") -let fluent = HBFluent(logger: logger) +let fluent = Fluent(logger: logger) // add sqlite database fluent.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite) // add migration @@ -27,11 +27,11 @@ if arguments.migrate { Fluent can be used from a route as follows. ```swift -let router = HBRouter() +let router = Router() router .group("todos") .get(":id") { request, context in - guard let id = context.parameters.get("id", as: UUID.self) else { return request.failure(HBHTTPError(.badRequest)) } + guard let id = context.parameters.get("id", as: UUID.self) else { return request.failure(HTTPError(.badRequest)) } return Todo.find(id, on: fluent.db()) } ``` @@ -40,7 +40,7 @@ Here we are returning a `Todo` with an id specified in the request URI. You can then bring this together by creating an application that uses the router and adding fluent to its list of services ```swift -var app = HBApplication(router: router) +var app = Application(router: router) // add the fluent service to the application so it can manage shutdown correctly app.addServices(fluent) try await app.runService() diff --git a/Sources/HummingbirdFluent/Deprecations.swift b/Sources/HummingbirdFluent/Deprecations.swift new file mode 100644 index 0000000..35cdb57 --- /dev/null +++ b/Sources/HummingbirdFluent/Deprecations.swift @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Hummingbird server framework project +// +// Copyright (c) 2024 the Hummingbird authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +// Below is a list of deprecated symbols with the "HB" prefix. These are available +// temporarily to ease transition from the old symbols that included the "HB" +// prefix to the new ones. +// +// This file will be removed before we do a 2.0 release + +@_documentation(visibility: internal) @available(*, deprecated, renamed: "Fluent") +public typealias HBFluent = Fluent +@_documentation(visibility: internal) @available(*, deprecated, renamed: "FluentPersistDriver") +public typealias HBFluentPersistDriver = FluentPersistDriver diff --git a/Sources/HummingbirdFluent/Fluent.swift b/Sources/HummingbirdFluent/Fluent.swift index 561efe3..5b9292e 100644 --- a/Sources/HummingbirdFluent/Fluent.swift +++ b/Sources/HummingbirdFluent/Fluent.swift @@ -22,10 +22,7 @@ public struct MainActorBox: Sendable { } /// Manage fluent databases and migrations -/// -/// You can either create this separate from `HBApplication` or add it to your application -/// using `HBApplication.addFluent`. -public struct HBFluent: Sendable, Service { +public struct Fluent: Sendable, Service { /// Event loop group public let eventLoopGroup: EventLoopGroup /// Logger @@ -39,7 +36,7 @@ public struct HBFluent: Sendable, Service { private let _databases: UnsafeTransfer private let _migrations: MainActorBox - /// Initialize HBFluent + /// Initialize Fluent /// - Parameters: /// - eventLoopGroup: EventLoopGroup used by databases /// - threadPool: NIOThreadPool used by databases diff --git a/Sources/HummingbirdFluent/Persist+fluent.swift b/Sources/HummingbirdFluent/Persist+fluent.swift index 9f688e1..2af0645 100644 --- a/Sources/HummingbirdFluent/Persist+fluent.swift +++ b/Sources/HummingbirdFluent/Persist+fluent.swift @@ -20,17 +20,17 @@ import NIOCore import ServiceLifecycle /// Fluent driver for persist system for storing persistent cross request key/value pairs -public final class HBFluentPersistDriver: HBPersistDriver { - let fluent: HBFluent +public final class FluentPersistDriver: PersistDriver { + let fluent: Fluent let databaseID: DatabaseID? let tidyUpFrequency: Duration - /// Initialize HBFluentPersistDriver + /// Initialize FluentPersistDriver /// - Parameters: /// - fluent: Fluent setup /// - databaseID: ID of database to use /// - tidyUpFrequequency: How frequently cleanup expired database entries should occur - public init(fluent: HBFluent, databaseID: DatabaseID? = nil, tidyUpFrequency: Duration = .seconds(600)) async { + public init(fluent: Fluent, databaseID: DatabaseID? = nil, tidyUpFrequency: Duration = .seconds(600)) async { self.fluent = fluent self.databaseID = databaseID self.tidyUpFrequency = tidyUpFrequency @@ -47,7 +47,7 @@ public final class HBFluentPersistDriver: HBPersistDriver { do { try await model.save(on: db) } catch let error as DatabaseError where error.isConstraintFailure { - throw HBPersistError.duplicate + throw PersistError.duplicate } catch { self.fluent.logger.debug("Error: \(error)") } @@ -109,7 +109,7 @@ public final class HBFluentPersistDriver: HBPersistDriver { } /// Service protocol requirements -extension HBFluentPersistDriver { +extension FluentPersistDriver { public func run() async throws { let timerSequence = AsyncTimerSequence(interval: self.tidyUpFrequency, clock: .suspending) .cancelOnGracefulShutdown() diff --git a/Tests/HummingbirdFluentTests/FluentTests.swift b/Tests/HummingbirdFluentTests/FluentTests.swift index 25f0863..9a682eb 100644 --- a/Tests/HummingbirdFluentTests/FluentTests.swift +++ b/Tests/HummingbirdFluentTests/FluentTests.swift @@ -24,7 +24,7 @@ import XCTest @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) final class FluentTests: XCTestCase { - final class Planet: Model, HBResponseCodable { + final class Planet: Model, ResponseCodable { // Name of the table or collection. static let schema = "planets" @@ -61,13 +61,13 @@ final class FluentTests: XCTestCase { } } - struct CreateResponse: HBResponseCodable { + struct CreateResponse: ResponseCodable { let id: UUID } func testPutGet() async throws { let logger = Logger(label: "FluentTests") - let fluent = HBFluent( + let fluent = Fluent( logger: logger ) // add sqlite database @@ -90,7 +90,7 @@ final class FluentTests: XCTestCase { // run migrations try await fluent.migrate() - let router = HBRouter() + let router = Router() router.put("planet") { request, context in let planet = try await request.decode(as: Planet.self, context: context) try await planet.create(on: fluent.db()) @@ -102,7 +102,7 @@ final class FluentTests: XCTestCase { .filter(\.$id == id) .first() } - var app = HBApplication(responder: router.buildResponder()) + var app = Application(responder: router.buildResponder()) app.addServices(fluent) try await app.test(.live) { client in let planet = Planet(name: "Saturn") diff --git a/Tests/HummingbirdFluentTests/PersistTests.swift b/Tests/HummingbirdFluentTests/PersistTests.swift index 590849a..3db70e3 100644 --- a/Tests/HummingbirdFluentTests/PersistTests.swift +++ b/Tests/HummingbirdFluentTests/PersistTests.swift @@ -20,10 +20,10 @@ import HummingbirdTesting import XCTest final class PersistTests: XCTestCase { - func createApplication(_ updateRouter: (HBRouter, HBPersistDriver) -> Void = { _, _ in }) async throws -> some HBApplicationProtocol { + func createApplication(_ updateRouter: (Router, PersistDriver) -> Void = { _, _ in }) async throws -> some ApplicationProtocol { var logger = Logger(label: "FluentTests") logger.logLevel = .trace - let fluent = HBFluent(logger: logger) + let fluent = Fluent(logger: logger) // add sqlite database fluent.databases.use(.sqlite(.memory), as: .sqlite) /* fluent.databases.use( @@ -39,11 +39,11 @@ final class PersistTests: XCTestCase { ), as: .psql ) */ - let persist = await HBFluentPersistDriver(fluent: fluent) + let persist = await FluentPersistDriver(fluent: fluent) // run migrations try await fluent.migrate() - let router = HBRouter() + let router = Router() router.put("/persist/:tag") { request, context -> HTTPResponse.Status in let buffer = try await request.body.collect(upTo: .max) @@ -52,23 +52,23 @@ final class PersistTests: XCTestCase { return .ok } router.put("/persist/:tag/:time") { request, context -> HTTPResponse.Status in - guard let time = context.parameters.get("time", as: Int.self) else { throw HBHTTPError(.badRequest) } + guard let time = context.parameters.get("time", as: Int.self) else { throw HTTPError(.badRequest) } let buffer = try await request.body.collect(upTo: .max) let tag = try context.parameters.require("tag") try await persist.set(key: tag, value: String(buffer: buffer), expires: .seconds(time)) return .ok } router.get("/persist/:tag") { _, context -> String? in - guard let tag = context.parameters.get("tag", as: String.self) else { throw HBHTTPError(.badRequest) } + guard let tag = context.parameters.get("tag", as: String.self) else { throw HTTPError(.badRequest) } return try await persist.get(key: tag, as: String.self) } router.delete("/persist/:tag") { _, context -> HTTPResponse.Status in - guard let tag = context.parameters.get("tag", as: String.self) else { throw HBHTTPError(.badRequest) } + guard let tag = context.parameters.get("tag", as: String.self) else { throw HTTPError(.badRequest) } try await persist.remove(key: tag) return .noContent } updateRouter(router, persist) - var app = HBApplication(responder: router.buildResponder()) + var app = Application(responder: router.buildResponder()) app.addServices(fluent, persist) return app @@ -112,8 +112,8 @@ final class PersistTests: XCTestCase { let tag = try context.parameters.require("tag") do { try await persist.create(key: tag, value: String(buffer: buffer)) - } catch let error as HBPersistError where error == .duplicate { - throw HBHTTPError(.conflict) + } catch let error as PersistError where error == .duplicate { + throw HTTPError(.conflict) } return .ok } @@ -171,13 +171,13 @@ final class PersistTests: XCTestCase { } let app = try await self.createApplication { router, persist in router.put("/codable/:tag") { request, context -> HTTPResponse.Status in - guard let tag = context.parameters.get("tag") else { throw HBHTTPError(.badRequest) } + guard let tag = context.parameters.get("tag") else { throw HTTPError(.badRequest) } let buffer = try await request.body.collect(upTo: .max) try await persist.set(key: tag, value: TestCodable(buffer: String(buffer: buffer))) return .ok } router.get("/codable/:tag") { _, context -> String? in - guard let tag = context.parameters.get("tag") else { throw HBHTTPError(.badRequest) } + guard let tag = context.parameters.get("tag") else { throw HTTPError(.badRequest) } let value = try await persist.get(key: tag, as: TestCodable.self) return value?.buffer }