Skip to content

Commit c7591c6

Browse files
Rename REST Exporter and handle function (#1)
1 parent fdf160d commit c7591c6

File tree

11 files changed

+42
-42
lines changed

11 files changed

+42
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Created by https://www.toptal.com/developers/gitignore/api/kotlin,intellij,gradle
33
# Edit at https://www.toptal.com/developers/gitignore?templates=kotlin,intellij,gradle
44

5+
.DS_Store
6+
57
### Intellij ###
68
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
79
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

src/main/kotlin/de/tum/in/ase/apodini/Handler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import de.tum.`in`.ase.apodini.modifiers.Modifier
66
import kotlinx.coroutines.CoroutineScope
77

88
interface Handler<Output> : ModifiableComponent<Handler<Output>> {
9-
suspend fun CoroutineScope.compute(): Output
9+
suspend fun CoroutineScope.handle(): Output
1010

1111
override fun modify(modifier: Modifier): Handler<Output> {
1212
return ModifiedHandler(this, listOf(modifier))

src/main/kotlin/de/tum/in/ase/apodini/WebService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package de.tum.`in`.ase.apodini
22

33
import de.tum.`in`.ase.apodini.configuration.ConfigurationBuilder
4-
import de.tum.`in`.ase.apodini.exporter.RESTExporter
4+
import de.tum.`in`.ase.apodini.exporter.REST
55
import de.tum.`in`.ase.apodini.model.semanticModel
66

77
interface WebService: Component {
88
fun ConfigurationBuilder.configure() {
9-
use(RESTExporter())
9+
use(REST())
1010
}
1111
}
1212

src/main/kotlin/de/tum/in/ase/apodini/exporter/RESTExporter.kt renamed to src/main/kotlin/de/tum/in/ase/apodini/exporter/REST.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import de.tum.`in`.ase.apodini.model.SemanticModel
66
import de.tum.`in`.ase.apodini.properties.options.HTTPParameterMode
77
import de.tum.`in`.ase.apodini.properties.options.http
88
import de.tum.`in`.ase.apodini.request.Request
9-
import de.tum.`in`.ase.apodini.types.BooleanType.encode
109
import de.tum.`in`.ase.apodini.types.Encoder
1110
import de.tum.`in`.ase.apodini.types.Object
1211
import io.ktor.application.*
@@ -20,11 +19,10 @@ import io.ktor.server.engine.*
2019
import io.ktor.server.netty.*
2120
import io.ktor.util.pipeline.*
2221
import kotlinx.coroutines.CoroutineScope
23-
import kotlinx.coroutines.runBlocking
2422
import java.util.*
2523

26-
class RESTExporter(
27-
private val port: Int = 8080,
24+
class REST(
25+
private val port: Int = 80,
2826
): Exporter {
2927
override fun export(model: SemanticModel) {
3028
val endpoints = model.endpoints.map { EvaluatedEndpoint(model, it) }
@@ -165,7 +163,7 @@ private fun <T> SemanticModel.Result<T>.encode(
165163
encoder.keyed {
166164
encode("_links") {
167165
keyed {
168-
encode("_self") {
166+
encode("self") {
169167
encodeString(linkToSelf.relativeURL(request.context.context.request, endpoints))
170168
}
171169
links.forEach { link ->

src/main/kotlin/de/tum/in/ase/apodini/impl/text.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fun ComponentBuilder.text(response: String): ModifiableComponent<*> {
1010
}
1111

1212
private class Text(val response: String): Handler<String> {
13-
override suspend fun CoroutineScope.compute(): String {
13+
override suspend fun CoroutineScope.handle(): String {
1414
return response
1515
}
1616
}

src/main/kotlin/de/tum/in/ase/apodini/internal/wrappers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class ModifiedComponent(private val component: Component, private val m
2222
}
2323

2424
internal class ModifiedHandler<T>(val handler: Handler<T>, val modifiers: List<Modifier>) : Handler<T> {
25-
override suspend fun CoroutineScope.compute(): T {
25+
override suspend fun CoroutineScope.handle(): T {
2626
throw IllegalArgumentException(
2727
"Unexpected call to `compute` to a handler wrapped in modifiers"
2828
)

src/main/kotlin/de/tum/in/ase/apodini/model/SemanticModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class SemanticModel internal constructor(
135135
property.update()
136136
}
137137

138-
val value = with(newInstance) { delegatedRequest.compute() }
138+
val value = with(newInstance) { delegatedRequest.handle() }
139139
val selfLink = selfEndpoint.link(value, request)!!
140140
val links = linkedEndpoints.mapNotNull { it.link(value, request) }
141141
return Result(value, typeDefinition, selfLink, links)

src/test/kotlin/de/tum/in/ase/apodini/test/RequestHandlingTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RequestHandlingTest : TestCase() {
2929
fun testObject() {
3030
val result = handle {
3131
+object : Handler<Foo> {
32-
override suspend fun CoroutineScope.compute(): Foo {
32+
override suspend fun CoroutineScope.handle(): Foo {
3333
return Foo("Test", 42)
3434
}
3535
}
@@ -41,7 +41,7 @@ class RequestHandlingTest : TestCase() {
4141
fun testArray() {
4242
val result = handle {
4343
+object : Handler<List<Int>> {
44-
override suspend fun CoroutineScope.compute(): List<Int> {
44+
override suspend fun CoroutineScope.handle(): List<Int> {
4545
return listOf(1, 2, 3)
4646
}
4747
}
@@ -55,7 +55,7 @@ class RequestHandlingTest : TestCase() {
5555
+object : Handler<String> {
5656
val name by parameter<String>()
5757

58-
override suspend fun CoroutineScope.compute(): String {
58+
override suspend fun CoroutineScope.handle(): String {
5959
return "Hello, $name!"
6060
}
6161
}
@@ -71,7 +71,7 @@ class RequestHandlingTest : TestCase() {
7171
+object : Handler<String> {
7272
val id by pathParameter
7373

74-
override suspend fun CoroutineScope.compute(): String {
74+
override suspend fun CoroutineScope.handle(): String {
7575
return "Hello from $id"
7676
}
7777
}
@@ -87,7 +87,7 @@ class RequestHandlingTest : TestCase() {
8787
+object : Handler<String> {
8888
val someSecret by environment { secret }
8989

90-
override suspend fun CoroutineScope.compute(): String {
90+
override suspend fun CoroutineScope.handle(): String {
9191
return "Secret = $someSecret. Don't tell anyone"
9292
}
9393
}
@@ -103,7 +103,7 @@ class RequestHandlingTest : TestCase() {
103103
+object : Handler<String> {
104104
val someSecret by environment { secret }
105105

106-
override suspend fun CoroutineScope.compute(): String {
106+
override suspend fun CoroutineScope.handle(): String {
107107
return "Secret = $someSecret. Don't tell anyone"
108108
}
109109
}
@@ -118,7 +118,7 @@ class RequestHandlingTest : TestCase() {
118118
assertFailsWith<IllegalArgumentException> {
119119
handle {
120120
+object : Handler<String> {
121-
override suspend fun CoroutineScope.compute(): String {
121+
override suspend fun CoroutineScope.handle(): String {
122122
throw IllegalArgumentException("Nope")
123123
}
124124
}.withEnvironment { logger { logger } }

src/test/kotlin/de/tum/in/ase/apodini/test/SemanticModelBuilderTest.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import de.tum.`in`.ase.apodini.Component
44
import de.tum.`in`.ase.apodini.ComponentBuilder
55
import de.tum.`in`.ase.apodini.Handler
66
import de.tum.`in`.ase.apodini.environment.EnvironmentKey
7-
import de.tum.`in`.ase.apodini.exporter.RESTExporter
7+
import de.tum.`in`.ase.apodini.exporter.REST
88
import de.tum.`in`.ase.apodini.impl.group
99
import de.tum.`in`.ase.apodini.impl.text
1010
import de.tum.`in`.ase.apodini.model.Operation
@@ -36,7 +36,7 @@ internal class SemanticModelBuilderTest : TestCase() {
3636
text("Hello World")
3737
}
3838
assertEquals(semanticModel.exporters.count(), 1)
39-
assert(semanticModel.exporters.first() is RESTExporter)
39+
assert(semanticModel.exporters.first() is REST)
4040
assert(semanticModel.globalEnvironment.keys.isEmpty())
4141
assertEquals(semanticModel.endpoints.count(), 1)
4242

@@ -51,7 +51,7 @@ internal class SemanticModelBuilderTest : TestCase() {
5151
fun testCustomHandlerWithOptionalString() {
5252
val semanticModel = semanticModel {
5353
+object : Handler<String?> {
54-
override suspend fun CoroutineScope.compute(): String? {
54+
override suspend fun CoroutineScope.handle(): String? {
5555
return null
5656
}
5757
}
@@ -70,7 +70,7 @@ internal class SemanticModelBuilderTest : TestCase() {
7070
+object : Handler<String> {
7171
val name by parameter<String?>()
7272

73-
override suspend fun CoroutineScope.compute(): String {
73+
override suspend fun CoroutineScope.handle(): String {
7474
return "Hello, ${name ?: "World"}"
7575
}
7676
}
@@ -95,7 +95,7 @@ internal class SemanticModelBuilderTest : TestCase() {
9595
http { query }
9696
}
9797

98-
override suspend fun CoroutineScope.compute(): String {
98+
override suspend fun CoroutineScope.handle(): String {
9999
return "Hello, $name"
100100
}
101101
}
@@ -167,7 +167,7 @@ internal class SemanticModelBuilderTest : TestCase() {
167167
+object : Handler<String> {
168168
val id by pathId
169169

170-
override suspend fun CoroutineScope.compute(): String {
170+
override suspend fun CoroutineScope.handle(): String {
171171
return "User, $id"
172172
}
173173
}
@@ -206,7 +206,7 @@ internal class SemanticModelBuilderTest : TestCase() {
206206
fun testEnvironmentOnHandlerWithUnaryPlus() {
207207
val semanticModel = semanticModel {
208208
+object : Handler<String> {
209-
override suspend fun CoroutineScope.compute(): String {
209+
override suspend fun CoroutineScope.handle(): String {
210210
return "Hello World"
211211
}
212212
}.withEnvironment {
@@ -520,27 +520,27 @@ private val Parameter<*>.id: UUID
520520
private class PersonHandler(id: PathParameter) : Handler<Person> {
521521
val id by id
522522

523-
override suspend fun CoroutineScope.compute(): Person {
523+
override suspend fun CoroutineScope.handle(): Person {
524524
return Person("Test")
525525
}
526526
}
527527

528528
private class ContentHandler(id: PathParameter) : Handler<Content> {
529529
val id by id
530530

531-
override suspend fun CoroutineScope.compute(): Content {
531+
override suspend fun CoroutineScope.handle(): Content {
532532
return Content("hello world", "1")
533533
}
534534
}
535535

536536
private object PersonHandlerWithoutId : Handler<Person> {
537-
override suspend fun CoroutineScope.compute(): Person {
537+
override suspend fun CoroutineScope.handle(): Person {
538538
return Person("Test")
539539
}
540540
}
541541

542542
private object ContentHandlerWithoutId : Handler<Content> {
543-
override suspend fun CoroutineScope.compute(): Content {
543+
override suspend fun CoroutineScope.handle(): Content {
544544
return Content("hello world", "1")
545545
}
546546
}
@@ -559,7 +559,7 @@ private class Content(val text: String, @Hidden val personId: String) : CustomTy
559559

560560
@Documented("Documented Handler")
561561
private object DocumentedHandler : Handler<String> {
562-
override suspend fun CoroutineScope.compute(): String {
562+
override suspend fun CoroutineScope.handle(): String {
563563
return "Hello, World!"
564564
}
565565
}

src/test/kotlin/de/tum/in/ase/apodini/test/example/SWAPI.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import de.tum.`in`.ase.apodini.*
99
import de.tum.`in`.ase.apodini.configuration.ConfigurationBuilder
1010
import de.tum.`in`.ase.apodini.environment.EnvironmentKey
1111
import de.tum.`in`.ase.apodini.environment.EnvironmentKeys
12-
import de.tum.`in`.ase.apodini.exporter.RESTExporter
12+
import de.tum.`in`.ase.apodini.exporter.REST
1313
import de.tum.`in`.ase.apodini.impl.group
1414
import de.tum.`in`.ase.apodini.impl.text
1515
import de.tum.`in`.ase.apodini.modifiers.ModifiableComponent
@@ -88,7 +88,7 @@ class SWAPI : WebService {
8888
}
8989

9090
override fun ConfigurationBuilder.configure() {
91-
use(RESTExporter(port = 8080))
91+
use(REST(port = 8080))
9292

9393
environment {
9494
store {
@@ -142,7 +142,7 @@ private inline fun <reified T> ComponentBuilder.itemRelationship(id: PathParamet
142142

143143
private class AllItemsHandler<T>(val items: SWAPIStore.() -> Map<Int, T>) : Handler<List<T>> {
144144
val store by environment { store }
145-
override suspend fun CoroutineScope.compute(): List<T> {
145+
override suspend fun CoroutineScope.handle(): List<T> {
146146
return store.items().values.toList()
147147
}
148148
}
@@ -151,7 +151,7 @@ private class ItemHandler<T>(id: PathParameter, val items: SWAPIStore.() -> Map<
151151
val id by id
152152
val store by environment { store }
153153

154-
override suspend fun CoroutineScope.compute(): T {
154+
override suspend fun CoroutineScope.handle(): T {
155155
return id.toIntOrNull()?.let { store.items()[it] } ?: throw NotFoundException()
156156
}
157157
}
@@ -160,7 +160,7 @@ private class ItemRelationshipHandler<T>(id: PathParameter, val items: SWAPIStor
160160
val id by id
161161
val store by environment { store }
162162

163-
override suspend fun CoroutineScope.compute(): T {
163+
override suspend fun CoroutineScope.handle(): T {
164164
return id.toIntOrNull()?.let { store.items(it) } ?: throw NotFoundException()
165165
}
166166
}

0 commit comments

Comments
 (0)