diff --git a/fleet/rhizomedb/src/main/kotlin/com/jetbrains/rhizomedb/EntityAttribute.kt b/fleet/rhizomedb/src/main/kotlin/com/jetbrains/rhizomedb/EntityAttribute.kt index 115d69c7cdfa..63c18143861f 100644 --- a/fleet/rhizomedb/src/main/kotlin/com/jetbrains/rhizomedb/EntityAttribute.kt +++ b/fleet/rhizomedb/src/main/kotlin/com/jetbrains/rhizomedb/EntityAttribute.kt @@ -105,6 +105,37 @@ fun DbContext.entity(entityAttribute: EntityAttribute entityOnNonUniqueAttribute(entityAttribute: EntityAttribute, 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().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().singleOrNullOrThrow()", "fleet.util.singleOrNullOrThrow") +) +inline fun entityCasted(entityAttribute: EntityAttribute, value: T): C? = + entities(entityAttribute, value).filterIsInstance().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()") +) +inline fun entitiesCasted(entityAttribute: EntityAttribute, value: T): Set = + entities(entityAttribute, value).filterIsInstance().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]