I am receiving a Category object from Retrofit:
data class Category(
val id: Int,
val title: String,
val type: CategoryType
)
data class CategoryList(
@SerializedName("categories")
val categories: List<Category>
)
where CategoryType is an enum class:
enum class CategoryType constructor(
var title: String
) {
CAT_CHICKEN("chicken"),
CAT_PORK("pork"),
CAT_STEAK("steak"),
companion object {
fun getCategoryTypeByName(text: String): CategoryType? {
return when (text) {
"chicken" -> CAT_CHICKEN
"pork" -> CAT_PORK
"steak" -> CAT_STEAK
else -> null
}
}
}
}
My api call is like this:
@GET("categs/melist/")
suspend fun getCategories(): Response<CategoryList>
How can I convert the 'type' variable that comes from the server as a string to a CategoryType object?