Skip to content

Commit 3adac4a

Browse files
feat: type-safe check whether a route can be handled
1 parent 82c784e commit 3adac4a

File tree

4 files changed

+168
-97
lines changed

4 files changed

+168
-97
lines changed

core/common/src/dev/programadorthi/routing/core/Routing.kt

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ import kotlin.native.HiddenFromObjC
4747
public class Routing internal constructor(
4848
internal val application: Application,
4949
) : Route(
50-
parent = application.environment.parentRouting,
51-
selector = RootRouteSelector(application.environment.rootPath),
52-
application.environment.developmentMode,
53-
application.environment,
54-
) {
50+
parent = application.environment.parentRouting,
51+
selector = RootRouteSelector(application.environment.rootPath),
52+
application.environment.developmentMode,
53+
application.environment,
54+
) {
5555
private val tracers = mutableListOf<(RoutingResolveTrace) -> Unit>()
5656
private val namedRoutes = mutableMapOf<String, Route>()
5757
private var disposed = false
@@ -60,19 +60,27 @@ public class Routing internal constructor(
6060
addDefaultTracing()
6161
}
6262

63-
public fun canHandleByName(name: String, lookUpOnParent: Boolean = false): Boolean {
63+
public fun canHandleByName(
64+
name: String,
65+
lookUpOnParent: Boolean = false,
66+
): Boolean {
6467
return when {
6568
!lookUpOnParent -> namedRoutes.containsKey(name)
66-
else -> generateSequence(seed = this) { it.parent?.asRouting }
67-
.firstOrNull { it.namedRoutes.containsKey(name) } != null
69+
else ->
70+
generateSequence(seed = this) { it.parent?.asRouting }
71+
.firstOrNull { it.namedRoutes.containsKey(name) } != null
6872
}
6973
}
7074

71-
public fun canHandleByPath(path: String, lookUpOnParent: Boolean = false): Boolean {
75+
public fun canHandleByPath(
76+
path: String,
77+
lookUpOnParent: Boolean = false,
78+
): Boolean {
7279
return when {
7380
!lookUpOnParent -> canHandleByPath(path = path, routing = this)
74-
else -> generateSequence(seed = this) { it.parent?.asRouting }
75-
.firstOrNull { canHandleByPath(path = path, routing = it) } != null
81+
else ->
82+
generateSequence(seed = this) { it.parent?.asRouting }
83+
.firstOrNull { canHandleByPath(path = path, routing = it) } != null
7684
}
7785
}
7886

@@ -133,7 +141,10 @@ public class Routing internal constructor(
133141
namedRoutes[name] = route
134142
}
135143

136-
private fun canHandleByPath(path: String, routing: Routing): Boolean {
144+
private fun canHandleByPath(
145+
path: String,
146+
routing: Routing,
147+
): Boolean {
137148
var routingChildren = routing.children
138149
var hasHandle = false
139150

core/common/test/dev/programadorthi/routing/core/RoutingTest.kt

Lines changed: 98 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ class RoutingTest {
212212
routing.call(
213213
name = "named",
214214
parameters =
215-
parametersOf(
216-
"id" to listOf("123"),
217-
"name" to listOf("routing"),
218-
),
215+
parametersOf(
216+
"id" to listOf("123"),
217+
"name" to listOf("routing"),
218+
),
219219
)
220220
advanceTimeBy(99)
221221

@@ -343,7 +343,7 @@ class RoutingTest {
343343
handle {
344344
call.redirectToName(
345345
name = "path2",
346-
parameters = parametersOf("key", "value")
346+
parameters = parametersOf("key", "value"),
347347
)
348348
}
349349
}
@@ -379,10 +379,10 @@ class RoutingTest {
379379
call.redirectToName(
380380
name = "path2",
381381
parameters =
382-
parametersOf(
383-
"id" to listOf("123"),
384-
"key" to listOf("value"),
385-
),
382+
parametersOf(
383+
"id" to listOf("123"),
384+
"key" to listOf("value"),
385+
),
386386
)
387387
}
388388
}
@@ -1585,12 +1585,13 @@ class RoutingTest {
15851585
@Test
15861586
fun shouldReturnsTrueWhenCanHandleByANamedRoute() {
15871587
// GIVEN
1588-
val routing = routing {
1589-
route(path = "/path", name = "path") {
1590-
handle {
1588+
val routing =
1589+
routing {
1590+
route(path = "/path", name = "path") {
1591+
handle {
1592+
}
15911593
}
15921594
}
1593-
}
15941595

15951596
// WHEN
15961597
val result = routing.canHandleByName(name = "path")
@@ -1620,12 +1621,13 @@ class RoutingTest {
16201621
@Test
16211622
fun shouldReturnsTrueWhenCanHandleByANamedRouteOnParent() {
16221623
// GIVEN
1623-
val parent = routing {
1624-
route(path = "/path", name = "path") {
1625-
handle {
1624+
val parent =
1625+
routing {
1626+
route(path = "/path", name = "path") {
1627+
handle {
1628+
}
16261629
}
16271630
}
1628-
}
16291631
val routing = routing(parent = parent, rootPath = "/child") {}
16301632

16311633
// WHEN
@@ -1638,12 +1640,13 @@ class RoutingTest {
16381640
@Test
16391641
fun shouldReturnsFalseWhenCanNotHandleByANamedRouteOnParent() {
16401642
// GIVEN
1641-
val parent = routing {
1642-
route(path = "/path") {
1643-
handle {
1643+
val parent =
1644+
routing {
1645+
route(path = "/path") {
1646+
handle {
1647+
}
16441648
}
16451649
}
1646-
}
16471650
val routing = routing(parent = parent, rootPath = "/child") {}
16481651

16491652
// WHEN
@@ -1656,12 +1659,13 @@ class RoutingTest {
16561659
@Test
16571660
fun shouldReturnsTrueWhenCanHandleByAPath() {
16581661
// GIVEN
1659-
val routing = routing {
1660-
route(path = "/path") {
1661-
handle {
1662+
val routing =
1663+
routing {
1664+
route(path = "/path") {
1665+
handle {
1666+
}
16621667
}
16631668
}
1664-
}
16651669

16661670
// WHEN
16671671
val result = routing.canHandleByPath(path = "/path")
@@ -1673,12 +1677,13 @@ class RoutingTest {
16731677
@Test
16741678
fun shouldReturnsFalseWhenCanNotHandleByAPath() {
16751679
// GIVEN
1676-
val routing = routing {
1677-
route(path = "/other") {
1678-
handle {
1680+
val routing =
1681+
routing {
1682+
route(path = "/other") {
1683+
handle {
1684+
}
16791685
}
16801686
}
1681-
}
16821687

16831688
// WHEN
16841689
val result = routing.canHandleByPath(path = "/path")
@@ -1690,17 +1695,17 @@ class RoutingTest {
16901695
@Test
16911696
fun shouldReturnsTrueWhenCanHandleByAPathWithMultiLevel() {
16921697
// GIVEN
1693-
val routing = routing {
1694-
route(path = "/level1") {
1695-
route(path = "/level2") {
1696-
route(path = "/level3") {
1697-
handle {
1698-
1698+
val routing =
1699+
routing {
1700+
route(path = "/level1") {
1701+
route(path = "/level2") {
1702+
route(path = "/level3") {
1703+
handle {
1704+
}
16991705
}
17001706
}
17011707
}
17021708
}
1703-
}
17041709

17051710
// WHEN
17061711
val result = routing.canHandleByPath(path = "/level1/level2/level3")
@@ -1712,13 +1717,13 @@ class RoutingTest {
17121717
@Test
17131718
fun shouldReturnsFalseWhenCanNotHandleByAPathWithMultiLevel() {
17141719
// GIVEN
1715-
val routing = routing {
1716-
route(path = "/level1") {
1717-
handle {
1718-
1720+
val routing =
1721+
routing {
1722+
route(path = "/level1") {
1723+
handle {
1724+
}
17191725
}
17201726
}
1721-
}
17221727

17231728
// WHEN
17241729
val result = routing.canHandleByPath(path = "/level1/level2/level3")
@@ -1730,12 +1735,13 @@ class RoutingTest {
17301735
@Test
17311736
fun shouldReturnsTrueWhenCanHandleByAPathOnParent() {
17321737
// GIVEN
1733-
val parent = routing {
1734-
route(path = "/path") {
1735-
handle {
1738+
val parent =
1739+
routing {
1740+
route(path = "/path") {
1741+
handle {
1742+
}
17361743
}
17371744
}
1738-
}
17391745
val routing = routing(parent = parent, rootPath = "/child") {}
17401746

17411747
// WHEN
@@ -1748,12 +1754,13 @@ class RoutingTest {
17481754
@Test
17491755
fun shouldReturnsFalseWhenCanNotHandleByAPathOnParent() {
17501756
// GIVEN
1751-
val parent = routing {
1752-
route(path = "/path") {
1753-
handle {
1757+
val parent =
1758+
routing {
1759+
route(path = "/path") {
1760+
handle {
1761+
}
17541762
}
17551763
}
1756-
}
17571764
val routing = routing(parent = parent, rootPath = "/child") {}
17581765

17591766
// WHEN
@@ -1766,17 +1773,17 @@ class RoutingTest {
17661773
@Test
17671774
fun shouldReturnsTrueWhenCanHandleByAPathWithMultiLevelOnParent() {
17681775
// GIVEN
1769-
val parent = routing {
1770-
route(path = "/level1") {
1771-
route(path = "/level2") {
1772-
route(path = "/level3") {
1773-
handle {
1774-
1776+
val parent =
1777+
routing {
1778+
route(path = "/level1") {
1779+
route(path = "/level2") {
1780+
route(path = "/level3") {
1781+
handle {
1782+
}
17751783
}
17761784
}
17771785
}
17781786
}
1779-
}
17801787
val routing = routing(parent = parent, rootPath = "/child") {}
17811788

17821789
// WHEN
@@ -1789,13 +1796,13 @@ class RoutingTest {
17891796
@Test
17901797
fun shouldReturnsFalseWhenCanNotHandleByAPathWithMultiLevelOnParent() {
17911798
// GIVEN
1792-
val routing = routing {
1793-
route(path = "/level1") {
1794-
handle {
1795-
1799+
val routing =
1800+
routing {
1801+
route(path = "/level1") {
1802+
handle {
1803+
}
17961804
}
17971805
}
1798-
}
17991806

18001807
// WHEN
18011808
val result = routing.canHandleByPath(path = "/level1/level2/level3", lookUpOnParent = true)
@@ -1807,24 +1814,27 @@ class RoutingTest {
18071814
@Test
18081815
fun shouldReturnsTrueWhenCanHandleByAPathInNestedRouting() {
18091816
// GIVEN
1810-
val parent = routing {
1811-
route(path = "/parent") {
1812-
handle {
1817+
val parent =
1818+
routing {
1819+
route(path = "/parent") {
1820+
handle {
1821+
}
18131822
}
18141823
}
1815-
}
1816-
val child = routing(parent = parent, rootPath = "/child") {
1817-
route(path = "/childPath") {
1818-
handle {
1824+
val child =
1825+
routing(parent = parent, rootPath = "/child") {
1826+
route(path = "/childPath") {
1827+
handle {
1828+
}
18191829
}
18201830
}
1821-
}
1822-
val grandchild = routing(parent = child, rootPath = "/grandchild") {
1823-
route(path = "/grandchildPath") {
1824-
handle {
1831+
val grandchild =
1832+
routing(parent = child, rootPath = "/grandchild") {
1833+
route(path = "/grandchildPath") {
1834+
handle {
1835+
}
18251836
}
18261837
}
1827-
}
18281838

18291839
// WHEN
18301840
val result = grandchild.canHandleByPath(path = "/child/childPath", lookUpOnParent = true)
@@ -1836,24 +1846,27 @@ class RoutingTest {
18361846
@Test
18371847
fun shouldReturnsFalseWhenCanNotHandleByAPathInNestedRouting() {
18381848
// GIVEN
1839-
val parent = routing {
1840-
route(path = "/parent") {
1841-
handle {
1849+
val parent =
1850+
routing {
1851+
route(path = "/parent") {
1852+
handle {
1853+
}
18421854
}
18431855
}
1844-
}
1845-
val child = routing(parent = parent, rootPath = "/child") {
1846-
route(path = "/childPath") {
1847-
handle {
1856+
val child =
1857+
routing(parent = parent, rootPath = "/child") {
1858+
route(path = "/childPath") {
1859+
handle {
1860+
}
18481861
}
18491862
}
1850-
}
1851-
val grandchild = routing(parent = child, rootPath = "/grandchild") {
1852-
route(path = "/grandchildPath") {
1853-
handle {
1863+
val grandchild =
1864+
routing(parent = child, rootPath = "/grandchild") {
1865+
route(path = "/grandchildPath") {
1866+
handle {
1867+
}
18541868
}
18551869
}
1856-
}
18571870

18581871
// WHEN
18591872
val result =

0 commit comments

Comments
 (0)