From c7591c6f0d00863824c98aa87fd184b2dfb17389 Mon Sep 17 00:00:00 2001 From: Paul Schmiedmayer Date: Thu, 3 Feb 2022 22:49:34 +0100 Subject: [PATCH] Rename REST Exporter and handle function (#1) --- .gitignore | 2 ++ .../kotlin/de/tum/in/ase/apodini/Handler.kt | 2 +- .../de/tum/in/ase/apodini/WebService.kt | 4 ++-- .../exporter/{RESTExporter.kt => REST.kt} | 8 +++---- .../kotlin/de/tum/in/ase/apodini/impl/text.kt | 2 +- .../tum/in/ase/apodini/internal/wrappers.kt | 2 +- .../tum/in/ase/apodini/model/SemanticModel.kt | 2 +- .../ase/apodini/test/RequestHandlingTest.kt | 14 +++++------ .../apodini/test/SemanticModelBuilderTest.kt | 24 +++++++++---------- .../tum/in/ase/apodini/test/example/SWAPI.kt | 10 ++++---- .../apodini/test/example/TestWebService.kt | 14 +++++------ 11 files changed, 42 insertions(+), 42 deletions(-) rename src/main/kotlin/de/tum/in/ase/apodini/exporter/{RESTExporter.kt => REST.kt} (98%) diff --git a/.gitignore b/.gitignore index 5729c42..c46db9e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ # Created by https://www.toptal.com/developers/gitignore/api/kotlin,intellij,gradle # Edit at https://www.toptal.com/developers/gitignore?templates=kotlin,intellij,gradle +.DS_Store + ### Intellij ### # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 diff --git a/src/main/kotlin/de/tum/in/ase/apodini/Handler.kt b/src/main/kotlin/de/tum/in/ase/apodini/Handler.kt index c4ebc32..4a68ba2 100644 --- a/src/main/kotlin/de/tum/in/ase/apodini/Handler.kt +++ b/src/main/kotlin/de/tum/in/ase/apodini/Handler.kt @@ -6,7 +6,7 @@ import de.tum.`in`.ase.apodini.modifiers.Modifier import kotlinx.coroutines.CoroutineScope interface Handler : ModifiableComponent> { - suspend fun CoroutineScope.compute(): Output + suspend fun CoroutineScope.handle(): Output override fun modify(modifier: Modifier): Handler { return ModifiedHandler(this, listOf(modifier)) diff --git a/src/main/kotlin/de/tum/in/ase/apodini/WebService.kt b/src/main/kotlin/de/tum/in/ase/apodini/WebService.kt index bebc872..fa73dce 100644 --- a/src/main/kotlin/de/tum/in/ase/apodini/WebService.kt +++ b/src/main/kotlin/de/tum/in/ase/apodini/WebService.kt @@ -1,12 +1,12 @@ package de.tum.`in`.ase.apodini import de.tum.`in`.ase.apodini.configuration.ConfigurationBuilder -import de.tum.`in`.ase.apodini.exporter.RESTExporter +import de.tum.`in`.ase.apodini.exporter.REST import de.tum.`in`.ase.apodini.model.semanticModel interface WebService: Component { fun ConfigurationBuilder.configure() { - use(RESTExporter()) + use(REST()) } } diff --git a/src/main/kotlin/de/tum/in/ase/apodini/exporter/RESTExporter.kt b/src/main/kotlin/de/tum/in/ase/apodini/exporter/REST.kt similarity index 98% rename from src/main/kotlin/de/tum/in/ase/apodini/exporter/RESTExporter.kt rename to src/main/kotlin/de/tum/in/ase/apodini/exporter/REST.kt index a5842ea..f0247ef 100644 --- a/src/main/kotlin/de/tum/in/ase/apodini/exporter/RESTExporter.kt +++ b/src/main/kotlin/de/tum/in/ase/apodini/exporter/REST.kt @@ -6,7 +6,6 @@ import de.tum.`in`.ase.apodini.model.SemanticModel import de.tum.`in`.ase.apodini.properties.options.HTTPParameterMode import de.tum.`in`.ase.apodini.properties.options.http import de.tum.`in`.ase.apodini.request.Request -import de.tum.`in`.ase.apodini.types.BooleanType.encode import de.tum.`in`.ase.apodini.types.Encoder import de.tum.`in`.ase.apodini.types.Object import io.ktor.application.* @@ -20,11 +19,10 @@ import io.ktor.server.engine.* import io.ktor.server.netty.* import io.ktor.util.pipeline.* import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.runBlocking import java.util.* -class RESTExporter( - private val port: Int = 8080, +class REST( + private val port: Int = 80, ): Exporter { override fun export(model: SemanticModel) { val endpoints = model.endpoints.map { EvaluatedEndpoint(model, it) } @@ -165,7 +163,7 @@ private fun SemanticModel.Result.encode( encoder.keyed { encode("_links") { keyed { - encode("_self") { + encode("self") { encodeString(linkToSelf.relativeURL(request.context.context.request, endpoints)) } links.forEach { link -> diff --git a/src/main/kotlin/de/tum/in/ase/apodini/impl/text.kt b/src/main/kotlin/de/tum/in/ase/apodini/impl/text.kt index 1d4e1d4..a6b2c1f 100644 --- a/src/main/kotlin/de/tum/in/ase/apodini/impl/text.kt +++ b/src/main/kotlin/de/tum/in/ase/apodini/impl/text.kt @@ -10,7 +10,7 @@ fun ComponentBuilder.text(response: String): ModifiableComponent<*> { } private class Text(val response: String): Handler { - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return response } } \ No newline at end of file diff --git a/src/main/kotlin/de/tum/in/ase/apodini/internal/wrappers.kt b/src/main/kotlin/de/tum/in/ase/apodini/internal/wrappers.kt index 6a1163c..8856709 100644 --- a/src/main/kotlin/de/tum/in/ase/apodini/internal/wrappers.kt +++ b/src/main/kotlin/de/tum/in/ase/apodini/internal/wrappers.kt @@ -22,7 +22,7 @@ internal class ModifiedComponent(private val component: Component, private val m } internal class ModifiedHandler(val handler: Handler, val modifiers: List) : Handler { - override suspend fun CoroutineScope.compute(): T { + override suspend fun CoroutineScope.handle(): T { throw IllegalArgumentException( "Unexpected call to `compute` to a handler wrapped in modifiers" ) diff --git a/src/main/kotlin/de/tum/in/ase/apodini/model/SemanticModel.kt b/src/main/kotlin/de/tum/in/ase/apodini/model/SemanticModel.kt index fad3979..88e8b99 100644 --- a/src/main/kotlin/de/tum/in/ase/apodini/model/SemanticModel.kt +++ b/src/main/kotlin/de/tum/in/ase/apodini/model/SemanticModel.kt @@ -135,7 +135,7 @@ class SemanticModel internal constructor( property.update() } - val value = with(newInstance) { delegatedRequest.compute() } + val value = with(newInstance) { delegatedRequest.handle() } val selfLink = selfEndpoint.link(value, request)!! val links = linkedEndpoints.mapNotNull { it.link(value, request) } return Result(value, typeDefinition, selfLink, links) diff --git a/src/test/kotlin/de/tum/in/ase/apodini/test/RequestHandlingTest.kt b/src/test/kotlin/de/tum/in/ase/apodini/test/RequestHandlingTest.kt index e0355fe..0ca4707 100644 --- a/src/test/kotlin/de/tum/in/ase/apodini/test/RequestHandlingTest.kt +++ b/src/test/kotlin/de/tum/in/ase/apodini/test/RequestHandlingTest.kt @@ -29,7 +29,7 @@ class RequestHandlingTest : TestCase() { fun testObject() { val result = handle { +object : Handler { - override suspend fun CoroutineScope.compute(): Foo { + override suspend fun CoroutineScope.handle(): Foo { return Foo("Test", 42) } } @@ -41,7 +41,7 @@ class RequestHandlingTest : TestCase() { fun testArray() { val result = handle { +object : Handler> { - override suspend fun CoroutineScope.compute(): List { + override suspend fun CoroutineScope.handle(): List { return listOf(1, 2, 3) } } @@ -55,7 +55,7 @@ class RequestHandlingTest : TestCase() { +object : Handler { val name by parameter() - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Hello, $name!" } } @@ -71,7 +71,7 @@ class RequestHandlingTest : TestCase() { +object : Handler { val id by pathParameter - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Hello from $id" } } @@ -87,7 +87,7 @@ class RequestHandlingTest : TestCase() { +object : Handler { val someSecret by environment { secret } - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Secret = $someSecret. Don't tell anyone" } } @@ -103,7 +103,7 @@ class RequestHandlingTest : TestCase() { +object : Handler { val someSecret by environment { secret } - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Secret = $someSecret. Don't tell anyone" } } @@ -118,7 +118,7 @@ class RequestHandlingTest : TestCase() { assertFailsWith { handle { +object : Handler { - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { throw IllegalArgumentException("Nope") } }.withEnvironment { logger { logger } } diff --git a/src/test/kotlin/de/tum/in/ase/apodini/test/SemanticModelBuilderTest.kt b/src/test/kotlin/de/tum/in/ase/apodini/test/SemanticModelBuilderTest.kt index bba72b4..dc9a25a 100644 --- a/src/test/kotlin/de/tum/in/ase/apodini/test/SemanticModelBuilderTest.kt +++ b/src/test/kotlin/de/tum/in/ase/apodini/test/SemanticModelBuilderTest.kt @@ -4,7 +4,7 @@ import de.tum.`in`.ase.apodini.Component import de.tum.`in`.ase.apodini.ComponentBuilder import de.tum.`in`.ase.apodini.Handler import de.tum.`in`.ase.apodini.environment.EnvironmentKey -import de.tum.`in`.ase.apodini.exporter.RESTExporter +import de.tum.`in`.ase.apodini.exporter.REST import de.tum.`in`.ase.apodini.impl.group import de.tum.`in`.ase.apodini.impl.text import de.tum.`in`.ase.apodini.model.Operation @@ -36,7 +36,7 @@ internal class SemanticModelBuilderTest : TestCase() { text("Hello World") } assertEquals(semanticModel.exporters.count(), 1) - assert(semanticModel.exporters.first() is RESTExporter) + assert(semanticModel.exporters.first() is REST) assert(semanticModel.globalEnvironment.keys.isEmpty()) assertEquals(semanticModel.endpoints.count(), 1) @@ -51,7 +51,7 @@ internal class SemanticModelBuilderTest : TestCase() { fun testCustomHandlerWithOptionalString() { val semanticModel = semanticModel { +object : Handler { - override suspend fun CoroutineScope.compute(): String? { + override suspend fun CoroutineScope.handle(): String? { return null } } @@ -70,7 +70,7 @@ internal class SemanticModelBuilderTest : TestCase() { +object : Handler { val name by parameter() - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Hello, ${name ?: "World"}" } } @@ -95,7 +95,7 @@ internal class SemanticModelBuilderTest : TestCase() { http { query } } - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Hello, $name" } } @@ -167,7 +167,7 @@ internal class SemanticModelBuilderTest : TestCase() { +object : Handler { val id by pathId - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "User, $id" } } @@ -206,7 +206,7 @@ internal class SemanticModelBuilderTest : TestCase() { fun testEnvironmentOnHandlerWithUnaryPlus() { val semanticModel = semanticModel { +object : Handler { - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Hello World" } }.withEnvironment { @@ -520,7 +520,7 @@ private val Parameter<*>.id: UUID private class PersonHandler(id: PathParameter) : Handler { val id by id - override suspend fun CoroutineScope.compute(): Person { + override suspend fun CoroutineScope.handle(): Person { return Person("Test") } } @@ -528,19 +528,19 @@ private class PersonHandler(id: PathParameter) : Handler { private class ContentHandler(id: PathParameter) : Handler { val id by id - override suspend fun CoroutineScope.compute(): Content { + override suspend fun CoroutineScope.handle(): Content { return Content("hello world", "1") } } private object PersonHandlerWithoutId : Handler { - override suspend fun CoroutineScope.compute(): Person { + override suspend fun CoroutineScope.handle(): Person { return Person("Test") } } private object ContentHandlerWithoutId : Handler { - override suspend fun CoroutineScope.compute(): Content { + override suspend fun CoroutineScope.handle(): Content { return Content("hello world", "1") } } @@ -559,7 +559,7 @@ private class Content(val text: String, @Hidden val personId: String) : CustomTy @Documented("Documented Handler") private object DocumentedHandler : Handler { - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Hello, World!" } } diff --git a/src/test/kotlin/de/tum/in/ase/apodini/test/example/SWAPI.kt b/src/test/kotlin/de/tum/in/ase/apodini/test/example/SWAPI.kt index c268324..57db0f6 100644 --- a/src/test/kotlin/de/tum/in/ase/apodini/test/example/SWAPI.kt +++ b/src/test/kotlin/de/tum/in/ase/apodini/test/example/SWAPI.kt @@ -9,7 +9,7 @@ import de.tum.`in`.ase.apodini.* import de.tum.`in`.ase.apodini.configuration.ConfigurationBuilder import de.tum.`in`.ase.apodini.environment.EnvironmentKey import de.tum.`in`.ase.apodini.environment.EnvironmentKeys -import de.tum.`in`.ase.apodini.exporter.RESTExporter +import de.tum.`in`.ase.apodini.exporter.REST import de.tum.`in`.ase.apodini.impl.group import de.tum.`in`.ase.apodini.impl.text import de.tum.`in`.ase.apodini.modifiers.ModifiableComponent @@ -88,7 +88,7 @@ class SWAPI : WebService { } override fun ConfigurationBuilder.configure() { - use(RESTExporter(port = 8080)) + use(REST(port = 8080)) environment { store { @@ -142,7 +142,7 @@ private inline fun ComponentBuilder.itemRelationship(id: PathParamet private class AllItemsHandler(val items: SWAPIStore.() -> Map) : Handler> { val store by environment { store } - override suspend fun CoroutineScope.compute(): List { + override suspend fun CoroutineScope.handle(): List { return store.items().values.toList() } } @@ -151,7 +151,7 @@ private class ItemHandler(id: PathParameter, val items: SWAPIStore.() -> Map< val id by id val store by environment { store } - override suspend fun CoroutineScope.compute(): T { + override suspend fun CoroutineScope.handle(): T { return id.toIntOrNull()?.let { store.items()[it] } ?: throw NotFoundException() } } @@ -160,7 +160,7 @@ private class ItemRelationshipHandler(id: PathParameter, val items: SWAPIStor val id by id val store by environment { store } - override suspend fun CoroutineScope.compute(): T { + override suspend fun CoroutineScope.handle(): T { return id.toIntOrNull()?.let { store.items(it) } ?: throw NotFoundException() } } diff --git a/src/test/kotlin/de/tum/in/ase/apodini/test/example/TestWebService.kt b/src/test/kotlin/de/tum/in/ase/apodini/test/example/TestWebService.kt index 303d73d..6f48211 100644 --- a/src/test/kotlin/de/tum/in/ase/apodini/test/example/TestWebService.kt +++ b/src/test/kotlin/de/tum/in/ase/apodini/test/example/TestWebService.kt @@ -5,7 +5,7 @@ import de.tum.`in`.ase.apodini.configuration.ConfigurationBuilder import de.tum.`in`.ase.apodini.environment.EnvironmentKey import de.tum.`in`.ase.apodini.environment.EnvironmentKeys import de.tum.`in`.ase.apodini.environment.request -import de.tum.`in`.ase.apodini.exporter.RESTExporter +import de.tum.`in`.ase.apodini.exporter.REST import de.tum.`in`.ase.apodini.impl.text import de.tum.`in`.ase.apodini.impl.group import de.tum.`in`.ase.apodini.logging.logger @@ -68,7 +68,7 @@ object TestWebService : WebService { override fun ConfigurationBuilder.configure() { use(PrintExporter) - use(RESTExporter(port = 8080)) + use(REST(port = 8080)) environment { secret { @@ -129,7 +129,7 @@ class CurrentlyAuthenticatedUser : Handler { private val authenticated by authenticated(User) private val logger by environment { logger } - override suspend fun CoroutineScope.compute(): User? { + override suspend fun CoroutineScope.handle(): User? { if (authenticated == null) { logger.debug("User is not authenticated") } @@ -146,7 +146,7 @@ class GreeterForUser(id: PathParameter) : Handler { private val request by environment { request } private val logger by environment { logger } - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { logger.info("Trying out logging") logger.debug { "Received id $id from $request" @@ -160,7 +160,7 @@ class GreeterForUser(id: PathParameter) : Handler { class PostsForUser(id: PathParameter) : Handler { private val id by id - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return "Posts from user $id" } } @@ -178,7 +178,7 @@ class Greeter: Handler { default("World") } - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { logger.debug("Received Secret: $secret") return "Hello, $name" } @@ -188,7 +188,7 @@ private var message = "Hello, World" class MessageUpdater : Handler { private val newValue by parameter() - override suspend fun CoroutineScope.compute(): String { + override suspend fun CoroutineScope.handle(): String { return message.also { message = newValue } } }