@@ -16,7 +16,7 @@ import HTTPTypes
1616import Synchronization
1717
1818/// A Trie router implementation
19- final class TrieRouter : OpenAPILambdaRouter {
19+ final class TrieRouter : OpenAPILambdaRouter , CustomStringConvertible {
2020 private let uriPath = Mutex < any URIPathCollection > ( URIPath ( ) )
2121
2222 /// add a route for a given HTTP method and path and associate a handler
@@ -32,6 +32,38 @@ final class TrieRouter: OpenAPILambdaRouter {
3232 ) {
3333 try self . uriPath. withLock { try $0. find ( method: method, path: path) }
3434 }
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+ }
3567}
3668
3769enum URIPathCollectionError : Error {
0 commit comments