org.http4k.contract.PathSegments.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4k-contract Show documentation
Show all versions of http4k-contract Show documentation
http4k typesafe HTTP contracts and OpenApi support
package org.http4k.contract
import org.http4k.core.Request
sealed class PathSegments {
abstract val parent: PathSegments
abstract fun toList(): List
abstract fun startsWith(other: PathSegments): Boolean
operator fun div(child: String): PathSegments = Slash(this, child)
operator fun div(child: PathSegments): PathSegments = child.toList().fold(this) { memo, next ->
memo / next
}
companion object {
operator fun invoke(str: String): PathSegments =
if (str == "" || str == "/") Root
else if (!str.startsWith("/")) PathSegments("/$str")
else {
val slash = str.lastIndexOf('/')
val prefix = PathSegments(str.substring(0, slash))
if (slash == str.length - 1) prefix else prefix / str.substring(slash + 1)
}
}
}
data class Slash(override val parent: PathSegments, private val child: String) : PathSegments() {
override fun toList(): List = parent.toList().plus(child)
override fun toString(): String = "$parent/$child"
override fun startsWith(other: PathSegments): Boolean = other.toList().let { toList().take(it.size) == it }
}
object Root : PathSegments() {
override fun toList(): List = emptyList()
override val parent: PathSegments = this
override fun toString(): String = ""
override fun startsWith(other: PathSegments): Boolean = other == Root
}
internal fun Request.isIn(contractRoot: PathSegments) = pathSegments().startsWith(contractRoot)
internal fun Request.pathSegments() = PathSegments(uri.path)
internal fun Request.without(pathSegments: PathSegments) = PathSegments(uri.path.replace(pathSegments.toString(), ""))