Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves legibility of Compatible.keyFor, NamedScreen, Named. #1113

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ public interface Compatible {

public companion object {
/**
* Calculates a suitable [Compatible.compatibilityKey] for a given [value] and [name].
* Calculates a suitable [Compatible.compatibilityKey] for a given [value], incorporating
* [name] if that is not blank. Includes the [compatibilityKey] for [value] if it
* implements [Compatible], to support recursion from wrapping.
*
* Style note: [name] is given more prominence than the key generate
*/
public fun keyFor(
value: Any,
name: String = ""
): String {
return ((value as? Compatible)?.compatibilityKey ?: value::class.java.name) +
if (name.isEmpty()) "" else "+$name"
val key = (value as? Compatible)?.compatibilityKey ?: value::class.java.name

return name.takeIf { it.isNotEmpty() }?.let { "$name($key)" } ?: key
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public data class Named<W : Any>(
require(name.isNotBlank()) { "name must not be blank." }
}

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

override fun toString(): String {
return "${super.toString()}: $compatibilityKey"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public data class NamedScreen<C : Screen>(
require(name.isNotBlank()) { "name must not be blank." }
}

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

@Deprecated("Use content", ReplaceWith("content"))
public val wrapped: C = content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal class NamedScreenTest {

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

private class Foo(override val compatibilityKey: String) : Compatible, Screen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.Test

// If you try to replace isTrue() with isTrue compilation fails.
@OptIn(WorkflowUiExperimentalApi::class)
@Suppress("UsePropertyAccessSyntax", "DEPRECATION")
@Suppress("DEPRECATION")
internal class NamedTest {
object Whut
object Hey
Expand Down Expand Up @@ -58,7 +58,7 @@ internal class NamedTest {

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

private class Foo(override val compatibilityKey: String) : Compatible
Expand Down