From 48b40f51f649367466e75c1eedb4529568538f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Roaldseth?= Date: Tue, 25 Jun 2024 09:54:00 +0200 Subject: [PATCH] Bytt til ktlint plugin for gradle --- build.gradle.kts | 27 ++------ .../kotlin/no/nav/dagpenger/events/Metrics.kt | 7 +- .../no/nav/dagpenger/events/MoshiAdapters.kt | 18 ++--- .../kotlin/no/nav/dagpenger/events/Packet.kt | 69 ++++++++++--------- .../no/nav/dagpenger/events/PacketTest.kt | 41 ++++++----- .../no/nav/dagpenger/events/ProblemTest.kt | 5 +- 6 files changed, 84 insertions(+), 83 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 3e7a7af..02e084a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,11 +1,11 @@ -import com.diffplug.spotless.LineEnding import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("java-library") kotlin("jvm") version Kotlin.version - id("com.diffplug.spotless") version "6.21.0" + id("org.jlleitschuh.gradle.ktlint") version "12.1.1" id("maven-publish") } @@ -46,10 +46,6 @@ val sourcesJar by tasks.registering(Jar::class) { from(sourceSets["main"].allSource) } -tasks.named("compileKotlin") { - dependsOn("spotlessCheck") -} - tasks.withType { useJUnitPlatform() testLogging { @@ -82,7 +78,9 @@ publishing { pom { name.set("dagpenger-events") description.set( - "Holder definisjonen av dagpenger inntekt (brukt av [dp-inntekt] og 'packet'. Packet er dagpengers gamle svar på JsonMessage på [Rapid and rivers]. Brukes stort sett bare av [dp-regel*] riggen. ", + """Holder definisjonen av dagpenger inntekt (brukt av [dp-inntekt] og 'packet'. + |Packet er dagpengers gamle svar på JsonMessage på [Rapid and rivers]. Brukes stort sett bare av [dp-regel*] riggen. + """.trimMargin(), ) url.set("https://github.com/navikt/dagpenger-events") withXml { @@ -111,17 +109,6 @@ publishing { } } -spotless { - kotlin { - ktlint() - } - kotlinGradle { - target("*.gradle.kts", "buildSrc/**/*.kt*") - ktlint() - } - - // Workaround for - // using idea found at - // . - lineEndings = LineEnding.PLATFORM_NATIVE // or any other except GIT_ATTRIBUTES +tasks.withType().configureEach { + dependsOn("ktlintFormat") } diff --git a/src/main/kotlin/no/nav/dagpenger/events/Metrics.kt b/src/main/kotlin/no/nav/dagpenger/events/Metrics.kt index 95e300c..8bffc42 100644 --- a/src/main/kotlin/no/nav/dagpenger/events/Metrics.kt +++ b/src/main/kotlin/no/nav/dagpenger/events/Metrics.kt @@ -2,6 +2,7 @@ package no.nav.dagpenger.events import io.prometheus.client.Summary -val packetPayloadByteSize = Summary.build() - .name("payload_size_bytes") - .help("Packet payload size in bytes.").register() +val packetPayloadByteSize = + Summary.build() + .name("payload_size_bytes") + .help("Packet payload size in bytes.").register() diff --git a/src/main/kotlin/no/nav/dagpenger/events/MoshiAdapters.kt b/src/main/kotlin/no/nav/dagpenger/events/MoshiAdapters.kt index 8650562..77b30ac 100644 --- a/src/main/kotlin/no/nav/dagpenger/events/MoshiAdapters.kt +++ b/src/main/kotlin/no/nav/dagpenger/events/MoshiAdapters.kt @@ -11,14 +11,15 @@ import java.time.LocalDateTime import java.time.YearMonth import java.time.format.DateTimeFormatter -val moshiInstance: Moshi = Moshi.Builder() - .add(YearMonthJsonAdapter()) - .add(LocalDateJsonAdapter()) - .add(LocalDateTimeJsonAdapter()) - .add(KotlinJsonAdapterFactory()) - .add(BigDecimalJsonAdapter()) - .add(URIJsonAdapter()) - .build()!! +val moshiInstance: Moshi = + Moshi.Builder() + .add(YearMonthJsonAdapter()) + .add(LocalDateJsonAdapter()) + .add(LocalDateTimeJsonAdapter()) + .add(KotlinJsonAdapterFactory()) + .add(BigDecimalJsonAdapter()) + .add(URIJsonAdapter()) + .build()!! class YearMonthJsonAdapter { @ToJson @@ -45,7 +46,6 @@ class LocalDateJsonAdapter { } class BigDecimalJsonAdapter { - @ToJson fun toJson(bigDecimal: BigDecimal): String { return bigDecimal.toString() diff --git a/src/main/kotlin/no/nav/dagpenger/events/Packet.kt b/src/main/kotlin/no/nav/dagpenger/events/Packet.kt index 65b7f71..0b02b93 100644 --- a/src/main/kotlin/no/nav/dagpenger/events/Packet.kt +++ b/src/main/kotlin/no/nav/dagpenger/events/Packet.kt @@ -10,7 +10,6 @@ import java.time.LocalDateTime import java.time.YearMonth class Packet constructor(jsonString: String = "{}") { - companion object { internal const val READ_COUNT = "system_read_count" internal const val CORRELATION_ID = "system_correlation_id" @@ -47,10 +46,11 @@ class Packet constructor(jsonString: String = "{}") { breadcrumbs = breadcrumbsAdapter.fromJsonValue(json[BREADCRUMBS]) ?: mutableListOf() System.getenv("NAIS_APP_NAME")?.let { - val breadcrumb = Breadcrumb( - it, - LocalDateTime.now(), - ) + val breadcrumb = + Breadcrumb( + it, + LocalDateTime.now(), + ) breadcrumbs.add(breadcrumb) } @@ -59,28 +59,36 @@ class Packet constructor(jsonString: String = "{}") { private fun getValue(key: String): Any? = json[key] - fun putValue(key: String, thing: Any) { + fun putValue( + key: String, + thing: Any, + ) { put(key, thing) } - private fun put(key: String, value: Any) { + private fun put( + key: String, + value: Any, + ) { if (json.containsKey(key)) throw IllegalArgumentException("Cannot overwrite existing key: $key") json[key] = value } - private fun mergeMembersJsonMap() = json.toMutableMap().apply { - this[PROBLEM] = problem - this[BREADCRUMBS] = breadcrumbs - } + private fun mergeMembersJsonMap() = + json.toMutableMap().apply { + this[PROBLEM] = problem + this[BREADCRUMBS] = breadcrumbs + } fun toJson(): String? = mergeMembersJsonMap().run(adapter::toJson) - override fun toString(): String = mergeMembersJsonMap().mapValues { entry -> - when { - entry.key.startsWith("system") -> entry.value - else -> "" - } - }.run(adapter::toJson) + override fun toString(): String = + mergeMembersJsonMap().mapValues { entry -> + when { + entry.key.startsWith("system") -> entry.value + else -> "" + } + }.run(adapter::toJson) fun hasField(key: String): Boolean = json.containsKey(key) @@ -98,8 +106,10 @@ class Packet constructor(jsonString: String = "{}") { fun getNullableYearMonth(key: String): YearMonth? = getValue(key)?.let { YearMonth.parse(it.toString()) } - fun getNullableObjectValue(key: String, decode: (Any) -> T): T? = - getValue(key)?.let(decode) + fun getNullableObjectValue( + key: String, + decode: (Any) -> T, + ): T? = getValue(key)?.let(decode) fun getNullableBoolean(key: String): Boolean? { val v: Any? = getValue(key) @@ -111,25 +121,22 @@ class Packet constructor(jsonString: String = "{}") { } } - fun getBigDecimalValue(key: String) = - getNullableBigDecimalValue(key) ?: throw IllegalArgumentException("Null value for key=$key") + fun getBigDecimalValue(key: String) = getNullableBigDecimalValue(key) ?: throw IllegalArgumentException("Null value for key=$key") fun getIntValue(key: String) = getNullableIntValue(key) ?: throw IllegalArgumentException("Null value for key=$key") - fun getLongValue(key: String) = - getNullableLongValue(key) ?: throw IllegalArgumentException("Null value for key=$key") + fun getLongValue(key: String) = getNullableLongValue(key) ?: throw IllegalArgumentException("Null value for key=$key") - fun getStringValue(key: String) = - getNullableStringValue(key) ?: throw IllegalArgumentException("Null value for key=$key") + fun getStringValue(key: String) = getNullableStringValue(key) ?: throw IllegalArgumentException("Null value for key=$key") - fun getLocalDate(key: String) = - getNullableLocalDate(key) ?: throw IllegalArgumentException("Null value for key=$key") + fun getLocalDate(key: String) = getNullableLocalDate(key) ?: throw IllegalArgumentException("Null value for key=$key") - fun getYearMonth(key: String) = - getNullableYearMonth(key) ?: throw IllegalArgumentException("Null value for key=$key") + fun getYearMonth(key: String) = getNullableYearMonth(key) ?: throw IllegalArgumentException("Null value for key=$key") - fun getObjectValue(key: String, decode: (Any) -> T): T = - getNullableObjectValue(key, decode) ?: throw IllegalArgumentException("Null value for key=$key") + fun getObjectValue( + key: String, + decode: (Any) -> T, + ): T = getNullableObjectValue(key, decode) ?: throw IllegalArgumentException("Null value for key=$key") fun getBoolean(key: String) = getNullableBoolean(key) ?: throw IllegalArgumentException("Null value for key=$key") diff --git a/src/test/kotlin/no/nav/dagpenger/events/PacketTest.kt b/src/test/kotlin/no/nav/dagpenger/events/PacketTest.kt index af18aa1..4860da9 100644 --- a/src/test/kotlin/no/nav/dagpenger/events/PacketTest.kt +++ b/src/test/kotlin/no/nav/dagpenger/events/PacketTest.kt @@ -313,13 +313,14 @@ class PacketTest { val packet = Packet(jsonString) - val problem = Problem( - type = URI.create("urn:error"), - title = "A problem", - status = 404, - detail = "An detailed error message", - instance = URI.create("urn:error:404"), - ) + val problem = + Problem( + type = URI.create("urn:error"), + title = "A problem", + status = 404, + detail = "An detailed error message", + instance = URI.create("urn:error:404"), + ) packet.addProblem(problem) val serializedPacket = Packet(packet.toJson()!!) assertTrue(serializedPacket.hasProblem()) @@ -350,15 +351,16 @@ class PacketTest { } """.trimIndent() val packet = Packet(jsonString) - val complex = ClassA( - "id", - listOf( - ClassB( - YearMonth.of(2019, 2), - listOf(ClassC(BigDecimal.ZERO, AnEnum.BBB), ClassC(BigDecimal.TEN, AnEnum.AAA)), + val complex = + ClassA( + "id", + listOf( + ClassB( + YearMonth.of(2019, 2), + listOf(ClassC(BigDecimal.ZERO, AnEnum.BBB), ClassC(BigDecimal.TEN, AnEnum.AAA)), + ), ), - ), - ) + ) val adapter = moshiInstance.adapter(ClassA::class.java) packet.putValue("complex", adapter.toJsonValue(complex)!!) @@ -379,10 +381,11 @@ class PacketTest { @Test fun `toString should hide non system Packet values`() { - val packetString = Packet().apply { - putValue(Packet.PROBLEM, "problemValue") - putValue("secret", "secretValue") - }.toString() + val packetString = + Packet().apply { + putValue(Packet.PROBLEM, "problemValue") + putValue("secret", "secretValue") + }.toString() assertFalse(packetString.contains("secretValue")) assertFalse(packetString.contains("problemValue")) diff --git a/src/test/kotlin/no/nav/dagpenger/events/ProblemTest.kt b/src/test/kotlin/no/nav/dagpenger/events/ProblemTest.kt index 1f3f261..d210824 100644 --- a/src/test/kotlin/no/nav/dagpenger/events/ProblemTest.kt +++ b/src/test/kotlin/no/nav/dagpenger/events/ProblemTest.kt @@ -16,7 +16,10 @@ internal class ProblemTest { instance = URI.create("urn:error:404"), ).toJson.apply { assertNotNull(this) - assertEquals("""{"type":"urn:error","title":"A problem","status":404,"detail":"An detailed error message","instance":"urn:error:404"}""", this) + assertEquals( + """{"type":"urn:error","title":"A problem","status":404,"detail":"An detailed error message","instance":"urn:error:404"}""", + this, + ) } } }