@@ -7,11 +7,16 @@ package io.jooby
77
88import kotlinx.coroutines.CoroutineStart
99import kotlin.reflect.KClass
10+ import kotlin.reflect.KProperty
1011
1112@DslMarker
1213@Target(AnnotationTarget .CLASS , AnnotationTarget .TYPEALIAS , AnnotationTarget .TYPE , AnnotationTarget .FUNCTION )
1314annotation class RouterDsl
1415
16+ @DslMarker
17+ @Target(AnnotationTarget .CLASS , AnnotationTarget .TYPEALIAS , AnnotationTarget .TYPE , AnnotationTarget .FUNCTION )
18+ annotation class OptionsDsl
19+
1520/* * Registry: */
1621inline fun <reified T > Registry.require (): T {
1722 return this .require(T ::class .java)
@@ -33,6 +38,10 @@ fun <T : Any> ServiceRegistry.get(klass: KClass<T>): T {
3338 return this .get(klass.java)
3439}
3540
41+ fun <T : Any > ServiceRegistry.getOrNull (klass : KClass <T >): T ? {
42+ return this .getOrNull(klass.java)
43+ }
44+
3645fun <T : Any > ServiceRegistry.put (klass : KClass <T >, service : T ): T ? {
3746 return this .put(klass.java, service)
3847}
@@ -42,6 +51,11 @@ fun <T : Any> ServiceRegistry.putIfAbsent(klass: KClass<T>, service: T): T? {
4251}
4352
4453/* * Value: */
54+
55+ inline operator fun <reified T > Value.getValue (thisRef : Any? , property : KProperty <* >): T {
56+ return this .get(property.name).to(object : Reified <T >() {})
57+ }
58+
4559operator fun Value.get (name : String ): Value {
4660 return this .get(name)
4761}
@@ -50,41 +64,57 @@ operator fun Value.get(index: Int): Value {
5064 return this .get(index)
5165}
5266
53- inline fun <reified T > Value.to (): T {
67+ inline fun <reified T > Value.to (): T ? {
5468 return this .to(object : Reified <T >() {})
5569}
5670
71+ infix fun <T : Any > Value.to (type : KClass <T >): T ? {
72+ return this .to(type.java)
73+ }
74+
5775/* * Context: */
76+ val Context .query: QueryString
77+ get() = this .query()
78+
5879inline fun <reified T > Context.query (): T {
59- val reified = object : Reified <T >() {}
60- return this .query(reified )
80+ return this .query(object : Reified <T >() {})
6181}
6282
83+ val Context .form: Formdata
84+ get() = this .form()
85+
6386inline fun <reified T > Context.form (): T {
6487 return this .form(object : Reified <T >() {})
6588}
6689
90+ val Context .multipart: Multipart
91+ get() = this .multipart()
92+
6793inline fun <reified T > Context.multipart (): T {
6894 return this .multipart(object : Reified <T >() {})
6995}
7096
97+ val Context .body: Body
98+ get() = this .body()
99+
71100inline fun <reified T > Context.body (): T {
72101 return this .body(object : Reified <T >() {})
73102}
74103
75104/* * Kooby: */
76-
77105open class Kooby constructor() : Jooby() {
78106
79107 constructor (init : Kooby .() -> Unit ) : this () {
80108 this .init ()
81109 }
82110
111+ @RouterDsl
83112 fun <T : Any > mvc (router : KClass <T >): Kooby {
84113 super .mvc(router.java)
85114 return this
86115 }
87116
117+ @RouterDsl
88118 fun <T : Any > mvc (router : KClass <T >, provider : () -> T ): Kooby {
89119 super .mvc(router.java, provider)
90120 return this
@@ -205,56 +235,55 @@ open class Kooby constructor() : Jooby() {
205235 return super .route(method, pattern) { ctx -> handler(HandlerContext (ctx)) }.setHandle(handler)
206236 }
207237
238+ @OptionsDsl
208239 fun serverOptions (configurer : ServerOptions .() -> Unit ): Kooby {
209240 val options = ServerOptions ()
210241 configurer(options)
211242 setServerOptions(options)
212243 return this
213244 }
214245
246+ @OptionsDsl
215247 fun routerOptions (configurer : RouterOptions .() -> Unit ): Kooby {
216248 val options = RouterOptions ()
217249 configurer(options)
218- setRouterOptions( options)
250+ this .routerOptions = options
219251 return this
220252 }
221253
254+ @OptionsDsl
222255 fun environmentOptions (configurer : EnvironmentOptions .() -> Unit ): Environment {
223256 val options = EnvironmentOptions ()
224257 configurer(options)
225258 val env = Environment .loadEnvironment(options)
226- setEnvironment( env)
259+ this .environment = env
227260 return env
228261 }
229262}
230263
231264/* * cors: */
232- @RouterDsl
265+ @OptionsDsl
233266fun cors (init : Cors .() -> Unit ): Cors {
234267 val cors = Cors ()
235268 cors.init ()
236269 return cors
237270}
238271
239272/* * runApp: */
240- @RouterDsl
241273fun runApp (args : Array <String >, mode : ExecutionMode , init : Kooby .() -> Unit ) {
242274 configurePackage(init )
243275 Jooby .runApp(args, mode, fun () = Kooby (init ))
244276}
245277
246- @RouterDsl
247278fun runApp (args : Array <String >, init : Kooby .() -> Unit ) {
248279 configurePackage(init )
249280 Jooby .runApp(args, ExecutionMode .DEFAULT , fun () = Kooby (init ))
250281}
251282
252- @RouterDsl
253283fun <T : Jooby > runApp (args : Array <String >, application : KClass <T >) {
254284 runApp(args, ExecutionMode .DEFAULT , application)
255285}
256286
257- @RouterDsl
258287fun <T : Jooby > runApp (args : Array <String >, mode : ExecutionMode , application : KClass <T >) {
259288 Jooby .runApp(args, mode, application.java)
260289}
0 commit comments