Skip to content

Commit

Permalink
Bytt til ktlint plugin for gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
androa committed Jun 25, 2024
1 parent 2426ec8 commit 48b40f5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 83 deletions.
27 changes: 7 additions & 20 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
}

Expand Down Expand Up @@ -46,10 +46,6 @@ val sourcesJar by tasks.registering(Jar::class) {
from(sourceSets["main"].allSource)
}

tasks.named("compileKotlin") {
dependsOn("spotlessCheck")
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -111,17 +109,6 @@ publishing {
}
}

spotless {
kotlin {
ktlint()
}
kotlinGradle {
target("*.gradle.kts", "buildSrc/**/*.kt*")
ktlint()
}

// Workaround for <https://github.com/diffplug/spotless/issues/1644>
// using idea found at
// <https://github.com/diffplug/spotless/issues/1527#issuecomment-1409142798>.
lineEndings = LineEnding.PLATFORM_NATIVE // or any other except GIT_ATTRIBUTES
tasks.withType<KotlinCompile>().configureEach {
dependsOn("ktlintFormat")
}
7 changes: 4 additions & 3 deletions src/main/kotlin/no/nav/dagpenger/events/Metrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
18 changes: 9 additions & 9 deletions src/main/kotlin/no/nav/dagpenger/events/MoshiAdapters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +46,6 @@ class LocalDateJsonAdapter {
}

class BigDecimalJsonAdapter {

@ToJson
fun toJson(bigDecimal: BigDecimal): String {
return bigDecimal.toString()
Expand Down
69 changes: 38 additions & 31 deletions src/main/kotlin/no/nav/dagpenger/events/Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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 -> "<REDACTED>"
}
}.run(adapter::toJson)
override fun toString(): String =
mergeMembersJsonMap().mapValues { entry ->
when {
entry.key.startsWith("system") -> entry.value
else -> "<REDACTED>"
}
}.run(adapter::toJson)

fun hasField(key: String): Boolean = json.containsKey(key)

Expand All @@ -98,8 +106,10 @@ class Packet constructor(jsonString: String = "{}") {

fun getNullableYearMonth(key: String): YearMonth? = getValue(key)?.let { YearMonth.parse(it.toString()) }

fun <T> getNullableObjectValue(key: String, decode: (Any) -> T): T? =
getValue(key)?.let(decode)
fun <T> getNullableObjectValue(
key: String,
decode: (Any) -> T,
): T? = getValue(key)?.let(decode)

fun getNullableBoolean(key: String): Boolean? {
val v: Any? = getValue(key)
Expand All @@ -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 <T : Any> getObjectValue(key: String, decode: (Any) -> T): T =
getNullableObjectValue(key, decode) ?: throw IllegalArgumentException("Null value for key=$key")
fun <T : Any> 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")

Expand Down
41 changes: 22 additions & 19 deletions src/test/kotlin/no/nav/dagpenger/events/PacketTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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>(ClassA::class.java)

packet.putValue("complex", adapter.toJsonValue(complex)!!)
Expand All @@ -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"))
Expand Down
5 changes: 4 additions & 1 deletion src/test/kotlin/no/nav/dagpenger/events/ProblemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
}

0 comments on commit 48b40f5

Please sign in to comment.