mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[fleet] rhizomedb: introduce temporary API to migrate lookup and lookupOne using base attributes on subclass
As documented in the code, lookup() was able to work with `subclass::baseAttribute` and return all instances of the subclass that has the base attribute value. With the new API, more boilerplate is necessary at the moment. For lookupOne, while the replacement would work in all cases, some may benefit from a simple `entity() as? Subclass`, but it has to be decided on a case-by-case basis. This API would keep track of such cases. GitOrigin-RevId: be9a435388959a1371e2dd90675f3bb3a6143bec
This commit is contained in:
committed by
intellij-monorepo-bot
parent
69ad4effce
commit
bf87308d07
@@ -105,6 +105,37 @@ fun <E : Entity, T : Any> DbContext<Q>.entity(entityAttribute: EntityAttribute<E
|
||||
fun <E : Entity, T : Any> entityOnNonUniqueAttribute(entityAttribute: EntityAttribute<E, T>, value: T): E? =
|
||||
entities(entityAttribute, value).singleOrNullOrThrow()
|
||||
|
||||
/**
|
||||
* Problematic case that this method solves temporarily:
|
||||
* ```kotlin
|
||||
* interface A: Entity { var i: Boolean }
|
||||
* interface B: A
|
||||
* interface C: A
|
||||
*
|
||||
* // ...
|
||||
* new(B) { i = true }
|
||||
* new(C) { i = true }
|
||||
* lookupOne(B::i, true) // worked, but entity(A.iAttr, true) as? B would fail (multiple result)
|
||||
* ```
|
||||
*
|
||||
* Replace either with:
|
||||
* - if there's at most one on base class + attribute is Unique: `entity() as? YourTarget`
|
||||
* - otherwise: inline content `entities(...).filterIsInstance<YourTarget>().singleOrNullOrThrow()`
|
||||
*/
|
||||
@Deprecated(
|
||||
"new API doesn't allow to call entity() on a attribute of a base class while expecting only a subtype to be returned",
|
||||
ReplaceWith("entities(entityAttribute, value).filterIsInstance<C>().singleOrNullOrThrow()", "fleet.util.singleOrNullOrThrow")
|
||||
)
|
||||
inline fun <E : Entity, T : Any, reified C: E> entityCasted(entityAttribute: EntityAttribute<E, T>, value: T): C? =
|
||||
entities(entityAttribute, value).filterIsInstance<C>().singleOrNullOrThrow()
|
||||
|
||||
@Deprecated(
|
||||
"new API doesn't allow to call entity() on a attribute of a base class while expecting only a subtype to be returned",
|
||||
ReplaceWith("entities(entityAttribute, value).filterIsInstance<C>()")
|
||||
)
|
||||
inline fun <E : Entity, T : Any, reified C: E> entitiesCasted(entityAttribute: EntityAttribute<E, T>, value: T): Set<C> =
|
||||
entities(entityAttribute, value).filterIsInstance<C>().toSet()
|
||||
|
||||
/**
|
||||
* Returns an [Entity] which has the attribute's value set to (or containing) a given [value]
|
||||
* Works for [EntityAttribute]s with [Indexing.UNIQUE] or [RefFlags.UNIQUE]
|
||||
|
||||
Reference in New Issue
Block a user