Skip to content

Commit

Permalink
Revert "KTOR-1684 Deprecate locationOrNull"
Browse files Browse the repository at this point in the history
This reverts commit 89c627a.
  • Loading branch information
Sergey Mashkov committed Jan 26, 2021
1 parent 2ec366d commit fb6675f
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 45 deletions.
1 change: 0 additions & 1 deletion ktor-features/ktor-locations/api/ktor-locations.api
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public final class io/ktor/locations/LocationKt {
public static final fun href (Lio/ktor/util/pipeline/PipelineContext;Ljava/lang/Object;)Ljava/lang/String;
public static final fun location (Lio/ktor/routing/Route;Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;)Lio/ktor/routing/Route;
public static final fun locationOrNull (Lio/ktor/application/ApplicationCall;Lkotlin/reflect/KClass;)Ljava/lang/Object;
public static final fun locationOrThrow (Lio/ktor/application/ApplicationCall;Lkotlin/reflect/KClass;)Ljava/lang/Object;
}

public abstract class io/ktor/locations/LocationPropertyInfo {
Expand Down
20 changes: 2 additions & 18 deletions ktor-features/ktor-locations/jvm/src/io/ktor/locations/Location.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,34 +228,18 @@ public fun <T : Any> Route.handle(dataClass: KClass<T>, body: suspend PipelineCo
}

/**
* Retrieves the current call's location or fails if it is not available (request is not handled by a location class),
* or not yet available (invoked too early before the locations feature takes place).
*
* Despite of the name, the function fails if no location found. This is why it's deprecated.
*/
@KtorExperimentalLocationsAPI
@Deprecated("Use location function instead.", ReplaceWith("location()"))
public inline fun <reified T : Any> ApplicationCall.locationOrNull(): T = location()

/**
* Retrieves the current call's location or fails if it is not available (request is not handled by a location class),
* Retrieves the current call's location or `null` if it is not available (request is not handled by a location class),
* or not yet available (invoked too early before the locations feature takes place).
*/
@KtorExperimentalLocationsAPI
public inline fun <reified T : Any> ApplicationCall.location(): T = locationOrThrow(T::class)
public inline fun <reified T : Any> ApplicationCall.locationOrNull(): T = locationOrNull(T::class)

@PublishedApi
internal fun <T : Any> ApplicationCall.locationOrNull(type: KClass<T>): T =
attributes.getOrNull(LocationInstanceKey)?.let { instance ->
type.cast(instance)
} ?: error("Location instance is not available for this call.)")

@PublishedApi
internal fun <T : Any> ApplicationCall.locationOrThrow(type: KClass<T>): T =
attributes.getOrNull(LocationInstanceKey)?.let { instance ->
type.cast(instance)
} ?: error("Location instance is not available for this call.)")

private val LocationInstanceKey = AttributeKey<Any>("LocationInstance")

private fun <T : Any> KClass<T>.cast(instance: Any): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,31 +499,5 @@ class LocationsTest {
assertEquals(HttpStatusCode.BadRequest, call.response.status())
}
}

@Test
@Suppress("DEPRECATION")
fun testLocationOrNull() {
withLocationsApplication {
application.routing {
get<index> { index ->
assertSame(index, call.locationOrNull())
assertSame(index, call.location())
call.respondText("OK")
}
get("/no-location") {
assertFails {
assertNull(call.locationOrNull<index>())
}
assertFails {
assertNull(call.location<index>())
}
call.respondText("OK")
}
}

urlShouldBeHandled("/", "OK")
urlShouldBeHandled("/no-location", "OK")
}
}
}

0 comments on commit fb6675f

Please sign in to comment.