From 03ba14d918ec9eaa880a4ceb7e41ab5b2a4afac0 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 29 Feb 2024 01:31:03 +0100 Subject: [PATCH] update ktlint --- .editorconfig | 6 ++ src/main/kotlin/eventDemo/app/AggregateId.kt | 4 +- src/main/kotlin/eventDemo/app/Card.kt | 4 +- src/main/kotlin/eventDemo/app/Command.kt | 4 +- .../kotlin/eventDemo/app/CommandStream.kt | 8 +- src/main/kotlin/eventDemo/app/EventStream.kt | 8 +- src/main/kotlin/eventDemo/app/actions/Card.kt | 15 +++- .../kotlin/eventDemo/app/actions/Command.kt | 8 +- src/main/kotlin/eventDemo/plugins/HTTP.kt | 4 +- src/main/kotlin/eventDemo/plugins/Sockets.kt | 3 +- .../kotlin/eventDemo/app/actions/CardTest.kt | 76 +++++++++---------- .../eventDemo/app/actions/CommandTest.kt | 74 +++++++++--------- .../eventDemo/app/actions/TestHttpClient.kt | 5 +- 13 files changed, 117 insertions(+), 102 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4f7927b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +[*.{kt,kts}] +ktlint_code_style = ktlint_official +ktlint_standard = enabled +ktlint_experimental = enabled +ktlint_standard_string-template-indent = disabled +ktlint_standard_multiline-expression-wrapping = disabled diff --git a/src/main/kotlin/eventDemo/app/AggregateId.kt b/src/main/kotlin/eventDemo/app/AggregateId.kt index 43c19ce..f23dae1 100644 --- a/src/main/kotlin/eventDemo/app/AggregateId.kt +++ b/src/main/kotlin/eventDemo/app/AggregateId.kt @@ -10,7 +10,9 @@ sealed interface AggregateId { @JvmInline @Serializable(with = GameIdSerializer::class) -value class GameId(override val id: UUID = UUID.randomUUID()) : AggregateId { +value class GameId( + override val id: UUID = UUID.randomUUID(), +) : AggregateId { constructor(id: String) : this(UUID.fromString(id)) override fun toString(): String = id.toString() diff --git a/src/main/kotlin/eventDemo/app/Card.kt b/src/main/kotlin/eventDemo/app/Card.kt index 4fa8f6d..8a4d554 100644 --- a/src/main/kotlin/eventDemo/app/Card.kt +++ b/src/main/kotlin/eventDemo/app/Card.kt @@ -8,9 +8,7 @@ data class Game( val id: GameId, ) { companion object { - fun new(): Game { - return Game(GameId()) - } + fun new(): Game = Game(GameId()) } } diff --git a/src/main/kotlin/eventDemo/app/Command.kt b/src/main/kotlin/eventDemo/app/Command.kt index 2ba3849..e47b487 100644 --- a/src/main/kotlin/eventDemo/app/Command.kt +++ b/src/main/kotlin/eventDemo/app/Command.kt @@ -7,7 +7,9 @@ import java.util.UUID @JvmInline @Serializable(with = CommandIdSerializer::class) -value class CommandId(private val id: UUID = UUID.randomUUID()) { +value class CommandId( + private val id: UUID = UUID.randomUUID(), +) { constructor(id: String) : this(UUID.fromString(id)) override fun toString(): String = id.toString() diff --git a/src/main/kotlin/eventDemo/app/CommandStream.kt b/src/main/kotlin/eventDemo/app/CommandStream.kt index 012b26e..ad25bdd 100644 --- a/src/main/kotlin/eventDemo/app/CommandStream.kt +++ b/src/main/kotlin/eventDemo/app/CommandStream.kt @@ -18,11 +18,7 @@ class CommandStream { commands.forEach { sendRequest(it) } } - fun readNext(): Command? { - return commandBus.firstOrNull() - } + fun readNext(): Command? = commandBus.firstOrNull() - fun readNext(commandClass: Class): U? { - return commandBus.filterIsInstance(commandClass).firstOrNull() - } + fun readNext(commandClass: Class): U? = commandBus.filterIsInstance(commandClass).firstOrNull() } diff --git a/src/main/kotlin/eventDemo/app/EventStream.kt b/src/main/kotlin/eventDemo/app/EventStream.kt index 80801d4..762922a 100644 --- a/src/main/kotlin/eventDemo/app/EventStream.kt +++ b/src/main/kotlin/eventDemo/app/EventStream.kt @@ -21,11 +21,7 @@ class EventStream { fun > read( aggregateId: ID, eventClass: Class, - ): U? { - return eventBus.get(aggregateId)?.filterIsInstance(eventClass)?.firstOrNull() - } + ): U? = eventBus.get(aggregateId)?.filterIsInstance(eventClass)?.firstOrNull() } -inline fun , ID : AggregateId> EventStream.read(aggregateId: ID): U? { - return this.read(aggregateId, U::class.java) -} +inline fun , ID : AggregateId> EventStream.read(aggregateId: ID): U? = this.read(aggregateId, U::class.java) diff --git a/src/main/kotlin/eventDemo/app/actions/Card.kt b/src/main/kotlin/eventDemo/app/actions/Card.kt index 5b87364..d3a115e 100644 --- a/src/main/kotlin/eventDemo/app/actions/Card.kt +++ b/src/main/kotlin/eventDemo/app/actions/Card.kt @@ -26,14 +26,20 @@ class Game( ) { @Serializable @Resource("card") - class Card(val game: Game) { + class Card( + val game: Game, + ) { @Serializable @Resource("") - class PutCard(val card: Card) + class PutCard( + val card: Card, + ) @Serializable @Resource("last") - class LastCard(val card: Card) + class LastCard( + val card: Card, + ) } } @@ -47,7 +53,8 @@ fun Routing.card() { } get { - eventStream.read(it.card.game.id) + eventStream + .read(it.card.game.id) ?.let { it1 -> call.respond(it1.card) } ?: call.response.status(HttpStatusCode.BadRequest) } diff --git a/src/main/kotlin/eventDemo/app/actions/Command.kt b/src/main/kotlin/eventDemo/app/actions/Command.kt index ba1539b..1af6cd5 100644 --- a/src/main/kotlin/eventDemo/app/actions/Command.kt +++ b/src/main/kotlin/eventDemo/app/actions/Command.kt @@ -19,7 +19,9 @@ import org.koin.ktor.ext.inject @Resource("/command") class CommandRoute { @Resource("send") - class Send(val commandRoute: CommandRoute) { + class Send( + val commandRoute: CommandRoute, + ) { @Serializable data class Response( val id: CommandId, @@ -29,7 +31,9 @@ class CommandRoute { } @Resource("next") - class Next(val commandRoute: CommandRoute) + class Next( + val commandRoute: CommandRoute, + ) } fun Routing.command() { diff --git a/src/main/kotlin/eventDemo/plugins/HTTP.kt b/src/main/kotlin/eventDemo/plugins/HTTP.kt index 8cd3bd9..f5f19f1 100644 --- a/src/main/kotlin/eventDemo/plugins/HTTP.kt +++ b/src/main/kotlin/eventDemo/plugins/HTTP.kt @@ -20,7 +20,9 @@ fun Application.configureHTTP() { } } -class BadRequestException(val httpError: HttpErrorBadRequest) : Exception() +class BadRequestException( + val httpError: HttpErrorBadRequest, +) : Exception() class HttpErrorBadRequest( statusCode: HttpStatusCode, diff --git a/src/main/kotlin/eventDemo/plugins/Sockets.kt b/src/main/kotlin/eventDemo/plugins/Sockets.kt index bea64a0..6a87f10 100644 --- a/src/main/kotlin/eventDemo/plugins/Sockets.kt +++ b/src/main/kotlin/eventDemo/plugins/Sockets.kt @@ -21,7 +21,8 @@ fun Application.configureSockets() { masking = false } routing { - webSocket("/ws") { // websocketSession + webSocket("/ws") { + // websocketSession for (frame in incoming) { if (frame is Frame.Text) { val text = frame.readText() diff --git a/src/test/kotlin/eventDemo/app/actions/CardTest.kt b/src/test/kotlin/eventDemo/app/actions/CardTest.kt index cd49d0f..aa83ac7 100644 --- a/src/test/kotlin/eventDemo/app/actions/CardTest.kt +++ b/src/test/kotlin/eventDemo/app/actions/CardTest.kt @@ -22,48 +22,48 @@ import org.koin.java.KoinJavaComponent.getKoin import org.koin.ktor.ext.inject import kotlin.test.assertEquals -class CardTest : FunSpec({ - test("/game/{id}/card") { - testApplication { - val client = httpClient() - application { - stopKoin() - module() - } - val id = GameId() - val card: Card = Card.Simple(1, Card.Color.Blue) - client.post("/game/$id/card") { - contentType(Json) - accept(Json) - setBody(card) - }.apply { - assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) +class CardTest : + FunSpec({ + test("/game/{id}/card") { + testApplication { + application { + stopKoin() + module() + } + val id = GameId() + val card: Card = Card.Simple(1, Card.Color.Blue) + httpClient() + .post("/game/$id/card") { + contentType(Json) + accept(Json) + setBody(card) + }.apply { + assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) - val eventStream = getKoin().get>() - assertEquals(PlayCardEvent(id, card), eventStream.read(id)) + val eventStream = getKoin().get>() + assertEquals(PlayCardEvent(id, card), eventStream.read(id)) + } } } - } - test("/game/{id}/card/last") { - testApplication { - val client = httpClient() - val id = GameId() - val card: Card = Card.Simple(1, Card.Color.Blue) - application { - stopKoin() - module() - val eventStream by inject>() - eventStream.publish( - PlayCardEvent(GameId(), Card.Simple(2, Card.Color.Yellow)), - PlayCardEvent(id, card), - ) - } + test("/game/{id}/card/last") { + testApplication { + val id = GameId() + val card: Card = Card.Simple(1, Card.Color.Blue) + application { + stopKoin() + module() + val eventStream by inject>() + eventStream.publish( + PlayCardEvent(GameId(), Card.Simple(2, Card.Color.Yellow)), + PlayCardEvent(id, card), + ) + } - client.get("/game/$id/card/last").apply { - assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) - assertEquals(card, this.call.body()) + httpClient().get("/game/$id/card/last").apply { + assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) + assertEquals(card, this.call.body()) + } } } - } -}) + }) diff --git a/src/test/kotlin/eventDemo/app/actions/CommandTest.kt b/src/test/kotlin/eventDemo/app/actions/CommandTest.kt index b285c04..4d8cbd3 100644 --- a/src/test/kotlin/eventDemo/app/actions/CommandTest.kt +++ b/src/test/kotlin/eventDemo/app/actions/CommandTest.kt @@ -22,47 +22,49 @@ import org.koin.java.KoinJavaComponent.getKoin import org.koin.ktor.ext.inject import kotlin.test.assertEquals -class CommandTest : FunSpec({ - test("/command/send") { - testApplication { - val client = httpClient() - application { - stopKoin() - module() - } - val command = PlayCardCommand(Game.new(), Card.Simple(1, Card.Color.Blue)) - client.post("/command/send") { - contentType(Json) - accept(Json) - setBody(command) - }.apply { - assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) +class CommandTest : + FunSpec({ + test("/command/send") { + testApplication { + val client = httpClient() + application { + stopKoin() + module() + } + val command = PlayCardCommand(Game.new(), Card.Simple(1, Card.Color.Blue)) + client + .post("/command/send") { + contentType(Json) + accept(Json) + setBody(command) + }.apply { + assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) - val commandStream = getKoin().get() - assertEquals(command, commandStream.readNext()) + val commandStream = getKoin().get() + assertEquals(command, commandStream.readNext()) + } } } - } - test("/command/next") { - testApplication { - val command = - PlayCardCommand( - Game.new(), - Card.Simple(1, Card.Color.Blue), - ) - application { - stopKoin() - module() + test("/command/next") { + testApplication { + val command = + PlayCardCommand( + Game.new(), + Card.Simple(1, Card.Color.Blue), + ) + application { + stopKoin() + module() - val commandStream by inject() - commandStream.sendRequest(command) - } + val commandStream by inject() + commandStream.sendRequest(command) + } - httpClient().get("/command/next").apply { - assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) - assertEquals(command, this.call.body()) + httpClient().get("/command/next").apply { + assertEquals(HttpStatusCode.OK, status, message = bodyAsText()) + assertEquals(command, this.call.body()) + } } } - } -}) + }) diff --git a/src/test/kotlin/eventDemo/app/actions/TestHttpClient.kt b/src/test/kotlin/eventDemo/app/actions/TestHttpClient.kt index d2195a1..a4716cf 100644 --- a/src/test/kotlin/eventDemo/app/actions/TestHttpClient.kt +++ b/src/test/kotlin/eventDemo/app/actions/TestHttpClient.kt @@ -9,8 +9,8 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.modules.SerializersModule import java.util.UUID -fun ApplicationTestBuilder.httpClient(): HttpClient { - return createClient { +fun ApplicationTestBuilder.httpClient(): HttpClient = + createClient { install(ContentNegotiation) { json( Json { @@ -22,4 +22,3 @@ fun ApplicationTestBuilder.httpClient(): HttpClient { ) } } -}