Skip to content

Commit

Permalink
Fixed function to find enum value
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanarodr committed May 2, 2024
1 parent a6ca847 commit c8188ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
12 changes: 0 additions & 12 deletions common/src/main/kotlin/br/com/stonks/common/utils/KotlinUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package br.com.stonks.feature.home.domain.types

import br.com.stonks.common.utils.safeEnumValueOf
import timber.log.Timber

enum class PortfolioType(val id: String) {
UNKNOWN("unknown"),
Expand All @@ -12,7 +12,12 @@ enum class PortfolioType(val id: String) {
companion object {

fun fromString(id: String): PortfolioType {
return safeEnumValueOf<PortfolioType>(id) ?: UNKNOWN
return try {
enumValues<PortfolioType>().find { it.id == id } ?: UNKNOWN
} catch (exception: IllegalArgumentException) {
Timber.e(exception, "Failure to find the enum value with ID '$id'")
UNKNOWN
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package br.com.stonks.feature.home.domain.types

import br.com.stonks.common.utils.safeEnumValueOf
import timber.log.Timber

enum class TransactionType(val id: String) {
UNKNOWN("unknown"),
Expand All @@ -10,7 +10,12 @@ enum class TransactionType(val id: String) {
companion object {

fun fromString(id: String): TransactionType {
return safeEnumValueOf<TransactionType>(id) ?: UNKNOWN
return try {
enumValues<TransactionType>().find { it.id == id } ?: UNKNOWN
} catch (exception: IllegalArgumentException) {
Timber.e(exception, "Failure to find the enum value with ID '$id'")
UNKNOWN
}
}
}
}

0 comments on commit c8188ba

Please sign in to comment.