Note: While it will require a decent understanding of Kotlin reflection, this is a very self-contained project and thus might be an ideal choice for someone interested in getting their feet wet as a Kweb contributor. If you want to tackle it please comment below to avoid duplicated effort
I've been working on routing, and have an approach that's both relatively simple and I think very powerful.
One thing that would add greatly to it is some code that could convert a URL path like /users/123/friends/94
into an object representation of the path, like Users(id=123, Friend(id = 94))
, similar to how Gson converts JSON into object representations and back again.
For example, let's create an object representation of our webapps paths as follows:
sealed class Entity {
data class User(val id : Int, val userEntity : UserEntity) : Entity()
class Root() : Entity()
}
sealed class UserEntity {
class Root() : UserEntity()
data class Friend(val id : Int) : UserEntity()
class Settings() : UserEntity()
}
Note the use of sealed classes, this will be nice when we use when
expressions to render the paths.
Here are some paths and the object equivalents:
/users/32
-> Entity.User(32, UserEntity.Root())
/users/152/friends/22
-> Entity.User(152, UserEntity.Friend(22))
/
-> Entity.Root()
Additionally, while defaults should be sensible and consistent with REST conventions for URL format, it should be possible to use annotations to override the default translations from path to object (much as with Gson).