From 0491a444aa63fcb9b7f1e800fa0d34debdcbef5e Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 15 Oct 2022 00:38:51 +0200 Subject: [PATCH 1/6] Fix enum class Status() --- .../fr/postgresjson/migration/Function.kt | 52 +++++++++++-------- .../fr/postgresjson/migration/Migrations.kt | 2 +- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/fr/postgresjson/migration/Function.kt b/src/main/kotlin/fr/postgresjson/migration/Function.kt index 20a31dc..73f445a 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Function.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Function.kt @@ -37,36 +37,44 @@ data class Function( ) override fun up(): Status { - try { - connection.sendQuery(up.script) - } catch (e: CompletionException) { - val cause = e.cause - if (cause is GenericDatabaseException && cause.errorMessage.fields['C'] == "42P13") { - connection.sendQuery("drop function ${down.getDefinition()}") + return try { + try { connection.sendQuery(up.script) + } catch (e: CompletionException) { + val cause = e.cause + if (cause is GenericDatabaseException && cause.errorMessage.fields['C'] == "42P13") { + connection.sendQuery("drop function ${down.getDefinition()}") + connection.sendQuery(up.script) + } } + + this::class.java.classLoader + .getResource("sql/migration/insertFunction.sql")!!.readText() + .let { connection.selectOne(it, listOf(up.name, up.getDefinition(), up.script, down.script)) } + ?.let { function -> + executedAt = function.executedAt + doExecute = Action.OK + } + + Status.OK + } catch (e: Throwable) { + Status.UP_FAIL } - - this::class.java.classLoader - .getResource("sql/migration/insertFunction.sql")!!.readText() - .let { connection.selectOne(it, listOf(up.name, up.getDefinition(), up.script, down.script)) } - ?.let { function -> - executedAt = function.executedAt - doExecute = Action.OK - } - - return Status.OK } override fun down(): Status { - connection.sendQuery(down.script) + return try { + connection.sendQuery(down.script) - this::class.java.classLoader - .getResource("sql/migration/deleteFunction.sql")!! - .readText() - .let { connection.sendQuery(it, listOf(down.name)) } + this::class.java.classLoader + .getResource("sql/migration/deleteFunction.sql")!! + .readText() + .let { connection.sendQuery(it, listOf(down.name)) } - return Status.OK + Status.OK + } catch (e: Throwable) { + Status.DOWN_FAIL + } } override fun test(): Status { diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index 751018f..4f62845 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -29,7 +29,7 @@ interface Migration { fun down(): Status fun test(): Status - enum class Status(i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) } + enum class Status(val i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) } enum class Action { OK, UP, DOWN } } From 18e9bddff5a75a14c6a45f81a8d4cde478b61784 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 15 Oct 2022 00:38:52 +0200 Subject: [PATCH 2/6] clean Migrations.kt --- .../kotlin/fr/postgresjson/migration/Migrations.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index 4f62845..3f2e395 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -33,7 +33,7 @@ interface Migration { enum class Action { OK, UP, DOWN } } -data class Migrations private constructor( +class Migrations private constructor( private val connection: Connection, private val migrationsScripts: MutableMap = mutableMapOf(), private val functions: MutableMap = mutableMapOf() @@ -49,7 +49,7 @@ data class Migrations private constructor( reset() } - fun reset() { + private fun reset() { migrationsScripts.clear() functions.clear() @@ -76,14 +76,14 @@ data class Migrations private constructor( */ private fun getMigrationFromDB() { this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let { - connection.select(it, object : TypeReference>() {}) + connection.select(it, object : TypeReference>() {}) .map { function -> functions[function.filename] = Function(function.up, function.down, connection, function.executedAt) } } this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let { - connection.select(it, object : TypeReference>() {}) + connection.select(it, object : TypeReference>() {}) .map { query -> migrationsScripts[query.filename] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt) } @@ -297,7 +297,7 @@ data class Migrations private constructor( return list.toMap() } - fun copy(): Migrations { + private fun copy(): Migrations { val queriesCopy = migrationsScripts.map { it.key to it.value.copy() }.toMap().toMutableMap() From f835685c30d003d13d80edf334e4a84c575e7e72 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 15 Oct 2022 00:38:53 +0200 Subject: [PATCH 3/6] clean ConnectionTest.kt --- .../kotlin/fr/postgresjson/ConnectionTest.kt | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index a388552..e3545c0 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -9,16 +9,15 @@ import fr.postgresjson.entity.Parameter import fr.postgresjson.entity.UuidEntity import fr.postgresjson.serializer.deserialize import fr.postgresjson.serializer.toTypeReference -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertTrue -import org.junit.jupiter.api.Assertions +import java.util.UUID +import kotlin.test.assertContains +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.assertThrows -import java.util.UUID -import kotlin.test.assertContains -import kotlin.test.assertNull @TestInstance(TestInstance.Lifecycle.PER_CLASS) class ConnectionTest : TestAbstract() { @@ -32,7 +31,7 @@ class ConnectionTest : TestAbstract() { fun getObject() { val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1") assertTrue(obj is ObjTest) - assertTrue(obj!!.id == UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) + assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), obj.id) } @Test @@ -60,7 +59,7 @@ class ConnectionTest : TestAbstract() { fun `test call request with args`() { val result: ObjTest? = connection.selectOne("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)", listOf("myName")) assertNotNull(result) - assertEquals("myName", result!!.name) + assertEquals("myName", result.name) } @Test @@ -69,7 +68,7 @@ class ConnectionTest : TestAbstract() { assertEquals("myName", this.rows[0].getString(0)?.deserialize()?.name) } assertNotNull(result) - assertEquals("myName", result!!.name) + assertEquals("myName", result.name) } @Test @@ -89,9 +88,8 @@ class ConnectionTest : TestAbstract() { val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00")) val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);", listOf(o)) assertNotNull(obj) - assertTrue(obj is ObjTest) - assertEquals(obj!!.id, UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00")) - assertEquals(obj.name, "myName") + assertEquals(UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"), obj.id) + assertEquals("myName", obj.name) } @Test @@ -99,15 +97,15 @@ class ConnectionTest : TestAbstract() { val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) val objUpdated: ObjTest? = connection.update("select ?::jsonb || jsonb_build_object('name', 'after');", obj.toTypeReference(), obj) assertTrue(objUpdated is ObjTest) - assertTrue(objUpdated!!.id == UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) - assertTrue(objUpdated.name == "after") + assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id) + assertEquals("after", objUpdated.name) } @Test fun callExec() { val o = ObjTest("myName") val result = connection.exec("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')", listOf(o)) - Assertions.assertEquals(1, result.rowsAffected) + assertEquals(1, result.rowsAffected) } @Test @@ -120,9 +118,10 @@ class ConnectionTest : TestAbstract() { "third" to 123 ) ) - assertEquals(result!!.first, "ff") - assertEquals(result.second, "sec") - assertEquals(result.third, 123) + assertNotNull(result) + assertEquals("ff", result.first) + assertEquals("sec", result.second) + assertEquals(123, result.third) } @Test @@ -134,7 +133,8 @@ class ConnectionTest : TestAbstract() { "second" to ParameterObject("two") ) ) - assertEquals("one", result!!.first.third) + assertNotNull(result) + assertEquals("one", result.first.third) assertEquals("two", result.second.third) } @@ -153,9 +153,9 @@ class ConnectionTest : TestAbstract() { "second" to "sec" ) ) - assertEquals(result[0].first, "ff") - assertEquals(result[0].second, "sec") - assertEquals(result[0].third, 123) + assertEquals("ff", result[0].first) + assertEquals("sec", result[0].second) + assertEquals(123, result[0].third) } @Test @@ -171,9 +171,9 @@ class ConnectionTest : TestAbstract() { "third" to 123, "second" to "sec" ) - assertEquals(result[0].first, "ff") - assertEquals(result[0].second, "sec") - assertEquals(result[0].third, 123) + assertEquals("ff", result[0].first) + assertEquals("sec", result[0].second) + assertEquals(123, result[0].third) } @Test @@ -192,10 +192,10 @@ class ConnectionTest : TestAbstract() { ) assertNotNull(result) - assertEquals(result.result[0].name, "ff") - assertEquals(result.result[1].name, "ff-2") - assertEquals(result.total, 10) - assertEquals(result.offset, 0) + assertEquals("ff", result.result[0].name) + assertEquals("ff-2", result.result[1].name) + assertEquals(10, result.total) + assertEquals(0, result.offset) } @Test @@ -214,8 +214,8 @@ class ConnectionTest : TestAbstract() { assertNotNull(result) assertTrue(result.result.isEmpty()) assertEquals(0, result.result.size) - assertEquals(result.total, 10) - assertEquals(result.offset, 0) + assertEquals(10, result.total) + assertEquals(0, result.offset) } @Test @@ -242,8 +242,8 @@ class ConnectionTest : TestAbstract() { assertNotNull(result) assertEquals("myName", result.result[0].name) assertEquals(1, result.result.size) - assertEquals(result.total, 10) - assertEquals(result.offset, 0) + assertEquals(10, result.total) + assertEquals(0, result.offset) } @Test @@ -336,11 +336,12 @@ class ConnectionTest : TestAbstract() { """.trimIndent(), params ) { - assertEquals("ff", it!!.first) + assertNotNull(it) + assertEquals("ff", it.first) assertEquals("plop", rows[0].getString("other")) } assertNotNull(result) - assertEquals("ff", result!!.first) + assertEquals("ff", result.first) assertEquals("sec", result.second) assertEquals(123, result.third) } @@ -371,7 +372,8 @@ class ConnectionTest : TestAbstract() { "second" to ParameterObject("two") ) ).let { result -> - assertEquals("one", result!!.first.third) + assertNotNull(result) + assertEquals("one", result.first.third) assertEquals("two", result.second.third) } } From a2b8483967b6ce0d85e09fb4bde063a32bb13c53 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 15 Oct 2022 00:38:53 +0200 Subject: [PATCH 4/6] clean RequesterTest.kt --- .../kotlin/fr/postgresjson/RequesterTest.kt | 70 +++++++++++-------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index 9ee5c4b..70f2b4b 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -11,10 +11,10 @@ import fr.postgresjson.connexion.selectOne import fr.postgresjson.connexion.update import fr.postgresjson.entity.UuidEntity import fr.postgresjson.serializer.deserialize -import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test import java.util.UUID +import kotlin.test.assertEquals import kotlin.test.assertNotNull class RequesterTest : TestAbstract() { @@ -83,13 +83,14 @@ class RequesterTest : TestAbstract() { @Test fun `add query from string`() { - val result: Int = Requester(connection) + val result: Int? = Requester(connection) .apply { addQuery("simpleTest", "select 42;") } .getQuery("simpleTest") .exec() - .rows[0].getInt(0)!! + .rows[0].getInt(0) - assertEquals(result, 42) + assertNotNull(result) + assertEquals(42, result) } @Test @@ -100,8 +101,9 @@ class RequesterTest : TestAbstract() { .getQuery("selectOne") .selectOne() - assertEquals(objTest!!.id, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528")) - assertEquals(objTest.name, "test") + assertNotNull(objTest) + assertEquals(objTest.id, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528")) + assertEquals("test", objTest.name) } @Test @@ -138,8 +140,9 @@ class RequesterTest : TestAbstract() { .getFunction("test_function") .selectOne(listOf("test", "plip")) - assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d")) - assertEquals(objTest.name, "test") + assertNotNull(objTest) + assertEquals(objTest.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d")) + assertEquals("test", objTest.name) } @Test @@ -284,10 +287,11 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on function`() { val resources = this::class.java.getResource("/sql/function/Test")?.toURI() - val obj: ObjTest = Requester(connection, functionsDirectory = resources) + val obj: ObjTest? = Requester(connection, functionsDirectory = resources) .getFunction("test_function") - .selectOne(mapOf("name" to "myName"))!! + .selectOne(mapOf("name" to "myName")) + assertNotNull(obj) assertEquals("myName", obj.name) } @@ -295,10 +299,11 @@ class RequesterTest : TestAbstract() { fun `call selectOne on function with object and named argument`() { val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val obj2 = ObjTest("original") - val obj: ObjTest = Requester(connection, functionsDirectory = resources) + val obj: ObjTest? = Requester(connection, functionsDirectory = resources) .getFunction("test_function_object") - .selectOne("resource" to obj2)!! + .selectOne("resource" to obj2) + assertNotNull(obj) assertEquals("changedName", obj.name) assertEquals("original", obj2.name) } @@ -307,10 +312,11 @@ class RequesterTest : TestAbstract() { fun `call selectOne on function with object`() { val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val obj2 = ObjTest("original") - val obj: ObjTest = Requester(connection, functionsDirectory = resources) + val obj: ObjTest? = Requester(connection, functionsDirectory = resources) .getFunction("test_function_object") - .update(obj2)!! + .update(obj2) + assertNotNull(obj) assertEquals("changedName", obj.name) assertEquals("original", obj2.name) } @@ -318,20 +324,22 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on function with object and no arguments`() { val resources = this::class.java.getResource("/sql/function/Test")?.toURI() - val obj: ObjTest = Requester(connection, functionsDirectory = resources) + val obj: ObjTest? = Requester(connection, functionsDirectory = resources) .getFunction("test_function") - .selectOne()!! + .selectOne() + assertNotNull(obj) assertEquals("plop", obj.name) } @Test fun `call selectOne on query`() { val resources = this::class.java.getResource("/sql/query")?.toURI() - val obj: ObjTest = Requester(connection, queriesDirectory = resources) + val obj: ObjTest? = Requester(connection, queriesDirectory = resources) .getQuery("selectOneWithParameters") - .selectOne(mapOf("name" to "myName"))!! + .selectOne(mapOf("name" to "myName")) + assertNotNull(obj) assertEquals("myName", obj.name) } @@ -342,6 +350,8 @@ class RequesterTest : TestAbstract() { .getFunction("test_function_multiple") .select(mapOf("name" to "myName")) + assertNotNull(obj[0]) + assertEquals("myName", obj[0].name) assertEquals("myName", obj[0].name) } @@ -366,7 +376,7 @@ class RequesterTest : TestAbstract() { assertEquals("ff-2", result[1].name) } }.apply { - select(object : TypeReference>() {}, mapOf("name" to "ff")).let { result -> + select(object : TypeReference>() {}, mapOf("name" to "ff")).let { result -> assertNotNull(result) assertEquals("ff", result[0].name) assertEquals("ff-2", result[1].name) @@ -385,7 +395,7 @@ class RequesterTest : TestAbstract() { assertEquals("ff-2", result[1].name) } }.apply { - select(object : TypeReference>() {}, "name" to "ff").let { result -> + select(object : TypeReference>() {}, "name" to "ff").let { result -> assertNotNull(result) assertEquals("ff", result[0].name) assertEquals("ff-2", result[1].name) @@ -404,7 +414,7 @@ class RequesterTest : TestAbstract() { assertEquals("aa-2", result[1].name) } }.apply { - select(object : TypeReference>() {}, listOf("ff", "aa")).let { result -> + select(object : TypeReference>() {}, listOf("ff", "aa")).let { result -> assertNotNull(result) assertEquals("ff", result[0].name) assertEquals("aa-2", result[1].name) @@ -438,7 +448,7 @@ class RequesterTest : TestAbstract() { assertEquals(0, offset) } }.apply { - select(1, 2, object : TypeReference>() {}, mapOf("name" to "ff")).run { + select(1, 2, object : TypeReference>() {}, mapOf("name" to "ff")).run { assertNotNull(result) assertEquals("ff", result[0].name) assertEquals("ff-2", result[1].name) @@ -478,17 +488,19 @@ class RequesterTest : TestAbstract() { Requester(connection, queriesDirectory = resources) .getQuery("selectOneWithParameters").apply { selectOne(mapOf("name" to "myName")) { - assertEquals("myName", it!!.name) + assertNotNull(it) + assertEquals("myName", it.name) assertEquals("plop", rows[0].getString("other")) - }!!.run { - assertEquals("myName", name) + }.run { + assertEquals("selectOneWithParameters", name) } }.apply { - selectOne(typeReference = object : TypeReference() {}, values = mapOf("name" to "myName")) { it -> - assertEquals("myName", it!!.name) + selectOne(typeReference = object : TypeReference() {}, values = mapOf("name" to "myName")) { + assertNotNull(it) + assertEquals("myName", it.name) assertEquals("plop", rows[0].getString("other")) - }!!.run { - assertEquals("myName", name) + }.run { + assertEquals("selectOneWithParameters", name) } } } From dab7358e397fb58b85ba1ff4c6bc22d0326e3c1a Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 15 Oct 2022 00:38:54 +0200 Subject: [PATCH 5/6] typo --- src/main/kotlin/fr/postgresjson/connexion/Paginated.kt | 6 +++--- src/main/kotlin/fr/postgresjson/migration/Migrations.kt | 6 +++--- .../{createFunctionShema.sql => createFunctionSchema.sql} | 0 .../{createHistoryShema.sql => createHistorySchema.sql} | 0 4 files changed, 6 insertions(+), 6 deletions(-) rename src/main/resources/sql/migration/{createFunctionShema.sql => createFunctionSchema.sql} (100%) rename src/main/resources/sql/migration/{createHistoryShema.sql => createHistorySchema.sql} (100%) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt b/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt index 02d23ad..f755b1e 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt @@ -14,9 +14,9 @@ data class Paginated( val totalPages: Int = (total.toDouble() / limit.toDouble()).ceil() init { - if (offset < 0) error("offset must be greather or equal than 0") - if (limit < 1) error("limit must be greather or equal than 1") - if (total < 0) error("total must be greather or equal than 0") + if (offset < 0) error("offset must be greater or equal than 0") + if (limit < 1) error("limit must be greater or equal than 1") + if (total < 0) error("total must be greater or equal than 0") } fun isLastPage(): Boolean = currentPage >= totalPages diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index 3f2e395..4c7d2cb 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -130,7 +130,7 @@ class Migrations private constructor( enum class Direction { UP, DOWN } internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException? = null) : - Throwable("The file $path whas not found", cause) + Throwable("The file $path was not found", cause) fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations { val currentFunction = functions[newDefinition.name] @@ -174,10 +174,10 @@ class Migrations private constructor( private fun initDB() { if (!initialized) { - this::class.java.classLoader.getResource("sql/migration/createHistoryShema.sql")!!.readText().let { + this::class.java.classLoader.getResource("sql/migration/createHistorySchema.sql")!!.readText().let { connection.sendQuery(it) } - this::class.java.classLoader.getResource("sql/migration/createFunctionShema.sql")!!.readText().let { + this::class.java.classLoader.getResource("sql/migration/createFunctionSchema.sql")!!.readText().let { connection.sendQuery(it) } initialized = true diff --git a/src/main/resources/sql/migration/createFunctionShema.sql b/src/main/resources/sql/migration/createFunctionSchema.sql similarity index 100% rename from src/main/resources/sql/migration/createFunctionShema.sql rename to src/main/resources/sql/migration/createFunctionSchema.sql diff --git a/src/main/resources/sql/migration/createHistoryShema.sql b/src/main/resources/sql/migration/createHistorySchema.sql similarity index 100% rename from src/main/resources/sql/migration/createHistoryShema.sql rename to src/main/resources/sql/migration/createHistorySchema.sql From 8da69d1288586896d0c8b3e58680f5c64bba015a Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 15 Oct 2022 00:38:55 +0200 Subject: [PATCH 6/6] clean syntax --- build.gradle.kts | 2 +- docker-compose.yml | 2 +- .../kotlin/fr/postgresjson/definition/Function.kt | 11 ++++------- .../kotlin/fr/postgresjson/utils/LoggerDelegate.kt | 2 +- .../kotlin/fr/postgresjson/utils/searchSqlFiles.kt | 2 +- src/test/kotlin/fr/postgresjson/ConnectionTest.kt | 6 +++--- 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 78fc83f..9d0c400 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -41,7 +41,7 @@ tasks.test { useJUnit() useJUnitPlatform() systemProperty("junit.jupiter.execution.parallel.enabled", true) - if (disableLint.toBoolean() == false) { + if (!disableLint.toBoolean()) { finalizedBy(tasks.ktlintCheck) } } diff --git a/docker-compose.yml b/docker-compose.yml index 0e8ea03..e36c83c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: context: docker/postgresql restart: always ports: - - 5555:5432 + - "5555:5432" environment: POSTGRES_DB: json_test POSTGRES_USER: test diff --git a/src/main/kotlin/fr/postgresjson/definition/Function.kt b/src/main/kotlin/fr/postgresjson/definition/Function.kt index 02c6dc1..1afa240 100644 --- a/src/main/kotlin/fr/postgresjson/definition/Function.kt +++ b/src/main/kotlin/fr/postgresjson/definition/Function.kt @@ -21,7 +21,7 @@ class Function( val queryMatch = functionRegex.find(script) if (queryMatch !== null) { - val functionName = queryMatch.groups.get("name")?.value?.trim() ?: error("Function name not found") + val functionName = queryMatch.groups["name"]?.value?.trim() ?: error("Function name not found") val functionParameters = queryMatch.groups["params"]?.value?.trim() this.returns = queryMatch.groups["return"]?.value?.trim() ?: "" @@ -51,15 +51,12 @@ class Function( fun getDefinition(): String { return parameters .filter { it.direction == Parameter.Direction.IN } - .joinToString(", ") { "${it.name} ${it.type}" }.let { - "$name ($it)" - } + .joinToString(", ") { "${it.name} ${it.type}" } + .let { "$name ($it)" } } fun getParametersIndexedByName(): Map { - return parameters.map { - it.name to it - }.toMap() + return parameters.associateBy { it.name } } infix fun `has same definition`(other: Function): Boolean { diff --git a/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt b/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt index bc27e00..3435a3e 100644 --- a/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt +++ b/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt @@ -6,5 +6,5 @@ import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty internal class LoggerDelegate : ReadOnlyProperty { - override fun getValue(thisRef: R, property: KProperty<*>) = LoggerFactory.getLogger(thisRef.javaClass.packageName) + override fun getValue(thisRef: R, property: KProperty<*>): Logger = LoggerFactory.getLogger(thisRef.javaClass.packageName) } diff --git a/src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt b/src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt index 36f2e68..4e41e21 100644 --- a/src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt +++ b/src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt @@ -14,7 +14,7 @@ import kotlin.streams.asSequence fun URL.searchSqlFiles() = this.toURI().searchSqlFiles() -fun URI.searchSqlFiles() = sequence { +fun URI.searchSqlFiles() = sequence { val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch") val uri: URI = this@searchSqlFiles logger.debug("""SQL files found in "${uri.toString().substringAfter('!')}" :""") diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index e3545c0..bca640b 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -9,15 +9,15 @@ import fr.postgresjson.entity.Parameter import fr.postgresjson.entity.UuidEntity import fr.postgresjson.serializer.deserialize import fr.postgresjson.serializer.toTypeReference +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.assertThrows import java.util.UUID import kotlin.test.assertContains import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.api.assertThrows @TestInstance(TestInstance.Lifecycle.PER_CLASS) class ConnectionTest : TestAbstract() {