@@ -16,7 +16,7 @@ import HTTPTypes
16
16
import Synchronization
17
17
18
18
/// A Trie router implementation
19
- final class TrieRouter : OpenAPILambdaRouter {
19
+ final class TrieRouter : OpenAPILambdaRouter , CustomStringConvertible {
20
20
private let uriPath = Mutex < any URIPathCollection > ( URIPath ( ) )
21
21
22
22
/// add a route for a given HTTP method and path and associate a handler
@@ -32,6 +32,38 @@ final class TrieRouter: OpenAPILambdaRouter {
32
32
) {
33
33
try self . uriPath. withLock { try $0. find ( method: method, path: path) }
34
34
}
35
+
36
+ var description : String {
37
+ uriPath. withLock { uriPath in
38
+ var routes : [ String ] = [ ]
39
+ collectRoutes ( from: uriPath. root ( ) , method: nil , path: " " , parameters: [ ] , routes: & routes)
40
+ return routes. joined ( separator: " \n " )
41
+ }
42
+ }
43
+
44
+ private func collectRoutes( from node: Node , method: HTTPRequest . Method ? , path: String , parameters: [ String ] , routes: inout [ String ] ) {
45
+ // If this node has a handler, we found a complete route
46
+ if let _ = node. handlerChild ( ) , let method = method {
47
+ let paramString = parameters. isEmpty ? " " : " " + parameters. map { " \( $0) =value " } . joined ( separator: " " )
48
+ routes. append ( " \( method. rawValue) \( path) \( paramString) " )
49
+ }
50
+
51
+ // Traverse all children
52
+ for (_, child) in node. children {
53
+ switch child. value {
54
+ case . httpMethod( let httpMethod) :
55
+ collectRoutes ( from: child, method: httpMethod, path: path, parameters: parameters, routes: & routes)
56
+ case . pathElement( let element) :
57
+ collectRoutes ( from: child, method: method, path: path + " / " + element, parameters: parameters, routes: & routes)
58
+ case . pathParameter( let param) :
59
+ var newParams = parameters
60
+ newParams. append ( param)
61
+ collectRoutes ( from: child, method: method, path: path + " /{ \( param) } " , parameters: newParams, routes: & routes)
62
+ case . handler, . root:
63
+ collectRoutes ( from: child, method: method, path: path, parameters: parameters, routes: & routes)
64
+ }
65
+ }
66
+ }
35
67
}
36
68
37
69
enum URIPathCollectionError : Error {
0 commit comments