From 2a738e0595ec479879f8a9f07d211dd6deba32ae Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 2 Oct 2019 11:16:28 +0200 Subject: [PATCH] remove id in EntityI interface & remove usless EntitiesCollections --- .idea/.gitignore | 3 +- .../fr/postgresjson/connexion/Connection.kt | 32 ++++----- .../postgresjson/connexion/EmbedExecutable.kt | 10 +-- .../fr/postgresjson/connexion/Executable.kt | 10 +-- .../fr/postgresjson/connexion/Function.kt | 28 ++++---- .../fr/postgresjson/connexion/Paginated.kt | 2 +- .../kotlin/fr/postgresjson/connexion/Query.kt | 20 +++--- .../entity/EntitiesCollections.kt | 42 ----------- .../kotlin/fr/postgresjson/entity/Entity.kt | 35 +++++---- .../fr/postgresjson/repository/Repository.kt | 2 +- .../fr/postgresjson/serializer/Serializer.kt | 71 +++---------------- src/test/kotlin/fr/postgresjson/EntityTest.kt | 4 +- 12 files changed, 83 insertions(+), 176 deletions(-) delete mode 100644 src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt diff --git a/.idea/.gitignore b/.idea/.gitignore index 3364827..3ad53bc 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -6,4 +6,5 @@ /compiler.xml /uiDesigner.xml /dataSources.xml -/sonarlint/ \ No newline at end of file +/sonarlint/ +/jarRepositories.xml \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 9614d56..768fa8f 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -38,14 +38,14 @@ class Connection( fun inTransaction(f: (Connection) -> CompletableFuture) = connect().inTransaction(f) - override fun > select( + override fun select( sql: String, typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit ): R? { val primaryObject = values.firstOrNull { - it is EntityI<*> && typeReference.type.typeName == it::class.java.name + it is EntityI && typeReference.type.typeName == it::class.java.name } as R? val result = exec(sql, compileArgs(values)) val json = result.rows[0].getString(0) @@ -62,14 +62,14 @@ class Connection( } } - inline fun > selectOne( + inline fun selectOne( sql: String, values: List = emptyList(), noinline block: SelectOneCallback = {} ): R? = select(sql, object: TypeReference() {}, values, block) - override fun > select( + override fun select( sql: String, typeReference: TypeReference, values: Map, @@ -80,14 +80,14 @@ class Connection( } } - inline fun > selectOne( + inline fun selectOne( sql: String, values: Map, noinline block: SelectOneCallback = {} ): R? = select(sql, object: TypeReference() {}, values, block) - override fun > select( + override fun select( sql: String, typeReference: TypeReference>, values: List, @@ -96,7 +96,7 @@ class Connection( val result = exec(sql, compileArgs(values)) val json = result.rows[0].getString(0) return if (json === null) { - listOf>() as List + listOf() as List } else { serializer.deserializeList(json, typeReference) }.also { @@ -104,14 +104,14 @@ class Connection( } } - inline fun > select( + inline fun select( sql: String, values: List = emptyList(), noinline block: SelectCallback = {} ): List = select(sql, object: TypeReference>() {}, values, block) - override fun > select( + override fun select( sql: String, page: Int, limit: Int, @@ -131,7 +131,7 @@ class Connection( return line.run { val json = rows[0].getString(0) val entities = if (json === null) { - listOf>() as List + listOf() as List } else { serializer.deserializeList(json, typeReference) } @@ -146,7 +146,7 @@ class Connection( } } - inline fun > select( + inline fun select( sql: String, page: Int, limit: Int, @@ -155,7 +155,7 @@ class Connection( ): Paginated = select(sql, page, limit, object: TypeReference>() {}, values, block) - override fun > select( + override fun select( sql: String, typeReference: TypeReference>, values: Map, @@ -166,7 +166,7 @@ class Connection( } } - inline fun > select( + inline fun select( sql: String, values: Map, noinline block: SelectCallback = {} @@ -201,10 +201,8 @@ class Connection( private fun compileArgs(values: List): List { return values.map { - if (it is EntityI<*>) { - serializer.serialize(it).apply { - serializer.collection.set>(it as EntityI) - } + if (it is EntityI) { + serializer.serialize(it) } else { it } diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt index bd61ff6..8ff9ea7 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt @@ -13,33 +13,33 @@ interface EmbedExecutable { /** * Select One entity with list of parameters */ - fun > select( + fun select( typeReference: TypeReference, values: List = emptyList(), block: SelectOneCallback = {} ): R? - fun > select( + fun select( typeReference: TypeReference, values: Map, block: SelectOneCallback = {} ): R? /* Select Miltiples */ - fun > select( + fun select( typeReference: TypeReference>, values: List = emptyList(), block: SelectCallback = {} ): List - fun > select( + fun select( typeReference: TypeReference>, values: Map, block: SelectCallback = {} ): List /* Select Paginated */ - fun > select( + fun select( page: Int, limit: Int, typeReference: TypeReference>, diff --git a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt index 2ef5bcb..7fb265e 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt @@ -7,14 +7,14 @@ import fr.postgresjson.entity.EntityI interface Executable { /* Select One */ - fun > select( + fun select( sql: String, typeReference: TypeReference, values: List = emptyList(), block: SelectOneCallback = {} ): R? - fun > select( + fun select( sql: String, typeReference: TypeReference, values: Map, @@ -23,14 +23,14 @@ interface Executable { /* Select Miltiples */ - fun > select( + fun select( sql: String, typeReference: TypeReference>, values: List = emptyList(), block: SelectCallback = {} ): List - fun > select( + fun select( sql: String, typeReference: TypeReference>, values: Map, @@ -39,7 +39,7 @@ interface Executable { /* Select Paginated */ - fun > select( + fun select( sql: String, page: Int, limit: Int, diff --git a/src/main/kotlin/fr/postgresjson/connexion/Function.kt b/src/main/kotlin/fr/postgresjson/connexion/Function.kt index 62255fc..d5cd5c3 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Function.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Function.kt @@ -17,7 +17,7 @@ class Function(val definition: Function, override val connection: Connection): E /** * Select One entity with list of parameters */ - override fun > select( + override fun select( typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit @@ -28,13 +28,13 @@ class Function(val definition: Function, override val connection: Connection): E return connection.select(sql, typeReference, values, block) } - inline fun > selectOne( + inline fun selectOne( values: List = emptyList(), noinline block: SelectOneCallback = {} ): R? = select(object: TypeReference() {}, values, block) - inline fun > selectOne( + inline fun selectOne( value: R, noinline block: SelectOneCallback = {} ): R? = @@ -43,7 +43,7 @@ class Function(val definition: Function, override val connection: Connection): E /** * Select One entity with named parameters */ - override fun > select( + override fun select( typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit @@ -54,13 +54,13 @@ class Function(val definition: Function, override val connection: Connection): E return connection.select(sql, typeReference, values, block) } - inline fun > selectOne( + inline fun selectOne( values: Map, noinline block: SelectOneCallback = {} ): R? = select(object: TypeReference() {}, values, block) - inline fun > selectOne( + inline fun selectOne( vararg values: Pair, noinline block: SelectOneCallback = {} ): R? = @@ -71,7 +71,7 @@ class Function(val definition: Function, override val connection: Connection): E /** * Select list of entities with list of parameters */ - override fun > select( + override fun select( typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit @@ -82,7 +82,7 @@ class Function(val definition: Function, override val connection: Connection): E return connection.select(sql, typeReference, values, block) } - inline fun > select( + inline fun select( values: List = emptyList(), noinline block: SelectCallback = {} ): List = @@ -91,7 +91,7 @@ class Function(val definition: Function, override val connection: Connection): E /** * Select list of entities with named parameters */ - override fun > select( + override fun select( typeReference: TypeReference>, values: Map, block: (QueryResult, List) -> Unit @@ -102,13 +102,13 @@ class Function(val definition: Function, override val connection: Connection): E return connection.select(sql, typeReference, values, block) } - inline fun > select( + inline fun select( values: Map, noinline block: SelectCallback = {} ): List = select(object: TypeReference>() {}, values, block) - inline fun > select( + inline fun select( vararg values: Pair, noinline block: SelectCallback = {} ): List = @@ -119,7 +119,7 @@ class Function(val definition: Function, override val connection: Connection): E /** * Select Multiple with pagination */ - override fun > select( + override fun select( page: Int, limit: Int, typeReference: TypeReference>, @@ -137,7 +137,7 @@ class Function(val definition: Function, override val connection: Connection): E return connection.select(sql, page, limit, typeReference, values, block) } - inline fun > select( + inline fun select( page: Int, limit: Int, values: Map = emptyMap(), @@ -145,7 +145,7 @@ class Function(val definition: Function, override val connection: Connection): E ): Paginated = select(page, limit, object: TypeReference>() {}, values, block) - inline fun > select( + inline fun select( page: Int, limit: Int, vararg values: Pair, diff --git a/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt b/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt index 9b00544..1ad55a9 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt @@ -2,7 +2,7 @@ package fr.postgresjson.connexion import fr.postgresjson.entity.EntityI -data class Paginated>( +data class Paginated( val result: List, val offset: Int, val limit: Int, diff --git a/src/main/kotlin/fr/postgresjson/connexion/Query.kt b/src/main/kotlin/fr/postgresjson/connexion/Query.kt index ab9624d..d95a856 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Query.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Query.kt @@ -12,7 +12,7 @@ class Query(override val name: String, private val sql: String, override val con /* Select One */ - override fun > select( + override fun select( typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit @@ -20,13 +20,13 @@ class Query(override val name: String, private val sql: String, override val con return connection.select(this.toString(), typeReference, values, block) } - inline fun > selectOne( + inline fun selectOne( values: List = emptyList(), noinline block: SelectOneCallback = {} ): R? = select(object: TypeReference() {}, values, block) - override fun > select( + override fun select( typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit @@ -34,7 +34,7 @@ class Query(override val name: String, private val sql: String, override val con return connection.select(this.toString(), typeReference, values, block) } - inline fun > selectOne( + inline fun selectOne( values: Map, noinline block: SelectOneCallback = {} ): R? = @@ -42,7 +42,7 @@ class Query(override val name: String, private val sql: String, override val con /* Select Multiples */ - override fun > select( + override fun select( typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit @@ -50,13 +50,13 @@ class Query(override val name: String, private val sql: String, override val con return connection.select(this.toString(), typeReference, values, block) } - inline fun > select( + inline fun select( values: List = emptyList(), noinline block: SelectCallback = {} ): List = select(object: TypeReference>() {}, values, block) - override fun > select( + override fun select( typeReference: TypeReference>, values: Map, block: (QueryResult, List) -> Unit @@ -64,13 +64,13 @@ class Query(override val name: String, private val sql: String, override val con return connection.select(this.toString(), typeReference, values, block) } - inline fun > select( + inline fun select( values: Map, noinline block: SelectCallback = {} ): List = select(object: TypeReference>() {}, values, block) - override fun > select( + override fun select( page: Int, limit: Int, typeReference: TypeReference>, @@ -82,7 +82,7 @@ class Query(override val name: String, private val sql: String, override val con /* Select Paginated */ - inline fun > select( + inline fun select( page: Int, limit: Int, values: Map = emptyMap(), diff --git a/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt b/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt deleted file mode 100644 index e9b7ede..0000000 --- a/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt +++ /dev/null @@ -1,42 +0,0 @@ -package fr.postgresjson.entity - -import kotlin.reflect.KClass - -class EntitiesCollections { - private val collections: MutableMap, EntityCollection>> = mutableMapOf() - - fun > get(id: I, className: KClass): R? { - val collection = collections[className] - val entity = collection?.get(id!!) - return entity as R? - } - - 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[entity.className]!!.set(entity as EntityI) - - return this - } - - class EntityCollection> { - private var collection: MutableMap = mutableMapOf() - - fun get(id: T): E? { - return collection[id] - } - - fun set(entity: E) { - val id = entity.id - if (id !== null) { - collection[id] = entity - } - } - } -} diff --git a/src/main/kotlin/fr/postgresjson/entity/Entity.kt b/src/main/kotlin/fr/postgresjson/entity/Entity.kt index bb18ed1..6b1e8be 100644 --- a/src/main/kotlin/fr/postgresjson/entity/Entity.kt +++ b/src/main/kotlin/fr/postgresjson/entity/Entity.kt @@ -6,13 +6,12 @@ import java.util.* import kotlin.reflect.KClass /* ID */ -interface EntityI { - var id: T? - val className: KClass> - @JsonIgnore() get() = this::class as KClass> +interface EntityI { + val className: KClass + @JsonIgnore() get() = this::class as KClass } -abstract class Entity(override var id: T? = null): EntityI +abstract class Entity(open var id: T? = null): EntityI open class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity(id) open class IdEntity(override var id: Int? = null): Entity(id) @@ -57,62 +56,62 @@ class EntityDeletedAtImp: EntityDeletedAt { } /* Author */ -interface EntityCreatedBy> { +interface EntityCreatedBy { var createdBy: T? } -interface EntityUpdatedBy> { +interface EntityUpdatedBy { var updatedBy: T? } -interface EntityDeletedBy> { +interface EntityDeletedBy { var deletedBy: T? } -class EntityCreatedByImp>( +class EntityCreatedByImp( override var createdBy: UserT? ): EntityCreatedBy -class EntityUpdatedByImp>( +class EntityUpdatedByImp( override var updatedBy: UserT? ): EntityUpdatedBy -class EntityDeletedByImp>( +class EntityDeletedByImp( override var deletedBy: UserT? ): EntityDeletedBy /* Mixed */ -class EntityDeletedImp>( +class EntityDeletedImp( override var deletedBy: UserT? = null ): EntityDeletedBy, EntityDeletedAt by EntityDeletedAtImp() -class EntityUpdatedImp>( +class EntityUpdatedImp( override var updatedAt: DateTime? = null, override var updatedBy: UserT? = null ): EntityUpdatedBy, EntityUpdatedAt by EntityUpdatedAtImp() -class EntityCreatedImp>( +class EntityCreatedImp( override var createdAt: DateTime? = null, override var createdBy: UserT? = null ): EntityCreatedBy, EntityCreatedAt by EntityCreatedAtImp() /* Published */ -interface Published> { +interface Published { var publishedAt: DateTime? var publishedBy: UserT? } -class EntityPublishedImp>( +class EntityPublishedImp( override var publishedBy: UserT? ): Published { override var publishedAt: DateTime? = null } /* Implementation */ -abstract class EntityImp>( +abstract class EntityImp( updatedBy: UserT? ): Entity(), EntityCreatedAt by EntityCreatedAtImp(), @@ -122,7 +121,7 @@ abstract class EntityImp>( EntityUpdatedBy by EntityUpdatedByImp(updatedBy), EntityDeletedBy by EntityDeletedByImp(updatedBy) -abstract class UuidEntityExtended>( +abstract class UuidEntityExtended( updatedBy: UserT?, publishedBy: UserT? ): diff --git a/src/main/kotlin/fr/postgresjson/repository/Repository.kt b/src/main/kotlin/fr/postgresjson/repository/Repository.kt index 58815a0..f7566a8 100644 --- a/src/main/kotlin/fr/postgresjson/repository/Repository.kt +++ b/src/main/kotlin/fr/postgresjson/repository/Repository.kt @@ -4,7 +4,7 @@ import fr.postgresjson.connexion.Requester import fr.postgresjson.entity.EntityI import kotlin.reflect.KClass -interface RepositoryI> { +interface RepositoryI { val entityName: KClass var requester: Requester fun getClassName(): String { diff --git a/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt b/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt index 7cf456e..2509ca4 100644 --- a/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt +++ b/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt @@ -1,29 +1,19 @@ package fr.postgresjson.serializer -import com.fasterxml.jackson.core.JsonParser -import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.core.type.TypeReference -import com.fasterxml.jackson.databind.* -import com.fasterxml.jackson.databind.deser.std.StdDeserializer +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.PropertyNamingStrategy +import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.datatype.joda.JodaModule import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import fr.postgresjson.entity.EntitiesCollections import fr.postgresjson.entity.EntityI -import fr.postgresjson.entity.IdEntity -import fr.postgresjson.entity.UuidEntity -import java.io.IOException -import java.util.* class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { - - var collection: EntitiesCollections = EntitiesCollections() - init { val module = SimpleModule() - module.addDeserializer(UuidEntity::class.java, EntityUuidDeserializer(collection)) - module.addDeserializer(IdEntity::class.java, EntityIdDeserializer(collection)) mapper.registerModule(module) mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE @@ -32,16 +22,16 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } - fun serialize(source: EntityI, pretty: Boolean = false): String { + fun serialize(source: EntityI, pretty: Boolean = false): String { return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source) else mapper.writeValueAsString(source) } - fun > deserialize(json: String, valueTypeRef: TypeReference): E { + fun deserialize(json: String, valueTypeRef: TypeReference): E { return this.mapper.readValue(json, valueTypeRef) } - inline fun > deserialize(json: String): E? { + inline fun deserialize(json: String): E? { return this.mapper.readValue(json) } @@ -53,50 +43,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { return deserializeList(json, object: TypeReference() {}) } - fun > deserialize(json: String, target: E): E { + fun deserialize(json: String, target: E): E { return mapper.readerForUpdating(target).readValue(json) } } -fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty) -inline fun > E.deserialize(json: String) = Serializer().deserialize(json, this) -inline fun > String.deserialize() = Serializer().deserialize(this) - - -class EntityUuidDeserializer @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer(vc) { - var collection: EntitiesCollections = EntitiesCollections() - - constructor(collection: EntitiesCollections): this() { - this.collection = collection - } - - @Throws(IOException::class, JsonProcessingException::class) - override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T { - val node = jp.codec.readTree(jp) - val id = node.get("id").asText() - val entity = collection.get(UUID.fromString(id)) - - return (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as T - } -} - - -class EntityIdDeserializer @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer(vc) { - var collection: EntitiesCollections = EntitiesCollections() - - constructor(collection: EntitiesCollections): this() { - this.collection = collection - } - - @Throws(IOException::class, JsonProcessingException::class) - override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T { - val node = jp.codec.readTree(jp) - val id = node.get("id").asInt() - val entity = collection.get(id) - - val obj = (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as EntityI - collection.set(obj) - - return obj as T - } -} \ No newline at end of file +fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty) +inline fun E.deserialize(json: String) = Serializer().deserialize(json, this) +inline fun String.deserialize() = Serializer().deserialize(this) \ No newline at end of file diff --git a/src/test/kotlin/fr/postgresjson/EntityTest.kt b/src/test/kotlin/fr/postgresjson/EntityTest.kt index c57e77f..b732d23 100644 --- a/src/test/kotlin/fr/postgresjson/EntityTest.kt +++ b/src/test/kotlin/fr/postgresjson/EntityTest.kt @@ -7,7 +7,7 @@ import org.junit.jupiter.api.TestInstance @TestInstance(TestInstance.Lifecycle.PER_CLASS) class EntityTest() { - private class User(override var id: Int?): EntityI + private class User(id: Int?): Entity(id) private class ObjTest(var name: String): UuidEntityExtended(User(1), User(2)) @Test @@ -15,7 +15,7 @@ class EntityTest() { val obj: ObjTest? = ObjTest("plop") assertTrue(obj is ObjTest) assertTrue(obj is UuidEntityExtended) - assertTrue(obj is EntityI) + assertTrue(obj is EntityI) assertTrue(obj is Entity) assertTrue(obj is Published) assertTrue(obj is EntityCreatedBy)