remove id in EntityI interface & remove usless EntitiesCollections
This commit is contained in:
@@ -38,14 +38,14 @@ class Connection(
|
||||
|
||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
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 <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -80,14 +80,14 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
@@ -96,7 +96,7 @@ class Connection(
|
||||
val result = exec(sql, compileArgs(values))
|
||||
val json = result.rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
listOf<EntityI<*>>() as List<R>
|
||||
listOf<EntityI>() as List<R>
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}.also {
|
||||
@@ -104,14 +104,14 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> 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<EntityI<*>>() as List<R>
|
||||
listOf<EntityI>() as List<R>
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}
|
||||
@@ -146,7 +146,7 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
@@ -155,7 +155,7 @@ class Connection(
|
||||
): Paginated<R> =
|
||||
select(sql, page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -166,7 +166,7 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
@@ -201,10 +201,8 @@ class Connection(
|
||||
|
||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||
return values.map {
|
||||
if (it is EntityI<*>) {
|
||||
serializer.serialize(it).apply {
|
||||
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
|
||||
}
|
||||
if (it is EntityI) {
|
||||
serializer.serialize(it)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
|
||||
@@ -13,33 +13,33 @@ interface EmbedExecutable {
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/* Select Miltiples */
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
/* Select Paginated */
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
|
||||
@@ -7,14 +7,14 @@ import fr.postgresjson.entity.EntityI
|
||||
interface Executable {
|
||||
/* Select One */
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -23,14 +23,14 @@ interface Executable {
|
||||
|
||||
/* Select Miltiples */
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -39,7 +39,7 @@ interface Executable {
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
fun <R: EntityI> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
|
||||
@@ -17,7 +17,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
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 <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
value: R,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
@@ -43,7 +43,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select One entity with named parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
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 <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): 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 <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
@@ -82,7 +82,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
@@ -91,7 +91,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select list of entities with named parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
@@ -102,13 +102,13 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
@@ -119,7 +119,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select Multiple with pagination
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
@@ -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 <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
@@ -145,7 +145,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
vararg values: Pair<String, Any?>,
|
||||
|
||||
@@ -2,7 +2,7 @@ package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
data class Paginated<T: EntityI<*>>(
|
||||
data class Paginated<T: EntityI>(
|
||||
val result: List<T>,
|
||||
val offset: Int,
|
||||
val limit: Int,
|
||||
|
||||
@@ -12,7 +12,7 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
|
||||
/* Select One */
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
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 <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
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 <reified R: EntityI<*>> selectOne(
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
@@ -42,7 +42,7 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
|
||||
/* Select Multiples */
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, List<R>) -> 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 <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, List<R>) -> 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 <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R: EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
@@ -82,7 +82,7 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
inline fun <reified R: EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class EntitiesCollections {
|
||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||
|
||||
fun <I, R: EntityI<I>> get(id: I, className: KClass<R>): R? {
|
||||
val collection = collections[className]
|
||||
val entity = collection?.get(id!!)
|
||||
return entity as R?
|
||||
}
|
||||
|
||||
inline fun <I, reified R: EntityI<I>> get(id: I): R? {
|
||||
return get(id, R::class)
|
||||
}
|
||||
|
||||
fun <I, R: EntityI<out I?>> set(entity: R): EntitiesCollections {
|
||||
if (collections[entity.className] == null) {
|
||||
collections[entity.className] = EntityCollection()
|
||||
}
|
||||
|
||||
collections[entity.className]!!.set(entity as EntityI<Any?>)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
class EntityCollection<T, E: EntityI<T?>> {
|
||||
private var collection: MutableMap<T, E> = mutableMapOf()
|
||||
|
||||
fun get(id: T): E? {
|
||||
return collection[id]
|
||||
}
|
||||
|
||||
fun set(entity: E) {
|
||||
val id = entity.id
|
||||
if (id !== null) {
|
||||
collection[id] = entity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,12 @@ import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/* ID */
|
||||
interface EntityI<T> {
|
||||
var id: T?
|
||||
val className: KClass<EntityI<T>>
|
||||
@JsonIgnore() get() = this::class as KClass<EntityI<T>>
|
||||
interface EntityI {
|
||||
val className: KClass<EntityI>
|
||||
@JsonIgnore() get() = this::class as KClass<EntityI>
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override var id: T? = null): EntityI<T>
|
||||
abstract class Entity<T>(open var id: T? = null): EntityI
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID>(id)
|
||||
open class IdEntity(override var id: Int? = null): Entity<Int>(id)
|
||||
|
||||
@@ -57,62 +56,62 @@ class EntityDeletedAtImp: EntityDeletedAt {
|
||||
}
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T: EntityI<*>> {
|
||||
interface EntityCreatedBy<T: EntityI> {
|
||||
var createdBy: T?
|
||||
}
|
||||
|
||||
interface EntityUpdatedBy<T: EntityI<*>> {
|
||||
interface EntityUpdatedBy<T: EntityI> {
|
||||
var updatedBy: T?
|
||||
}
|
||||
|
||||
interface EntityDeletedBy<T: EntityI<*>> {
|
||||
interface EntityDeletedBy<T: EntityI> {
|
||||
var deletedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT: EntityI<*>>(
|
||||
class EntityCreatedByImp<UserT: EntityI>(
|
||||
override var createdBy: UserT?
|
||||
): EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT: EntityI<*>>(
|
||||
class EntityUpdatedByImp<UserT: EntityI>(
|
||||
override var updatedBy: UserT?
|
||||
): EntityUpdatedBy<UserT>
|
||||
|
||||
class EntityDeletedByImp<UserT: EntityI<*>>(
|
||||
class EntityDeletedByImp<UserT: EntityI>(
|
||||
override var deletedBy: UserT?
|
||||
): EntityDeletedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityDeletedImp<UserT: EntityI<*>>(
|
||||
class EntityDeletedImp<UserT: EntityI>(
|
||||
override var deletedBy: UserT? = null
|
||||
): EntityDeletedBy<UserT>,
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT: EntityI<*>>(
|
||||
class EntityUpdatedImp<UserT: EntityI>(
|
||||
override var updatedAt: DateTime? = null,
|
||||
override var updatedBy: UserT? = null
|
||||
): EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||
|
||||
class EntityCreatedImp<UserT: EntityI<*>>(
|
||||
class EntityCreatedImp<UserT: EntityI>(
|
||||
override var createdAt: DateTime? = null,
|
||||
override var createdBy: UserT? = null
|
||||
): EntityCreatedBy<UserT>,
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT: EntityI<*>> {
|
||||
interface Published<UserT: EntityI> {
|
||||
var publishedAt: DateTime?
|
||||
var publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT: EntityI<*>>(
|
||||
class EntityPublishedImp<UserT: EntityI>(
|
||||
override var publishedBy: UserT?
|
||||
): Published<UserT> {
|
||||
override var publishedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT: EntityI<*>>(
|
||||
abstract class EntityImp<T, UserT: EntityI>(
|
||||
updatedBy: UserT?
|
||||
): Entity<T>(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
@@ -122,7 +121,7 @@ abstract class EntityImp<T, UserT: EntityI<*>>(
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
|
||||
abstract class UuidEntityExtended<T, UserT: EntityI<*>>(
|
||||
abstract class UuidEntityExtended<T, UserT: EntityI>(
|
||||
updatedBy: UserT?,
|
||||
publishedBy: UserT?
|
||||
):
|
||||
|
||||
@@ -4,7 +4,7 @@ import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
interface RepositoryI<E: EntityI<*>> {
|
||||
interface RepositoryI<E: EntityI> {
|
||||
val entityName: KClass<E>
|
||||
var requester: Requester
|
||||
fun getClassName(): String {
|
||||
|
||||
@@ -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 <T> serialize(source: EntityI<T>, pretty: Boolean = false): String {
|
||||
fun serialize(source: EntityI, pretty: Boolean = false): String {
|
||||
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
|
||||
else mapper.writeValueAsString(source)
|
||||
}
|
||||
|
||||
fun <E: EntityI<*>> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
fun <E: EntityI> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
return this.mapper.readValue(json, valueTypeRef)
|
||||
}
|
||||
|
||||
inline fun <reified E: EntityI<*>> deserialize(json: String): E? {
|
||||
inline fun <reified E: EntityI> deserialize(json: String): E? {
|
||||
return this.mapper.readValue(json)
|
||||
}
|
||||
|
||||
@@ -53,50 +43,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
return deserializeList(json, object: TypeReference<E>() {})
|
||||
}
|
||||
|
||||
fun <E: EntityI<*>> deserialize(json: String, target: E): E {
|
||||
fun <E: EntityI> deserialize(json: String, target: E): E {
|
||||
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> EntityI<T>.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
inline fun <reified E: EntityI<*>> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
inline fun <reified E: EntityI<*>> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
|
||||
|
||||
class EntityUuidDeserializer<T: UuidEntity> @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer<T>(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<JsonNode>(jp)
|
||||
val id = node.get("id").asText()
|
||||
val entity = collection.get<UUID, UuidEntity>(UUID.fromString(id))
|
||||
|
||||
return (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as T
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class EntityIdDeserializer<T: IdEntity> @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer<T>(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<JsonNode>(jp)
|
||||
val id = node.get("id").asInt()
|
||||
val entity = collection.get<Int, IdEntity>(id)
|
||||
|
||||
val obj = (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as EntityI<Int>
|
||||
collection.set(obj)
|
||||
|
||||
return obj as T
|
||||
}
|
||||
}
|
||||
fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
inline fun <reified E: EntityI> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
inline fun <reified E: EntityI> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
@@ -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<Int?>
|
||||
private class User(id: Int?): Entity<Int?>(id)
|
||||
private class ObjTest(var name: String): UuidEntityExtended<Int?, User>(User(1), User(2))
|
||||
|
||||
@Test
|
||||
@@ -15,7 +15,7 @@ class EntityTest() {
|
||||
val obj: ObjTest? = ObjTest("plop")
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj is UuidEntityExtended<Int?, User>)
|
||||
assertTrue(obj is EntityI<Int?>)
|
||||
assertTrue(obj is EntityI)
|
||||
assertTrue(obj is Entity<Int?>)
|
||||
assertTrue(obj is Published<User>)
|
||||
assertTrue(obj is EntityCreatedBy<User>)
|
||||
|
||||
Reference in New Issue
Block a user