Skip to content

Commit

Permalink
Use a Logger instead of print() (#126)
Browse files Browse the repository at this point in the history
* Add a logger to TrieRouter to replace use of print()
* Clean up README and manifest
* Deprecate AnyRouter (not used by Vapor, not to mention implemented poorly)
* Remove useless test
* Clean up CI
  • Loading branch information
gwynne committed Aug 16, 2023
1 parent 9bf0bf3 commit e0539da
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
with:
package_name: routing-kit
modules: RoutingKit
pathsToInvalidate: /routing-kit
pathsToInvalidate: /routingkit/*
9 changes: 0 additions & 9 deletions .github/workflows/main-codecov.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/projectboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ on:
jobs:
update_project_boards:
name: Update project boards
uses: vapor/ci/.github/workflows/update-project-boards-for-issue.yml@reusable-workflows
uses: vapor/ci/.github/workflows/update-project-boards-for-issue.yml@main
secrets: inherit
12 changes: 4 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
on:
pull_request: { branches: ['*'] }
push: { branches: ['main'] }


pull_request: { types: [opened, reopened, synchronize, ready_for_review] }
push: { branches: [ main ] }
env:
LOG_LEVEL: info
SWIFT_DETERMINISTIC_HASHING: 1

jobs:

unit-tests:
uses: vapor/ci/.github/workflows/run-unit-tests.yml@reusable-workflows
with:
with_coverage: true
uses: vapor/ci/.github/workflows/run-unit-tests.yml@main

upstream-check:
runs-on: ubuntu-latest
container: swift:5.7-jammy
container: swift:5.8-jammy
steps:
- name: Check out self
uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5.2
// swift-tools-version:5.6
import PackageDescription

let package = Package(
Expand All @@ -13,7 +13,7 @@ let package = Package(
.library(name: "RoutingKit", targets: ["RoutingKit"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.2")
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3")
],
targets: [
.target(name: "RoutingKit", dependencies: [
Expand Down
37 changes: 11 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
<p align="center">
<img
src="https://user-images.githubusercontent.com/1342803/58581614-f9e97580-821d-11e9-9c7a-39da66e9dbf7.png"
height="64"
alt="RoutingKit"
>
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/1130717/260613479-e5838c91-74ae-4f8f-87ce-db37b518fc13.png">
<source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/1130717/260613528-2ba056bc-19d0-468b-b971-88d333ad70d6.png">
<img src="https://user-images.githubusercontent.com/1130717/260613528-2ba056bc-19d0-468b-b971-88d333ad70d6.png" height="96" alt="RoutingKit">
</picture>
<br>
<br>
<a href="https://docs.vapor.codes/4.0/">
<img src="http://img.shields.io/badge/read_the-docs-2196f3.svg" alt="API Docs">
</a>
<a href="http://vapor.team">
<img src="https://img.shields.io/discord/431917998102675485.svg" alt="Team Chat">
</a>
<a href="LICENSE">
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-5.2-brightgreen.svg" alt="Swift 5.2">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-5.5-brightgreen.svg" alt="Swift 5.5">
</a>
<a href="https://github.com/vapor/routing-kit/actions">
<img src="https://github.com/vapor/routing-kit/workflows/test/badge.svg" alt="Continuous Integration">
</a>
<a href="https://codecov.io/gh/vapor/routing-kit">
<img src="https://codecov.io/gh/vapor/routing-kit/branch/main/graph/badge.svg?token=V6gIGTBlmg">
</a>
<a href="https://docs.vapor.codes/4.0/"><img src="https://img.shields.io/badge/read_the-docs-2196f3.svg" alt="Documentation"></a>
<a href="https://discord.gg/vapor"><img src="https://img.shields.io/discord/431917998102675485.svg" alt="Team Chat"></a>
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License"></a>
<a href="https://github.com/vapor/routing-kit/actions/workflows/test.yml"><img src="https://github.com/vapor/routing-kit/actions/workflows/test.yml/badge.svg" alt="Continuous Integration"></a>
<a href="https://swift.org"><img src="https://img.shields.io/badge/swift-5.6-brightgreen.svg" alt="Swift 5.6"></a>
</p>
<br>
2 changes: 2 additions & 0 deletions Sources/RoutingKit/AnyRouter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// A router that performs type erasure by wrapping another router.
@available(*, deprecated, message: "This type is no longer useful in Swift 5.7")
public struct AnyRouter<Output>: Router {
private let box: _AnyRouterBase<Output>

Expand All @@ -17,6 +18,7 @@ public struct AnyRouter<Output>: Router {

extension Router {
/// Wraps this router with a type eraser.
@available(*, deprecated, message: "This method is no longer useful in Swift 5.7")
public func eraseToAnyRouter() -> AnyRouter<Output> {
return AnyRouter(self)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/RoutingKit/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct Parameters {
public init() {
self.values = [:]
self.catchall = Catchall()
self.logger = Logger(label: "routing-kit-parameters-logger")
self.logger = Logger(label: "codes.vapor.routingkit")
}

/// Creates a new `Parameters`.
Expand All @@ -33,7 +33,7 @@ public struct Parameters {
public init(_ logger: Logger?) {
self.values = [:]
self.catchall = Catchall()
self.logger = logger ?? Logger(label: "routing-kit-parameters-logger")
self.logger = logger ?? Logger(label: "codes.vapor.routingkit")
}

/// Grabs the named parameter from the parameter bag.
Expand Down
15 changes: 14 additions & 1 deletion Sources/RoutingKit/TrieRouter.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Logging

/// Generic `TrieRouter` built using the "trie" tree algorithm.
///
/// Use `register(...)` to register routes into the router. Use `route(...)` to then fetch a matching
Expand All @@ -17,6 +19,9 @@ public final class TrieRouter<Output>: Router, CustomStringConvertible {

/// The root node.
private var root: Node

/// Configured logger.
public let logger: Logger

/// Create a new `TrieRouter`.
///
Expand All @@ -25,6 +30,14 @@ public final class TrieRouter<Output>: Router, CustomStringConvertible {
public init(_ type: Output.Type = Output.self, options: Set<ConfigurationOption> = []) {
self.root = Node()
self.options = options
self.logger = .init(label: "codes.vapor.routingkit")
}

/// Create a new `TrieRouter`.
public init(_ type: Output.Type = Output.self, options: Set<ConfigurationOption> = [], logger: Logger) {
self.root = Node()
self.options = options
self.logger = logger
}

/// Registers a new `Route` to this router.
Expand Down Expand Up @@ -53,7 +66,7 @@ public final class TrieRouter<Output>: Router, CustomStringConvertible {

// if this node already has output, we are overriding a route
if current.output != nil {
print("[Routing] Warning: Overriding route output at: \(path.string)")
self.logger.info("[Routing] Overriding route output at: \(path.string)")
}

// after iterating over all path components, we can set the output
Expand Down
9 changes: 0 additions & 9 deletions Tests/RoutingKitTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,6 @@ final class RouterTests: XCTestCase {
var params = Parameters()
XCTAssertEqual(router.route(path: ["fun", "meaning_of_universe"], parameters: &params), 42)
}

func testDocs2() throws {
let router = TrieRouter(String.self)
router.register("show_user", at: [.constant("users"), .parameter("user_id")])

var params = Parameters()
_ = router.route(path: ["users", "42"], parameters: &params)
print(params)
}

// https://github.com/vapor/routing/issues/64
func testParameterPercentDecoding() throws {
Expand Down

0 comments on commit e0539da

Please sign in to comment.