Skip to content

Commit

Permalink
Add smol caching & a utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
pyricau committed Dec 6, 2024
1 parent cd7485e commit 4668c23
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import okio.Buffer
import okio.ByteString
import okio.EOFException
import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlin.concurrent.Volatile
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.reflect.KClass
Expand Down Expand Up @@ -58,6 +59,17 @@ public class WorkflowIdentifier internal constructor(

private val proxiedIdentifiers = generateSequence(this) { it.proxiedIdentifier }

@Volatile
private var cachedToString: String? = null

/**
* Either a [KClass] or [KType] representing the "real" type that this identifier
* identifies – i.e. which is not an [ImpostorWorkflow].
*/
public val realType: WorkflowIdentifierType by lazy(PUBLICATION) {
proxiedIdentifiers.last().type
}

/**
* If this identifier is snapshottable, returns the serialized form of the identifier.
* If it is not snapshottable, returns null.
Expand All @@ -83,23 +95,24 @@ public class WorkflowIdentifier internal constructor(
}
}

/**
* Returns either a [KClass] or [KType] representing the "real" type that this identifier
* identifies – i.e. which is not an [ImpostorWorkflow].
*/
public fun getRealIdentifierType(): WorkflowIdentifierType = proxiedIdentifiers.last().type
@Deprecated("This is now a lazily computed val", ReplaceWith("realType"))
public fun getRealIdentifierType(): WorkflowIdentifierType = realType

/**
* If this identifier identifies an [ImpostorWorkflow], returns the result of that workflow's
* [ImpostorWorkflow.describeRealIdentifier] method, otherwise returns a description of this
* identifier including the name of its workflow type and any [ImpostorWorkflow.realIdentifier]s.
*
*/
override fun toString(): String =
description?.invoke()
override fun toString(): String {
return cachedToString ?: (description?.invoke()
?: proxiedIdentifiers
.joinToString { it.typeName }
.let { "WorkflowIdentifier($it)" }
.let { "WorkflowIdentifier($it)" })
.also {
cachedToString = it
}
}

override fun equals(other: Any?): Boolean = when {
this === other -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,22 @@ internal class WorkflowIdentifierTest {

@Test fun getRealIdentifierType_returns_self_for_non_impostor_workflow() {
val id = TestWorkflow1.identifier
assertEquals(Snapshottable(TestWorkflow1::class), id.getRealIdentifierType())
assertEquals(Snapshottable(TestWorkflow1::class), id.realType)
}

@Test fun getRealIdentifierType_returns_real_identifier_for_impostor_workflow() {
val id = TestImpostor1(TestWorkflow1).identifier
assertEquals(Snapshottable(TestWorkflow1::class), id.getRealIdentifierType())
assertEquals(Snapshottable(TestWorkflow1::class), id.realType)
}

@Test fun getRealIdentifierType_returns_leaf_real_identifier_for_impostor_workflow_chain() {
val id = TestImpostor2(TestImpostor1(TestWorkflow1)).identifier
assertEquals(Snapshottable(TestWorkflow1::class), id.getRealIdentifierType())
assertEquals(Snapshottable(TestWorkflow1::class), id.realType)
}

@Test fun getRealIdentifierType_returns_KType_of_unsnapshottable_identifier() {
val id = TestUnsnapshottableImpostor(typeOf<List<String>>()).identifier
assertEquals(Unsnapshottable(typeOf<List<String>>()), id.getRealIdentifierType())
assertEquals(Unsnapshottable(typeOf<List<String>>()), id.realType)
}

public object TestWorkflow1 : Workflow<Nothing, Nothing, Nothing> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public interface WorkflowInterceptor {
/** The parent [WorkflowSession] of this workflow, or null if this is the root workflow. */
public val parent: WorkflowSession?

/**
* true if this is the root workflow, in which case [parent] is null.
*/
public val isRootWorkflow: Boolean
get() = parent == null

/** The [RuntimeConfig] of the runtime this session is executing in. */
public val runtimeConfig: RuntimeConfig
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ internal fun createRenderChildInvocation(
internal fun WorkflowIdentifier.realTypeMatchesExpectation(
expected: WorkflowIdentifier
): Boolean {
val expectedType = expected.getRealIdentifierType()
val actualType = getRealIdentifierType()
val expectedType = expected.realType
val actualType = realType
return actualType.matchesExpectation(expectedType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public class TracingWorkflowInterceptor internal constructor(
}

private fun WorkflowIdentifier.toLoggingName(): String {
val type = getRealIdentifierType()
val type = realType
return when {
type is Snapshottable && type.kClass != null -> type.kClass!!.toLoggingName()
type is Unsnapshottable -> type.kType.toLoggingName()
Expand Down

0 comments on commit 4668c23

Please sign in to comment.