All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.contract.routeSpec.kt Maven / Gradle / Ivy

There is a newer version: 5.31.0.0
Show newest version
package org.http4k.contract

import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Uri
import org.http4k.lens.Path
import org.http4k.lens.PathLens

abstract class ContractRouteSpec internal constructor(val pathFn: (PathSegments) -> PathSegments,
                                                      val routeMeta: RouteMeta,
                                                      vararg val pathLenses: PathLens<*>) {
    abstract infix operator fun  div(next: PathLens): ContractRouteSpec

    open infix operator fun div(next: String) = div(Path.fixed(next))

    internal fun describe(contractRoot: PathSegments): String = "${pathFn(contractRoot)}${if (pathLenses.isNotEmpty()) "/${pathLenses.joinToString("/")}" else ""}"

    open inner class ContractRequestBuilder(internal val method: Method) {
        fun newRequest(baseUri: Uri = Uri.of("")) = Request(method, "").uri(baseUri.path(describe(Root)))
    }

    abstract infix fun bindContract(method: Method): ContractRequestBuilder
}

class ContractRouteSpec0 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta) : ContractRouteSpec(pathFn, routeMeta) {
    override infix operator fun div(next: String) = ContractRouteSpec0({ it / next }, routeMeta)

    override infix operator fun  div(next: PathLens) = ContractRouteSpec1(pathFn, routeMeta, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: HttpHandler) = with(this@ContractRouteSpec0) { ContractRoute(method, this, routeMeta) { fn } }
        infix fun to(fn: () -> HttpHandler) = with(this@ContractRouteSpec0) { ContractRoute(method, this, routeMeta) { fn() } }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec1 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta, val a: PathLens) : ContractRouteSpec(pathFn, routeMeta, a) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec2(pathFn, routeMeta, a, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A) -> HttpHandler) = with(this@ContractRouteSpec1) {
            ContractRoute(method, this, routeMeta) { fn(it[a]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec2 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta, val a: PathLens, val b: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec3(pathFn, routeMeta, a, b, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B) -> HttpHandler) = with(this@ContractRouteSpec2) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec3 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                   val a: PathLens, val b: PathLens, val c: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec4(pathFn, routeMeta, a, b, c, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C) -> HttpHandler) = with(this@ContractRouteSpec3) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec4 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                          val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec5(pathFn, routeMeta, a, b, c, d, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D) -> HttpHandler) = with(this@ContractRouteSpec4) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec5 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                                 val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec6(pathFn, routeMeta, a, b, c, d, e, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D, E) -> HttpHandler) = with(this@ContractRouteSpec5) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d], it[e]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec6 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                                        val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens, val f: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec7(pathFn, routeMeta, a, b, c, d, e, f, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D, E, F) -> HttpHandler) = with(this@ContractRouteSpec6) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d], it[e], it[f]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec7 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                                               val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
                                                                                               val f: PathLens, val g: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec8(pathFn, routeMeta, a, b, c, d, e, f, g, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D, E, F, G) -> HttpHandler) = with(this@ContractRouteSpec7) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d], it[e], it[f], it[g]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec8 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                                                      val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
                                                                                                      val f: PathLens, val g: PathLens, val h: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g, h) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec9(pathFn, routeMeta, a, b, c, d, e, f, g, h, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D, E, F, G, H) -> HttpHandler) = with(this@ContractRouteSpec8) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d], it[e], it[f], it[g], it[h]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec9 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                                                             val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
                                                                                                             val f: PathLens, val g: PathLens, val h: PathLens, val i: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g, h, i) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = ContractRouteSpec10(pathFn, routeMeta, a, b, c, d, e, f, g, h, i, next)

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D, E, F, G, H, I) -> HttpHandler): ContractRoute =
            with(this@ContractRouteSpec9) {
                ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d], it[e], it[f], it[g], it[h], it[i]) }
            }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}

class ContractRouteSpec10 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
                                                                                                                     val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
                                                                                                                     val f: PathLens, val g: PathLens, val h: PathLens, val i: PathLens,
                                                                                                                     val j: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g, h, i, j) {
    override infix operator fun div(next: String) = div(Path.fixed(next))

    override infix operator fun  div(next: PathLens) = throw UnsupportedOperationException("no longer paths!")

    inner class Binder(method: Method) : ContractRequestBuilder(method) {
        infix fun to(fn: (A, B, C, D, E, F, G, H, I, J) -> HttpHandler) = with(this@ContractRouteSpec10) {
            ContractRoute(method, this, routeMeta) { fn(it[a], it[b], it[c], it[d], it[e], it[f], it[g], it[h], it[i], it[j]) }
        }
    }

    override infix fun bindContract(method: Method) = Binder(method)
}