From bf87308d07c24e2bb0a335e22658e6c885bba25b Mon Sep 17 00:00:00 2001 From: Clement de La Bourdonnaye Date: Thu, 29 Aug 2024 10:14:56 +0200 Subject: [PATCH] [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 --- .../jetbrains/rhizomedb/EntityAttribute.kt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) 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]