From 96b335875dfe5e188ab636da058445463369844e Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 24 Jun 2019 15:47:18 +0200 Subject: [PATCH] refactoring: EntitiesCollections & add EntityI.className --- .../fr/postgresjson/connexion/Connection.kt | 4 +++- ...tyCollection.kt => EntitiesCollections.kt} | 22 ++++++++++++------- .../kotlin/fr/postgresjson/entity/Entity.kt | 4 ++++ .../fr/postgresjson/repository/Repository.kt | 4 ++-- .../kotlin/fr/postgresjson/ConnectionTest.kt | 5 +++-- 5 files changed, 26 insertions(+), 13 deletions(-) rename src/main/kotlin/fr/postgresjson/entity/{EntityCollection.kt => EntitiesCollections.kt} (50%) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index d1be719..10ff537 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -59,7 +59,9 @@ class Connection( private fun compileArgs(values: List): List { return values.map { if (it is EntityI<*>) { - serializer.serialize(it) + val json = serializer.serialize(it) + serializer.collection.set>(it as EntityI) + json } else { it } diff --git a/src/main/kotlin/fr/postgresjson/entity/EntityCollection.kt b/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt similarity index 50% rename from src/main/kotlin/fr/postgresjson/entity/EntityCollection.kt rename to src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt index 2a2efe1..7bd3918 100644 --- a/src/main/kotlin/fr/postgresjson/entity/EntityCollection.kt +++ b/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt @@ -2,21 +2,27 @@ package fr.postgresjson.entity import kotlin.reflect.KClass -class EntityCollection { - val collections: MutableMap, EntityCollection>> = mutableMapOf() +class EntitiesCollections { + private val collections: MutableMap, EntityCollection>> = mutableMapOf() - inline fun > get(id: I): R? { - val collection = collections[R::class] + fun > get(id: I, className: KClass): R? { + val collection = collections[className] val entity = collection?.get(id!!) return entity as R? } - inline fun > set(entity: R) { - if (collections[R::class] == null) { - collections[R::class] = EntityCollection() + inline fun > get(id: I): R? { + return get(id, R::class) + } + + fun > set(entity: R): EntitiesCollections { + if (collections[entity.className] == null) { + collections[entity.className] = EntityCollection() } - collections[R::class]!!.set(entity as EntityI) + collections[entity.className]!!.set(entity as EntityI) + + return this } class EntityCollection> { diff --git a/src/main/kotlin/fr/postgresjson/entity/Entity.kt b/src/main/kotlin/fr/postgresjson/entity/Entity.kt index 4f9b132..dbd0796 100644 --- a/src/main/kotlin/fr/postgresjson/entity/Entity.kt +++ b/src/main/kotlin/fr/postgresjson/entity/Entity.kt @@ -1,11 +1,15 @@ package fr.postgresjson.entity +import com.fasterxml.jackson.annotation.JsonIgnore import org.joda.time.DateTime import java.util.* +import kotlin.reflect.KClass /* ID */ interface EntityI { var id: T? + val className: KClass> + @JsonIgnore() get() = this::class as KClass> } abstract class Entity(override var id: T? = null) : EntityI diff --git a/src/main/kotlin/fr/postgresjson/repository/Repository.kt b/src/main/kotlin/fr/postgresjson/repository/Repository.kt index 8473c82..208b465 100644 --- a/src/main/kotlin/fr/postgresjson/repository/Repository.kt +++ b/src/main/kotlin/fr/postgresjson/repository/Repository.kt @@ -2,7 +2,7 @@ package fr.postgresjson.repository import fr.postgresjson.Serializer import fr.postgresjson.connexion.Requester -import fr.postgresjson.entity.EntityCollection +import fr.postgresjson.entity.EntitiesCollections import fr.postgresjson.entity.EntityI import kotlin.reflect.KClass @@ -17,7 +17,7 @@ abstract class Repository>(override val entityName: KClass fun findById(id: T): EntityI? { val sql = requester.getQuery(entityName.toString()) - return when (val e = EntityCollection().get(id)) { + return when (val e = EntitiesCollections().get(id)) { null -> { // TODO create Request Serializer().deserialize>("""{"plop", "plip"}""") diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index 97f016b..bc062b6 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -58,10 +58,11 @@ class ConnectionTest(): TestAbstract() { @Test fun callRequestWithArgsEntity() { val o = ObjTest("myName") - val obj: ObjTest? = connection.selectOne("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o)) + o.id = 88 + val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id int, name text);", listOf(o)) assertTrue(obj !== null) assertTrue(obj is ObjTest) - assertTrue(obj!!.id == 1) + assertTrue(obj!!.id == 88) assertTrue(obj.name == "myName") }