Add some check on as*Id() methods, to fail in debug mode.

This commit is contained in:
Benoit Marty
2023-04-07 15:27:34 +02:00
committed by Benoit Marty
parent 3b49ab0e7a
commit 40660ca317
7 changed files with 61 additions and 6 deletions

View File

@@ -16,9 +16,14 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import java.io.Serializable
@JvmInline
value class EventId(val value: String) : Serializable
fun String.asEventId() = EventId(this)
fun String.asEventId() = EventId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isEventId(this)) {
error("`$this` is not a valid event Id")
}
}

View File

@@ -91,6 +91,14 @@ object MatrixPatterns {
PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER
)
/**
* Tells if a string is a valid session Id. This is an alias for [isUserId]
*
* @param str the string to test
* @return true if the string is a valid session id
*/
fun isSessionId(str: String?) = isUserId(str)
/**
* Tells if a string is a valid user Id.
*
@@ -101,6 +109,14 @@ object MatrixPatterns {
return str != null && str matches PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER
}
/**
* Tells if a string is a valid space id. This is an alias for [isRoomId]
*
* @param str the string to test
* @return true if the string is a valid space Id
*/
fun isSpaceId(str: String?) = isRoomId(str)
/**
* Tells if a string is a valid room id.
*
@@ -134,6 +150,14 @@ object MatrixPatterns {
str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4)
}
/**
* Tells if a string is a valid thread id. This is an alias for [isEventId].
*
* @param str the string to test
* @return true if the string is a valid thread id.
*/
fun isThreadId(str: String?) = isEventId(str)
/**
* Tells if a string is a valid group id.
*

View File

@@ -16,9 +16,14 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import java.io.Serializable
@JvmInline
value class RoomId(val value: String) : Serializable
fun String.asRoomId() = RoomId(this)
fun String.asRoomId() = RoomId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isRoomId(this)) {
error("`$this` is not a valid room Id")
}
}

View File

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

View File

@@ -16,6 +16,7 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import java.io.Serializable
@JvmInline
@@ -26,4 +27,8 @@ value class SpaceId(val value: String) : Serializable
*/
val MAIN_SPACE = SpaceId("!mainSpace")
fun String.asSpaceId() = SpaceId(this)
fun String.asSpaceId() = SpaceId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isSpaceId(this)) {
error("`$this` is not a valid space Id")
}
}

View File

@@ -16,9 +16,14 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import java.io.Serializable
@JvmInline
value class ThreadId(val value: String) : Serializable
fun String.asThreadId() = ThreadId(this)
fun String.asThreadId() = ThreadId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isThreadId(this)) {
error("`$this` is not a valid Thread Id")
}
}

View File

@@ -16,9 +16,14 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import java.io.Serializable
@JvmInline
value class UserId(val value: String) : Serializable
fun String.asUserId() = UserId(this)
fun String.asUserId() = UserId(this).also {
if (BuildConfig.DEBUG && !MatrixPatterns.isUserId(this)) {
error("`$this` is not a valid user Id")
}
}