Skip to content

Commit

Permalink
feat: check whether a route can be handled
Browse files Browse the repository at this point in the history
  • Loading branch information
programadorthi committed Apr 27, 2024
1 parent d03c4f1 commit 82c784e
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 30 deletions.
16 changes: 0 additions & 16 deletions core/common/src/dev/programadorthi/routing/core/Route.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ public open class Route(
}
}

/**
* Return list of endpoints with handlers under this route.
*/
public fun Route.getAllRoutes(): List<Route> {
val endpoints = mutableListOf<Route>()
getAllRoutes(endpoints)
return endpoints
}

internal fun Route.allSelectors(): List<RouteSelector> {
val selectors = mutableListOf(selector)
var other = parent
Expand All @@ -135,10 +126,3 @@ internal fun Route.allSelectors(): List<RouteSelector> {
// We need reverse to starting from top-most parent
return selectors.reversed()
}

private fun Route.getAllRoutes(endpoints: MutableList<Route>) {
if (handlers.isEmpty()) {
endpoints.add(this)
}
children.forEach { it.getAllRoutes(endpoints) }
}
45 changes: 40 additions & 5 deletions core/common/src/dev/programadorthi/routing/core/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ import kotlin.native.HiddenFromObjC
public class Routing internal constructor(
internal val application: Application,
) : Route(
parent = application.environment.parentRouting,
selector = RootRouteSelector(application.environment.rootPath),
application.environment.developmentMode,
application.environment,
) {
parent = application.environment.parentRouting,
selector = RootRouteSelector(application.environment.rootPath),
application.environment.developmentMode,
application.environment,
) {
private val tracers = mutableListOf<(RoutingResolveTrace) -> Unit>()
private val namedRoutes = mutableMapOf<String, Route>()
private var disposed = false
Expand All @@ -60,6 +60,22 @@ public class Routing internal constructor(
addDefaultTracing()
}

public fun canHandleByName(name: String, lookUpOnParent: Boolean = false): Boolean {
return when {
!lookUpOnParent -> namedRoutes.containsKey(name)
else -> generateSequence(seed = this) { it.parent?.asRouting }
.firstOrNull { it.namedRoutes.containsKey(name) } != null
}
}

public fun canHandleByPath(path: String, lookUpOnParent: Boolean = false): Boolean {
return when {
!lookUpOnParent -> canHandleByPath(path = path, routing = this)
else -> generateSequence(seed = this) { it.parent?.asRouting }
.firstOrNull { canHandleByPath(path = path, routing = it) } != null
}
}

public fun execute(call: ApplicationCall) {
var current: Routing? = this
while (current?.disposed == true && current.parent != null) {
Expand Down Expand Up @@ -117,6 +133,25 @@ public class Routing internal constructor(
namedRoutes[name] = route
}

private fun canHandleByPath(path: String, routing: Routing): Boolean {
var routingChildren = routing.children
var hasHandle = false

val fakeRoute = Route(parent = null, selector = routing.selector)
fakeRoute.createRouteFromPath(path)

generateSequence(seed = fakeRoute.children) { it.firstOrNull()?.children }
.flatten()
.forEach { child ->
val found =
routingChildren.firstOrNull { it.selector == child.selector } ?: return false
hasHandle = found.handlers.isNotEmpty()
routingChildren = found.children
}

return hasHandle
}

private fun removeChild(route: Route) {
val parentRoute = route.parent ?: return
parentRoute.childList.remove(route)
Expand Down
Loading

0 comments on commit 82c784e

Please sign in to comment.