Skip to content

Commit

Permalink
修复 ActionInvoker 覆盖 Router 从而导致丢失部分路由的问题。
Browse files Browse the repository at this point in the history
实现 Synonym。
  • Loading branch information
IceCream-QAQ committed Jul 13, 2020
1 parent 3c7b25e commit e839f38
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ abstract class NewControllerLoader : Loader {
val actionMethodName = method.name

val path = action.value
val aa = getActionRouter(path, controllerRouter, rootRouter)
val actionRootRouter = aa.router
val actionPath = aa.path
// val aa = getActionRouter(path, controllerRouter, rootRouter)
// val actionRootRouter = aa.router
// val actionPath = aa.path

// val methodInvoker = createMethodInvoker(instance, method)
val actionInvoker = createActionInvoker(actionRootRouter.level + 1, method, instance)
// actionInvoker.invoker = methodInvoker

val abs = ArrayList<NewMethodInvoker>()
Expand All @@ -116,13 +115,19 @@ abstract class NewControllerLoader : Loader {
aas.add(invoker)
}

val actionInvoker = getActionInvoker(path, controllerRouter, rootRouter, method, instance)

actionInvoker.befores = abs.toTypedArray()
actionInvoker.afters = aas.toTypedArray()

val mi = getMatchItem(actionPath, actionInvoker)
val synonym = method.getAnnotation(Synonym::class.java) ?:continue
for (s in synonym.value) {
val sai = getActionInvoker(s, controllerRouter, rootRouter, method, instance)

sai.befores = abs.toTypedArray()
sai.afters = aas.toTypedArray()
}

if (mi == null) actionRootRouter.noMatch[actionPath] = actionInvoker
else actionRootRouter.needMath.add(mi)
}
}

Expand Down Expand Up @@ -221,6 +226,27 @@ abstract class NewControllerLoader : Loader {

data class ActionRouterAndPath(val router: NewRouterImpl, val path: String)

fun getActionInvoker(path: String, controllerRouter: NewRouterImpl, rootRouter: NewRouterImpl, method: Method, instance: Any): NewActionInvoker {
val aa = getActionRouter(path, controllerRouter, rootRouter)
val actionRootRouter = aa.router
val actionPath = aa.path

val actionInvoker = createActionInvoker(actionRootRouter.level + 1, method, instance)

val mi = getMatchItem(actionPath, actionInvoker)

if (mi == null) {
val router = actionRootRouter.noMatch[actionPath]
if (router != null) {
actionInvoker.needMath.addAll(router.needMath)
actionInvoker.noMatch.putAll(router.noMatch)
}
actionRootRouter.noMatch[actionPath] = actionInvoker
} else actionRootRouter.needMath.add(mi)

return actionInvoker
}

fun getActionRouter(pathString: String, controllerRouter: NewRouterImpl, rootRouter: NewRouterImpl): ActionRouterAndPath {
var path = pathString
val router = if (path[0] == '/') {
Expand Down
16 changes: 14 additions & 2 deletions src/test/kotlin/com/icecreamqaq/test/yu/TestApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,44 @@ class TestApp : DefaultApp() {
paths[0] = "menu"
newRouter.invoke(paths[0], nac)
println("menu: " + nac.result)
nac.result = null
nac.saves = HashMap()

paths[0] = "m111b222"
newRouter.invoke(paths[0], nac)
println("m111b222: " + nac.result)
nac.result = null
nac.saves = HashMap()

paths[0] = "c333b444"
newRouter.invoke(paths[0], nac)
println("c333b444: " + nac.result)
nac.result = null
nac.saves = HashMap()

paths[0] = "11"
newRouter.invoke(paths[0], nac)
println("11: " + nac.result)
nac.result = null
nac.saves = HashMap()

paths[1] = "menu"
newRouter.invoke(paths[0], nac)
println(nac.result)
println("menu: ${nac.result}")
nac.result = null
nac.saves = HashMap()

paths[1] = "123123"
paths[2] = "menu"
newRouter.invoke(paths[0], nac)
println(nac.result)
println("menu2: ${nac.result}")
nac.result = null
nac.saves = HashMap()

paths[0] = "BV1vh411o7p3"
newRouter.invoke(paths[0], nac)
println("BV: " + nac.result)
nac.result = null
nac.saves = HashMap()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.icecreamqaq.test.yu.controller

import com.IceCreamQAQ.Yu.annotation.Action
import com.IceCreamQAQ.Yu.annotation.After
import com.IceCreamQAQ.Yu.annotation.NewDefaultController
import com.IceCreamQAQ.Yu.annotation.Path
import com.IceCreamQAQ.Yu.annotation.*

@NewDefaultController
class TestNewController {
Expand All @@ -12,6 +9,7 @@ class TestNewController {
fun menu() = "menu"

@Action("m{abc}b{bbc}")
@Synonym(["c{abc}b{bbc}"])
fun mb(abc: String, bbc: String) = "mb: abc = $abc, bbc = $bbc."

@Action("{id:BV.{10,10}}")
Expand Down

0 comments on commit e839f38

Please sign in to comment.