Support ReplicationId in PersistenceId extract utilities (#31122)

This commit is contained in:
Patrik Nordwall 2022-02-14 10:58:20 +01:00 committed by GitHub
parent 5f78f78263
commit 7a25618748
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 6 deletions

View file

@ -130,20 +130,28 @@ object PersistenceId {
* If the separator `|` is not found it return the empty String (`""`).
*/
def extractEntityType(id: String): String = {
if (ReplicationId.isReplicationId(id))
ReplicationId.fromString(id).typeName
else {
val i = id.indexOf(PersistenceId.DefaultSeparator)
if (i == -1) ""
else id.substring(0, i)
}
}
/**
* Extract the `entityId` from a persistence id String with the default separator `|`.
* If the separator `|` is not found it return the `id`.
*/
def extractEntityId(id: String): String = {
if (ReplicationId.isReplicationId(id))
ReplicationId.fromString(id).entityId
else {
val i = id.indexOf(PersistenceId.DefaultSeparator)
if (i == -1) id
else id.substring(i + 1)
}
}
def unapply(persistenceId: PersistenceId): Option[(String, String)] =
Some((persistenceId.entityTypeHint, persistenceId.entityId))

View file

@ -54,6 +54,13 @@ class PersistenceIdSpec extends AnyWordSpec with Matchers with LogCapturing {
PersistenceId("SomeType", "abc").entityTypeHint should ===("SomeType")
}
"be able to extract entityTypeHint from ReplicationId" in {
val replicaId = ReplicationId("SomeType", "abc", ReplicaId("A"))
val pid = replicaId.persistenceId
pid.entityTypeHint should ===("SomeType")
PersistenceId.extractEntityType(pid.id) should ===("SomeType")
}
"be able to extract entityId" in {
PersistenceId.extractEntityId("SomeType|abc") should ===("abc")
PersistenceId.extractEntityId("abc") should ===("abc")
@ -68,6 +75,13 @@ class PersistenceIdSpec extends AnyWordSpec with Matchers with LogCapturing {
case _ => fail()
}
}
"be able to extract entityId from ReplicationId" in {
val replicaId = ReplicationId("SomeType", "abc", ReplicaId("A"))
val pid = replicaId.persistenceId
pid.entityId should ===("abc")
PersistenceId.extractEntityId(pid.id) should ===("abc")
}
}
}