Reputation: 16723
My application has the following Enum
.
object UserTokenType extends Enumeration {
type TokenType = Value
val RegistrationConfirmation = Value("RegistrationConfirmation")
val ResetPasswordConfirmation = Value("ResetPasswordConfirmation")
}
I store the value of the enum in the database by using the id
of the Enum
. When I read the value from the database, I want to recreate the Enum
so that I can pass it around in my data model. At the moment, I have done it as follows
val userTokenType:UserTokenType.TokenType = if(row.getInt("is_sign_up") == UserTokenType.RegistrationConfirmation.id) {
UserTokenType.RegistrationConfirmation
}else{
UserTokenType.ResetPasswordConfirmation
}
Is above the right way of converting an Int
into an Enum
?
Upvotes: 0
Views: 38
Reputation: 2124
As @som-snytt mentioned in the comment, you can use [apply(x:Int)](https://www.scala-lang.org/api/current/scala/Enumeration.html#apply(x:Int) :Enumeration.this.Value) to construct enum:
row.getInt("is_sign_up") match {
case id if id >= 0 && id < UserTokenType.maxId => Some(UserTokenType(id))
case _ => None
}
Upvotes: 1