Skip to content

Instantly share code, notes, and snippets.

@C06A
Created May 23, 2019 02:39
Show Gist options
  • Save C06A/e80b783a1d34cc9d80e339da0d7b5b53 to your computer and use it in GitHub Desktop.
Save C06A/e80b783a1d34cc9d80e339da0d7b5b53 to your computer and use it in GitHub Desktop.
Micronaut parameters binding in Kotlin controller
package com.helpchoice.kotlin.hal
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller("/", produces = [MediaType.APPLICATION_HAL_JSON])
class StackSample {
// @Get("/repeat/{value}{/stack")
// fun dup(value: Number, stack: List<Number>): Map<String, Any?>
@Get("/repeat/{value:[0-9]+(\\.[0-9]+)}{/stack:[0-9]+(\\.[0-9]+)?(/[0-9]+(\\.[0-9]+)?)*}/")
fun dup(value: String, stack: String?): Map<String, Any?> {
val baseHref = "/$value/$value/${stack}"
val selfHref = "/repeat/$value/$stack"
return mapOf(
"_links" to mapOf(
"self" to mapOf(
"href" to selfHref,
"type" to MediaType.APPLICATION_HAL_JSON
)
),
"stack" to stack,
"value" to value
)
}
// @Get("/repeat/{value}{/stack")
// fun dup(stack: List<Number>): Map<String, Any?>
@Get("/{stack:[0-9]+(\\.[0-9]+)?(/[0-9]+(\\.[0-9]+)?)*}/")
fun number(stack: String): Map<String, Any?> {
val numbers = stack.split("/")
// val (_, value) = NumericStack(numbers, 1)
val selfHref = "/$stack"
return mapOf(
"_links" to mapOf(
"self" to mapOf(
"href" to selfHref,
"type" to MediaType.APPLICATION_HAL_JSON
)
),
"stack" to stack
)
}
}
package com.helpchoice.kotlin.hal
import io.kotlintest.specs.StringSpec
internal class StackSampleTest : StringSpec() {
init {
fun stack() {
}
fun dup() {
}
}
}
@C06A
Copy link
Author

C06A commented May 23, 2019

This is a small sample project I try to make with Micronaut in Kotlin.

Unfortunately the only parameters binding I was able to do as in the code. However I would like to be able to do something like in comments. Is it possible to do that and why it doesn't work for me?

Also for the tests I couldn't figure out how to start the test server from KotlinTest. Any ideas?

Thanx in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment