Skip to content

Commit

Permalink
Allow accessing actual values from certain native enums
Browse files Browse the repository at this point in the history
  • Loading branch information
clementetb committed Aug 8, 2022
1 parent a92ca9c commit 3ddf15b
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ expect enum class AppErrorCategory {
companion object {
fun fromInt(nativeValue: Int): AppErrorCategory
}

fun toInt(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ expect enum class ClientErrorCode {
companion object {
fun fromInt(nativeValue: Int): ClientErrorCode
}

fun toInt(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ expect enum class ProtocolConnectionErrorCode {
companion object {
fun fromInt(nativeValue: Int): ProtocolConnectionErrorCode
}

fun toInt(): Int
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ expect enum class SyncErrorCodeCategory {
companion object {
fun fromInt(nativeValue: Int): SyncErrorCodeCategory
}

fun toInt(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ actual enum class AppErrorCategory(actual val description: String, val nativeVal
return fromInt(nativeValue.value.toInt())
}
}

actual fun toInt(): Int = nativeValue.value.toInt()
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ actual enum class ClientErrorCode(actual val description: String, val nativeValu
}
error("Unknown client error code: $nativeValue")
}

internal fun of(nativeValue: realm_app_errno_client): ClientErrorCode {
for (value in values()) {
if (value.nativeValue == nativeValue) {
return value
}
}
error("Unknown client error code: $nativeValue")
}
}

actual fun toInt(): Int = nativeValue.toInt()
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ actual enum class ProtocolConnectionErrorCode(actual val description: String, va
error("Unknown protocol connection error code: $nativeValue")
}
}

actual fun toInt(): Int = nativeValue.toInt()
}

actual enum class ProtocolSessionErrorCode(actual val description: String, val nativeValue: realm_sync_errno_session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ actual enum class SyncErrorCodeCategory(actual val description: String, val nati
error("Unknown sync error code category: $nativeValue")
}
}

actual fun toInt(): Int = nativeValue.value.toInt()
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ actual enum class AppErrorCategory(actual val description: String, override val
}
error("Unknown app error category value: $nativeValue")
}

@JvmStatic
fun of(nativeValue: Int): AppErrorCategory {
return fromInt(nativeValue)
}
}

actual fun toInt(): Int = nativeValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ actual enum class ClientErrorCode(actual val description: String, override val n
}
error("Unknown client error code: $nativeValue")
}

internal fun of(nativeValue: Int): ClientErrorCode {
return fromInt(nativeValue)
}
}

actual fun toInt(): Int = nativeValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ actual enum class ProtocolConnectionErrorCode(actual val description: String, ov
error("Unknown protocol connection error code: $nativeValue")
}
}

actual fun toInt(): Int = nativeValue
}

actual enum class ProtocolSessionErrorCode(actual val description: String, override val nativeValue: Int) : NativeEnumerated {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ actual enum class SyncErrorCodeCategory(actual val description: String, override
}
error("Unknown sync error code category value: $nativeValue")
}

@JvmStatic
fun of(nativeValue: Int): SyncErrorCodeCategory {
for (value in values()) {
if (value.nativeValue == nativeValue) {
return value
}
}
error("Unknown sync error code category value: $nativeValue")
}
}

actual fun toInt(): Int = nativeValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RealmSyncUtilsTest {
fun convertSyncErrorCode_unknownErrorCode1() {
val syncException = convertSyncErrorCode(
SyncErrorCode(
category = SyncErrorCodeCategory.RLM_SYNC_ERROR_CATEGORY_UNKNOWN.nativeValue,
category = SyncErrorCodeCategory.RLM_SYNC_ERROR_CATEGORY_UNKNOWN.toInt(),
value = 9999,
message = "Placeholder message"
)
Expand All @@ -48,7 +48,7 @@ class RealmSyncUtilsTest {
fun convertSyncErrorCode_unknownErrorCode2() {
val syncException = convertSyncErrorCode(
SyncErrorCode(
category = SyncErrorCodeCategory.RLM_SYNC_ERROR_CATEGORY_CONNECTION.nativeValue,
category = SyncErrorCodeCategory.RLM_SYNC_ERROR_CATEGORY_CONNECTION.toInt(),
value = 9999,
message = "Placeholder message"
)
Expand All @@ -62,7 +62,7 @@ class RealmSyncUtilsTest {
val syncException = convertSyncErrorCode(
SyncErrorCode(
category = 9999,
value = ProtocolConnectionErrorCode.RLM_SYNC_ERR_CONNECTION_CONNECTION_CLOSED.nativeValue,
value = ProtocolConnectionErrorCode.RLM_SYNC_ERR_CONNECTION_CONNECTION_CLOSED.toInt(),
message = "Placeholder message"
)
)
Expand All @@ -74,7 +74,7 @@ class RealmSyncUtilsTest {
fun convertAppError_unknownErrorCode() {
val appException = convertAppError(
AppError(
category = AppErrorCategory.RLM_APP_ERROR_CATEGORY_CUSTOM.nativeValue,
category = AppErrorCategory.RLM_APP_ERROR_CATEGORY_CUSTOM.toInt(),
errorCode = 9999,
message = "Placeholder message",
httpStatusCode = 9999,
Expand All @@ -90,7 +90,7 @@ class RealmSyncUtilsTest {
val appException = convertAppError(
AppError(
category = 9999,
errorCode = ClientErrorCode.RLM_APP_ERR_CLIENT_USER_NOT_FOUND.nativeValue,
errorCode = ClientErrorCode.RLM_APP_ERR_CLIENT_USER_NOT_FOUND.toInt(),
message = "Placeholder message",
httpStatusCode = 9999,
linkToServerLog = null
Expand Down

0 comments on commit 3ddf15b

Please sign in to comment.