clean entities
This commit is contained in:
21
.idea/runConfigurations/Publish_To_MavenLocal.xml
generated
Normal file
21
.idea/runConfigurations/Publish_To_MavenLocal.xml
generated
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Publish To MavenLocal" type="GradleRunConfiguration" factoryName="Gradle" singleton="false">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="publishToMavenLocal" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
23
.idea/runConfigurations/Test_and_Publish_To_MavenLocal.xml
generated
Normal file
23
.idea/runConfigurations/Test_and_Publish_To_MavenLocal.xml
generated
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Test and Publish To MavenLocal" type="GradleRunConfiguration" factoryName="Gradle" singleton="false">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="publishToMavenLocal" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2">
|
||||||
|
<option name="RunConfigurationTask" enabled="true" run_configuration_name="tests" run_configuration_type="JUnit" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
2
.idea/runConfigurations/tests.xml
generated
2
.idea/runConfigurations/tests.xml
generated
@@ -1,5 +1,5 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="tests" type="JUnit" factoryName="JUnit">
|
<configuration default="false" name="tests" type="JUnit" factoryName="JUnit" singleton="false">
|
||||||
<module name="postgresjson.test" />
|
<module name="postgresjson.test" />
|
||||||
<extension name="net.ashald.envfile">
|
<extension name="net.ashald.envfile">
|
||||||
<option name="IS_ENABLED" value="false" />
|
<option name="IS_ENABLED" value="false" />
|
||||||
|
|||||||
@@ -5,13 +5,13 @@ import kotlin.reflect.KClass
|
|||||||
class EntitiesCollections {
|
class EntitiesCollections {
|
||||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||||
|
|
||||||
fun <I, R: EntityI<I?>> get(id: I, className: KClass<R>): R? {
|
fun <I, R: EntityI<I>> get(id: I, className: KClass<R>): R? {
|
||||||
val collection = collections[className]
|
val collection = collections[className]
|
||||||
val entity = collection?.get(id!!)
|
val entity = collection?.get(id!!)
|
||||||
return entity as R?
|
return entity as R?
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <I, reified R: EntityI<I?>> get(id: I): R? {
|
inline fun <I, reified R: EntityI<I>> get(id: I): R? {
|
||||||
return get(id, R::class)
|
return get(id, R::class)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import kotlin.reflect.KClass
|
|||||||
/* ID */
|
/* ID */
|
||||||
interface EntityI<T> {
|
interface EntityI<T> {
|
||||||
var id: T?
|
var id: T?
|
||||||
val className: KClass<EntityI<T?>>
|
val className: KClass<EntityI<T>>
|
||||||
@JsonIgnore() get() = this::class as KClass<EntityI<T?>>
|
@JsonIgnore() get() = this::class as KClass<EntityI<T>>
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Entity<T>(override var id: T? = null): EntityI<T?>
|
abstract class Entity<T>(override var id: T? = null): EntityI<T>
|
||||||
abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID?>(id)
|
abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID>(id)
|
||||||
abstract class IdEntity(override var id: Int? = null): Entity<Int?>(id)
|
abstract class IdEntity(override var id: Int? = null): Entity<Int>(id)
|
||||||
|
|
||||||
/* Version */
|
/* Version */
|
||||||
interface EntityVersioning<ID, NUMBER> {
|
interface EntityVersioning<ID, NUMBER> {
|
||||||
@@ -53,13 +53,13 @@ interface UpdatedBy<T: EntityI<*>> {
|
|||||||
var updatedBy: T?
|
var updatedBy: T?
|
||||||
}
|
}
|
||||||
|
|
||||||
class EntityCreatedByImp<UserT: EntityI<*>>: CreatedBy<UserT> {
|
class EntityCreatedByImp<UserT: EntityI<*>>(
|
||||||
override var createdBy: UserT? = null
|
override var createdBy: UserT?
|
||||||
}
|
): CreatedBy<UserT>
|
||||||
|
|
||||||
class EntityUpdatedByImp<UserT: EntityI<*>>: UpdatedBy<UserT> {
|
class EntityUpdatedByImp<UserT: EntityI<*>>(
|
||||||
override var updatedBy: UserT? = null
|
override var updatedBy: UserT?
|
||||||
}
|
): UpdatedBy<UserT>
|
||||||
|
|
||||||
/* Published */
|
/* Published */
|
||||||
interface Published<UserT: EntityI<*>> {
|
interface Published<UserT: EntityI<*>> {
|
||||||
@@ -67,19 +67,25 @@ interface Published<UserT: EntityI<*>> {
|
|||||||
var publishedBy: UserT?
|
var publishedBy: UserT?
|
||||||
}
|
}
|
||||||
|
|
||||||
class EntityPublishedImp<UserT: EntityI<*>>: Published<UserT> {
|
class EntityPublishedImp<UserT: EntityI<*>>(
|
||||||
|
override var publishedBy: UserT?
|
||||||
|
): Published<UserT> {
|
||||||
override var publishedAt: DateTime? = null
|
override var publishedAt: DateTime? = null
|
||||||
override var publishedBy: UserT? = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Implementation */
|
/* Implementation */
|
||||||
abstract class EntityImp<T, UserT: EntityI<*>>: Entity<T>(),
|
abstract class EntityImp<T, UserT: EntityI<*>>(
|
||||||
|
updatedBy: UserT?
|
||||||
|
): Entity<T>(),
|
||||||
EntityCreatedAt by EntityCreatedAtImp(),
|
EntityCreatedAt by EntityCreatedAtImp(),
|
||||||
EntityUpdatedAt by EntityUpdatedAtImp(),
|
EntityUpdatedAt by EntityUpdatedAtImp(),
|
||||||
CreatedBy<UserT> by EntityCreatedByImp(),
|
CreatedBy<UserT> by EntityCreatedByImp(updatedBy),
|
||||||
UpdatedBy<UserT> by EntityUpdatedByImp()
|
UpdatedBy<UserT> by EntityUpdatedByImp(updatedBy)
|
||||||
|
|
||||||
abstract class EntityExtended<T, UserT: EntityI<*>>:
|
abstract class UuidEntityExtended<T, UserT: EntityI<*>>(
|
||||||
EntityImp<T, UserT>(),
|
updatedBy: UserT?,
|
||||||
|
publishedBy: UserT?
|
||||||
|
):
|
||||||
|
EntityImp<T, UserT>(updatedBy),
|
||||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(),
|
EntityVersioning<UUID, Int> by UuidEntityVersioning(),
|
||||||
Published<UserT> by EntityPublishedImp()
|
Published<UserT> by EntityPublishedImp(publishedBy)
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> EntityI<T?>.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
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<*>> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||||
inline fun <reified E: EntityI<*>> String.deserialize() = Serializer().deserialize<E>(this)
|
inline fun <reified E: EntityI<*>> String.deserialize() = Serializer().deserialize<E>(this)
|
||||||
|
|
||||||
@@ -92,9 +92,9 @@ class EntityIdDeserializer<T: IdEntity> @JvmOverloads constructor(vc: Class<*>?
|
|||||||
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T {
|
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T {
|
||||||
val node = jp.codec.readTree<JsonNode>(jp)
|
val node = jp.codec.readTree<JsonNode>(jp)
|
||||||
val id = node.get("id").asInt()
|
val id = node.get("id").asInt()
|
||||||
val entity = collection.get<Int?, IdEntity>(id)
|
val entity = collection.get<Int, IdEntity>(id)
|
||||||
|
|
||||||
val obj = (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as EntityI<Int?>
|
val obj = (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as EntityI<Int>
|
||||||
collection.set(obj)
|
collection.set(obj)
|
||||||
|
|
||||||
return obj as T
|
return obj as T
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import org.junit.jupiter.api.TestInstance
|
|||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class EntityTest() {
|
class EntityTest() {
|
||||||
private class User(override var id: Int?): EntityI<Int?>
|
private class User(override var id: Int?): EntityI<Int?>
|
||||||
private class ObjTest(var name: String): EntityExtended<Int?, User>()
|
private class ObjTest(var name: String): UuidEntityExtended<Int?, User>(User(1), User(2))
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getObject() {
|
fun getObject() {
|
||||||
val obj: ObjTest? = ObjTest("plop")
|
val obj: ObjTest? = ObjTest("plop")
|
||||||
assertTrue(obj is ObjTest)
|
assertTrue(obj is ObjTest)
|
||||||
assertTrue(obj is EntityExtended<Int?, User>)
|
assertTrue(obj is UuidEntityExtended<Int?, User>)
|
||||||
assertTrue(obj is EntityI<Int?>)
|
assertTrue(obj is EntityI<Int?>)
|
||||||
assertTrue(obj is Entity<Int?>)
|
assertTrue(obj is Entity<Int?>)
|
||||||
assertTrue(obj is Published<User>)
|
assertTrue(obj is Published<User>)
|
||||||
|
|||||||
Reference in New Issue
Block a user