Skip to content

Commit e370cbb

Browse files
Merge pull request #1232 from square/sedwards/action-naming
Add public `debuggingName` to `WorkflowAction`, use it in default `toString()`
2 parents 86dab95 + be89c02 commit e370cbb

File tree

18 files changed

+72
-45
lines changed

18 files changed

+72
-45
lines changed

workflow-core/api/workflow-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ public abstract class com/squareup/workflow1/WorkflowAction {
330330
public static final field Companion Lcom/squareup/workflow1/WorkflowAction$Companion;
331331
public fun <init> ()V
332332
public abstract fun apply (Lcom/squareup/workflow1/WorkflowAction$Updater;)V
333+
public fun getDebuggingName ()Ljava/lang/String;
334+
public fun toString ()Ljava/lang/String;
333335
}
334336

335337
public final class com/squareup/workflow1/WorkflowAction$Companion {

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/Sink.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package com.squareup.workflow1
55

66
import kotlinx.coroutines.flow.Flow
7-
import kotlinx.coroutines.flow.collect
87
import kotlinx.coroutines.suspendCancellableCoroutine
98
import kotlin.coroutines.resume
109
import kotlin.jvm.JvmMultifileClass
@@ -87,6 +86,7 @@ internal suspend fun <
8786
suspendCancellableCoroutine<Unit> { continuation ->
8887
val resumingAction = object : WorkflowAction<PropsT, StateT, OutputT>() {
8988
override fun toString(): String = "sendAndAwaitApplication($action)"
89+
9090
override fun Updater.apply() {
9191
// Don't execute anything if the caller was cancelled while we were in the queue.
9292
if (!continuation.isActive) return

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/StatefulWorkflow.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,15 +998,18 @@ public fun <PropsT, StateT, OutputT, RenderingT>
998998
* of the receiving [StatefulWorkflow]. The action will invoke the given [lambda][update]
999999
* when it is [applied][WorkflowAction.apply].
10001000
*
1001-
* @param name Function that returns a string describing the update for debugging, included
1002-
* in [toString].
1001+
* @param name Function that returns a string describing the update for debugging, this will
1002+
* be returned by [WorkflowAction.debuggingName], which is in turn included in the default
1003+
* [WorkflowAction.toString].
10031004
* @param update Function that defines the workflow update.
10041005
*/
10051006
public fun <PropsT, StateT, OutputT, RenderingT>
10061007
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.action(
10071008
name: () -> String,
10081009
update: WorkflowAction<PropsT, StateT, OutputT>.Updater.() -> Unit
10091010
): WorkflowAction<PropsT, StateT, OutputT> = object : WorkflowAction<PropsT, StateT, OutputT>() {
1011+
override val debuggingName: String
1012+
get() = name()
1013+
10101014
override fun Updater.apply() = update.invoke(this)
1011-
override fun toString(): String = "action(${name()})-${this@action}"
10121015
}

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/StatelessWorkflow.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,18 @@ public fun <PropsT, OutputT, RenderingT>
141141
* of the receiving [StatefulWorkflow]. The action will invoke the given [lambda][update]
142142
* when it is [applied][WorkflowAction.apply].
143143
*
144-
* @param name Function that returns a string describing the update for debugging, included in
145-
* [toString].
144+
* @param name Function that returns a string describing the update for debugging, this will
145+
* be returned by [WorkflowAction.debuggingName], which is in turn included in the default
146+
* [WorkflowAction.toString].
146147
* @param update Function that defines the workflow update.
147148
*/
148149
public fun <PropsT, OutputT, RenderingT>
149150
StatelessWorkflow<PropsT, OutputT, RenderingT>.action(
150151
name: () -> String,
151152
update: WorkflowAction<PropsT, *, OutputT>.Updater.() -> Unit
152153
): WorkflowAction<PropsT, Nothing, OutputT> = object : WorkflowAction<PropsT, Nothing, OutputT>() {
154+
override val debuggingName: String
155+
get() = name()
156+
153157
override fun Updater.apply() = update.invoke(this)
154-
override fun toString(): String = "action(${name()})-${this@action}"
155158
}

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/WorkerWorkflow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ private class EmitWorkerOutputAction<P, S, O>(
8888
private val renderKey: String,
8989
private val output: O,
9090
) : WorkflowAction<P, S, O>() {
91-
override fun toString(): String =
92-
WorkflowIdentifierTypeNamer.uniqueName(EmitWorkerOutputAction::class) +
91+
override val debuggingName: String
92+
get() = CommonKClassTypeNamer.uniqueName(EmitWorkerOutputAction::class) +
9393
"(worker=$worker, key=$renderKey)"
9494

9595
override fun Updater.apply() {

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/WorkflowAction.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ import kotlin.jvm.JvmOverloads
3131
*/
3232
public abstract class WorkflowAction<in PropsT, StateT, out OutputT> {
3333

34+
/**
35+
* The name to use for debugging. This is handy for logging and is used by the default
36+
* [toString] implementation provided here.
37+
*/
38+
public open val debuggingName: String = CommonKClassTypeNamer.uniqueName(this::class)
39+
3440
/**
3541
* The context for calls to [WorkflowAction.apply]. Allows the action to read and change the
3642
* [state], and to emit an [output][setOutput] value.
@@ -60,6 +66,8 @@ public abstract class WorkflowAction<in PropsT, StateT, out OutputT> {
6066
*/
6167
public abstract fun Updater.apply()
6268

69+
public override fun toString(): String = "action($debuggingName)-@${hashCode()}"
70+
6371
public companion object {
6472
/**
6573
* Returns a [WorkflowAction] that does nothing: no output will be emitted, and
@@ -72,7 +80,7 @@ public abstract class WorkflowAction<in PropsT, StateT, out OutputT> {
7280
NO_ACTION as WorkflowAction<Any?, StateT, OutputT>
7381

7482
private val NO_ACTION = object : WorkflowAction<Any?, Any?, Any?>() {
75-
override fun toString(): String = "WorkflowAction.noAction()"
83+
override val debuggingName: String = "noAction()"
7684

7785
override fun Updater.apply() {
7886
// Noop
@@ -124,9 +132,10 @@ public fun <PropsT, StateT, OutputT> action(
124132
name: () -> String,
125133
apply: WorkflowAction<PropsT, StateT, OutputT>.Updater.() -> Unit
126134
): WorkflowAction<PropsT, StateT, OutputT> = object : WorkflowAction<PropsT, StateT, OutputT>() {
127-
override fun Updater.apply() = apply.invoke(this)
135+
override val debuggingName: String
136+
get() = name()
128137

129-
override fun toString(): String = "WorkflowAction(${name()})@${hashCode()}"
138+
override fun Updater.apply() = apply.invoke(this)
130139
}
131140

132141
/** Applies this [WorkflowAction] to [state]. */

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/WorkflowIdentifierType.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed class WorkflowIdentifierType {
2424
val kClass: KClass<*>? = null,
2525
) : WorkflowIdentifierType() {
2626
public constructor(kClass: KClass<*>) : this(
27-
WorkflowIdentifierTypeNamer.uniqueName(kClass),
27+
CommonKClassTypeNamer.uniqueName(kClass),
2828
kClass
2929
)
3030
}
@@ -46,6 +46,6 @@ public sealed class WorkflowIdentifierType {
4646
}
4747
}
4848

49-
internal expect object WorkflowIdentifierTypeNamer {
49+
internal expect object CommonKClassTypeNamer {
5050
public fun uniqueName(kClass: KClass<*>): String
5151
}

workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkflowIdentifierTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ internal class WorkflowIdentifierTest {
186186
) : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow {
187187
override val realIdentifier: WorkflowIdentifier = proxied.identifier
188188
override fun describeRealIdentifier(): String =
189-
"TestImpostor1(${WorkflowIdentifierTypeNamer.uniqueName(proxied::class)})"
189+
"TestImpostor1(${CommonKClassTypeNamer.uniqueName(proxied::class)})"
190190
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =
191191
throw NotImplementedError()
192192
}

workflow-core/src/iosMain/kotlin/com.squareup.workflow1/WorkflowIdentifierTypeNamer.kt renamed to workflow-core/src/iosMain/kotlin/com.squareup.workflow1/CommonKClassTypeNamer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.squareup.workflow1
22

33
import kotlin.reflect.KClass
44

5-
internal actual object WorkflowIdentifierTypeNamer {
5+
internal actual object CommonKClassTypeNamer {
66
public actual fun uniqueName(kClass: KClass<*>): String {
77
return kClass.qualifiedName ?: kClass.toString()
88
}

workflow-core/src/jsMain/kotlin/com.squareup.workflow1/WorkflowIdentifierTypeNamer.kt renamed to workflow-core/src/jsMain/kotlin/com.squareup.workflow1/CommonKClassTypeNamer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.squareup.workflow1
22

33
import kotlin.reflect.KClass
44

5-
internal actual object WorkflowIdentifierTypeNamer {
5+
internal actual object CommonKClassTypeNamer {
66
// Stores mappings between KClass instances and their assigned names.
77
val mappings = mutableMapOf<KClass<*>, String>()
88

0 commit comments

Comments
 (0)