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,9 +130,13 @@ object PersistenceId {
* If the separator `|` is not found it return the empty String (`""`). * If the separator `|` is not found it return the empty String (`""`).
*/ */
def extractEntityType(id: String): String = { def extractEntityType(id: String): String = {
val i = id.indexOf(PersistenceId.DefaultSeparator) if (ReplicationId.isReplicationId(id))
if (i == -1) "" ReplicationId.fromString(id).typeName
else id.substring(0, i) else {
val i = id.indexOf(PersistenceId.DefaultSeparator)
if (i == -1) ""
else id.substring(0, i)
}
} }
/** /**
@ -140,9 +144,13 @@ object PersistenceId {
* If the separator `|` is not found it return the `id`. * If the separator `|` is not found it return the `id`.
*/ */
def extractEntityId(id: String): String = { def extractEntityId(id: String): String = {
val i = id.indexOf(PersistenceId.DefaultSeparator) if (ReplicationId.isReplicationId(id))
if (i == -1) id ReplicationId.fromString(id).entityId
else id.substring(i + 1) else {
val i = id.indexOf(PersistenceId.DefaultSeparator)
if (i == -1) id
else id.substring(i + 1)
}
} }
def unapply(persistenceId: PersistenceId): Option[(String, String)] = def unapply(persistenceId: PersistenceId): Option[(String, String)] =

View file

@ -54,6 +54,13 @@ class PersistenceIdSpec extends AnyWordSpec with Matchers with LogCapturing {
PersistenceId("SomeType", "abc").entityTypeHint should ===("SomeType") 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 { "be able to extract entityId" in {
PersistenceId.extractEntityId("SomeType|abc") should ===("abc") PersistenceId.extractEntityId("SomeType|abc") should ===("abc")
PersistenceId.extractEntityId("abc") should ===("abc") PersistenceId.extractEntityId("abc") should ===("abc")
@ -68,6 +75,13 @@ class PersistenceIdSpec extends AnyWordSpec with Matchers with LogCapturing {
case _ => fail() 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")
}
} }
} }