Improve asXId and make tests pass in release and debug mode.

This commit is contained in:
Benoit Marty 2023-04-12 09:14:55 +02:00 committed by Benoit Marty
parent b081ecfb6a
commit eb6276e09d
15 changed files with 121 additions and 25 deletions

View file

@ -22,8 +22,12 @@ import java.io.Serializable
@JvmInline
value class EventId(val value: String) : Serializable
fun String.asEventId() = EventId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isEventId(this)) {
fun String.asEventId() = if (MatrixPatterns.isEventId(this)) {
EventId(this)
} else {
if (BuildConfig.DEBUG) {
error("`$this` is not a valid event Id")
} else {
null
}
}

View file

@ -22,8 +22,12 @@ import java.io.Serializable
@JvmInline
value class RoomId(val value: String) : Serializable
fun String.asRoomId() = RoomId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isRoomId(this)) {
fun String.asRoomId() = if (MatrixPatterns.isRoomId(this)) {
RoomId(this)
} else {
if (BuildConfig.DEBUG) {
error("`$this` is not a valid room Id")
} else {
null
}
}

View file

@ -20,8 +20,12 @@ import io.element.android.libraries.matrix.api.BuildConfig
typealias SessionId = UserId
fun String.asSessionId() = SessionId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isSessionId(this)) {
fun String.asSessionId() = if (MatrixPatterns.isSessionId(this)) {
SessionId(this)
} else {
if (BuildConfig.DEBUG) {
error("`$this` is not a valid session Id")
} else {
null
}
}

View file

@ -27,8 +27,12 @@ value class SpaceId(val value: String) : Serializable
*/
val MAIN_SPACE = SpaceId("!mainSpace")
fun String.asSpaceId() = SpaceId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isSpaceId(this)) {
fun String.asSpaceId() = if (MatrixPatterns.isSpaceId(this)) {
SpaceId(this)
} else {
if (BuildConfig.DEBUG) {
error("`$this` is not a valid space Id")
} else {
null
}
}

View file

@ -22,8 +22,12 @@ import java.io.Serializable
@JvmInline
value class ThreadId(val value: String) : Serializable
fun String.asThreadId() = ThreadId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isThreadId(this)) {
error("`$this` is not a valid Thread Id")
fun String.asThreadId() = if (MatrixPatterns.isThreadId(this)) {
ThreadId(this)
} else {
if (BuildConfig.DEBUG) {
error("`$this` is not a valid thread Id")
} else {
null
}
}

View file

@ -22,8 +22,12 @@ import java.io.Serializable
@JvmInline
value class UserId(val value: String) : Serializable
fun String.asUserId() = UserId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isUserId(this)) {
fun String.asUserId() = if (MatrixPatterns.isUserId(this)) {
UserId(this)
} else {
if (BuildConfig.DEBUG) {
error("`$this` is not a valid user Id")
} else {
null
}
}