in dnq/src/main/kotlin/kotlinx/dnq/XdModel.kt [168:193]
fun <T : XdEntity> toXd(entity: Entity): T {
// this is hack to support abstract type in XdEntityType.filter {} api
if (entity is FakeTransientEntity) {
return entity.toXdHandlingAbstraction()
}
if (entity is TransientEntityImpl && !entity.isReadonly /* filter entities created over snapshot transaction */) {
toXdCache.tryKey(entity.cacheKey)?.let { cachedValue ->
if (cachedValue.entity == entity) {
return cachedValue.xdEntity as T
}
}
}
val xdHierarchyNode = getOrThrow(entity.type)
val entityType = xdHierarchyNode.entityType
if (entityType is XdNaturalWrapper) {
return entityType.naturalWrap(entity).asCached as T
}
val entityConstructor = xdHierarchyNode.entityConstructor
?: throw UnsupportedOperationException("Constructor for the type ${entity.type} is not found")
return entityConstructor(entity).asCached as T
}