Hello! Could a <#C0F4UNJET|dokka> plugin be writte...
# dokka
v
Hello! Could a #dokka plugin be written which could parse code like this:
Copy code
class MyClass {
@Router
fun myRouter() =
  route {
    get("/hello") {
      println("hello")
    }
    get("/bananas") {
      println("bananas")
    }
}
}
And generate an output something like this?
Copy code
GET /hello com.example.MyClass
GET /bananas com.example.MyClass
I stumbled upon a medium article which suggested Dokka could do this sort of static analysis.
i
Hi! Dokka is able to parse signatures, so it would see
@Router fun myRouter(): Route
, but I'm pretty sure it can't inspect function bodies out of the box, since it's not needed for API reference documentation
If paths were given in the annotation body like in Spring (so
@GetMapping("/hello")
), it'd be easy to do
v
Yes. I've been trying to minimise annotations - the kotless project has managed to what I'm hoping to do, but I think that's because kotless was written by JB and is parsing the Kotlin source code directly (with something called Psi?).
I've confirmed that ksp and KAPT can't do it either. I'll need to change my plans.
😔 1
Given that Kotlin positively encourages functional programming, trailing lambdas and DSLs, it's disappointing that these tools can't look inside function bodies. Everything's a function, we need to be able to look inside!
i
You might want to play with the compiler/parser, but it has little documentation and I don't think it can be considered to be stable or user friendly Someone wrote an article outlining how to set it up and even states you can inspect function bodies: https://jitinsharma.com/posts/parsing-kotlin-using-code-kotlin/
v
I think I'll look for a different design. I'm not clever enough to poke about with the compiler!
Thanks for the link though, I'll read it through.