lundi 24 octobre 2016

Read values from case object field which is extending a trait using reflection

This might be a weird question, but I am trying something and stuck at this point.

For enumuerators, I am using the sealed trait - case object approach. I am trying to generate a Typescript class for each of the annotations to avoid mismatch between Frontend and Backend values for enums. Here is my sample enum implementation.

sealed trait BankingTypeEnum {
  val id: Long
  val name: String
}

object BankingTypeEnum {

  case object Cheque extends BankingTypeEnum {
    override val id: Long = 1
    override val name: String = "Loan"
  }

  case object Current extends BankingTypeEnum {
    override val id: Long = 2
    override val name: String = "Current"
  }

  case object Savings extends BankingTypeEnum {
    override val id: Long = 3
    override val name: String = "Savings"
  }

}

What I want to do is to get the values into a simple case class which can be later used to generating the typescript file. However, I am not sure how I can get the field values at generation time using reflection. If I use case class instead of case object, I can create an instance of the class using reflection, but I do not want to do that. Is there any other way, I can get the values, id and name fields of the case object enums ?





Aucun commentaire:

Enregistrer un commentaire