Skip to content

Commit 0a14ba0

Browse files
authored
Merge pull request #1113 from square/ray/invert-keyFor
Improves legibility of `Compatible.keyFor`, `NamedScreen`, `Named`.
2 parents 5b3c774 + db33461 commit 0a14ba0

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/Compatible.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ public interface Compatible {
3636

3737
public companion object {
3838
/**
39-
* Calculates a suitable [Compatible.compatibilityKey] for a given [value] and [name].
39+
* Calculates a suitable [Compatible.compatibilityKey] for a given [value], incorporating
40+
* [name] if that is not blank. Includes the [compatibilityKey] for [value] if it
41+
* implements [Compatible], to support recursion from wrapping.
42+
*
43+
* Style note: [name] is given more prominence than the key generate
4044
*/
4145
public fun keyFor(
4246
value: Any,
4347
name: String = ""
4448
): String {
45-
return ((value as? Compatible)?.compatibilityKey ?: value::class.java.name) +
46-
if (name.isEmpty()) "" else "+$name"
49+
val key = (value as? Compatible)?.compatibilityKey ?: value::class.java.name
50+
51+
return name.takeIf { it.isNotEmpty() }?.let { "$name($key)" } ?: key
4752
}
4853
}
4954
}

workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/Named.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public data class Named<W : Any>(
1515
require(name.isNotBlank()) { "name must not be blank." }
1616
}
1717

18-
override val compatibilityKey: String = Compatible.keyFor(wrapped, "Named($name)")
18+
override val compatibilityKey: String = Compatible.keyFor(wrapped, "Named:$name")
1919

2020
override fun toString(): String {
2121
return "${super.toString()}: $compatibilityKey"

workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/NamedScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public data class NamedScreen<C : Screen>(
1616
require(name.isNotBlank()) { "name must not be blank." }
1717
}
1818

19-
override val compatibilityKey: String = Compatible.keyFor(content, "NamedScreen($name)")
19+
override val compatibilityKey: String = Compatible.keyFor(content, "NamedScreen:$name")
2020

2121
@Deprecated("Use content", ReplaceWith("content"))
2222
public val wrapped: C = content

workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/NamedScreenTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal class NamedScreenTest {
5757

5858
@Test fun `recursive keys are legible`() {
5959
assertThat(NamedScreen(NamedScreen(Hey, "one"), "ho").compatibilityKey)
60-
.isEqualTo("com.squareup.workflow1.ui.NamedScreenTest\$Hey+NamedScreen(one)+NamedScreen(ho)")
60+
.isEqualTo("NamedScreen:ho(NamedScreen:one(com.squareup.workflow1.ui.NamedScreenTest\$Hey))")
6161
}
6262

6363
private class Foo(override val compatibilityKey: String) : Compatible, Screen

workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/NamedTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.junit.Test
55

66
// If you try to replace isTrue() with isTrue compilation fails.
77
@OptIn(WorkflowUiExperimentalApi::class)
8-
@Suppress("UsePropertyAccessSyntax", "DEPRECATION")
8+
@Suppress("DEPRECATION")
99
internal class NamedTest {
1010
object Whut
1111
object Hey
@@ -58,7 +58,7 @@ internal class NamedTest {
5858

5959
@Test fun `recursive keys are legible`() {
6060
assertThat(Named(Named(Hey, "one"), "ho").compatibilityKey)
61-
.isEqualTo("com.squareup.workflow1.ui.NamedTest\$Hey+Named(one)+Named(ho)")
61+
.isEqualTo("Named:ho(Named:one(com.squareup.workflow1.ui.NamedTest\$Hey))")
6262
}
6363

6464
private class Foo(override val compatibilityKey: String) : Compatible

0 commit comments

Comments
 (0)