Skip to content

Commit 050b82f

Browse files
committed
Kotlin enhancements (#818)
* onStart/onStop callback fix #810 * Kooby as receiver fix #817
1 parent 2a93a23 commit 050b82f

File tree

5 files changed

+148
-18
lines changed

5 files changed

+148
-18
lines changed

modules/jooby-lang-kotlin/pom.xml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.jooby</groupId>
77
<artifactId>modules</artifactId>
8-
<version>1.2.0-SNAPSHOT</version>
8+
<version>1.1.3-SNAPSHOT</version>
99
</parent>
1010

1111
<modelVersion>4.0.0</modelVersion>
@@ -22,18 +22,18 @@
2222
<artifactId>kotlin-maven-plugin</artifactId>
2323
<groupId>org.jetbrains.kotlin</groupId>
2424
<version>${kotlin.version}</version>
25+
<configuration>
26+
<jvmTarget>1.8</jvmTarget>
27+
<args>
28+
<arg>-java-parameters</arg>
29+
</args>
30+
</configuration>
2531
<executions>
2632
<execution>
2733
<id>compile</id>
2834
<goals>
2935
<goal>compile</goal>
3036
</goals>
31-
<configuration>
32-
<args>
33-
<arg>-java-parameters</arg>
34-
</args>
35-
<jvmTarget>1.8</jvmTarget>
36-
</configuration>
3737
</execution>
3838

3939
<execution>
@@ -109,7 +109,16 @@
109109
<artifactId>kotlin-stdlib</artifactId>
110110
</dependency>
111111

112-
<!-- Test dependencies -->
112+
<dependency>
113+
<groupId>org.jetbrains.kotlin</groupId>
114+
<artifactId>kotlin-stdlib-jre7</artifactId>
115+
</dependency>
116+
117+
<dependency>
118+
<groupId>org.jetbrains.kotlin</groupId>
119+
<artifactId>kotlin-stdlib-jre8</artifactId>
120+
</dependency>
121+
113122
<dependency>
114123
<groupId>org.jooby</groupId>
115124
<artifactId>jooby</artifactId>

modules/jooby-lang-kotlin/src/main/kotlin/org/jooby/Jooby.kt

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package org.jooby
33
import kotlin.reflect.KClass
44
import org.jooby.spi.Server
55
import java.util.SortedSet
6+
import com.sun.xml.internal.bind.v2.schemagen.episode.Klass
7+
8+
@DslMarker
9+
annotation class DslJooby
610

711
/**
812
* Collection of utility class and method to make Jooby more Kotlin.
913
*/
14+
@DslJooby
1015
class KRouteGroup(b: Route.Props<Route.Group>) : Route.Props<Route.Group> by b {
1116
private val g = b as Route.Group
1217

@@ -164,7 +169,12 @@ class KRouteGroup(b: Route.Props<Route.Group>) : Route.Props<Route.Group> by b {
164169
/**
165170
* Collection of utility class and method to make Jooby more Kotlin.
166171
*/
167-
open class Kooby: Jooby() {
172+
@DslJooby
173+
open class Kooby constructor(): Jooby() {
174+
constructor(init: Kooby.() -> Unit): this() {
175+
this.init()
176+
}
177+
168178
fun <T:Any> use(klass: KClass<T>): Route.Collection {
169179
return use(klass.java)
170180
}
@@ -225,6 +235,21 @@ open class Kooby: Jooby() {
225235
fun head(pattern: String = "/", init: Request.() -> Any): Route.Definition {
226236
return head(pattern, {req-> req.init()})
227237
}
238+
239+
fun onStart(init: Registry.() -> Unit): Jooby {
240+
this.onStart({registry-> registry.init()})
241+
return this
242+
}
243+
244+
fun onStarted(init: Registry.() -> Unit): Jooby {
245+
this.onStarted({registry-> registry.init()})
246+
return this
247+
}
248+
249+
fun onStop(init: Registry.() -> Unit): Jooby {
250+
this.onStop({registry-> registry.init()})
251+
return this
252+
}
228253
}
229254

230255
/**
@@ -252,18 +277,19 @@ fun jooby(init: Kooby.() -> Unit): Jooby {
252277
* </pre>
253278
*/
254279
fun run(vararg args: String, init: Kooby.() -> Unit): Unit {
255-
Jooby.run({ ->
256-
val app = Kooby()
257-
app.init()
258-
app
259-
}, args)
280+
Jooby.run({-> Kooby(init)}, args)
281+
}
282+
283+
fun run(supplier: () -> Jooby, vararg args: String): Unit {
284+
Jooby.run(supplier, args)
260285
}
261286

262287
// Redefine functions with class arguments
263288
fun <T:Throwable> Router.err(klass: KClass<T>, handler: Err.Handler): Router {
264-
return this.err(klass.java, handler)
265-
}
289+
return this.err(klass.java, handler)
290+
}
266291

292+
// *********************************** Registry **************************************************
267293
fun <T:Any> Registry.require(klass: KClass<T>): T {
268294
return this.require(klass.java)
269295
}
@@ -277,6 +303,18 @@ fun <T:Any> LifeCycle.lifeCycle(klass: KClass<T>): LifeCycle {
277303
return this.lifeCycle(klass.java)
278304
}
279305

306+
fun Env.onStart(init: Registry.() -> Unit): LifeCycle {
307+
return this.onStart({registry-> registry.init()})
308+
}
309+
310+
fun Env.onStarted(init: Registry.() -> Unit): LifeCycle {
311+
return this.onStarted({registry-> registry.init()})
312+
}
313+
314+
fun Env.onStop(init: Registry.() -> Unit): LifeCycle {
315+
return this.onStop({registry-> registry.init()})
316+
}
317+
280318
// *********************************** Mutant *****************************************************
281319
val Mutant.booleanValue: Boolean
282320
get() = booleanValue()
@@ -333,22 +371,42 @@ fun <T:Enum<T>> Mutant.toEnum(type: KClass<T>): T {
333371
return this.toEnum(type.java)
334372
}
335373

374+
inline fun <reified T:Enum<T>> Mutant.toEnum(): T {
375+
return this.toEnum(T::class.java)
376+
}
377+
336378
fun <T:Any> Mutant.toOptional(type: KClass<T>): java.util.Optional<T> {
337379
return this.toOptional(type.java)
338380
}
339381

382+
inline fun <reified T> Mutant.toOptional(): java.util.Optional<T> {
383+
return this.toOptional(T::class.java)
384+
}
385+
340386
fun <T:Any> Mutant.toList(type: KClass<T>): List<T> {
341387
return this.toList(type.java)
342388
}
343389

390+
inline fun <reified T> Mutant.toList(): List<T> {
391+
return this.toList(T::class.java)
392+
}
393+
344394
fun <T:Any> Mutant.toSet(type: KClass<T>): Set<T> {
345395
return this.toSet(type.java)
346396
}
347397

398+
inline fun <reified T> Mutant.toSet(): Set<T> {
399+
return this.toSet(T::class.java)
400+
}
401+
348402
fun <T:Comparable<T>> Mutant.toSortedSet(type: KClass<T>): SortedSet<T> {
349403
return this.toSortedSet(type.java)
350404
}
351405

406+
inline fun <reified T:Comparable<T>> Mutant.toSortedSet(): SortedSet<T> {
407+
return this.toSortedSet(T::class.java)
408+
}
409+
352410
// *********************************** Request *****************************************************
353411
fun <T:Any> Request.set(type: KClass<T>, value: Any): Request {
354412
return this.set(type.java, value)

modules/jooby-lang-kotlin/src/test/kotlin/examples/kotlin/App.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package examples.kotlin
22

3-
import org.jooby.Jooby
4-
import org.jooby.Kooby
3+
import org.jooby.*
54

65
/**
76
* Use Kooby to make Jooby more Kotlin.
@@ -14,6 +13,7 @@ class App: Kooby() {
1413
get ("/") {
1514
"Hi Kotlin"
1615
}
16+
1717
}
1818
}
1919

modules/jooby-lang-kotlin/src/test/kotlin/examples/kotlin/Fn.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ fun main(args: Array<String>) {
1313
"Hi $name!!"
1414
}
1515

16+
// reified version
17+
post {
18+
val h = header<Int>("id")
19+
val name = param<String>("name")
20+
val user = body<User>()
21+
name + user + h
22+
}
23+
1624
with {
1725
get("/with") { "With" }
1826
}.name("w")
@@ -56,5 +64,17 @@ fun main(args: Array<String>) {
5664
val user = body<User>()
5765
user
5866
}
67+
68+
onStart {
69+
println("Starting")
70+
}
71+
72+
onStarted {
73+
println("Started")
74+
}
75+
76+
onStop {
77+
println("Stopped")
78+
}
5979
}
6080
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package examples.kotlin
2+
3+
import org.jooby.*
4+
5+
class KApp: Kooby({
6+
7+
use({env, conf, binder ->
8+
env.onStart {->
9+
println("Start from module")
10+
}
11+
println("XXX New module $env $conf $binder")
12+
})
13+
14+
get {
15+
"Hi Kotlin"
16+
}
17+
18+
onStart {
19+
println("Starting")
20+
}
21+
22+
onStarted {
23+
println("Started")
24+
}
25+
26+
onStop {
27+
println("Stopped")
28+
}
29+
30+
route("/") {
31+
get {->
32+
val value = param("x").value
33+
"x $value"
34+
}
35+
}
36+
})
37+
38+
/**
39+
* Start Jooby
40+
*/
41+
fun main(args: Array<String>) {
42+
Jooby.run(::App, args)
43+
}

0 commit comments

Comments
 (0)