#728 - Update Kotlin DSLs to use WebMvcLinkBuilder.

This commit is contained in:
Greg Turnquist
2019-02-20 10:44:46 -06:00
committed by Oliver Drotbohm
parent e82d42f45a
commit 943e9afc08
4 changed files with 28 additions and 24 deletions

View File

@@ -18,25 +18,26 @@ package org.springframework.hateoas.mvc
import org.springframework.hateoas.Affordance
import org.springframework.hateoas.Link
import org.springframework.hateoas.mvc.ControllerLinkBuilder.afford
import org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn
import org.springframework.hateoas.mvc.WebMvcLinkBuilder.afford
import org.springframework.hateoas.mvc.WebMvcLinkBuilder.methodOn
/**
* Create a new [Link] with additional [Affordance]s.
*
* @author Roland Kulcsár
* @author Greg Turnquist
* @since 1.0
*/
inline infix fun Link.andAffordances(setup: AffordanceBuilderDsl.() -> Unit): Link {
inline infix fun Link.andAffordances(setup: WebMvcAffordanceBuilderDsl.() -> Unit): Link {
val builder = AffordanceBuilderDsl()
val builder = WebMvcAffordanceBuilderDsl()
builder.setup()
return andAffordances(builder.affordances)
}
/**
* Extract a [Link] from the [ControllerLinkBuilder] and look up the related [Affordance]. Should
* Extract a [Link] from the [WebMvcLinkBuilder] and look up the related [Affordance]. Should
* only be one.
*
* @author Roland Kulcsár
@@ -50,7 +51,7 @@ inline fun <reified C> afford(func: C.() -> Unit): Affordance = afford(methodOn(
* @author Roland Kulcsár
* @since 1.0
*/
open class AffordanceBuilderDsl(val affordances: MutableList<Affordance> = mutableListOf()) {
open class WebMvcAffordanceBuilderDsl(val affordances: MutableList<Affordance> = mutableListOf()) {
inline fun <reified C> afford(func: C.() -> Any) {

View File

@@ -19,19 +19,20 @@ package org.springframework.hateoas.mvc
import org.springframework.hateoas.Link
import org.springframework.hateoas.LinkRelation
import org.springframework.hateoas.ResourceSupport
import org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo
import org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn
import org.springframework.hateoas.mvc.WebMvcLinkBuilder.linkTo
import org.springframework.hateoas.mvc.WebMvcLinkBuilder.methodOn
import kotlin.reflect.KClass
/**
* Create a [ControllerLinkBuilder] pointing to a [func] method.
* Create a [WebMvcLinkBuilder] pointing to a [func] method.
*
* @author Roland Kulcsár
* @author Oliver Drotbohm
* @author Greg Turnquist
* @since 1.0
*/
inline fun <reified C> linkTo(func: C.() -> Unit): ControllerLinkBuilder = linkTo(methodOn(C::class.java).apply(func))
inline fun <reified C> linkTo(func: C.() -> Unit): WebMvcLinkBuilder = linkTo(methodOn(C::class.java).apply(func))
/**
* Create a [Link] with the given [rel].
@@ -39,8 +40,8 @@ inline fun <reified C> linkTo(func: C.() -> Unit): ControllerLinkBuilder = linkT
* @author Roland Kulcsár
* @since 1.0
*/
infix fun ControllerLinkBuilder.withRel(rel: LinkRelation): Link = withRel(rel)
infix fun ControllerLinkBuilder.withRel(rel: String): Link = withRel(rel)
infix fun WebMvcLinkBuilder.withRel(rel: LinkRelation): Link = withRel(rel)
infix fun WebMvcLinkBuilder.withRel(rel: String): Link = withRel(rel)
/**
* Add [links] to the [R] resource.
@@ -48,9 +49,9 @@ infix fun ControllerLinkBuilder.withRel(rel: String): Link = withRel(rel)
* @author Roland Kulcsár
* @since 1.0
*/
fun <C, R : ResourceSupport> R.add(controller: Class<C>, links: LinkBuilderDsl<C, R>.(R) -> Unit): R {
fun <C, R : ResourceSupport> R.add(controller: Class<C>, links: WebMvcLinkBuilderDsl<C, R>.(R) -> Unit): R {
val builder = LinkBuilderDsl(controller, this)
val builder = WebMvcLinkBuilderDsl(controller, this)
builder.links(this)
return this
@@ -62,7 +63,7 @@ fun <C, R : ResourceSupport> R.add(controller: Class<C>, links: LinkBuilderDsl<C
* @author Roland Kulcsár
* @since 1.0
*/
fun <C : Any, R : ResourceSupport> R.add(controller: KClass<C>, links: LinkBuilderDsl<C, R>.(R) -> Unit): R {
fun <C : Any, R : ResourceSupport> R.add(controller: KClass<C>, links: WebMvcLinkBuilderDsl<C, R>.(R) -> Unit): R {
return add(controller.java, links)
}
@@ -72,24 +73,24 @@ fun <C : Any, R : ResourceSupport> R.add(controller: KClass<C>, links: LinkBuild
* @author Roland Kulcsár
* @since 1.0
*/
open class LinkBuilderDsl<C, R : ResourceSupport>(val controller: Class<C>, val resource: R) {
open class WebMvcLinkBuilderDsl<C, R : ResourceSupport>(val controller: Class<C>, val resource: R) {
/**
* Create a [ControllerLinkBuilder] pointing to [func] method.
* Create a [WebMvcLinkBuilder] pointing to [func] method.
*/
fun <R> linkTo(func: C.() -> R): ControllerLinkBuilder = linkTo(methodOn(controller).run(func))
fun <R> linkTo(func: C.() -> R): WebMvcLinkBuilder = linkTo(methodOn(controller).run(func))
/**
* Add a link with the given [rel] to the [resource].
*/
infix fun ControllerLinkBuilder.withRel(rel: String): Link {
infix fun WebMvcLinkBuilder.withRel(rel: String): Link {
return this withRel(LinkRelation.of(rel))
}
/**
* Add a link with the given [rel] to the [resource].
*/
infix fun ControllerLinkBuilder.withRel(rel: LinkRelation): Link {
infix fun WebMvcLinkBuilder.withRel(rel: LinkRelation): Link {
val link = withRel(rel)
resource.add(link)

View File

@@ -23,11 +23,12 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
/**
* Unit tests for [LinkBuilderDsl] and [AffordanceBuilderDsl].
* Unit tests for [WebMvcLinkBuilderDsl] and [WebMvcAffordanceBuilderDsl].
*
* @author Roland Kulcsár
* @author Greg Turnquist
*/
class AffordanceBuilderDslUnitTest : TestUtils() {
class WebMvcAffordanceBuilderDslUnitTest : TestUtils() {
/**
* @see #715

View File

@@ -22,11 +22,12 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
/**
* Unit tests for [LinkBuilderDsl] and [AffordanceBuilderDsl].
* Unit tests for [WebMvcLinkBuilderDsl] and [WebMvcAffordanceBuilderDsl].
*
* @author Roland Kulcsár
* @author Greg Turnquist
*/
class LinkBuilderDslUnitTest : TestUtils() {
class WebMvcLinkBuilderDslUnitTest : TestUtils() {
private val REL_PRODUCTS = "products"